mirror of
https://github.com/outline/outline.git
synced 2026-11-21 13:34:02 +03:00
* Unsubscribe from document updates when a user is removed from a document * dummy default
73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
import { Transaction } from "sequelize";
|
|
import subscriptionCreator from "@server/commands/subscriptionCreator";
|
|
import { createContext } from "@server/context";
|
|
import { Subscription, User } from "@server/models";
|
|
import { sequelize } from "@server/storage/database";
|
|
import { DocumentUserEvent, Event } from "@server/types";
|
|
import BaseProcessor from "./BaseProcessor";
|
|
|
|
export default class DocumentSubscriptionProcessor extends BaseProcessor {
|
|
static applicableEvents: Event["name"][] = [
|
|
"documents.add_user",
|
|
"documents.remove_user",
|
|
];
|
|
|
|
async perform(event: DocumentUserEvent) {
|
|
const user = await User.findByPk(event.userId);
|
|
if (!user) {
|
|
return;
|
|
}
|
|
|
|
switch (event.name) {
|
|
case "documents.add_user": {
|
|
return this.addUser(event, user);
|
|
}
|
|
|
|
case "documents.remove_user": {
|
|
return this.removeUser(event, user);
|
|
}
|
|
|
|
default:
|
|
}
|
|
}
|
|
|
|
private async addUser(event: DocumentUserEvent, user: User) {
|
|
await sequelize.transaction(async (transaction) => {
|
|
await subscriptionCreator({
|
|
ctx: createContext({
|
|
user,
|
|
authType: event.authType,
|
|
ip: event.ip,
|
|
transaction,
|
|
}),
|
|
documentId: event.documentId,
|
|
event: "documents.update",
|
|
resubscribe: false,
|
|
});
|
|
});
|
|
}
|
|
|
|
private async removeUser(event: DocumentUserEvent, user: User) {
|
|
await sequelize.transaction(async (transaction) => {
|
|
const subscription = await Subscription.findOne({
|
|
where: {
|
|
userId: user.id,
|
|
documentId: event.documentId,
|
|
event: "documents.update",
|
|
},
|
|
transaction,
|
|
lock: Transaction.LOCK.UPDATE,
|
|
});
|
|
|
|
await subscription?.destroyWithCtx(
|
|
createContext({
|
|
user,
|
|
authType: event.authType,
|
|
ip: event.ip,
|
|
transaction,
|
|
})
|
|
);
|
|
});
|
|
}
|
|
}
|