Skip to content

Commit

Permalink
Merge pull request #2378 from daostack/feature/CW-2363-inbox-collecti…
Browse files Browse the repository at this point in the history
…on-usage

FE usage of the inbox subcollection #2363
  • Loading branch information
andreymikhadyuk authored Dec 18, 2023
2 parents a274946 + c351c22 commit 85413f2
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 237 deletions.
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
72 changes: 71 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,67 @@ 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(
"itemUpdatedAt",
"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 = (
options: {
userId: string;
endBefore: Timestamp;
unread?: boolean;
orderBy?: "itemUpdatedAt" | "updatedAt";
},
callback: (
data: {
item: InboxItem;
statuses: {
isAdded: boolean;
isRemoved: boolean;
};
}[],
) => void,
): UnsubscribeFunction => {
const { userId, endBefore, unread, orderBy = "itemUpdatedAt" } = options;
let query = this.getInboxSubCollection(userId)
.orderBy(orderBy, "desc")
.endBefore(endBefore);

if (unread) {
query = query.where("unread", "==", true);
}

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

0 comments on commit 85413f2

Please sign in to comment.