From f5acd9c72b4d1c69ee079cbfeb0680d9a791fddf Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 17:34:44 +0000 Subject: [PATCH] feat: Document verification and freshness tracking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a verification lifecycle to documents, rebuilt on top of the new main history. A user with admin-level access to a document (document admin membership or team admin) can mark it as verified, vouching that its content is accurate. Verification carries an optional expiry deadline derived from a re-verification interval resolved through a document → collection → team preference inheritance chain. - documents.verify / documents.unverify API endpoints, gated by new verify/unverify abilities - the feature requires the new DocumentVerification plan entitlement, included in the Business and Enterprise plans, and remains limited to cloud hosting - verificationInterval override on documents.update and a collection default on collections.update (team admin only), plus a team-wide default via the verificationInterval team preference - daily VerificationExpirySweepTask emits idempotent documents.verification_expired events without mutating documents; expiry is derived from the materialized verificationExpiresAt deadline - notification and email to the verifier when their verification lapses; suppressed when the verifier is deleted, suspended, or lost access - verification state excluded from duplicates and public shares; the interval override is carried over when duplicating Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01Mb5McXaNbEDyyCZXPAdGAY --- app/models/Notification.ts | 4 + .../server/tasks/DeliverWebhookTask.ts | 3 + server/commands/documentCreator.ts | 3 + server/commands/documentDuplicator.test.ts | 25 ++ server/commands/documentDuplicator.ts | 1 + server/commands/documentUpdater.ts | 29 ++ .../DocumentVerificationExpiredEmail.tsx | 118 ++++++ ...121000000-add-verification-to-documents.js | 96 +++++ server/models/Collection.ts | 5 + server/models/Document.test.ts | 113 ++++++ server/models/Document.ts | 167 ++++++++ server/policies/document.ts | 19 + server/presenters/collection.ts | 1 + server/presenters/document.ts | 4 + server/queues/processors/EmailsProcessor.ts | 15 + .../processors/NotificationsProcessor.ts | 8 + ...rificationExpiredNotificationsTask.test.ts | 108 ++++++ ...entVerificationExpiredNotificationsTask.ts | 54 +++ .../tasks/VerificationExpirySweepTask.test.ts | 147 +++++++ .../tasks/VerificationExpirySweepTask.ts | 82 ++++ .../api/collections/collections.test.ts | 47 +++ server/routes/api/collections/collections.ts | 10 +- server/routes/api/collections/schema.ts | 2 + server/routes/api/documents/documents.test.ts | 364 ++++++++++++++++++ server/routes/api/documents/documents.ts | 58 +++ server/routes/api/documents/schema.ts | 15 + server/routes/api/teams/schema.ts | 7 + server/types.ts | 16 + shared/types/notifications.ts | 2 + shared/types/plans.ts | 2 + shared/types/team.ts | 3 + 31 files changed, 1527 insertions(+), 1 deletion(-) create mode 100644 server/emails/templates/DocumentVerificationExpiredEmail.tsx create mode 100644 server/migrations/20261121000000-add-verification-to-documents.js create mode 100644 server/queues/tasks/DocumentVerificationExpiredNotificationsTask.test.ts create mode 100644 server/queues/tasks/DocumentVerificationExpiredNotificationsTask.ts create mode 100644 server/queues/tasks/VerificationExpirySweepTask.test.ts create mode 100644 server/queues/tasks/VerificationExpirySweepTask.ts diff --git a/app/models/Notification.ts b/app/models/Notification.ts index cec80ea8e6..1b59a8ee97 100644 --- a/app/models/Notification.ts +++ b/app/models/Notification.ts @@ -56,6 +56,7 @@ class Notification extends Model { NotificationEventType.CreateRevision, NotificationEventType.AddUserToDocument, NotificationEventType.RequestDocumentAccess, + NotificationEventType.VerificationExpired, ], collections: [ NotificationEventType.CreateCollection, @@ -349,6 +350,9 @@ class Notification extends Model { ? settingsPath("groups", this.groupId, "members") : ""; } + 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 f0d1674c92..fdae7d232a 100644 --- a/plugins/webhooks/server/tasks/DeliverWebhookTask.ts +++ b/plugins/webhooks/server/tasks/DeliverWebhookTask.ts @@ -208,6 +208,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 4416b0db43..d52b68a243 100644 --- a/server/commands/documentCreator.ts +++ b/server/commands/documentCreator.ts @@ -25,6 +25,7 @@ type Props = Optional< | "apiImportId" | "fullWidth" | "preferences" + | "verificationInterval" | "sourceMetadata" | "editorVersion" | "publishedAt" @@ -174,6 +175,7 @@ export default async function documentCreator( template, fullWidth, preferences, + verificationInterval, importId, apiImportId, createdAt, @@ -248,6 +250,7 @@ export default async function documentCreator( sourceMetadata, fullWidth: fullWidth ?? template?.fullWidth, preferences, + 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 f04698a070..73f0bd6020 100644 --- a/server/commands/documentDuplicator.test.ts +++ b/server/commands/documentDuplicator.test.ts @@ -35,6 +35,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 8543878970..ec4d0b8723 100644 --- a/server/commands/documentDuplicator.ts +++ b/server/commands/documentDuplicator.ts @@ -217,6 +217,7 @@ async function duplicateRoots( color: original.color, fullWidth: original.fullWidth, preferences: original.preferences, + verificationInterval: original.verificationInterval, title: item.title, content: ProsemirrorHelper.replaceDocumentReferences( ProsemirrorHelper.removeMarks(DocumentHelper.toProsemirror(original), [ diff --git a/server/commands/documentUpdater.ts b/server/commands/documentUpdater.ts index a7d16f22ba..a14fcafb44 100644 --- a/server/commands/documentUpdater.ts +++ b/server/commands/documentUpdater.ts @@ -1,4 +1,5 @@ import type { DocumentPreferences, TextEditMode } from "@shared/types"; +import { Day } from "@shared/utils/time"; import { DocumentConflictError, ValidationError } from "@server/errors"; import { Document } from "@server/models"; import { DocumentHelper } from "@server/models/helpers/DocumentHelper"; @@ -26,6 +27,8 @@ type Props = { preferences?: DocumentPreferences | null; /** 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 document revision the changes are based on, the update is rejected if it no longer matches */ @@ -60,6 +63,7 @@ export default async function documentUpdater( fullWidth, preferences, insightsEnabled, + verificationInterval, editMode, findText, lastRevision, @@ -87,6 +91,21 @@ export default async function documentUpdater( ...preferences, }; } + 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, @@ -135,6 +154,12 @@ export default async function documentUpdater( } const changed = document.changed(); + const verificationOnly = + !!changed && + changed.every( + (attr) => + attr === "verificationInterval" || attr === "verificationExpiresAt" + ); const { collection } = document; if (publish && cId) { @@ -142,6 +167,10 @@ export default async function documentUpdater( document.collectionId = cId; } await document.publish(ctx, { collectionId: cId }); + } 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 }); } 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..580128b341 --- /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.displayTitle, + }); + } + + protected preview({ document }: Props): string { + return this.t("“{{ documentTitle }}” is due to be reviewed again", { + documentTitle: document.displayTitle, + }); + } + + protected renderAsText({ teamUrl, document }: Props): string { + return ` +${this.t("Your verification of “{{ documentTitle }}” has expired", { documentTitle: document.displayTitle })} + +${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.displayTitle, + })} + +

+ {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." + )} +

+ +

+ +

+ + +