Files
outline/server/queues/processors/UserDeletedProcessor.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

126 lines
2.8 KiB
TypeScript

import { Op } from "sequelize";
import {
ApiKey,
Collection,
Document,
GroupUser,
OAuthAuthentication,
Star,
Subscription,
UserAuthentication,
UserMembership,
WebhookSubscription,
} from "@server/models";
import { sequelize } from "@server/storage/database";
import type { Event as TEvent, UserEvent } from "@server/types";
import BaseProcessor from "./BaseProcessor";
export default class UserDeletedProcessor extends BaseProcessor {
static applicableEvents: TEvent["name"][] = ["users.delete"];
async perform(event: UserEvent) {
await sequelize.transaction(async (transaction) => {
await GroupUser.destroy({
where: {
userId: event.userId,
},
transaction,
individualHooks: true,
});
await UserAuthentication.destroy({
where: {
userId: event.userId,
},
transaction,
});
await UserMembership.destroy({
where: {
userId: event.userId,
},
transaction,
});
await Subscription.destroy({
where: {
userId: event.userId,
},
transaction,
});
await ApiKey.destroy({
where: {
userId: event.userId,
},
transaction,
});
await OAuthAuthentication.destroy({
where: {
userId: event.userId,
},
transaction,
});
await Star.destroy({
where: {
userId: event.userId,
},
transaction,
});
// The personal collection becomes an ordinary private collection, so
// that admins can recover the documents in it.
await Collection.update(
{ ownerId: null },
{
where: {
ownerId: event.userId,
},
transaction,
}
);
await Document.unscoped().update(
{
ownerIds: sequelize.fn(
"array_remove",
sequelize.col("ownerIds"),
event.userId
),
},
{
where: {
teamId: event.teamId,
ownerIds: { [Op.contains]: [event.userId] },
},
paranoid: false,
silent: true,
transaction,
}
);
await Collection.unscoped().update(
{
ownerIds: sequelize.fn(
"array_remove",
sequelize.col("ownerIds"),
event.userId
),
},
{
where: {
teamId: event.teamId,
ownerIds: { [Op.contains]: [event.userId] },
},
paranoid: false,
silent: true,
transaction,
}
);
await WebhookSubscription.update(
{ enabled: false },
{
where: {
createdById: event.userId,
enabled: true,
},
transaction,
}
);
});
}
}