mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
* feat: Add document unsubscribe link to comment emails - Comment and resolved emails now link to unsubscribe from the document, above the global unsubscribe link. - Thread replies and resolve notifications skip users who unsubscribed from the document after their last involvement in the thread. * refactor: Check only deleted document subscriptions for thread unsubscribe * fix: Make the document unsubscribe link a document-only opt-out - The email link no longer deletes the collection subscription, and records or refreshes a document opt-out on every click. - A document opt-out takes precedence over an older collection subscription. - Users mentioned in the thread after they opt out get thread notifications again.
329 lines
9.8 KiB
TypeScript
329 lines
9.8 KiB
TypeScript
import { uniq, uniqBy } from "es-toolkit/compat";
|
|
import { Op } from "sequelize";
|
|
import {
|
|
NotificationEventType,
|
|
MentionType,
|
|
SubscriptionType,
|
|
} from "@shared/types";
|
|
import Logger from "@server/logging/Logger";
|
|
import type { Document, Collection } from "@server/models";
|
|
import {
|
|
User,
|
|
Subscription,
|
|
Comment,
|
|
Notification,
|
|
View,
|
|
} from "@server/models";
|
|
import { canUserAccessDocument } from "@server/utils/permissions";
|
|
import { ProsemirrorHelper } from "./ProsemirrorHelper";
|
|
|
|
export default class NotificationHelper {
|
|
/**
|
|
* Get the recipients of a notification for a collection event.
|
|
*
|
|
* @param collection The collection to get recipients for
|
|
* @param eventType The event type
|
|
* @returns A list of recipients
|
|
*/
|
|
public static getCollectionNotificationRecipients = async (
|
|
collection: Collection,
|
|
eventType: NotificationEventType
|
|
): Promise<User[]> => {
|
|
// Find all the users that have notifications enabled for this event
|
|
// type at all and aren't the one that performed the action.
|
|
let recipients = await User.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.ne]: collection.createdById,
|
|
},
|
|
teamId: collection.teamId,
|
|
},
|
|
});
|
|
|
|
recipients = recipients.filter((recipient) =>
|
|
recipient.subscribedToEventType(eventType)
|
|
);
|
|
|
|
return recipients;
|
|
};
|
|
|
|
/**
|
|
* Get the recipients of a notification for a comment event.
|
|
*
|
|
* @param document The document associated with the comment
|
|
* @param comment The comment to get recipients for
|
|
* @param actorId The creator of the comment
|
|
* @returns A list of recipients
|
|
*/
|
|
public static getCommentNotificationRecipients = async (
|
|
document: Document,
|
|
comment: Comment,
|
|
actorId: string
|
|
): Promise<User[]> => {
|
|
let recipients: User[];
|
|
|
|
// If this is a reply to another comment, we want to notify all users
|
|
// that are involved in the thread of this comment (i.e. the original
|
|
// comment and all replies to it).
|
|
if (comment.parentCommentId) {
|
|
const contextComments = await Comment.findAll({
|
|
attributes: ["id", "createdById", "createdAt", "data"],
|
|
where: {
|
|
[Op.or]: [
|
|
{ id: comment.parentCommentId },
|
|
{ parentCommentId: comment.parentCommentId },
|
|
],
|
|
},
|
|
});
|
|
|
|
const createdUserIdsInThread = contextComments.map((c) => c.createdById);
|
|
const mentionedUserIdsInThread = contextComments
|
|
.flatMap((c) =>
|
|
ProsemirrorHelper.parseMentions(
|
|
ProsemirrorHelper.toProsemirror(c.data),
|
|
{ type: MentionType.User }
|
|
)
|
|
)
|
|
.map((mention) => mention.modelId);
|
|
|
|
const unsubscribedUserIds = await this.getUsersUnsubscribedFromThread(
|
|
document.id,
|
|
contextComments
|
|
);
|
|
const userIdsInThread = uniq([
|
|
...createdUserIdsInThread,
|
|
...mentionedUserIdsInThread,
|
|
]).filter(
|
|
(userId) => userId !== actorId && !unsubscribedUserIds.has(userId)
|
|
);
|
|
|
|
recipients = await User.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: userIdsInThread,
|
|
},
|
|
teamId: document.teamId,
|
|
},
|
|
});
|
|
|
|
recipients = recipients.filter((recipient) =>
|
|
recipient.subscribedToEventType(NotificationEventType.CreateComment)
|
|
);
|
|
} else {
|
|
recipients = await this.getDocumentNotificationRecipients({
|
|
document,
|
|
notificationType: NotificationEventType.CreateComment,
|
|
actorId,
|
|
// We will check below, this just prevents duplicate queries
|
|
disableAccessCheck: true,
|
|
});
|
|
}
|
|
|
|
recipients = recipients.filter((recipient) => !recipient.isSuspended);
|
|
|
|
// Recipients that viewed the document since the comment was made do not
|
|
// need a notification.
|
|
const views = recipients.length
|
|
? await View.findAll({
|
|
attributes: ["userId"],
|
|
where: {
|
|
userId: recipients.map((recipient) => recipient.id),
|
|
documentId: document.id,
|
|
updatedAt: {
|
|
[Op.gt]: comment.createdAt,
|
|
},
|
|
},
|
|
})
|
|
: [];
|
|
const viewedUserIds = new Set(views.map((view) => view.userId));
|
|
|
|
const filtered: User[] = [];
|
|
|
|
for (const recipient of recipients) {
|
|
if (viewedUserIds.has(recipient.id)) {
|
|
Logger.info(
|
|
"processor",
|
|
`suppressing notification to ${recipient.id} because doc viewed`
|
|
);
|
|
continue;
|
|
}
|
|
|
|
// Check the recipient has access to the collection this document is in. Just
|
|
// because they are subscribed doesn't mean they still have access to read
|
|
// the document.
|
|
if (await canUserAccessDocument(recipient, document.id)) {
|
|
filtered.push(recipient);
|
|
}
|
|
}
|
|
|
|
return filtered;
|
|
};
|
|
|
|
/**
|
|
* Get the users who unsubscribed from the document and were not involved in
|
|
* the thread after they unsubscribed. A user is involved when they comment in
|
|
* the thread, or are mentioned in a comment in the thread.
|
|
*
|
|
* @param documentId The id of the document the thread belongs to.
|
|
* @param comments The comments in the thread.
|
|
* @returns The ids of the users that unsubscribed.
|
|
*/
|
|
public static getUsersUnsubscribedFromThread = async (
|
|
documentId: string,
|
|
comments: Comment[]
|
|
): Promise<Set<string>> => {
|
|
const deletedSubscriptions = await Subscription.findAll({
|
|
attributes: ["userId", "deletedAt"],
|
|
where: {
|
|
documentId,
|
|
event: SubscriptionType.Document,
|
|
deletedAt: { [Op.ne]: null },
|
|
},
|
|
paranoid: false,
|
|
});
|
|
if (!deletedSubscriptions.length) {
|
|
return new Set();
|
|
}
|
|
|
|
// Mention notifications record when a user was mentioned, including in edits.
|
|
const mentions = await Notification.unscoped().findAll({
|
|
attributes: ["userId", "createdAt"],
|
|
where: {
|
|
event: NotificationEventType.MentionedInComment,
|
|
userId: deletedSubscriptions.map((subscription) => subscription.userId),
|
|
commentId: comments.map((comment) => comment.id),
|
|
},
|
|
});
|
|
|
|
return new Set(
|
|
deletedSubscriptions
|
|
.filter(
|
|
({ userId, deletedAt }) =>
|
|
!!deletedAt &&
|
|
!comments.some(
|
|
(comment) =>
|
|
comment.createdById === userId && comment.createdAt > deletedAt
|
|
) &&
|
|
!mentions.some(
|
|
(mention) =>
|
|
mention.userId === userId && mention.createdAt > deletedAt
|
|
)
|
|
)
|
|
.map((subscription) => subscription.userId)
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Get the recipients of a notification for a document event.
|
|
*
|
|
* @param document The document to get recipients for.
|
|
* @param notificationType The notification type for which to find the recipients.
|
|
* @param actorId The id of the user that performed the action.
|
|
* @param disableAccessCheck Whether to disable the access check for the document.
|
|
* @returns A list of recipients
|
|
*/
|
|
public static getDocumentNotificationRecipients = async ({
|
|
document,
|
|
notificationType,
|
|
actorId,
|
|
disableAccessCheck = false,
|
|
}: {
|
|
document: Document;
|
|
notificationType: NotificationEventType;
|
|
actorId: string;
|
|
disableAccessCheck?: boolean;
|
|
}): Promise<User[]> => {
|
|
let recipients: User[];
|
|
|
|
if (notificationType === NotificationEventType.PublishDocument) {
|
|
recipients = await User.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.ne]: actorId,
|
|
},
|
|
teamId: document.teamId,
|
|
notificationSettings: {
|
|
[notificationType]: true,
|
|
},
|
|
},
|
|
});
|
|
} else {
|
|
const userFilter = { userId: { [Op.ne]: actorId } };
|
|
const userInclude = [{ association: "user" as const, required: true }];
|
|
|
|
const [collectionSubs, documentSubs]: Subscription[][] =
|
|
await Promise.all([
|
|
document.collectionId
|
|
? Subscription.findAll({
|
|
where: {
|
|
...userFilter,
|
|
event: SubscriptionType.Document,
|
|
collectionId: document.collectionId,
|
|
},
|
|
include: userInclude,
|
|
})
|
|
: [],
|
|
Subscription.findAll({
|
|
where: {
|
|
...userFilter,
|
|
event: SubscriptionType.Document,
|
|
documentId: document.id,
|
|
},
|
|
include: userInclude,
|
|
}),
|
|
]);
|
|
|
|
// A document opt-out takes precedence over an older collection subscription.
|
|
const optedOut = collectionSubs.length
|
|
? await Subscription.findAll({
|
|
attributes: ["userId", "deletedAt"],
|
|
where: {
|
|
event: SubscriptionType.Document,
|
|
documentId: document.id,
|
|
userId: collectionSubs.map((s) => s.userId),
|
|
deletedAt: { [Op.ne]: null },
|
|
},
|
|
paranoid: false,
|
|
})
|
|
: [];
|
|
const activeCollectionSubs = collectionSubs.filter(
|
|
(collectionSub) =>
|
|
!optedOut.some(
|
|
(optOut) =>
|
|
optOut.userId === collectionSub.userId &&
|
|
!!optOut.deletedAt &&
|
|
optOut.deletedAt > collectionSub.updatedAt
|
|
)
|
|
);
|
|
|
|
recipients = uniqBy(
|
|
[...activeCollectionSubs, ...documentSubs].map((s) => s.user),
|
|
(user) => user.id
|
|
);
|
|
}
|
|
|
|
const filtered = [];
|
|
|
|
for (const recipient of recipients) {
|
|
if (
|
|
recipient.isSuspended ||
|
|
!recipient.subscribedToEventType(notificationType)
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
// Check the recipient has access to the collection this document is in. Just
|
|
// because they are subscribed doesn't mean they still have access to read
|
|
// the document.
|
|
if (
|
|
disableAccessCheck ||
|
|
(await canUserAccessDocument(recipient, document.id))
|
|
) {
|
|
filtered.push(recipient);
|
|
}
|
|
}
|
|
|
|
return filtered;
|
|
};
|
|
}
|