From c81802b3bbdd78b3d55f5bd414b282498fb3c83c Mon Sep 17 00:00:00 2001 From: Hemachandar <132386067+hmacr@users.noreply.github.com> Date: Tue, 11 Feb 2025 08:02:51 +0530 Subject: [PATCH] Fetch subscription data using 'subscriptions.info' API (#8368) * Fetch subscription data using 'subscriptions.info' API * use getByDocumentId * throw 404 * unnecessary notfound error --- app/actions/definitions/documents.tsx | 2 +- app/menus/DocumentMenu.tsx | 2 +- app/models/Document.ts | 6 ++-- app/stores/DocumentsStore.ts | 6 ++-- app/stores/SubscriptionsStore.ts | 34 +++++++++++++++++++ .../api/subscriptions/subscriptions.test.ts | 19 +++++++++++ 6 files changed, 60 insertions(+), 9 deletions(-) diff --git a/app/actions/definitions/documents.tsx b/app/actions/definitions/documents.tsx index e42cd4d0a8..d8df464fcc 100644 --- a/app/actions/definitions/documents.tsx +++ b/app/actions/definitions/documents.tsx @@ -358,7 +358,7 @@ export const unsubscribeDocument = createAction({ const document = stores.documents.get(activeDocumentId); - await document?.unsubscribe(currentUserId); + await document?.unsubscribe(); toast.success(t("Unsubscribed from document notifications")); }, diff --git a/app/menus/DocumentMenu.tsx b/app/menus/DocumentMenu.tsx index f4022b83ce..b992745a22 100644 --- a/app/menus/DocumentMenu.tsx +++ b/app/menus/DocumentMenu.tsx @@ -96,7 +96,7 @@ const MenuTrigger: React.FC = ({ label, onTrigger }) => { const { model: document, menuState } = useMenuContext(); const { data, loading, error, request } = useRequest(() => - subscriptions.fetchPage({ + subscriptions.fetchOne({ documentId: document.id, event: "documents.update", }) diff --git a/app/models/Document.ts b/app/models/Document.ts index 807ea76f49..a77d1578c1 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -307,9 +307,7 @@ export default class Document extends ArchivableModel implements Searchable { */ @computed get isSubscribed(): boolean { - return !!this.store.rootStore.subscriptions.orderedData.find( - (subscription) => subscription.documentId === this.id - ); + return !!this.store.rootStore.subscriptions.getByDocumentId(this.id); } /** @@ -501,7 +499,7 @@ export default class Document extends ArchivableModel implements Searchable { * @returns A promise that resolves when the subscription is destroyed. */ @action - unsubscribe = (userId: string) => this.store.unsubscribe(userId, this); + unsubscribe = () => this.store.unsubscribe(this); @action view = () => { diff --git a/app/stores/DocumentsStore.ts b/app/stores/DocumentsStore.ts index 2c71e71f98..fec4a1f317 100644 --- a/app/stores/DocumentsStore.ts +++ b/app/stores/DocumentsStore.ts @@ -816,9 +816,9 @@ export default class DocumentsStore extends Store { event: "documents.update", }); - unsubscribe = (userId: string, document: Document) => { - const subscription = this.rootStore.subscriptions.orderedData.find( - (s) => s.documentId === document.id && s.userId === userId + unsubscribe = (document: Document) => { + const subscription = this.rootStore.subscriptions.getByDocumentId( + document.id ); return subscription?.delete(); diff --git a/app/stores/SubscriptionsStore.ts b/app/stores/SubscriptionsStore.ts index fd9a700779..cc2867a938 100644 --- a/app/stores/SubscriptionsStore.ts +++ b/app/stores/SubscriptionsStore.ts @@ -1,4 +1,8 @@ +import invariant from "invariant"; +import { action } from "mobx"; import Subscription from "~/models/Subscription"; +import { client } from "~/utils/ApiClient"; +import { AuthorizationError, NotFoundError } from "~/utils/errors"; import RootStore from "./RootStore"; import Store, { RPCAction } from "./base/Store"; @@ -8,4 +12,34 @@ export default class SubscriptionsStore extends Store { constructor(rootStore: RootStore) { super(rootStore, Subscription); } + + @action + async fetchOne({ documentId, event }: { documentId: string; event: string }) { + const subscription = this.getByDocumentId(documentId); + + if (subscription) { + return subscription; + } + + this.isFetching = true; + + try { + const res = await client.post(`/${this.apiEndpoint}.info`, { + documentId, + event, + }); + invariant(res?.data, "Data should be available"); + return this.add(res.data); + } catch (err) { + if (err instanceof AuthorizationError || err instanceof NotFoundError) { + return; + } + throw err; + } finally { + this.isFetching = false; + } + } + + getByDocumentId = (documentId: string): Subscription | undefined => + this.find({ documentId }); } diff --git a/server/routes/api/subscriptions/subscriptions.test.ts b/server/routes/api/subscriptions/subscriptions.test.ts index ec07da330f..f8caf23e8d 100644 --- a/server/routes/api/subscriptions/subscriptions.test.ts +++ b/server/routes/api/subscriptions/subscriptions.test.ts @@ -202,6 +202,25 @@ describe("#subscriptions.info", () => { expect(response0.data.documentId).toEqual(document0.id); }); + it("should throw 404 if no subscription found", async () => { + const author = await buildUser(); + const subscriber = await buildUser({ teamId: author.teamId }); + const document = await buildDocument({ + userId: author.id, + teamId: author.teamId, + }); + + const res = await server.post("/api/subscriptions.info", { + body: { + token: subscriber.getJwtToken(), + documentId: document.id, + event: "documents.update", + }, + }); + + expect(res.status).toEqual(404); + }); + it("should not allow outsiders to gain info about a subscription", async () => { const creator = await buildUser(); const subscriber = await buildUser({ teamId: creator.teamId });