diff --git a/app/actions/definitions/collections.tsx b/app/actions/definitions/collections.tsx index 1b9a0bdd0a..3db4970c20 100644 --- a/app/actions/definitions/collections.tsx +++ b/app/actions/definitions/collections.tsx @@ -261,7 +261,7 @@ export const unsubscribeCollection = createAction({ const collection = stores.collections.get(activeCollectionId); - await collection?.unsubscribe(currentUserId); + await collection?.unsubscribe(); toast.success(t("Unsubscribed from collection notifications")); }, diff --git a/app/menus/CollectionMenu.tsx b/app/menus/CollectionMenu.tsx index 0f7a5e4314..417a4649b5 100644 --- a/app/menus/CollectionMenu.tsx +++ b/app/menus/CollectionMenu.tsx @@ -74,7 +74,7 @@ function CollectionMenu({ const file = React.useRef(null); const { data, loading, error, request } = useRequest(() => - subscriptions.fetchPage({ + subscriptions.fetchOne({ collectionId: collection.id, event: SubscriptionEventType.Collection, }) diff --git a/app/menus/DocumentMenu.tsx b/app/menus/DocumentMenu.tsx index f003d32771..e2eb026bbd 100644 --- a/app/menus/DocumentMenu.tsx +++ b/app/menus/DocumentMenu.tsx @@ -1,6 +1,7 @@ import capitalize from "lodash/capitalize"; import isEmpty from "lodash/isEmpty"; import isUndefined from "lodash/isUndefined"; +import noop from "lodash/noop"; import { observer } from "mobx-react"; import { EditIcon, InputIcon, RestoreIcon, SearchIcon } from "outline-icons"; import * as React from "react"; @@ -101,10 +102,12 @@ const MenuTrigger: React.FC = ({ label, onTrigger }) => { documentId: document.id, event: SubscriptionEventType.Document, }), - subscriptions.fetchOne({ - collectionId: document.collectionId, - event: SubscriptionEventType.Collection, - }), + document.collectionId + ? subscriptions.fetchOne({ + collectionId: document.collectionId, + event: SubscriptionEventType.Collection, + }) + : noop, ]) ); @@ -193,7 +196,7 @@ const MenuContent: React.FC = observer(function MenuContent_({ if (can.createDocument) { filtered.push({ type: "button", - onClick: ev => + onClick: (ev) => handleRestore(ev, { collectionId: collection.id, }), @@ -224,7 +227,7 @@ const MenuContent: React.FC = observer(function MenuContent_({ visible: !!(document.isWorkspaceTemplate || collection?.isActive) && !!(can.restore || can.unarchive), - onClick: ev => handleRestore(ev), + onClick: (ev) => handleRestore(ev), icon: , }, { @@ -336,7 +339,7 @@ const MenuContent: React.FC = observer(function MenuContent_({ label={t("Full width")} labelPosition="left" checked={document.fullWidth} - onChange={ev => { + onChange={(ev) => { const fullWidth = ev.currentTarget.checked; user.setPreference( UserPreference.FullWidthDocuments, @@ -438,10 +441,7 @@ function DocumentMenu({ - + {isMenuVisible ? ( subscription.collectionId === this.id - ); + return !!this.store.rootStore.subscriptions.getByCollectionId(this.id); } @computed @@ -402,7 +400,7 @@ export default class Collection extends ParanoidModel { * @returns A promise that resolves when the subscription is destroyed. */ @action - unsubscribe = (userId: string) => this.store.unsubscribe(userId, this); + unsubscribe = () => this.store.unsubscribe(this); archive = () => this.store.archive(this); diff --git a/app/stores/CollectionsStore.ts b/app/stores/CollectionsStore.ts index cd313a16f1..6fb7b5e09b 100644 --- a/app/stores/CollectionsStore.ts +++ b/app/stores/CollectionsStore.ts @@ -220,9 +220,9 @@ export default class CollectionsStore extends Store { event: SubscriptionEventType.Collection, }); - unsubscribe = (userId: string, collection: Collection) => { - const subscription = this.rootStore.subscriptions.orderedData.find( - (s) => s.collectionId === collection.id && s.userId === userId + unsubscribe = (collection: Collection) => { + const subscription = this.rootStore.subscriptions.getByCollectionId( + collection.id ); return subscription?.delete(); diff --git a/app/stores/SubscriptionsStore.ts b/app/stores/SubscriptionsStore.ts index cc2867a938..7e33ad495e 100644 --- a/app/stores/SubscriptionsStore.ts +++ b/app/stores/SubscriptionsStore.ts @@ -1,5 +1,6 @@ import invariant from "invariant"; import { action } from "mobx"; +import { SubscriptionEventType } from "@shared/types"; import Subscription from "~/models/Subscription"; import { client } from "~/utils/ApiClient"; import { AuthorizationError, NotFoundError } from "~/utils/errors"; @@ -14,8 +15,15 @@ export default class SubscriptionsStore extends Store { } @action - async fetchOne({ documentId, event }: { documentId: string; event: string }) { - const subscription = this.getByDocumentId(documentId); + async fetchOne( + options: + | { documentId: string; event: SubscriptionEventType.Document } + | { collectionId: string; event: SubscriptionEventType.Collection } + ) { + const subscription = + options.event === SubscriptionEventType.Collection + ? this.getByCollectionId(options.collectionId) + : this.getByDocumentId(options.documentId); if (subscription) { return subscription; @@ -24,10 +32,7 @@ export default class SubscriptionsStore extends Store { this.isFetching = true; try { - const res = await client.post(`/${this.apiEndpoint}.info`, { - documentId, - event, - }); + const res = await client.post(`/${this.apiEndpoint}.info`, options); invariant(res?.data, "Data should be available"); return this.add(res.data); } catch (err) { @@ -42,4 +47,7 @@ export default class SubscriptionsStore extends Store { getByDocumentId = (documentId: string): Subscription | undefined => this.find({ documentId }); + + getByCollectionId = (collectionId: string): Subscription | undefined => + this.find({ collectionId }); } diff --git a/server/queues/processors/DocumentSubscriptionProcessor.ts b/server/queues/processors/DocumentSubscriptionProcessor.ts index 3a7041d988..45cfe327d4 100644 --- a/server/queues/processors/DocumentSubscriptionProcessor.ts +++ b/server/queues/processors/DocumentSubscriptionProcessor.ts @@ -44,9 +44,9 @@ export default class DocumentSubscriptionProcessor extends BaseProcessor { }, batchLimit: 10, }, - async groupUsers => { + async (groupUsers) => { await Promise.all( - groupUsers.map(groupUser => + groupUsers.map((groupUser) => DocumentSubscriptionTask.schedule({ ...event, name: userEventName,