mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
* 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) <noreply@anthropic.com> * 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) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { createContext } from "@server/context";
|
|
import {
|
|
buildCollection,
|
|
buildDocument,
|
|
buildTeam,
|
|
buildUser,
|
|
} from "@server/test/factories";
|
|
|
|
describe("ParanoidModel", () => {
|
|
describe("deletedById", () => {
|
|
it("should record the acting user on destroy", async () => {
|
|
const user = await buildUser();
|
|
const document = await buildDocument({ teamId: user.teamId });
|
|
|
|
await document.destroy(createContext({ user }).context);
|
|
await document.reload({ paranoid: false });
|
|
|
|
expect(document.deletedAt).toBeTruthy();
|
|
expect(document.deletedById).toEqual(user.id);
|
|
});
|
|
|
|
it("should clear the recorded user on restore", async () => {
|
|
const user = await buildUser();
|
|
const document = await buildDocument({ teamId: user.teamId });
|
|
|
|
await document.destroy(createContext({ user }).context);
|
|
await document.restore();
|
|
await document.reload();
|
|
|
|
expect(document.deletedAt).toBeNull();
|
|
expect(document.deletedById).toBeNull();
|
|
});
|
|
|
|
it("should record nothing when the context is unauthenticated", async () => {
|
|
const document = await buildDocument();
|
|
|
|
await document.destroy();
|
|
await document.reload({ paranoid: false });
|
|
|
|
expect(document.deletedAt).toBeTruthy();
|
|
expect(document.deletedById).toBeNull();
|
|
});
|
|
|
|
it("should ignore models without the column", async () => {
|
|
const team = await buildTeam();
|
|
const user = await buildUser({ teamId: team.id });
|
|
// A team must keep at least one collection, so build a second to delete.
|
|
await buildCollection({ teamId: team.id, userId: user.id });
|
|
const collection = await buildCollection({
|
|
teamId: team.id,
|
|
userId: user.id,
|
|
});
|
|
|
|
await collection.destroy(createContext({ user }).context);
|
|
|
|
expect(collection.deletedAt).toBeTruthy();
|
|
});
|
|
});
|
|
});
|