diff --git a/app/models/Notification.ts b/app/models/Notification.ts index a09b9ab1f1..634fbed8d9 100644 --- a/app/models/Notification.ts +++ b/app/models/Notification.ts @@ -285,6 +285,9 @@ class Notification extends Model { case NotificationEventType.ExportCompleted: { return settingsPath("export"); } + case NotificationEventType.VerificationExpired: { + return this.document?.path; + } default: this.event satisfies never; return; diff --git a/plugins/webhooks/server/tasks/DeliverWebhookTask.ts b/plugins/webhooks/server/tasks/DeliverWebhookTask.ts index d622995911..ebfa096ef2 100644 --- a/plugins/webhooks/server/tasks/DeliverWebhookTask.ts +++ b/plugins/webhooks/server/tasks/DeliverWebhookTask.ts @@ -196,6 +196,9 @@ export default class DeliverWebhookTask extends BaseTask { case "documents.move": case "documents.update": case "documents.title_change": + case "documents.verify": + case "documents.unverify": + case "documents.verification_expired": await this.handleDocumentEvent(subscription, event); return; case "documents.add_user": diff --git a/server/commands/documentCreator.ts b/server/commands/documentCreator.ts index 94c596a783..483627358b 100644 --- a/server/commands/documentCreator.ts +++ b/server/commands/documentCreator.ts @@ -22,6 +22,7 @@ type Props = Optional< | "importId" | "apiImportId" | "fullWidth" + | "verificationInterval" | "sourceMetadata" | "editorVersion" | "publishedAt" @@ -161,6 +162,7 @@ export default async function documentCreator( content, template, fullWidth, + verificationInterval, importId, apiImportId, createdAt, @@ -229,6 +231,7 @@ export default async function documentCreator( apiImportId, sourceMetadata, fullWidth: fullWidth ?? template?.fullWidth, + verificationInterval, icon: icon ?? template?.icon, color: color ?? template?.color, title: titleWithReplacements, diff --git a/server/commands/documentDuplicator.test.ts b/server/commands/documentDuplicator.test.ts index 69db89dfba..fa730a6d3e 100644 --- a/server/commands/documentDuplicator.test.ts +++ b/server/commands/documentDuplicator.test.ts @@ -31,6 +31,31 @@ describe("documentDuplicator", () => { expect(response[0].publishedAt).toBeInstanceOf(Date); }); + it("should not copy verification state, but keep the interval override", async () => { + const user = await buildUser(); + const original = await buildDocument({ + userId: user.id, + teamId: user.teamId, + verifiedAt: new Date(), + verifiedById: user.id, + verificationExpiresAt: new Date(), + verificationInterval: 90, + }); + + const response = await withAPIContext(user, (ctx) => + documentDuplicator(ctx, { + document: original, + collection: original.collection, + }) + ); + + expect(response).toHaveLength(1); + expect(response[0].verifiedAt).toEqual(null); + expect(response[0].verifiedById).toEqual(null); + expect(response[0].verificationExpiresAt).toEqual(null); + expect(response[0].verificationInterval).toEqual(90); + }); + it("should duplicate document with title override", async () => { const user = await buildUser(); const original = await buildDocument({ diff --git a/server/commands/documentDuplicator.ts b/server/commands/documentDuplicator.ts index ba47943b13..bb2e528cfe 100644 --- a/server/commands/documentDuplicator.ts +++ b/server/commands/documentDuplicator.ts @@ -36,6 +36,7 @@ export default async function documentDuplicator( icon: document.icon, color: document.color, fullWidth: document.fullWidth, + verificationInterval: document.verificationInterval, title: title ?? document.title, content: ProsemirrorHelper.removeMarks( DocumentHelper.toProsemirror(document), @@ -87,6 +88,7 @@ export default async function documentDuplicator( icon: childDocument.icon, color: childDocument.color, fullWidth: childDocument.fullWidth, + verificationInterval: childDocument.verificationInterval, title: childDocument.title, content: ProsemirrorHelper.removeMarks( DocumentHelper.toProsemirror(childDocument), diff --git a/server/commands/documentUpdater.ts b/server/commands/documentUpdater.ts index 0a19209b31..f32d1afc48 100644 --- a/server/commands/documentUpdater.ts +++ b/server/commands/documentUpdater.ts @@ -1,4 +1,5 @@ import type { TextEditMode } from "@shared/types"; +import { Day } from "@shared/utils/time"; import { Event, Document } from "@server/models"; import { DocumentHelper } from "@server/models/helpers/DocumentHelper"; import { TextHelper } from "@server/models/helpers/TextHelper"; @@ -25,6 +26,8 @@ type Props = { fullWidth?: boolean; /** Whether insights should be visible on the document */ insightsEnabled?: boolean; + /** Re-verification cadence override in days, null clears the override */ + verificationInterval?: number | null; /** The edit mode: "replace", "append", "prepend", or "patch" */ editMode?: TextEditMode; /** The markdown text to find when using "patch" edit mode */ @@ -54,6 +57,7 @@ export default async function documentUpdater( templateId, fullWidth, insightsEnabled, + verificationInterval, editMode, findText, publish, @@ -86,6 +90,21 @@ export default async function documentUpdater( if (insightsEnabled !== undefined) { document.insightsEnabled = insightsEnabled; } + if (verificationInterval !== undefined) { + document.verificationInterval = verificationInterval; + + // recompute the materialized deadline when currently verified, as the + // effective interval may have changed, including falling back through + // the collection and team inheritance chain when the override is cleared. + if (document.verifiedAt) { + const interval = await document.resolveVerificationInterval({ + transaction, + }); + document.verificationExpiresAt = interval + ? new Date(document.verifiedAt.getTime() + interval * Day.ms) + : null; + } + } if (text !== undefined) { document = DocumentHelper.applyMarkdownToDocument( document, @@ -98,6 +117,12 @@ export default async function documentUpdater( } const changed = document.changed(); + const verificationOnly = + !!changed && + changed.every( + (attr) => + attr === "verificationInterval" || attr === "verificationExpiresAt" + ); const eventData = done !== undefined ? { done } : undefined; const event = { @@ -112,6 +137,10 @@ export default async function documentUpdater( document.collectionId = cId; } await document.publish(ctx, { collectionId: cId, data: eventData }); + } else if (verificationOnly) { + // changing the verification cadence is a policy change, not a content + // edit, so it must not update the edit timestamps or the last editor + await document.saveWithCtx(ctx, { silent: true }, { data: eventData }); } else if (changed) { document.lastModifiedById = user.id; document.updatedBy = user; diff --git a/server/emails/templates/DocumentVerificationExpiredEmail.tsx b/server/emails/templates/DocumentVerificationExpiredEmail.tsx new file mode 100644 index 0000000000..9f7da0ec18 --- /dev/null +++ b/server/emails/templates/DocumentVerificationExpiredEmail.tsx @@ -0,0 +1,118 @@ +import * as React from "react"; +import { NotificationEventType } from "@shared/types"; +import { Document } from "@server/models"; +import NotificationSettingsHelper from "@server/models/helpers/NotificationSettingsHelper"; +import type { EmailProps } from "./BaseEmail"; +import BaseEmail, { EmailMessageCategory } from "./BaseEmail"; +import Body from "./components/Body"; +import Button from "./components/Button"; +import EmailTemplate from "./components/EmailLayout"; +import EmptySpace from "./components/EmptySpace"; +import Footer from "./components/Footer"; +import Header from "./components/Header"; +import Heading from "./components/Heading"; + +type InputProps = EmailProps & { + userId: string; + documentId: string; + teamUrl: string; +}; + +type BeforeSend = { + document: Document; + unsubscribeUrl: string; +}; + +type Props = InputProps & BeforeSend; + +/** + * Email sent to the verifier of a document when their verification has + * expired and the document is due to be reviewed again. + */ +export default class DocumentVerificationExpiredEmail extends BaseEmail< + InputProps, + BeforeSend +> { + protected get category() { + return EmailMessageCategory.Notification; + } + + protected async beforeSend(props: InputProps) { + const document = await Document.unscoped().findByPk(props.documentId); + if (!document) { + return false; + } + + return { + document, + unsubscribeUrl: this.unsubscribeUrl(props), + }; + } + + protected unsubscribeUrl({ userId }: InputProps) { + return NotificationSettingsHelper.unsubscribeUrl( + userId, + NotificationEventType.VerificationExpired + ); + } + + protected subject({ document }: Props) { + return this.t("Your verification of “{{ documentTitle }}” has expired", { + documentTitle: document.titleWithDefault, + }); + } + + protected preview({ document }: Props): string { + return this.t("“{{ documentTitle }}” is due to be reviewed again", { + documentTitle: document.titleWithDefault, + }); + } + + protected renderAsText({ teamUrl, document }: Props): string { + return ` +${this.t("Your verification of “{{ documentTitle }}” has expired", { documentTitle: document.titleWithDefault })} + +${this.t( + "You previously verified this document as accurate. That verification has now lapsed – please review the content and verify it again if it is still correct." +)} + +${this.t("Open Document")}: ${teamUrl}${document.url} +`; + } + + protected render(props: Props) { + const { document, teamUrl, unsubscribeUrl } = props; + const documentLink = `${teamUrl}${document.url}?ref=notification-email`; + + return ( + +
+ + + + {this.t("Your verification of “{{ documentTitle }}” has expired", { + documentTitle: document.titleWithDefault, + })} + +

+ {this.t( + "You previously verified the document as accurate. That verification has now lapsed – please review the content and verify it again if it is still correct." + )} +

+ +

+ +

+ + +