From def3a42752e8f990ef5872b6e4e3dfb8a168aaaa Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 22 Aug 2026 21:49:31 -0400 Subject: [PATCH] feat: Record who deleted a document (#13484) * feat: Record who deleted a document The trash inferred the deleting user from lastModifiedById, so a nested page kept whoever last edited it and deleting a document rewrote its edit history. Add a dedicated deletedById column and set it from the base model. - Migration adds the column, backfills from lastModifiedById, and validates the foreign key without holding an exclusive lock. The backfill runs in batches against a temporary index covering only the rows still to write, so it stays linear on a large table, and every statement is idempotent because the migration cannot run in a transaction. - ParanoidModel records the acting user on destroy and clears it on restore, for any model that has the column. Sequelize soft deletes with the same UPDATE that stamps deletedAt, so the value costs no extra query. - Deleting a document no longer touches lastModifiedById on its descendants. - Present the deleting user, batched in presentDocuments, and use it for the trash notice and the document meta line rather than the last editor. Co-Authored-By: Claude Opus 5 (1M context) * fix: Do not overwrite an eager loaded deleting user when batching The batch lookup in presentDocuments skipped documents that already carried the association, but the assignment loop ran for all of them, so a document that arrived with deletedBy loaded was reset to null and fell back to a per-document query. Assign only where the association is missing, and deduplicate the ids so a page deleted by one person asks for one row rather than one per document. Co-Authored-By: Claude Opus 5 (1M context) --------- Co-authored-by: Claude Opus 5 (1M context) --- app/components/DocumentMeta.tsx | 6 +- app/models/Document.ts | 8 ++ app/scenes/Document/components/Notices.tsx | 2 +- app/stores/DocumentsStore.test.ts | 33 +++++++ app/stores/DocumentsStore.ts | 11 ++- ...0818120000-add-deletedById-to-documents.js | 82 +++++++++++++++++ server/models/Collection.test.ts | 11 +-- server/models/Collection.ts | 31 +++---- server/models/Document.ts | 12 ++- server/models/base/ParanoidModel.test.ts | 59 ++++++++++++ server/models/base/ParanoidModel.ts | 36 +++++++- server/presenters/document.test.ts | 90 +++++++++++++++++++ server/presenters/document.ts | 37 +++++++- server/routes/api/documents/documents.test.ts | 38 +++++++- server/routes/api/documents/documents.ts | 7 +- 15 files changed, 418 insertions(+), 45 deletions(-) create mode 100644 app/stores/DocumentsStore.test.ts create mode 100644 server/migrations/20260818120000-add-deletedById-to-documents.js create mode 100644 server/models/base/ParanoidModel.test.ts create mode 100644 server/presenters/document.test.ts diff --git a/app/components/DocumentMeta.tsx b/app/components/DocumentMeta.tsx index 1a0381c6ee..32a04a0bfc 100644 --- a/app/components/DocumentMeta.tsx +++ b/app/components/DocumentMeta.tsx @@ -93,9 +93,11 @@ const DocumentMeta: React.FC = ({ } else if (deletedAt) { content = ( - {lastUpdatedByCurrentUser + {document.deletedBy?.id === user.id ? t("You deleted") - : t("{{ userName }} deleted", { userName })}{" "} + : t("{{ userName }} deleted", { + userName: document.deletedBy?.name ?? t("Unknown"), + })}{" "} ); diff --git a/app/models/Document.ts b/app/models/Document.ts index 0b35cb2b3e..a95f705849 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -204,6 +204,14 @@ export default class Document extends ArchivableModel implements Searchable { @observable updatedBy: User | undefined; + /** + * The user that deleted this document, only set while the document is in the + * trash. + */ + @Relation(() => User) + @observable + deletedBy: User | undefined; + @observable publishedAt: string | undefined; diff --git a/app/scenes/Document/components/Notices.tsx b/app/scenes/Document/components/Notices.tsx index ab70d9350b..e6db1e735a 100644 --- a/app/scenes/Document/components/Notices.tsx +++ b/app/scenes/Document/components/Notices.tsx @@ -65,7 +65,7 @@ export default function Notices({ document }: Props) { description={permanentlyDeletedDescription()} > {t("Deleted by {{userName}}", { - userName: document.updatedBy?.name ?? t("Unknown"), + userName: document.deletedBy?.name ?? t("Unknown"), })}