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>
91 lines
2.9 KiB
TypeScript
91 lines
2.9 KiB
TypeScript
import { Document } from "@server/models";
|
|
import { presentDocuments } from "@server/presenters/document";
|
|
import {
|
|
buildCollection,
|
|
buildDocument,
|
|
buildUser,
|
|
} from "@server/test/factories";
|
|
import { withAPIContext } from "@server/test/support";
|
|
import { sequelize } from "@server/storage/database";
|
|
|
|
describe("presentDocuments", () => {
|
|
describe("deletedBy", () => {
|
|
it("should resolve the deleting user in a single query", async () => {
|
|
const user = await buildUser();
|
|
const collection = await buildCollection({ teamId: user.teamId });
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
collectionId: collection.id,
|
|
});
|
|
await withAPIContext(user, (ctx) => document.destroyWithCtx(ctx));
|
|
}
|
|
|
|
const documents = await Document.scope("withDrafts").findAll({
|
|
where: { collectionId: collection.id },
|
|
paranoid: false,
|
|
});
|
|
expect(documents.length).toEqual(3);
|
|
|
|
const queries: string[] = [];
|
|
const query = sequelize.query.bind(sequelize);
|
|
const spy = vi
|
|
.spyOn(sequelize, "query")
|
|
.mockImplementation((sql, options) => {
|
|
queries.push(typeof sql === "string" ? sql : sql.query);
|
|
return query(sql, options);
|
|
});
|
|
|
|
try {
|
|
await presentDocuments(undefined, documents);
|
|
} finally {
|
|
spy.mockRestore();
|
|
}
|
|
|
|
expect(documents.every((doc) => doc.deletedBy?.id === user.id)).toBe(
|
|
true
|
|
);
|
|
|
|
expect(queries.filter((sql) => /FROM "users"/.test(sql)).length).toEqual(
|
|
1
|
|
);
|
|
});
|
|
|
|
it("should not overwrite an already loaded deleting user", async () => {
|
|
const userA = await buildUser();
|
|
const userB = await buildUser({ teamId: userA.teamId });
|
|
const collection = await buildCollection({ teamId: userA.teamId });
|
|
|
|
const documentA = await buildDocument({
|
|
teamId: userA.teamId,
|
|
collectionId: collection.id,
|
|
});
|
|
const documentB = await buildDocument({
|
|
teamId: userA.teamId,
|
|
collectionId: collection.id,
|
|
});
|
|
await withAPIContext(userA, (ctx) => documentA.destroyWithCtx(ctx));
|
|
await withAPIContext(userB, (ctx) => documentB.destroyWithCtx(ctx));
|
|
|
|
// One document arrives with the association loaded, the other without.
|
|
const loaded = await Document.scope("withDrafts").findOne({
|
|
where: { id: documentA.id },
|
|
include: [{ association: "deletedBy", paranoid: false }],
|
|
paranoid: false,
|
|
rejectOnEmpty: true,
|
|
});
|
|
const unloaded = await Document.scope("withDrafts").findOne({
|
|
where: { id: documentB.id },
|
|
paranoid: false,
|
|
rejectOnEmpty: true,
|
|
});
|
|
|
|
await presentDocuments(undefined, [loaded, unloaded]);
|
|
|
|
expect(loaded.deletedBy?.id).toEqual(userA.id);
|
|
expect(unloaded.deletedBy?.id).toEqual(userB.id);
|
|
});
|
|
});
|
|
});
|