Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FE usage of the inbox subcollection #2363 #2378

Merged
merged 18 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/services/Chat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { stringify } from "query-string";
import { ApiEndpoint } from "@/shared/constants";
import { ApiEndpoint, FirestoreDataSource } from "@/shared/constants";
import { DMUser, UnsubscribeFunction } from "@/shared/interfaces";
import {
GetChatChannelMessagesResponse,
Expand All @@ -20,7 +20,7 @@ import {
firestoreDataConverter,
getUserName,
} from "@/shared/utils";
import firebase from "@/shared/utils/firebase";
import firebase, { isFirestoreCacheError } from "@/shared/utils/firebase";
import Api, { CancelToken } from "./Api";

const chatChannelConverter = firestoreDataConverter<ChatChannel>();
Expand Down Expand Up @@ -234,6 +234,31 @@ class ChatService {
});
};

public getChatChannelById = async (
chatChannelId: string,
source = FirestoreDataSource.Default,
): Promise<ChatChannel | null> => {
try {
const snapshot = await this.getChatChannelCollection()
.doc(chatChannelId)
.get({ source });

return snapshot?.data() || null;
} catch (error) {
if (
source === FirestoreDataSource.Cache &&
isFirestoreCacheError(error)
) {
return this.getChatChannelById(
chatChannelId,
FirestoreDataSource.Server,
);
}

throw error;
}
};

public getChatChannels = async (options: {
participantId: string;
startAt?: Timestamp;
Expand Down
31 changes: 29 additions & 2 deletions src/services/FeedItemFollows.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiEndpoint } from "@/shared/constants";
import { ApiEndpoint, FirestoreDataSource } from "@/shared/constants";
import { DocChange } from "@/shared/constants/docChange";
import { UnsubscribeFunction } from "@/shared/interfaces";
import { FollowFeedItemPayload } from "@/shared/interfaces/api";
Expand All @@ -10,7 +10,7 @@ import {
Timestamp,
} from "@/shared/models";
import { firestoreDataConverter } from "@/shared/utils";
import firebase from "@/shared/utils/firebase";
import firebase, { isFirestoreCacheError } from "@/shared/utils/firebase";
import Api, { CancelToken } from "./Api";
import CommonService from "./Common";
import CommonFeedService from "./CommonFeed";
Expand All @@ -26,6 +26,33 @@ class FeedItemFollowsService {
.collection(SubCollections.FeedItemFollows)
.withConverter(converter);

public getFeedItemFollowDataById = async (
userId: string,
feedItemFollowId: string,
source = FirestoreDataSource.Default,
): Promise<FeedItemFollow | null> => {
try {
const snapshot = await this.getFeedItemFollowsSubCollection(userId)
.doc(feedItemFollowId)
.get({ source });

return snapshot?.data() || null;
} catch (error) {
if (
source === FirestoreDataSource.Cache &&
isFirestoreCacheError(error)
) {
return this.getFeedItemFollowDataById(
userId,
feedItemFollowId,
FirestoreDataSource.Server,
);
}

throw error;
}
};

public getUserFeedItemFollowData = async (
userId: string,
feedItemId: string,
Expand Down
60 changes: 59 additions & 1 deletion src/services/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
Collection,
CommonFeed,
FeedItemFollowWithMetadata,
InboxItem,
SubCollections,
Timestamp,
User,
} from "@/shared/models";
Expand All @@ -22,11 +24,18 @@ import Api from "./Api";
import { waitForUserToBeLoaded } from "./utils";

const converter = firestoreDataConverter<User>();
const inboxConverter = firestoreDataConverter<InboxItem>();

class UserService {
private getUsersCollection = () =>
firebase.firestore().collection(Collection.Users).withConverter(converter);

private getInboxSubCollection = (userId: string) =>
this.getUsersCollection()
.doc(userId)
.collection(SubCollections.Inbox)
.withConverter(inboxConverter);

public updateUser = async (user: User): Promise<User> => {
const body: UpdateUserDto = {
userId: user.uid,
Expand Down Expand Up @@ -135,7 +144,7 @@ class UserService {
});
};

public getInboxItems = async (
public getInboxItemsWithMetadata = async (
options: {
startAfter?: Timestamp | null;
limit?: number;
Expand Down Expand Up @@ -197,6 +206,55 @@ class UserService {
hasMore: data.hasMore,
};
};

public getInboxItems = async (options: {
userId: string;
startAt?: Timestamp;
endAt?: Timestamp;
}): Promise<InboxItem[]> => {
const { userId, startAt, endAt } = options;
let query = this.getInboxSubCollection(userId).orderBy("updatedAt", "desc");

if (startAt) {
query = query.startAt(startAt);
}
if (endAt) {
query = query.endAt(endAt);
}

const snapshot = await query.get();

return snapshot.docs.map((doc) => doc.data());
};

public subscribeToNewInboxItems = (
userId: string,
endBefore: Timestamp,
callback: (
data: {
item: InboxItem;
statuses: {
isAdded: boolean;
isRemoved: boolean;
};
}[],
) => void,
): UnsubscribeFunction => {
const query = this.getInboxSubCollection(userId)
.orderBy("updatedAt", "desc")
.endBefore(endBefore);

return query.onSnapshot((snapshot) => {
const data = snapshot.docChanges().map((docChange) => ({
item: docChange.doc.data(),
statuses: {
isAdded: docChange.type === "added",
isRemoved: docChange.type === "removed",
},
}));
callback(data);
});
};
}

export default new UserService();
Loading
Loading