Files
outline/server/queues/processors/DocumentSubscriptionProcessor.ts
T
Tom Moor 9666d8e825 feat: Add collection owners
- Collections can have owners, who also own every document in the
  collection, shown first in document bylines
- Owner is a role in the collection share panel and invite picker for
  individual users, adding an owner gives them the manage permission
- Lowering an owner's permission or removing their access removes the
  ownership
- ownerIds on collections.create, collections.update and the MCP
  collection tools
- The document owner filter includes documents in owned collections
- Access requests go to document and collection owners together
- Collection owners are subscribed to the collection and notified when
  they are added
- Deleted users are removed from collection owners
- Renames the personal collection policy helper to
  isPersonalCollectionOwner
2026-11-06 22:45:07 -05:00

111 lines
3.0 KiB
TypeScript

import { GroupUser } from "@server/models";
import type {
CollectionGroupEvent,
CollectionOwnerEvent,
CollectionUserEvent,
DocumentGroupEvent,
DocumentOwnerEvent,
DocumentUserEvent,
Event,
} from "@server/types";
import CollectionSubscriptionAddOwnerTask from "../tasks/CollectionSubscriptionAddOwnerTask";
import CollectionSubscriptionRemoveUserTask from "../tasks/CollectionSubscriptionRemoveUserTask";
import DocumentSubscriptionAddOwnerTask from "../tasks/DocumentSubscriptionAddOwnerTask";
import DocumentSubscriptionRemoveUserTask from "../tasks/DocumentSubscriptionRemoveUserTask";
import BaseProcessor from "./BaseProcessor";
type ReceivedEvent =
| CollectionUserEvent
| CollectionGroupEvent
| DocumentUserEvent
| DocumentGroupEvent
| DocumentOwnerEvent
| CollectionOwnerEvent;
export default class DocumentSubscriptionProcessor extends BaseProcessor {
static applicableEvents: Event["name"][] = [
"collections.remove_user",
"collections.remove_group",
"documents.remove_user",
"documents.remove_group",
"documents.add_owner",
"collections.add_owner",
];
async perform(event: ReceivedEvent) {
switch (event.name) {
case "collections.remove_user": {
await new CollectionSubscriptionRemoveUserTask().schedule(event);
return;
}
case "collections.remove_group":
return this.handleRemoveGroupFromCollection(event);
case "documents.remove_user": {
await new DocumentSubscriptionRemoveUserTask().schedule(event);
return;
}
case "documents.remove_group":
return this.handleRemoveGroupFromDocument(event);
case "documents.add_owner": {
await new DocumentSubscriptionAddOwnerTask().schedule(event);
return;
}
case "collections.add_owner": {
await new CollectionSubscriptionAddOwnerTask().schedule(event);
return;
}
default:
}
}
private async handleRemoveGroupFromCollection(event: CollectionGroupEvent) {
await GroupUser.findAllInBatches<GroupUser>(
{
where: {
groupId: event.modelId,
},
batchLimit: 10,
},
async (groupUsers) => {
await Promise.all(
groupUsers.map((groupUser) =>
new CollectionSubscriptionRemoveUserTask().schedule({
...event,
name: "collections.remove_user",
userId: groupUser.userId,
} as CollectionUserEvent)
)
);
}
);
}
private async handleRemoveGroupFromDocument(event: DocumentGroupEvent) {
await GroupUser.findAllInBatches<GroupUser>(
{
where: {
groupId: event.modelId,
},
batchLimit: 10,
},
async (groupUsers) => {
await Promise.all(
groupUsers.map((groupUser) =>
new DocumentSubscriptionRemoveUserTask().schedule({
...event,
name: "documents.remove_user",
userId: groupUser.userId,
} as DocumentUserEvent)
)
);
}
);
}
}