fix: relationships.list drafts (#13170)

This commit is contained in:
Tom Moor
2026-07-27 19:25:50 -04:00
committed by GitHub
parent 7b751ab04b
commit d0900e7277
3 changed files with 69 additions and 10 deletions
+51 -1
View File
@@ -1,5 +1,5 @@
import { EmptyResultError, Op } from "sequelize";
import { CollectionPermission } from "@shared/types";
import { CollectionPermission, DocumentPermission } from "@shared/types";
import slugify from "@shared/utils/slugify";
import { parser } from "@server/editor";
import Document from "@server/models/Document";
@@ -413,6 +413,56 @@ describe("findByIds", () => {
);
expect(documents.length).toBe(1);
});
it("should not return another user's unfiled draft", async () => {
const team = await buildTeam();
const author = await buildUser({ teamId: team.id });
const user = await buildUser({ teamId: team.id });
const draft = await buildDraftDocument({
teamId: team.id,
userId: author.id,
collectionId: null,
});
const documents = await Document.findByIds([draft.id], {
userId: user.id,
});
expect(documents.length).toBe(0);
});
it("should return the user's own unfiled draft", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const draft = await buildDraftDocument({
teamId: team.id,
userId: user.id,
collectionId: null,
});
const documents = await Document.findByIds([draft.id], {
userId: user.id,
});
expect(documents.length).toBe(1);
});
it("should return an unfiled draft shared with the user", async () => {
const team = await buildTeam();
const author = await buildUser({ teamId: team.id });
const user = await buildUser({ teamId: team.id });
const draft = await buildDraftDocument({
teamId: team.id,
userId: author.id,
collectionId: null,
});
await UserMembership.create({
createdById: author.id,
documentId: draft.id,
userId: user.id,
permission: DocumentPermission.Read,
});
const documents = await Document.findByIds([draft.id], {
userId: user.id,
});
expect(documents.length).toBe(1);
});
});
describe("tasks", () => {
+17 -8
View File
@@ -899,14 +899,23 @@ class Document extends ArchivableModel<
return documents;
}
return documents.filter(
(doc) =>
(!doc.collection?.isPrivate && !user?.isGuest) ||
(doc.collection?.memberships.length || 0) > 0 ||
(doc.collection?.groupMemberships.length || 0) > 0 ||
doc.memberships.length > 0 ||
doc.groupMemberships.length > 0
);
return documents.filter((doc) => {
if (doc.memberships.length > 0 || doc.groupMemberships.length > 0) {
return true;
}
// A document without a collection is either an unfiled draft or lives in
// a collection the user cannot see access is limited to the creator.
if (!doc.collection) {
return doc.createdById === userId;
}
return (
(!doc.collection.isPrivate && !user?.isGuest) ||
doc.collection.memberships.length > 0 ||
doc.collection.groupMemberships.length > 0
);
});
}
// instance methods
+1 -1
View File
@@ -70,7 +70,7 @@ class Relationship extends IdModel<
const documents = await Document.findByIds(
relationships.map((relationship) => relationship.reverseDocumentId),
{
attributes: ["id"],
attributes: ["id", "createdById"],
userId: user.id,
includeState: false,
includeViews: false,