From 879aa2c3d820dcb532b793c98476c9b4bef6d167 Mon Sep 17 00:00:00 2001 From: Philip Okugbe <16838612+Philipinho@users.noreply.github.com> Date: Tue, 31 Mar 2026 16:03:59 +0100 Subject: [PATCH] feat: page update notifications (#2074) * feat: watchers notification and email preferences * fix: email copy * digests * clean up * fix * clean up * move backlinks queue-up to history processor * fix * fix keys * feat: group notifications * filter * adjust email digest window --- .../public/locales/en-US/translation.json | 18 ++ .../components/mention/mention-list.tsx | 1 + .../components/slash-menu/command-list.tsx | 13 +- .../components/slash-menu/render-items.ts | 2 +- .../components/notification-item.tsx | 3 + .../components/notification-list.tsx | 10 +- .../components/notification-popover.tsx | 22 +- .../notification/notification.module.css | 1 + .../queries/notification-query.ts | 6 +- .../services/notification-service.ts | 1 + .../notification/types/notification.types.ts | 5 +- .../components/header/page-header-menu.tsx | 27 ++ .../features/page/queries/watcher-query.ts | 43 +++ .../features/page/services/watcher-service.ts | 16 ++ .../user/components/notification-pref.tsx | 117 ++++++++ .../src/features/user/types/user.types.ts | 12 + .../settings/account/account-preferences.tsx | 5 + .../extensions/persistence.extension.ts | 10 - .../processors/history.processor.ts | 53 +++- .../core/notification/dto/notification.dto.ts | 10 +- .../notification/notification.constants.ts | 38 +++ .../notification/notification.controller.ts | 7 +- .../core/notification/notification.module.ts | 2 + .../notification/notification.processor.ts | 16 ++ .../core/notification/notification.service.ts | 56 +++- .../services/comment.notification.ts | 6 + .../page-update-email-rate-limiter.ts | 43 +++ .../services/page.notification.ts | 256 +++++++++++++++++- .../src/core/user/dto/update-user.dto.ts | 20 ++ apps/server/src/core/user/user.service.ts | 19 ++ .../src/core/watcher/watcher.controller.ts | 30 +- .../server/src/core/watcher/watcher.module.ts | 7 +- .../repos/notification/notification.repo.ts | 38 ++- .../src/database/repos/user/user.repo.ts | 19 ++ .../queue/constants/queue.constants.ts | 1 + .../queue/constants/queue.interface.ts | 7 + .../emails/page-update-digest-email.tsx | 76 ++++++ .../emails/page-update-email.tsx | 36 +++ .../transactional/partials/partials.tsx | 4 + 39 files changed, 983 insertions(+), 73 deletions(-) create mode 100644 apps/client/src/features/page/queries/watcher-query.ts create mode 100644 apps/client/src/features/page/services/watcher-service.ts create mode 100644 apps/client/src/features/user/components/notification-pref.tsx create mode 100644 apps/server/src/core/notification/services/page-update-email-rate-limiter.ts create mode 100644 apps/server/src/integrations/transactional/emails/page-update-digest-email.tsx create mode 100644 apps/server/src/integrations/transactional/emails/page-update-email.tsx diff --git a/apps/client/public/locales/en-US/translation.json b/apps/client/public/locales/en-US/translation.json index 19149612..b1c1ed6c 100644 --- a/apps/client/public/locales/en-US/translation.json +++ b/apps/client/public/locales/en-US/translation.json @@ -674,6 +674,24 @@ "{{name}} mentioned you on a page": "{{name}} mentioned you on a page.", "{{name}} gave you edit access to a page": "{{name}} gave you edit access to a page.", "{{name}} gave you view access to a page": "{{name}} gave you view access to a page.", + "{{name}} updated a page": "{{name}} updated a page.", + "Watch page": "Watch page", + "Stop watching": "Stop watching", + "Email notifications": "Email notifications", + "Page updates": "Page updates", + "Get notified when pages you watch are updated.": "Get notified when pages you watch are updated.", + "Page mentions": "Page mentions", + "Get notified when someone mentions you on a page.": "Get notified when someone mentions you on a page.", + "Comment mentions": "Comment mentions", + "Get notified when someone mentions you in a comment.": "Get notified when someone mentions you in a comment.", + "New comments": "New comments", + "Get notified about new comments on threads you participate in.": "Get notified about new comments on threads you participate in.", + "Resolved comments": "Resolved comments", + "Get notified when your comment is resolved.": "Get notified when your comment is resolved.", + "You are now watching this page": "You are now watching this page", + "You are no longer watching this page": "You are no longer watching this page", + "Direct": "Direct", + "Updates": "Updates", "Today": "Today", "Yesterday": "Yesterday", "This week": "This week", diff --git a/apps/client/src/features/editor/components/mention/mention-list.tsx b/apps/client/src/features/editor/components/mention/mention-list.tsx index f086df49..af6f8a1d 100644 --- a/apps/client/src/features/editor/components/mention/mention-list.tsx +++ b/apps/client/src/features/editor/components/mention/mention-list.tsx @@ -294,6 +294,7 @@ const MentionList = forwardRef((props, ref) => { w={popupWidth} scrollbars={"y"} scrollbarSize={6} + overscrollBehavior={"contain"} styles={{ content: { minWidth: 0 } }} > {renderItems?.map((item, index) => { diff --git a/apps/client/src/features/editor/components/slash-menu/command-list.tsx b/apps/client/src/features/editor/components/slash-menu/command-list.tsx index ab1dcafd..54d6cd17 100644 --- a/apps/client/src/features/editor/components/slash-menu/command-list.tsx +++ b/apps/client/src/features/editor/components/slash-menu/command-list.tsx @@ -87,7 +87,13 @@ const CommandList = ({ return flatItems.length > 0 ? ( - + {Object.entries(items).map(([category, categoryItems]) => (
@@ -103,10 +109,7 @@ const CommandList = ({ })} > - + diff --git a/apps/client/src/features/editor/components/slash-menu/render-items.ts b/apps/client/src/features/editor/components/slash-menu/render-items.ts index 057e8214..041aa036 100644 --- a/apps/client/src/features/editor/components/slash-menu/render-items.ts +++ b/apps/client/src/features/editor/components/slash-menu/render-items.ts @@ -49,7 +49,7 @@ const renderItems = () => { getReferenceClientRect = props.clientRect; popup = document.createElement("div"); - popup.style.zIndex = "9999"; + popup.style.zIndex = "199"; popup.style.position = "absolute"; popup.style.top = "0"; popup.style.left = "0"; diff --git a/apps/client/src/features/notification/components/notification-item.tsx b/apps/client/src/features/notification/components/notification-item.tsx index 0ef81e44..0fd4f44b 100644 --- a/apps/client/src/features/notification/components/notification-item.tsx +++ b/apps/client/src/features/notification/components/notification-item.tsx @@ -49,6 +49,8 @@ export function NotificationItem({ return notification.data?.role === "writer" ? "{{name}} gave you edit access to a page" : "{{name}} gave you view access to a page"; + case "page.updated": + return "{{name}} updated a page"; default: return ""; } @@ -75,6 +77,7 @@ export function NotificationItem({ }; const handleMarkRead = (e: React.MouseEvent) => { + e.preventDefault(); e.stopPropagation(); markReadIfNeeded(); }; diff --git a/apps/client/src/features/notification/components/notification-list.tsx b/apps/client/src/features/notification/components/notification-list.tsx index 4c992c57..4cd30677 100644 --- a/apps/client/src/features/notification/components/notification-list.tsx +++ b/apps/client/src/features/notification/components/notification-list.tsx @@ -3,17 +3,23 @@ import { IconBellOff } from "@tabler/icons-react"; import { useTranslation } from "react-i18next"; import { useEffect, useRef } from "react"; import { NotificationItem } from "./notification-item"; -import { INotification, NotificationFilter } from "../types/notification.types"; +import { + INotification, + NotificationFilter, + NotificationTab, +} from "../types/notification.types"; import { groupNotificationsByTime } from "../notification.utils"; import { useNotificationsQuery } from "../queries/notification-query"; import classes from "../notification.module.css"; type NotificationListProps = { + tab: NotificationTab; filter: NotificationFilter; onNavigate: () => void; }; export function NotificationList({ + tab, filter, onNavigate, }: NotificationListProps) { @@ -24,7 +30,7 @@ export function NotificationList({ hasNextPage, fetchNextPage, isFetchingNextPage, - } = useNotificationsQuery(); + } = useNotificationsQuery(tab as string); const sentinelRef = useRef(null); diff --git a/apps/client/src/features/notification/components/notification-popover.tsx b/apps/client/src/features/notification/components/notification-popover.tsx index 8ebfedad..161ac1e6 100644 --- a/apps/client/src/features/notification/components/notification-popover.tsx +++ b/apps/client/src/features/notification/components/notification-popover.tsx @@ -6,6 +6,7 @@ import { Menu, Popover, ScrollArea, + Tabs, Text, Tooltip, } from "@mantine/core"; @@ -18,15 +19,20 @@ import { } from "@tabler/icons-react"; import { useTranslation } from "react-i18next"; import { NotificationList } from "./notification-list"; -import { NotificationFilter } from "../types/notification.types"; +import { + NotificationFilter, + NotificationTab, +} from "../types/notification.types"; import { useMarkAllReadMutation, useUnreadCountQuery, } from "../queries/notification-query"; +import classes from "../notification.module.css"; export function NotificationPopover() { const { t } = useTranslation(); const [opened, setOpened] = useState(false); + const [tab, setTab] = useState("direct"); const [filter, setFilter] = useState("all"); const { data: unreadData } = useUnreadCountQuery(); @@ -125,13 +131,27 @@ export function NotificationPopover() { + setTab(value as NotificationTab)} + variant="default" + color="dark" + > + + {t("Direct")} + {t("Updates")} + + + setOpened(false)} /> diff --git a/apps/client/src/features/notification/notification.module.css b/apps/client/src/features/notification/notification.module.css index d56986ac..09802628 100644 --- a/apps/client/src/features/notification/notification.module.css +++ b/apps/client/src/features/notification/notification.module.css @@ -13,3 +13,4 @@ .divider { border-color: light-dark(var(--mantine-color-gray-2), var(--mantine-color-dark-5)); } + diff --git a/apps/client/src/features/notification/queries/notification-query.ts b/apps/client/src/features/notification/queries/notification-query.ts index 363482b1..92c46560 100644 --- a/apps/client/src/features/notification/queries/notification-query.ts +++ b/apps/client/src/features/notification/queries/notification-query.ts @@ -15,10 +15,10 @@ import { export const NOTIFICATION_KEY = ["notifications"]; export const UNREAD_COUNT_KEY = ["notifications", "unread-count"]; -export function useNotificationsQuery() { +export function useNotificationsQuery(type?: string) { return useInfiniteQuery({ - queryKey: NOTIFICATION_KEY, - queryFn: ({ pageParam }) => getNotifications({ cursor: pageParam }), + queryKey: [...NOTIFICATION_KEY, type], + queryFn: ({ pageParam }) => getNotifications({ cursor: pageParam, type }), initialPageParam: undefined as string | undefined, getNextPageParam: (lastPage) => lastPage.meta.hasNextPage ? lastPage.meta.nextCursor : undefined, diff --git a/apps/client/src/features/notification/services/notification-service.ts b/apps/client/src/features/notification/services/notification-service.ts index 8adf4909..7e4b8d2c 100644 --- a/apps/client/src/features/notification/services/notification-service.ts +++ b/apps/client/src/features/notification/services/notification-service.ts @@ -5,6 +5,7 @@ import { IPagination } from "@/lib/types"; export async function getNotifications(params: { limit?: number; cursor?: string; + type?: string; }): Promise> { const req = await api.post>( "/notifications", diff --git a/apps/client/src/features/notification/types/notification.types.ts b/apps/client/src/features/notification/types/notification.types.ts index 811805d0..f64e3648 100644 --- a/apps/client/src/features/notification/types/notification.types.ts +++ b/apps/client/src/features/notification/types/notification.types.ts @@ -3,7 +3,8 @@ export type NotificationType = | "comment.created" | "comment.resolved" | "page.user_mention" - | "page.permission_granted"; + | "page.permission_granted" + | "page.updated"; export type INotification = { id: string; @@ -38,3 +39,5 @@ export type INotification = { }; export type NotificationFilter = "all" | "unread"; + +export type NotificationTab = "direct" | "updates" | "all"; diff --git a/apps/client/src/features/page/components/header/page-header-menu.tsx b/apps/client/src/features/page/components/header/page-header-menu.tsx index 2660b2ba..5ba9d40e 100644 --- a/apps/client/src/features/page/components/header/page-header-menu.tsx +++ b/apps/client/src/features/page/components/header/page-header-menu.tsx @@ -3,6 +3,8 @@ import { IconArrowRight, IconArrowsHorizontal, IconDots, + IconEye, + IconEyeOff, IconFileExport, IconHistory, IconLink, @@ -40,6 +42,11 @@ import { PageStateSegmentedControl } from "@/features/user/components/page-state import MovePageModal from "@/features/page/components/move-page-modal.tsx"; import { useTimeAgo } from "@/hooks/use-time-ago.tsx"; import { PageShareModal } from "@/ee/page-permission"; +import { + useWatchStatusQuery, + useWatchPageMutation, + useUnwatchPageMutation, +} from "@/features/page/queries/watcher-query"; interface PageHeaderMenuProps { readOnly?: boolean; @@ -123,6 +130,9 @@ function PageActionMenu({ readOnly }: PageActionMenuProps) { ] = useDisclosure(false); const [pageEditor] = useAtom(pageEditorAtom); const pageUpdatedAt = useTimeAgo(page?.updatedAt); + const { data: watchStatus } = useWatchStatusQuery(page?.id); + const watchPage = useWatchPageMutation(); + const unwatchPage = useUnwatchPageMutation(); const handleCopyLink = () => { const pageUrl = @@ -185,6 +195,23 @@ function PageActionMenu({ readOnly }: PageActionMenuProps) { > {t("Copy as Markdown")} + + {watchStatus?.watching ? ( + } + onClick={() => unwatchPage.mutate(page.id)} + > + {t("Stop watching")} + + ) : ( + } + onClick={() => watchPage.mutate(page.id)} + > + {t("Watch page")} + + )} + }> diff --git a/apps/client/src/features/page/queries/watcher-query.ts b/apps/client/src/features/page/queries/watcher-query.ts new file mode 100644 index 00000000..0c9eba0f --- /dev/null +++ b/apps/client/src/features/page/queries/watcher-query.ts @@ -0,0 +1,43 @@ +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { + watchPage, + unwatchPage, + getWatchStatus, +} from "@/features/page/services/watcher-service"; +import { notifications } from "@mantine/notifications"; +import { useTranslation } from "react-i18next"; + +const WATCHER_KEY = "watcher"; + +export function useWatchStatusQuery(pageId: string) { + return useQuery({ + queryKey: [WATCHER_KEY, pageId], + queryFn: () => getWatchStatus(pageId), + enabled: !!pageId, + staleTime: 60_000, + }); +} + +export function useWatchPageMutation() { + const queryClient = useQueryClient(); + const { t } = useTranslation(); + return useMutation({ + mutationFn: (pageId: string) => watchPage(pageId), + onSuccess: (_data, pageId) => { + queryClient.setQueryData([WATCHER_KEY, pageId], { watching: true }); + notifications.show({ message: t("You are now watching this page") }); + }, + }); +} + +export function useUnwatchPageMutation() { + const queryClient = useQueryClient(); + const { t } = useTranslation(); + return useMutation({ + mutationFn: (pageId: string) => unwatchPage(pageId), + onSuccess: (_data, pageId) => { + queryClient.setQueryData([WATCHER_KEY, pageId], { watching: false }); + notifications.show({ message: t("You are no longer watching this page") }); + }, + }); +} diff --git a/apps/client/src/features/page/services/watcher-service.ts b/apps/client/src/features/page/services/watcher-service.ts new file mode 100644 index 00000000..d0c1416b --- /dev/null +++ b/apps/client/src/features/page/services/watcher-service.ts @@ -0,0 +1,16 @@ +import api from "@/lib/api-client"; + +export async function watchPage(pageId: string): Promise<{ watching: boolean }> { + const req = await api.post<{ watching: boolean }>("/pages/watch", { pageId }); + return req.data; +} + +export async function unwatchPage(pageId: string): Promise<{ watching: boolean }> { + const req = await api.post<{ watching: boolean }>("/pages/unwatch", { pageId }); + return req.data; +} + +export async function getWatchStatus(pageId: string): Promise<{ watching: boolean }> { + const req = await api.post<{ watching: boolean }>("/pages/watch-status", { pageId }); + return req.data; +} diff --git a/apps/client/src/features/user/components/notification-pref.tsx b/apps/client/src/features/user/components/notification-pref.tsx new file mode 100644 index 00000000..e8a983ed --- /dev/null +++ b/apps/client/src/features/user/components/notification-pref.tsx @@ -0,0 +1,117 @@ +import { userAtom } from "@/features/user/atoms/current-user-atom.ts"; +import { updateUser } from "@/features/user/services/user-service.ts"; +import { IUser, IUserSettings } from "@/features/user/types/user.types.ts"; +import { Switch, Text, Title, Stack } from "@mantine/core"; +import { useAtom } from "jotai"; +import React, { useState } from "react"; +import { useTranslation } from "react-i18next"; +import { + ResponsiveSettingsRow, + ResponsiveSettingsContent, + ResponsiveSettingsControl, +} from "@/components/ui/responsive-settings-row"; + +type NotificationKey = keyof NonNullable; + +const notificationItems: { + key: NotificationKey; + dtoField: keyof IUser; + label: string; + description: string; +}[] = [ + { + key: "page.updated", + dtoField: "notificationPageUpdates", + label: "Page updates", + description: "Get notified when pages you watch are updated.", + }, + { + key: "page.userMention", + dtoField: "notificationPageUserMention", + label: "Page mentions", + description: "Get notified when someone mentions you on a page.", + }, + { + key: "comment.userMention", + dtoField: "notificationCommentUserMention", + label: "Comment mentions", + description: "Get notified when someone mentions you in a comment.", + }, + { + key: "comment.created", + dtoField: "notificationCommentCreated", + label: "New comments", + description: + "Get notified about new comments on threads you participate in.", + }, + { + key: "comment.resolved", + dtoField: "notificationCommentResolved", + label: "Resolved comments", + description: "Get notified when your comment is resolved.", + }, +]; + +function NotificationToggle({ + settingKey, + dtoField, + label, + description, +}: { + settingKey: NotificationKey; + dtoField: keyof IUser; + label: string; + description: string; +}) { + const { t } = useTranslation(); + const [user, setUser] = useAtom(userAtom); + const [checked, setChecked] = useState( + user.settings?.notifications?.[settingKey] !== false, + ); + + const handleChange = async (event: React.ChangeEvent) => { + const value = event.currentTarget.checked; + setChecked(value); + try { + const updatedUser = await updateUser({ [dtoField]: value } as any); + setUser(updatedUser); + } catch { + setChecked(!value); + } + }; + + return ( + + + {t(label)} + + {t(description)} + + + + + + + + ); +} + +export default function NotificationPref() { + const { t } = useTranslation(); + + return ( + + {t("Email notifications")} + + {notificationItems.map((item) => ( + + ))} + + ); +} diff --git a/apps/client/src/features/user/types/user.types.ts b/apps/client/src/features/user/types/user.types.ts index 80d86706..75d45bfd 100644 --- a/apps/client/src/features/user/types/user.types.ts +++ b/apps/client/src/features/user/types/user.types.ts @@ -20,6 +20,11 @@ export interface IUser { deletedAt: Date; fullPageWidth: boolean; // used for update pageEditMode: string; // used for update + notificationPageUpdates: boolean; // used for update + notificationPageUserMention: boolean; // used for update + notificationCommentUserMention: boolean; // used for update + notificationCommentCreated: boolean; // used for update + notificationCommentResolved: boolean; // used for update hasGeneratedPassword?: boolean; } @@ -33,6 +38,13 @@ export interface IUserSettings { fullPageWidth: boolean; pageEditMode: string; }; + notifications?: { + "page.updated"?: boolean; + "page.userMention"?: boolean; + "comment.userMention"?: boolean; + "comment.created"?: boolean; + "comment.resolved"?: boolean; + }; } export enum PageEditMode { diff --git a/apps/client/src/pages/settings/account/account-preferences.tsx b/apps/client/src/pages/settings/account/account-preferences.tsx index f082ea1b..caedc1b0 100644 --- a/apps/client/src/pages/settings/account/account-preferences.tsx +++ b/apps/client/src/pages/settings/account/account-preferences.tsx @@ -3,6 +3,7 @@ import AccountLanguage from "@/features/user/components/account-language.tsx"; import AccountTheme from "@/features/user/components/account-theme.tsx"; import PageWidthPref from "@/features/user/components/page-width-pref.tsx"; import PageEditPref from "@/features/user/components/page-state-pref"; +import NotificationPref from "@/features/user/components/notification-pref"; import { getAppName } from "@/lib/config.ts"; import { Divider } from "@mantine/core"; import { Helmet } from "react-helmet-async"; @@ -33,6 +34,10 @@ export default function AccountPreferences() { + + + + ); } diff --git a/apps/server/src/collaboration/extensions/persistence.extension.ts b/apps/server/src/collaboration/extensions/persistence.extension.ts index 642d0761..d32e4778 100644 --- a/apps/server/src/collaboration/extensions/persistence.extension.ts +++ b/apps/server/src/collaboration/extensions/persistence.extension.ts @@ -18,12 +18,10 @@ import { QueueJob, QueueName } from '../../integrations/queue/constants'; import { Queue } from 'bullmq'; import { extractMentions, - extractPageMentions, extractUserMentions, } from '../../common/helpers/prosemirror/utils'; import { isDeepStrictEqual } from 'node:util'; import { - IPageBacklinkJob, IPageHistoryJob, IPageMentionNotificationJob, } from '../../integrations/queue/constants/queue.interface'; @@ -43,7 +41,6 @@ export class PersistenceExtension implements Extension { constructor( private readonly pageRepo: PageRepo, @InjectKysely() private readonly db: KyselyDB, - @InjectQueue(QueueName.GENERAL_QUEUE) private generalQueue: Queue, @InjectQueue(QueueName.AI_QUEUE) private aiQueue: Queue, @InjectQueue(QueueName.HISTORY_QUEUE) private historyQueue: Queue, @InjectQueue(QueueName.NOTIFICATION_QUEUE) private notificationQueue: Queue, @@ -165,13 +162,6 @@ export class PersistenceExtension implements Extension { await this.collabHistory.addContributors(pageId, editingUserIds); const mentions = extractMentions(tiptapJson); - const pageMentions = extractPageMentions(mentions); - - await this.generalQueue.add(QueueJob.PAGE_BACKLINKS, { - pageId: pageId, - workspaceId: page.workspaceId, - mentions: pageMentions, - } as IPageBacklinkJob); const userMentions = extractUserMentions(mentions); const oldMentions = page.content ? extractMentions(page.content) : []; diff --git a/apps/server/src/collaboration/processors/history.processor.ts b/apps/server/src/collaboration/processors/history.processor.ts index 315dba0b..d7e27f60 100644 --- a/apps/server/src/collaboration/processors/history.processor.ts +++ b/apps/server/src/collaboration/processors/history.processor.ts @@ -1,8 +1,17 @@ import { Logger, OnModuleDestroy } from '@nestjs/common'; import { OnWorkerEvent, Processor, WorkerHost } from '@nestjs/bullmq'; -import { Job } from 'bullmq'; +import { InjectQueue } from '@nestjs/bullmq'; +import { Job, Queue } from 'bullmq'; import { QueueJob, QueueName } from '../../integrations/queue/constants'; -import { IPageHistoryJob } from '../../integrations/queue/constants/queue.interface'; +import { + IPageBacklinkJob, + IPageHistoryJob, + IPageUpdateNotificationJob, +} from '../../integrations/queue/constants/queue.interface'; +import { + extractMentions, + extractPageMentions, +} from '../../common/helpers/prosemirror/utils'; import { PageHistoryRepo } from '@docmost/db/repos/page/page-history.repo'; import { PageRepo } from '@docmost/db/repos/page/page.repo'; import { isDeepStrictEqual } from 'node:util'; @@ -18,6 +27,8 @@ export class HistoryProcessor extends WorkerHost implements OnModuleDestroy { private readonly pageRepo: PageRepo, private readonly collabHistory: CollabHistoryService, private readonly watcherService: WatcherService, + @InjectQueue(QueueName.NOTIFICATION_QUEUE) private notificationQueue: Queue, + @InjectQueue(QueueName.GENERAL_QUEUE) private generalQueue: Queue, ) { super(); } @@ -47,8 +58,7 @@ export class HistoryProcessor extends WorkerHost implements OnModuleDestroy { !lastHistory || !isDeepStrictEqual(lastHistory.content, page.content) ) { - const contributorIds = - await this.collabHistory.popContributors(pageId); + const contributorIds = await this.collabHistory.popContributors(pageId); try { await this.watcherService.addPageWatchers( @@ -61,12 +71,39 @@ export class HistoryProcessor extends WorkerHost implements OnModuleDestroy { await this.pageHistoryRepo.saveHistory(page, { contributorIds }); this.logger.debug(`History created for page: ${pageId}`); } catch (err) { - await this.collabHistory.addContributors( - pageId, - contributorIds, - ); + await this.collabHistory.addContributors(pageId, contributorIds); throw err; } + + const mentions = extractMentions(page.content); + const pageMentions = extractPageMentions(mentions); + + await this.generalQueue + .add(QueueJob.PAGE_BACKLINKS, { + pageId, + workspaceId: page.workspaceId, + mentions: pageMentions, + } as IPageBacklinkJob) + .catch((err) => { + this.logger.error( + `Failed to queue backlinks for ${pageId}: ${err.message}`, + ); + }); + + if (contributorIds.length > 0 && lastHistory?.content) { + await this.notificationQueue + .add(QueueJob.PAGE_UPDATED, { + pageId, + spaceId: page.spaceId, + workspaceId: page.workspaceId, + actorIds: contributorIds, + } as IPageUpdateNotificationJob) + .catch((err) => { + this.logger.error( + `Failed to queue page update notification for ${pageId}: ${err.message}`, + ); + }); + } } } catch (err) { throw err; diff --git a/apps/server/src/core/notification/dto/notification.dto.ts b/apps/server/src/core/notification/dto/notification.dto.ts index 0b0bde94..b583c746 100644 --- a/apps/server/src/core/notification/dto/notification.dto.ts +++ b/apps/server/src/core/notification/dto/notification.dto.ts @@ -1,4 +1,5 @@ -import { IsArray, IsOptional, IsUUID } from 'class-validator'; +import { IsArray, IsIn, IsOptional, IsString, IsUUID } from 'class-validator'; +import { PaginationOptions } from '@docmost/db/pagination/pagination-options'; export class NotificationIdDto { @IsUUID() @@ -11,3 +12,10 @@ export class MarkNotificationsReadDto { @IsOptional() notificationIds?: string[]; } + +export class ListNotificationsDto extends PaginationOptions { + @IsOptional() + @IsString() + @IsIn(['direct', 'updates', 'all']) + type?: 'direct' | 'updates' | 'all' = 'all'; +} diff --git a/apps/server/src/core/notification/notification.constants.ts b/apps/server/src/core/notification/notification.constants.ts index 56d2ecad..8f7f5049 100644 --- a/apps/server/src/core/notification/notification.constants.ts +++ b/apps/server/src/core/notification/notification.constants.ts @@ -4,7 +4,45 @@ export const NotificationType = { COMMENT_RESOLVED: 'comment.resolved', PAGE_USER_MENTION: 'page.user_mention', PAGE_PERMISSION_GRANTED: 'page.permission_granted', + PAGE_UPDATED: 'page.updated', } as const; export type NotificationType = (typeof NotificationType)[keyof typeof NotificationType]; + +export type NotificationSettingKey = + | 'page.updated' + | 'page.userMention' + | 'comment.userMention' + | 'comment.created' + | 'comment.resolved'; + +export const NotificationTypeToSettingKey: Partial< + Record +> = { + [NotificationType.PAGE_UPDATED]: 'page.updated', + [NotificationType.PAGE_USER_MENTION]: 'page.userMention', + [NotificationType.COMMENT_USER_MENTION]: 'comment.userMention', + [NotificationType.COMMENT_CREATED]: 'comment.created', + [NotificationType.COMMENT_RESOLVED]: 'comment.resolved', +}; + +export type NotificationTab = 'direct' | 'updates' | 'all'; + +export const DIRECT_NOTIFICATION_TYPES: NotificationType[] = [ + NotificationType.COMMENT_USER_MENTION, + NotificationType.COMMENT_CREATED, + NotificationType.COMMENT_RESOLVED, + NotificationType.PAGE_USER_MENTION, + NotificationType.PAGE_PERMISSION_GRANTED, +]; + +export const UPDATES_NOTIFICATION_TYPES: NotificationType[] = [ + NotificationType.PAGE_UPDATED, +]; + +export function getTypesForTab(tab: NotificationTab): NotificationType[] | undefined { + if (tab === 'direct') return DIRECT_NOTIFICATION_TYPES; + if (tab === 'updates') return UPDATES_NOTIFICATION_TYPES; + return undefined; +} diff --git a/apps/server/src/core/notification/notification.controller.ts b/apps/server/src/core/notification/notification.controller.ts index d041414f..be5ee1d3 100644 --- a/apps/server/src/core/notification/notification.controller.ts +++ b/apps/server/src/core/notification/notification.controller.ts @@ -9,9 +9,8 @@ import { import { NotificationService } from './notification.service'; import { AuthUser } from '../../common/decorators/auth-user.decorator'; import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard'; -import { PaginationOptions } from '@docmost/db/pagination/pagination-options'; import { User } from '@docmost/db/types/entity.types'; -import { MarkNotificationsReadDto } from './dto/notification.dto'; +import { ListNotificationsDto, MarkNotificationsReadDto } from './dto/notification.dto'; @UseGuards(JwtAuthGuard) @Controller('notifications') @@ -21,10 +20,10 @@ export class NotificationController { @HttpCode(HttpStatus.OK) @Post('/') async getNotifications( - @Body() pagination: PaginationOptions, + @Body() dto: ListNotificationsDto, @AuthUser() user: User, ) { - return this.notificationService.findByUserId(user.id, pagination); + return this.notificationService.findByUserId(user.id, dto, dto.type); } @HttpCode(HttpStatus.OK) diff --git a/apps/server/src/core/notification/notification.module.ts b/apps/server/src/core/notification/notification.module.ts index a142eaf8..83778294 100644 --- a/apps/server/src/core/notification/notification.module.ts +++ b/apps/server/src/core/notification/notification.module.ts @@ -4,6 +4,7 @@ import { NotificationController } from './notification.controller'; import { NotificationProcessor } from './notification.processor'; import { CommentNotificationService } from './services/comment.notification'; import { PageNotificationService } from './services/page.notification'; +import { PageUpdateEmailRateLimiter } from './services/page-update-email-rate-limiter'; @Module({ imports: [], @@ -13,6 +14,7 @@ import { PageNotificationService } from './services/page.notification'; NotificationProcessor, CommentNotificationService, PageNotificationService, + PageUpdateEmailRateLimiter, ], exports: [NotificationService], }) diff --git a/apps/server/src/core/notification/notification.processor.ts b/apps/server/src/core/notification/notification.processor.ts index f7c8b577..e3d3a883 100644 --- a/apps/server/src/core/notification/notification.processor.ts +++ b/apps/server/src/core/notification/notification.processor.ts @@ -8,6 +8,7 @@ import { ICommentNotificationJob, ICommentResolvedNotificationJob, IPageMentionNotificationJob, + IPageUpdateNotificationJob, IPermissionGrantedNotificationJob, } from '../../integrations/queue/constants/queue.interface'; import { CommentNotificationService } from './services/comment.notification'; @@ -35,6 +36,7 @@ export class NotificationProcessor | ICommentNotificationJob | ICommentResolvedNotificationJob | IPageMentionNotificationJob + | IPageUpdateNotificationJob | IPermissionGrantedNotificationJob, void >, @@ -76,6 +78,20 @@ export class NotificationProcessor break; } + case QueueJob.PAGE_UPDATED: { + await this.pageNotificationService.processPageUpdate( + job.data as IPageUpdateNotificationJob, + appUrl, + ); + break; + } + + case QueueJob.PAGE_UPDATE_DIGEST: { + const { userId } = job.data as unknown as { userId: string }; + await this.pageNotificationService.processDigest(userId, appUrl); + break; + } + default: this.logger.warn(`Unknown notification job: ${job.name}`); } diff --git a/apps/server/src/core/notification/notification.service.ts b/apps/server/src/core/notification/notification.service.ts index 493b673e..1f88bf59 100644 --- a/apps/server/src/core/notification/notification.service.ts +++ b/apps/server/src/core/notification/notification.service.ts @@ -6,6 +6,8 @@ import { InsertableNotification } from '@docmost/db/types/entity.types'; import { PaginationOptions } from '@docmost/db/pagination/pagination-options'; import { WsGateway } from '../../ws/ws.gateway'; import { MailService } from '../../integrations/mail/mail.service'; +import { NotificationTab, NotificationType, NotificationTypeToSettingKey } from './notification.constants'; +import { PagePermissionRepo } from '@docmost/db/repos/page/page-permission.repo'; @Injectable() export class NotificationService { @@ -13,12 +15,23 @@ export class NotificationService { constructor( private readonly notificationRepo: NotificationRepo, + private readonly pagePermissionRepo: PagePermissionRepo, private readonly wsGateway: WsGateway, private readonly mailService: MailService, @InjectKysely() private readonly db: KyselyDB, ) {} async create(data: InsertableNotification) { + const user = await this.db + .selectFrom('users') + .select(['id']) + .where('id', '=', data.userId) + .where('deletedAt', 'is', null) + .where('deactivatedAt', 'is', null) + .executeTakeFirst(); + + if (!user) return null; + const notification = await this.notificationRepo.insert(data); this.wsGateway.server @@ -28,8 +41,35 @@ export class NotificationService { return notification; } - async findByUserId(userId: string, pagination: PaginationOptions) { - return this.notificationRepo.findByUserId(userId, pagination); + async findByUserId( + userId: string, + pagination: PaginationOptions, + type: NotificationTab = 'all', + ) { + const result = await this.notificationRepo.findByUserId( + userId, + pagination, + type, + ); + + const pageIds = result.items + .map((n: any) => n.pageId) + .filter(Boolean); + + if (pageIds.length > 0) { + const accessiblePageIds = + await this.pagePermissionRepo.filterAccessiblePageIds({ + pageIds, + userId, + }); + const accessibleSet = new Set(accessiblePageIds); + + result.items = result.items.filter( + (n: any) => !n.pageId || accessibleSet.has(n.pageId), + ); + } + + return result; } async getUnreadCount(userId: string) { @@ -53,17 +93,27 @@ export class NotificationService { notificationId: string, subject: string, template: any, + type?: NotificationType, ) { try { const user = await this.db .selectFrom('users') - .select(['email']) + .select(['email', 'settings']) .where('id', '=', userId) .where('deletedAt', 'is', null) + .where('deactivatedAt', 'is', null) .executeTakeFirst(); if (!user?.email) return; + if (type) { + const settingKey = NotificationTypeToSettingKey[type]; + if (settingKey) { + const settings = user.settings as any; + if (settings?.notifications?.[settingKey] === false) return; + } + } + await this.mailService.sendToQueue({ to: user.email, subject, diff --git a/apps/server/src/core/notification/services/comment.notification.ts b/apps/server/src/core/notification/services/comment.notification.ts index e75da302..c79c2895 100644 --- a/apps/server/src/core/notification/services/comment.notification.ts +++ b/apps/server/src/core/notification/services/comment.notification.ts @@ -86,12 +86,14 @@ export class CommentNotificationService { spaceId, commentId, }); + if (!notification) continue; await this.notificationService.queueEmail( userId, notification.id, `${actor.name} mentioned you in a comment`, CommentMentionEmail({ actorName: actor.name, pageTitle, pageUrl }), + NotificationType.COMMENT_USER_MENTION, ); notifiedUserIds.add(userId); @@ -110,12 +112,14 @@ export class CommentNotificationService { spaceId, commentId, }); + if (!notification) continue; await this.notificationService.queueEmail( recipientId, notification.id, `${actor.name} commented on ${pageTitle}`, CommentCreateEmail({ actorName: actor.name, pageTitle, pageUrl }), + NotificationType.COMMENT_CREATED, ); } } @@ -171,6 +175,7 @@ export class CommentNotificationService { spaceId, commentId, }); + if (!notification) return; const subject = `${actor.name} resolved a comment on ${pageTitle}`; @@ -179,6 +184,7 @@ export class CommentNotificationService { notification.id, subject, CommentResolvedEmail({ actorName: actor.name, pageTitle, pageUrl }), + NotificationType.COMMENT_RESOLVED, ); } diff --git a/apps/server/src/core/notification/services/page-update-email-rate-limiter.ts b/apps/server/src/core/notification/services/page-update-email-rate-limiter.ts new file mode 100644 index 00000000..59867f41 --- /dev/null +++ b/apps/server/src/core/notification/services/page-update-email-rate-limiter.ts @@ -0,0 +1,43 @@ +import { Injectable } from '@nestjs/common'; +import { RedisService } from '@nestjs-labs/nestjs-ioredis'; +import type { Redis } from 'ioredis'; + +const KEY_PREFIX = 'page-update:emails:'; +const DIGEST_PREFIX = 'page-update:digest:'; +const TTL_SECONDS = 86400; // 24 hours +const MAX_IMMEDIATE_EMAILS = 4; + +@Injectable() +export class PageUpdateEmailRateLimiter { + private readonly redis: Redis; + + constructor(private readonly redisService: RedisService) { + this.redis = this.redisService.getOrThrow(); + } + + async canSendEmail(userId: string): Promise { + const key = KEY_PREFIX + userId; + const count = await this.redis.incr(key); + await this.redis.expire(key, TTL_SECONDS, 'NX'); + return count <= MAX_IMMEDIATE_EMAILS; + } + + async addToDigest(userId: string, notificationId: string): Promise { + const key = DIGEST_PREFIX + userId; + const len = await this.redis.rpush(key, notificationId); + await this.redis.expire(key, TTL_SECONDS); + return len === 1; + } + + async popDigest(userId: string): Promise { + const key = DIGEST_PREFIX + userId; + const [ids] = await this.redis + .multi() + .lrange(key, 0, -1) + .del(key) + .exec(); + + return (ids?.[1] as string[]) ?? []; + } + +} diff --git a/apps/server/src/core/notification/services/page.notification.ts b/apps/server/src/core/notification/services/page.notification.ts index a8d951dd..9e5c75dd 100644 --- a/apps/server/src/core/notification/services/page.notification.ts +++ b/apps/server/src/core/notification/services/page.notification.ts @@ -1,25 +1,43 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; import { InjectKysely } from 'nestjs-kysely'; +import { InjectQueue } from '@nestjs/bullmq'; +import { Queue } from 'bullmq'; import { KyselyDB } from '@docmost/db/types/kysely.types'; import { IPageMentionNotificationJob, + IPageUpdateNotificationJob, IPermissionGrantedNotificationJob, } from '../../../integrations/queue/constants/queue.interface'; import { NotificationService } from '../notification.service'; import { NotificationType } from '../notification.constants'; +import { NotificationRepo } from '@docmost/db/repos/notification/notification.repo'; import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo'; import { PagePermissionRepo } from '@docmost/db/repos/page/page-permission.repo'; +import { WatcherRepo } from '@docmost/db/repos/watcher/watcher.repo'; +import { PageUpdateEmailRateLimiter } from './page-update-email-rate-limiter'; import { PageMentionEmail } from '@docmost/transactional/emails/page-mention-email'; +import { PageUpdateEmail } from '@docmost/transactional/emails/page-update-email'; +import { PageUpdateDigestEmail } from '@docmost/transactional/emails/page-update-digest-email'; import { PermissionGrantedEmail } from '@docmost/transactional/emails/permission-granted-email'; import { getPageTitle } from '../../../common/helpers'; +import { QueueJob, QueueName } from '../../../integrations/queue/constants'; + +const PAGE_UPDATE_COOLDOWN_HOURS = 7; +const DIGEST_DELAY_MS = 12 * 60 * 60 * 1000; // 12 hours @Injectable() export class PageNotificationService { + private readonly logger = new Logger(PageNotificationService.name); + constructor( @InjectKysely() private readonly db: KyselyDB, private readonly notificationService: NotificationService, + private readonly notificationRepo: NotificationRepo, private readonly spaceMemberRepo: SpaceMemberRepo, private readonly pagePermissionRepo: PagePermissionRepo, + private readonly watcherRepo: WatcherRepo, + private readonly rateLimiter: PageUpdateEmailRateLimiter, + @InjectQueue(QueueName.NOTIFICATION_QUEUE) private notificationQueue: Queue, ) {} async processPageMention(data: IPageMentionNotificationJob, appUrl: string) { @@ -41,10 +59,9 @@ export class PageNotificationService { ); const usersWithPageAccess = - await this.pagePermissionRepo.getUserIdsWithPageAccess( - pageId, - [...usersWithSpaceAccess], - ); + await this.pagePermissionRepo.getUserIdsWithPageAccess(pageId, [ + ...usersWithSpaceAccess, + ]); const usersWithAccess = new Set(usersWithPageAccess); const accessibleMentions = newMentions.filter((m) => @@ -97,6 +114,7 @@ export class PageNotificationService { spaceId, data: { mentionId }, }); + if (!notification) continue; const pageUrl = `${basePageUrl}`; const subject = `${actor.name} mentioned you in ${pageTitle}`; @@ -106,6 +124,7 @@ export class PageNotificationService { notification.id, subject, PageMentionEmail({ actorName: actor.name, pageTitle, pageUrl }), + NotificationType.PAGE_USER_MENTION, ); } } @@ -139,6 +158,7 @@ export class PageNotificationService { spaceId, data: { role }, }); + if (!notification) continue; const subject = `${actor.name} gave you ${accessLabel} access to ${pageTitle}`; @@ -156,6 +176,232 @@ export class PageNotificationService { } } + async processPageUpdate(data: IPageUpdateNotificationJob, appUrl: string) { + const { pageId, spaceId, workspaceId, actorIds } = data; + + const watcherIds = await this.watcherRepo.getPageWatcherIds(pageId); + if (watcherIds.length === 0) return; + + const actorSet = new Set(actorIds); + const candidateIds = watcherIds.filter((id) => !actorSet.has(id)); + if (candidateIds.length === 0) return; + + const eligibleUsers = await this.getEligiblePageUpdateUsers(candidateIds); + if (eligibleUsers.size === 0) return; + + const afterPrefs = [...eligibleUsers.keys()]; + + const recentlyNotified = + await this.notificationRepo.getRecentlyNotifiedUserIds( + afterPrefs, + pageId, + NotificationType.PAGE_UPDATED, + PAGE_UPDATE_COOLDOWN_HOURS, + ); + const afterCooldown = afterPrefs.filter((id) => !recentlyNotified.has(id)); + if (afterCooldown.length === 0) return; + + const usersWithSpaceAccess = + await this.spaceMemberRepo.getUserIdsWithSpaceAccess( + afterCooldown, + spaceId, + ); + + const usersWithPageAccess = + await this.pagePermissionRepo.getUserIdsWithPageAccess(pageId, [ + ...usersWithSpaceAccess, + ]); + if (usersWithPageAccess.length === 0) return; + + const recipientIds = new Set(usersWithPageAccess); + const actorId = actorIds[0]; + + const context = await this.getPageContext(actorId, pageId, spaceId, appUrl); + if (!context) return; + + const { actor, pageTitle, basePageUrl } = context; + + for (const userId of recipientIds) { + const notification = await this.notificationService.create({ + userId, + workspaceId, + type: NotificationType.PAGE_UPDATED, + actorId, + pageId, + spaceId, + }); + if (!notification) continue; + + const canSend = await this.rateLimiter.canSendEmail(userId); + if (canSend) { + await this.notificationService.queueEmail( + userId, + notification.id, + `${actor.name} updated ${pageTitle}`, + PageUpdateEmail({ + userName: eligibleUsers.get(userId) ?? '', + actorName: actor.name, + pageTitle, + pageUrl: basePageUrl, + }), + NotificationType.PAGE_UPDATED, + ); + } else { + const isFirst = await this.rateLimiter.addToDigest( + userId, + notification.id, + ); + if (isFirst) { + await this.scheduleDigest(userId, workspaceId); + } + } + } + } + + private async getEligiblePageUpdateUsers( + userIds: string[], + ): Promise> { + if (userIds.length === 0) return new Map(); + + const users = await this.db + .selectFrom('users') + .select(['id', 'name', 'settings']) + .where('id', 'in', userIds) + .where('deletedAt', 'is', null) + .where('deactivatedAt', 'is', null) + .execute(); + + const eligible = new Map(); + for (const u of users) { + const settings = u.settings as any; + if (settings?.notifications?.['page.updated'] !== false) { + eligible.set(u.id, u.name); + } + } + return eligible; + } + + private async scheduleDigest( + userId: string, + workspaceId: string, + ): Promise { + await this.notificationQueue + .add( + QueueJob.PAGE_UPDATE_DIGEST, + { userId, workspaceId }, + { delay: DIGEST_DELAY_MS, removeOnComplete: true }, + ) + .catch((err) => { + this.logger.error( + `Failed to schedule digest for ${userId}: ${err.message}`, + ); + }); + } + + async processDigest(userId: string, appUrl: string): Promise { + const notificationIds = await this.rateLimiter.popDigest(userId); + if (notificationIds.length === 0) return; + + const [user, notifications] = await Promise.all([ + this.db + .selectFrom('users') + .select(['id', 'name']) + .where('id', '=', userId) + .executeTakeFirst(), + this.db + .selectFrom('notifications') + .select(['id', 'pageId', 'actorId']) + .where('id', 'in', notificationIds) + .execute(), + ]); + + if (!user || notifications.length === 0) return; + + const pageIds = [ + ...new Set(notifications.map((n) => n.pageId).filter(Boolean)), + ]; + const actorIds = [ + ...new Set(notifications.map((n) => n.actorId).filter(Boolean)), + ]; + + const allPages = await this.db + .selectFrom('pages') + .innerJoin('spaces', 'spaces.id', 'pages.spaceId') + .select([ + 'pages.id', + 'pages.title', + 'pages.slugId', + 'pages.spaceId', + 'spaces.slug as spaceSlug', + ]) + .where('pages.id', 'in', pageIds) + .execute(); + + if (allPages.length === 0) return; + + const spaceIds = [...new Set(allPages.map((p) => p.spaceId))]; + + const accessibleSpaceIds = new Set(); + for (const spaceId of spaceIds) { + const usersWithAccess = + await this.spaceMemberRepo.getUserIdsWithSpaceAccess([userId], spaceId); + if (usersWithAccess.has(userId)) accessibleSpaceIds.add(spaceId); + } + + const spaceFilteredPages = allPages.filter((p) => + accessibleSpaceIds.has(p.spaceId), + ); + if (spaceFilteredPages.length === 0) return; + + const accessiblePageIds = new Set(); + for (const p of spaceFilteredPages) { + const hasAccess = await this.pagePermissionRepo.getUserIdsWithPageAccess( + p.id, + [userId], + ); + if (hasAccess.includes(userId)) accessiblePageIds.add(p.id); + } + + const pages = spaceFilteredPages.filter((p) => accessiblePageIds.has(p.id)); + if (pages.length === 0) return; + + const actors = actorIds.length > 0 + ? await this.db + .selectFrom('users') + .select(['id', 'name']) + .where('id', 'in', actorIds) + .execute() + : []; + + const actorMap = new Map(actors.map((a) => [a.id, a.name])); + const pageActors = new Map>(); + for (const n of notifications) { + if (!n.pageId || !n.actorId) continue; + const names = pageActors.get(n.pageId) ?? new Set(); + const name = actorMap.get(n.actorId); + if (name) names.add(name); + pageActors.set(n.pageId, names); + } + + const pageUpdates = pages.map((p) => ({ + title: getPageTitle(p.title), + url: `${appUrl}/s/${p.spaceSlug}/p/${p.slugId}`, + updatedBy: [...(pageActors.get(p.id) ?? [])], + })); + + await this.notificationService.queueEmail( + userId, + notificationIds[0], + `Your digest: ${pageUpdates.length} page ${pageUpdates.length === 1 ? 'update' : 'updates'}`, + PageUpdateDigestEmail({ + userName: user.name, + pageUpdates, + totalUpdates: pageUpdates.length, + }), + NotificationType.PAGE_UPDATED, + ); + } + private async getPageContext( actorId: string, pageId: string, diff --git a/apps/server/src/core/user/dto/update-user.dto.ts b/apps/server/src/core/user/dto/update-user.dto.ts index 3f771339..f1c02c51 100644 --- a/apps/server/src/core/user/dto/update-user.dto.ts +++ b/apps/server/src/core/user/dto/update-user.dto.ts @@ -35,4 +35,24 @@ export class UpdateUserDto extends PartialType( @MaxLength(70) @IsString() confirmPassword: string; + + @IsOptional() + @IsBoolean() + notificationPageUpdates: boolean; + + @IsOptional() + @IsBoolean() + notificationPageUserMention: boolean; + + @IsOptional() + @IsBoolean() + notificationCommentUserMention: boolean; + + @IsOptional() + @IsBoolean() + notificationCommentCreated: boolean; + + @IsOptional() + @IsBoolean() + notificationCommentResolved: boolean; } diff --git a/apps/server/src/core/user/user.service.ts b/apps/server/src/core/user/user.service.ts index 59bc08ec..fa229827 100644 --- a/apps/server/src/core/user/user.service.ts +++ b/apps/server/src/core/user/user.service.ts @@ -7,6 +7,7 @@ import { UnauthorizedException, } from '@nestjs/common'; import { UpdateUserDto } from './dto/update-user.dto'; +import { NotificationSettingKey } from '../notification/notification.constants'; import { comparePasswordHash, diffAuditTrackedFields } from 'src/common/helpers/utils'; import { Workspace } from '@docmost/db/types/entity.types'; import { validateSsoEnforcement } from '../auth/auth.util'; @@ -60,6 +61,24 @@ export class UserService { ); } + const notificationSettings: Record = { + notificationPageUpdates: 'page.updated', + notificationPageUserMention: 'page.userMention', + notificationCommentUserMention: 'comment.userMention', + notificationCommentCreated: 'comment.created', + notificationCommentResolved: 'comment.resolved', + }; + + for (const [dtoField, settingKey] of Object.entries(notificationSettings)) { + if (typeof updateUserDto[dtoField] !== 'undefined') { + return this.userRepo.updateNotificationSetting( + userId, + settingKey, + updateUserDto[dtoField], + ); + } + } + const userBefore = { name: user.name, email: user.email, locale: user.locale }; if (updateUserDto.name) { diff --git a/apps/server/src/core/watcher/watcher.controller.ts b/apps/server/src/core/watcher/watcher.controller.ts index 8709719a..cd10fa37 100644 --- a/apps/server/src/core/watcher/watcher.controller.ts +++ b/apps/server/src/core/watcher/watcher.controller.ts @@ -1,8 +1,6 @@ -/*** - import { +import { Body, Controller, - ForbiddenException, HttpCode, HttpStatus, NotFoundException, @@ -16,12 +14,7 @@ import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard'; import { User, Workspace } from '@docmost/db/types/entity.types'; import { WatcherPageDto } from './dto/watcher.dto'; import { PageRepo } from '@docmost/db/repos/page/page.repo'; -import SpaceAbilityFactory from '../casl/abilities/space-ability.factory'; -import { - SpaceCaslAction, - SpaceCaslSubject, -} from '../casl/interfaces/space-ability.type'; - +import { PageAccessService } from '../page/page-access/page-access.service'; @UseGuards(JwtAuthGuard) @Controller('pages') @@ -29,7 +22,7 @@ export class WatcherController { constructor( private readonly watcherService: WatcherService, private readonly pageRepo: PageRepo, - private readonly spaceAbility: SpaceAbilityFactory, + private readonly pageAccessService: PageAccessService, ) {} @HttpCode(HttpStatus.OK) @@ -44,10 +37,7 @@ export class WatcherController { throw new NotFoundException('Page not found'); } - const ability = await this.spaceAbility.createForUser(user, page.spaceId); - if (ability.cannot(SpaceCaslAction.Read, SpaceCaslSubject.Page)) { - throw new ForbiddenException(); - } + await this.pageAccessService.validateCanView(page, user); await this.watcherService.watchPage( user.id, @@ -67,10 +57,7 @@ export class WatcherController { throw new NotFoundException('Page not found'); } - const ability = await this.spaceAbility.createForUser(user, page.spaceId); - if (ability.cannot(SpaceCaslAction.Read, SpaceCaslSubject.Page)) { - throw new ForbiddenException(); - } + await this.pageAccessService.validateCanView(page, user); await this.watcherService.unwatchPage(user.id, page.id); @@ -85,15 +72,10 @@ export class WatcherController { throw new NotFoundException('Page not found'); } - const ability = await this.spaceAbility.createForUser(user, page.spaceId); - if (ability.cannot(SpaceCaslAction.Read, SpaceCaslSubject.Page)) { - throw new ForbiddenException(); - } + await this.pageAccessService.validateCanView(page, user); const watching = await this.watcherService.isWatchingPage(user.id, page.id); return { watching }; } - } -***/ diff --git a/apps/server/src/core/watcher/watcher.module.ts b/apps/server/src/core/watcher/watcher.module.ts index 68ab5624..76267b5a 100644 --- a/apps/server/src/core/watcher/watcher.module.ts +++ b/apps/server/src/core/watcher/watcher.module.ts @@ -1,10 +1,11 @@ import { Module } from '@nestjs/common'; import { WatcherService } from './watcher.service'; -import { CaslModule } from '../casl/casl.module'; +import { WatcherController } from './watcher.controller'; +import { PageAccessModule } from '../page/page-access/page-access.module'; @Module({ - imports: [CaslModule], - controllers: [], + imports: [PageAccessModule], + controllers: [WatcherController], providers: [WatcherService], exports: [WatcherService], }) diff --git a/apps/server/src/database/repos/notification/notification.repo.ts b/apps/server/src/database/repos/notification/notification.repo.ts index 19add3c6..2914dbfc 100644 --- a/apps/server/src/database/repos/notification/notification.repo.ts +++ b/apps/server/src/database/repos/notification/notification.repo.ts @@ -11,6 +11,7 @@ import { ExpressionBuilder } from 'kysely'; import { DB } from '@docmost/db/types/db'; import { jsonObjectFrom } from 'kysely/helpers/postgres'; import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo'; +import { NotificationTab, NotificationType } from '../../../core/notification/notification.constants'; @Injectable() export class NotificationRepo { @@ -27,8 +28,12 @@ export class NotificationRepo { .executeTakeFirst(); } - async findByUserId(userId: string, pagination: PaginationOptions) { - const query = this.db + async findByUserId( + userId: string, + pagination: PaginationOptions, + type: NotificationTab = 'all', + ) { + let query = this.db .selectFrom('notifications') .selectAll('notifications') .select((eb) => this.withActor(eb)) @@ -42,6 +47,12 @@ export class NotificationRepo { ]), ); + if (type === 'direct') { + query = query.where('type', '!=', NotificationType.PAGE_UPDATED); + } else if (type === 'updates') { + query = query.where('type', '=', NotificationType.PAGE_UPDATED); + } + return executeWithCursorPagination(query, { perPage: pagination.limit, cursor: pagination.cursor, @@ -138,6 +149,29 @@ export class NotificationRepo { .execute(); } + async getRecentlyNotifiedUserIds( + userIds: string[], + pageId: string, + type: string, + withinHours: number, + ): Promise> { + if (userIds.length === 0) return new Set(); + + const cutoff = new Date(Date.now() - withinHours * 60 * 60 * 1000); + + const rows = await this.db + .selectFrom('notifications') + .select('userId') + .where('userId', 'in', userIds) + .where('pageId', '=', pageId) + .where('type', '=', type) + .where('createdAt', '>', cutoff) + .groupBy('userId') + .execute(); + + return new Set(rows.map((r) => r.userId)); + } + withActor(eb: ExpressionBuilder) { return jsonObjectFrom( eb diff --git a/apps/server/src/database/repos/user/user.repo.ts b/apps/server/src/database/repos/user/user.repo.ts index c3903357..eaaa318e 100644 --- a/apps/server/src/database/repos/user/user.repo.ts +++ b/apps/server/src/database/repos/user/user.repo.ts @@ -13,6 +13,7 @@ import { PaginationOptions } from '../../pagination/pagination-options'; import { executeWithCursorPagination } from '@docmost/db/pagination/cursor-pagination'; import { ExpressionBuilder, sql } from 'kysely'; import { jsonObjectFrom } from 'kysely/helpers/postgres'; +import { NotificationSettingKey } from '../../../core/notification/notification.constants'; @Injectable() export class UserRepo { @@ -191,6 +192,24 @@ export class UserRepo { .executeTakeFirst(); } + async updateNotificationSetting( + userId: string, + settingKey: NotificationSettingKey, + settingValue: boolean, + ) { + return await this.db + .updateTable('users') + .set({ + settings: sql`COALESCE(settings, '{}'::jsonb) + || jsonb_build_object('notifications', COALESCE(settings->'notifications', '{}'::jsonb) + || jsonb_build_object(${sql.lit(settingKey)}, ${sql.lit(settingValue)}))`, + updatedAt: new Date(), + }) + .where('id', '=', userId) + .returning(this.baseFields) + .executeTakeFirst(); + } + withUserMfa(eb: ExpressionBuilder) { return jsonObjectFrom( eb diff --git a/apps/server/src/integrations/queue/constants/queue.constants.ts b/apps/server/src/integrations/queue/constants/queue.constants.ts index 1c66a5f3..92d15426 100644 --- a/apps/server/src/integrations/queue/constants/queue.constants.ts +++ b/apps/server/src/integrations/queue/constants/queue.constants.ts @@ -69,6 +69,7 @@ export enum QueueJob { COMMENT_RESOLVED_NOTIFICATION = 'comment-resolved-notification', PAGE_MENTION_NOTIFICATION = 'page-mention-notification', PAGE_PERMISSION_GRANTED = 'page-permission-granted', + PAGE_UPDATE_DIGEST = 'page-update-digest', AUDIT_LOG = 'audit-log', AUDIT_CLEANUP = 'audit-cleanup', diff --git a/apps/server/src/integrations/queue/constants/queue.interface.ts b/apps/server/src/integrations/queue/constants/queue.interface.ts index 4254bbdc..f0683a4e 100644 --- a/apps/server/src/integrations/queue/constants/queue.interface.ts +++ b/apps/server/src/integrations/queue/constants/queue.interface.ts @@ -60,6 +60,13 @@ export interface IPageMentionNotificationJob { workspaceId: string; } +export interface IPageUpdateNotificationJob { + pageId: string; + spaceId: string; + workspaceId: string; + actorIds: string[]; +} + export interface IPermissionGrantedNotificationJob { userIds: string[]; pageId: string; diff --git a/apps/server/src/integrations/transactional/emails/page-update-digest-email.tsx b/apps/server/src/integrations/transactional/emails/page-update-digest-email.tsx new file mode 100644 index 00000000..f847291d --- /dev/null +++ b/apps/server/src/integrations/transactional/emails/page-update-digest-email.tsx @@ -0,0 +1,76 @@ +import { Link, Section, Text } from '@react-email/components'; +import * as React from 'react'; +import { content, link, paragraph } from '../css/styles'; +import { getGreetingName, MailBody } from '../partials/partials'; + +interface PageUpdate { + title: string; + url: string; + updatedBy: string[]; +} + +interface Props { + userName: string; + pageUpdates: PageUpdate[]; + totalUpdates: number; +} + +export const PageUpdateDigestEmail = ({ + userName, + pageUpdates, + totalUpdates, +}: Props) => { + return ( + +
+ + Hi {getGreetingName(userName)}, + + + There {totalUpdates === 1 ? 'has' : 'have'} been{' '} + + {totalUpdates} update{totalUpdates === 1 ? '' : 's'} + {' '} + since your last update. + + + {pageUpdates.map((page, i) => ( +
+ + + {page.title} + + + {page.updatedBy.length > 0 && ( + + Edited by {page.updatedBy.join(', ')} + + )} +
+ ))} +
+
+ ); +}; + +const pageCard = { + borderLeft: '3px solid #e8e5ef', + paddingLeft: '12px', + marginBottom: '12px', +}; + +const pageTitle = { + ...paragraph, + margin: '0 0 2px 0', + fontSize: 14, + fontWeight: 'bold' as const, +}; + +const updatedByText = { + ...paragraph, + margin: '0', + fontSize: 13, + color: '#666', +}; + +export default PageUpdateDigestEmail; diff --git a/apps/server/src/integrations/transactional/emails/page-update-email.tsx b/apps/server/src/integrations/transactional/emails/page-update-email.tsx new file mode 100644 index 00000000..188d8a34 --- /dev/null +++ b/apps/server/src/integrations/transactional/emails/page-update-email.tsx @@ -0,0 +1,36 @@ +import { Link, Section, Text } from '@react-email/components'; +import * as React from 'react'; +import { content, link, paragraph } from '../css/styles'; +import { EmailButton, getGreetingName, MailBody } from '../partials/partials'; + +interface Props { + userName: string; + actorName: string; + pageTitle: string; + pageUrl: string; +} + +export const PageUpdateEmail = ({ + userName, + actorName, + pageTitle, + pageUrl, +}: Props) => { + return ( + +
+ Hi {getGreetingName(userName)}, + + {actorName} updated{' '} + + {pageTitle} + + . + +
+ View page +
+ ); +}; + +export default PageUpdateEmail; diff --git a/apps/server/src/integrations/transactional/partials/partials.tsx b/apps/server/src/integrations/transactional/partials/partials.tsx index f97eb989..c5b30e6b 100644 --- a/apps/server/src/integrations/transactional/partials/partials.tsx +++ b/apps/server/src/integrations/transactional/partials/partials.tsx @@ -87,3 +87,7 @@ export function MailFooter() { ); } + +export function getGreetingName(name?: string): string { + return name?.split(' ')[0] || 'there'; +}