Files
outline/server/queues/tasks/DocumentAddUserNotificationsTask.ts
T
Tom Moor 0ab57a1fb0 feat: Manage document owners from the share panel
- Owner is a role in the share panel permission menu 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
- Owners are sorted to the top of the member list
- Owners added through the API or MCP are given the manage permission
- Owners do not also receive the "added to document" notification
- Only added owners are validated, so suspended owners can still be removed
- Deleted users are removed from document owners
- Removes the owners section from Insights
2026-11-06 22:45:07 -05:00

84 lines
2.3 KiB
TypeScript

import type { DocumentPermission } from "@shared/types";
import { NotificationEventType } from "@shared/types";
import Logger from "@server/logging/Logger";
import { Document, Notification, User } from "@server/models";
import type { DocumentUserEvent } from "@server/types";
import { isElevatedPermission } from "@server/utils/permissions";
import { BaseTask, TaskPriority } from "./base/BaseTask";
export default class DocumentAddUserNotificationsTask extends BaseTask<DocumentUserEvent> {
public async perform(event: DocumentUserEvent) {
const permission = event.changes?.attributes.permission as
| DocumentPermission
| undefined;
if (!permission) {
Logger.info(
"task",
`permission not available in the DocumentAddUserNotificationsTask event`,
{
name: event.name,
modelId: event.modelId,
}
);
return;
}
const recipient = await User.findByPk(event.userId);
if (
!recipient ||
recipient.isSuspended ||
!recipient.subscribedToEventType(NotificationEventType.AddUserToDocument)
) {
return;
}
// Owners receive a notification for being made an owner instead.
if (
recipient.subscribedToEventType(NotificationEventType.AddOwnerToDocument)
) {
const document = await Document.unscoped().findByPk(event.documentId, {
attributes: ["id", "ownerIds"],
});
if (document?.ownerIds?.includes(recipient.id)) {
return;
}
}
const isElevated = await isElevatedPermission({
userId: recipient.id,
documentId: event.documentId,
permission,
skipMembershipId: event.modelId,
});
if (!isElevated) {
Logger.debug(
"task",
`Suppressing notification for user ${event.userId} as the new permission does not elevate user's permission to the document`,
{
documentId: event.documentId,
userId: event.userId,
permission,
}
);
return;
}
await Notification.create({
event: NotificationEventType.AddUserToDocument,
userId: event.userId,
actorId: event.actorId,
teamId: event.teamId,
documentId: event.documentId,
membershipId: event.modelId,
});
}
public get options() {
return {
priority: TaskPriority.Background,
};
}
}