mirror of
https://github.com/outline/outline.git
synced 2026-11-21 13:34:02 +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.
645 lines
21 KiB
TypeScript
645 lines
21 KiB
TypeScript
import { addMinutes, subMinutes } from "date-fns";
|
|
import { DocumentPermission, NotificationEventType } from "@shared/types";
|
|
import { UserMembership, View } from "@server/models";
|
|
import { sequelize } from "@server/storage/database";
|
|
import {
|
|
buildComment,
|
|
buildDocument,
|
|
buildDraftDocument,
|
|
buildNotification,
|
|
buildSubscription,
|
|
buildUser,
|
|
} from "@server/test/factories";
|
|
import NotificationHelper from "./NotificationHelper";
|
|
|
|
describe("NotificationHelper", () => {
|
|
describe("getCommentNotificationRecipients", () => {
|
|
it("should only return users who have notification enabled for comment creation and are subscribed to the document in case of new thread", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const notificationEnabledUser = await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: { [NotificationEventType.CreateComment]: true },
|
|
});
|
|
const notificationDisabledUser = await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: { [NotificationEventType.CreateComment]: false },
|
|
});
|
|
await Promise.all([
|
|
buildSubscription({
|
|
userId: documentAuthor.id,
|
|
documentId: document.id,
|
|
}),
|
|
buildSubscription({
|
|
userId: notificationEnabledUser.id,
|
|
documentId: document.id,
|
|
}),
|
|
buildSubscription({
|
|
userId: notificationDisabledUser.id,
|
|
documentId: document.id,
|
|
}),
|
|
]);
|
|
|
|
const comment = await buildComment({
|
|
documentId: document.id,
|
|
userId: documentAuthor.id,
|
|
});
|
|
|
|
const recipients =
|
|
await NotificationHelper.getCommentNotificationRecipients(
|
|
document,
|
|
comment,
|
|
comment.createdById
|
|
);
|
|
|
|
expect(recipients.length).toEqual(1);
|
|
expect(recipients[0].id).toEqual(notificationEnabledUser.id);
|
|
});
|
|
|
|
it("should only return users who have notification enabled for comment creation and are subscribed to the document in case of new thread in draft", async () => {
|
|
const documentAuthor = await buildUser();
|
|
|
|
// create a draft
|
|
const document = await buildDraftDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
collectionId: null,
|
|
});
|
|
|
|
// add a bunch of users as direct members
|
|
const user = await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: { [NotificationEventType.CreateComment]: true },
|
|
});
|
|
const user2 = await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: { [NotificationEventType.CreateComment]: true },
|
|
});
|
|
const user3 = await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: { [NotificationEventType.CreateComment]: true },
|
|
});
|
|
await UserMembership.create({
|
|
documentId: document.id,
|
|
userId: user.id,
|
|
permission: DocumentPermission.Read,
|
|
createdById: user.id,
|
|
});
|
|
await UserMembership.create({
|
|
documentId: document.id,
|
|
userId: user2.id,
|
|
permission: DocumentPermission.Read,
|
|
createdById: user.id,
|
|
});
|
|
await UserMembership.create({
|
|
documentId: document.id,
|
|
userId: user3.id,
|
|
permission: DocumentPermission.Read,
|
|
createdById: user.id,
|
|
});
|
|
|
|
// Add a subscription for only one of those users
|
|
await Promise.all([
|
|
buildSubscription({
|
|
userId: user.id,
|
|
}),
|
|
buildSubscription({
|
|
userId: user2.id,
|
|
}),
|
|
buildSubscription({
|
|
userId: user3.id,
|
|
documentId: document.id,
|
|
}),
|
|
]);
|
|
|
|
const comment = await buildComment({
|
|
documentId: document.id,
|
|
userId: documentAuthor.id,
|
|
});
|
|
|
|
const recipients =
|
|
await NotificationHelper.getCommentNotificationRecipients(
|
|
document,
|
|
comment,
|
|
comment.createdById
|
|
);
|
|
|
|
expect(recipients.length).toEqual(1);
|
|
expect(recipients[0].id).toEqual(user3.id);
|
|
});
|
|
|
|
it("should only return users who have notification enabled for comment creation and are in the thread in case of child comment", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const notificationEnabledUserInThread = await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: { [NotificationEventType.CreateComment]: true },
|
|
});
|
|
const notificationEnabledUserNotInThread = await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: { [NotificationEventType.CreateComment]: true },
|
|
});
|
|
const notificationDisabledUser = await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: {
|
|
[NotificationEventType.CreateComment]: false,
|
|
},
|
|
});
|
|
await Promise.all([
|
|
buildSubscription({
|
|
userId: documentAuthor.id,
|
|
documentId: document.id,
|
|
}),
|
|
buildSubscription({
|
|
userId: notificationEnabledUserInThread.id,
|
|
documentId: document.id,
|
|
}),
|
|
buildSubscription({
|
|
userId: notificationEnabledUserNotInThread.id,
|
|
documentId: document.id,
|
|
}),
|
|
buildSubscription({
|
|
userId: notificationDisabledUser.id,
|
|
documentId: document.id,
|
|
}),
|
|
]);
|
|
const parentComment = await buildComment({
|
|
documentId: document.id,
|
|
userId: notificationEnabledUserInThread.id,
|
|
});
|
|
const childComment = await buildComment({
|
|
documentId: document.id,
|
|
userId: documentAuthor.id,
|
|
parentCommentId: parentComment.id,
|
|
});
|
|
|
|
const recipients =
|
|
await NotificationHelper.getCommentNotificationRecipients(
|
|
document,
|
|
childComment,
|
|
childComment.createdById
|
|
);
|
|
|
|
expect(recipients.length).toEqual(1);
|
|
expect(recipients[0].id).toEqual(notificationEnabledUserInThread.id);
|
|
});
|
|
|
|
it("should not return users who have notification disabled for comment creation and are in the thread in case of child comment", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const notificationEnabledUserInThread = await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: { [NotificationEventType.CreateComment]: false },
|
|
});
|
|
const notificationEnabledUserNotInThread = await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: { [NotificationEventType.CreateComment]: true },
|
|
});
|
|
const notificationDisabledUser = await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: {
|
|
[NotificationEventType.CreateComment]: false,
|
|
},
|
|
});
|
|
await Promise.all([
|
|
buildSubscription({
|
|
userId: documentAuthor.id,
|
|
documentId: document.id,
|
|
}),
|
|
buildSubscription({
|
|
userId: notificationEnabledUserInThread.id,
|
|
documentId: document.id,
|
|
}),
|
|
buildSubscription({
|
|
userId: notificationEnabledUserNotInThread.id,
|
|
documentId: document.id,
|
|
}),
|
|
buildSubscription({
|
|
userId: notificationDisabledUser.id,
|
|
documentId: document.id,
|
|
}),
|
|
]);
|
|
const parentComment = await buildComment({
|
|
documentId: document.id,
|
|
userId: notificationEnabledUserInThread.id,
|
|
});
|
|
const childComment = await buildComment({
|
|
documentId: document.id,
|
|
userId: documentAuthor.id,
|
|
parentCommentId: parentComment.id,
|
|
});
|
|
|
|
const recipients =
|
|
await NotificationHelper.getCommentNotificationRecipients(
|
|
document,
|
|
childComment,
|
|
childComment.createdById
|
|
);
|
|
|
|
expect(recipients.length).toEqual(0);
|
|
});
|
|
|
|
it("should return users who have notification enabled and are in the thread but not explicitly subscribed to document", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const notificationEnabledUserInThread = await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: { [NotificationEventType.CreateComment]: true },
|
|
});
|
|
await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: {
|
|
[NotificationEventType.CreateComment]: false,
|
|
},
|
|
});
|
|
const parentComment = await buildComment({
|
|
documentId: document.id,
|
|
userId: notificationEnabledUserInThread.id,
|
|
});
|
|
const childComment = await buildComment({
|
|
documentId: document.id,
|
|
userId: documentAuthor.id,
|
|
parentCommentId: parentComment.id,
|
|
});
|
|
|
|
const recipients =
|
|
await NotificationHelper.getCommentNotificationRecipients(
|
|
document,
|
|
childComment,
|
|
childComment.createdById
|
|
);
|
|
|
|
expect(recipients.length).toEqual(1);
|
|
expect(recipients[0].id).toEqual(notificationEnabledUserInThread.id);
|
|
});
|
|
|
|
it("should not return users who viewed the document after the comment", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const [viewedBefore, viewedAfter] = await Promise.all([
|
|
buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: {
|
|
[NotificationEventType.CreateComment]: true,
|
|
},
|
|
}),
|
|
buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: {
|
|
[NotificationEventType.CreateComment]: true,
|
|
},
|
|
}),
|
|
]);
|
|
await Promise.all(
|
|
[viewedBefore, viewedAfter].map((user) =>
|
|
buildSubscription({ userId: user.id, documentId: document.id })
|
|
)
|
|
);
|
|
|
|
const comment = await buildComment({
|
|
documentId: document.id,
|
|
userId: documentAuthor.id,
|
|
});
|
|
await Promise.all(
|
|
[
|
|
{ user: viewedBefore, updatedAt: subMinutes(comment.createdAt, 1) },
|
|
{ user: viewedAfter, updatedAt: addMinutes(comment.createdAt, 1) },
|
|
].map(async ({ user, updatedAt }) => {
|
|
const view = await View.create({
|
|
userId: user.id,
|
|
documentId: document.id,
|
|
});
|
|
await sequelize.query(
|
|
'UPDATE views SET "updatedAt" = :updatedAt WHERE id = :id',
|
|
{ replacements: { updatedAt, id: view.id } }
|
|
);
|
|
})
|
|
);
|
|
|
|
const recipients =
|
|
await NotificationHelper.getCommentNotificationRecipients(
|
|
document,
|
|
comment,
|
|
comment.createdById
|
|
);
|
|
|
|
expect(recipients.map((recipient) => recipient.id)).toEqual([
|
|
viewedBefore.id,
|
|
]);
|
|
});
|
|
|
|
it("should not return users in the thread who unsubscribed from the document after they were involved", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const userInThread = await buildUser({ teamId: document.teamId });
|
|
const parentComment = await buildComment({
|
|
documentId: document.id,
|
|
userId: userInThread.id,
|
|
});
|
|
await buildSubscription({
|
|
userId: userInThread.id,
|
|
documentId: document.id,
|
|
deletedAt: addMinutes(new Date(), 1),
|
|
});
|
|
const childComment = await buildComment({
|
|
documentId: document.id,
|
|
userId: documentAuthor.id,
|
|
parentCommentId: parentComment.id,
|
|
});
|
|
|
|
const recipients =
|
|
await NotificationHelper.getCommentNotificationRecipients(
|
|
document,
|
|
childComment,
|
|
childComment.createdById
|
|
);
|
|
|
|
expect(recipients.length).toEqual(0);
|
|
});
|
|
|
|
it("should return users in the thread who were involved again after they unsubscribed from the document", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const userInThread = await buildUser({ teamId: document.teamId });
|
|
await buildSubscription({
|
|
userId: userInThread.id,
|
|
documentId: document.id,
|
|
deletedAt: subMinutes(new Date(), 1),
|
|
});
|
|
const parentComment = await buildComment({
|
|
documentId: document.id,
|
|
userId: userInThread.id,
|
|
});
|
|
const childComment = await buildComment({
|
|
documentId: document.id,
|
|
userId: documentAuthor.id,
|
|
parentCommentId: parentComment.id,
|
|
});
|
|
|
|
const recipients =
|
|
await NotificationHelper.getCommentNotificationRecipients(
|
|
document,
|
|
childComment,
|
|
childComment.createdById
|
|
);
|
|
|
|
expect(recipients.length).toEqual(1);
|
|
expect(recipients[0].id).toEqual(userInThread.id);
|
|
});
|
|
|
|
it("should return users in the thread who were mentioned after they unsubscribed from the document", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const userInThread = await buildUser({ teamId: document.teamId });
|
|
const parentComment = await buildComment({
|
|
documentId: document.id,
|
|
userId: userInThread.id,
|
|
});
|
|
await buildSubscription({
|
|
userId: userInThread.id,
|
|
documentId: document.id,
|
|
deletedAt: addMinutes(new Date(), 1),
|
|
});
|
|
const mention = await buildNotification({
|
|
event: NotificationEventType.MentionedInComment,
|
|
userId: userInThread.id,
|
|
teamId: document.teamId,
|
|
documentId: document.id,
|
|
commentId: parentComment.id,
|
|
});
|
|
await sequelize.query(
|
|
'UPDATE notifications SET "createdAt" = :createdAt WHERE id = :id',
|
|
{
|
|
replacements: {
|
|
createdAt: addMinutes(new Date(), 2),
|
|
id: mention.id,
|
|
},
|
|
}
|
|
);
|
|
const childComment = await buildComment({
|
|
documentId: document.id,
|
|
userId: documentAuthor.id,
|
|
parentCommentId: parentComment.id,
|
|
});
|
|
|
|
const recipients =
|
|
await NotificationHelper.getCommentNotificationRecipients(
|
|
document,
|
|
childComment,
|
|
childComment.createdById
|
|
);
|
|
|
|
expect(recipients.map((recipient) => recipient.id)).toEqual([
|
|
userInThread.id,
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("getDocumentNotificationRecipients", () => {
|
|
it("should not return collection subscribers who opted out of the document later", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const subscribedUser = await buildUser({ teamId: document.teamId });
|
|
await buildSubscription({
|
|
userId: subscribedUser.id,
|
|
collectionId: document.collectionId!,
|
|
});
|
|
await buildSubscription({
|
|
userId: subscribedUser.id,
|
|
documentId: document.id,
|
|
deletedAt: addMinutes(new Date(), 1),
|
|
});
|
|
|
|
const recipients =
|
|
await NotificationHelper.getDocumentNotificationRecipients({
|
|
document,
|
|
notificationType: NotificationEventType.UpdateDocument,
|
|
actorId: documentAuthor.id,
|
|
});
|
|
|
|
expect(recipients.length).toEqual(0);
|
|
});
|
|
|
|
it("should return collection subscribers who subscribed after they opted out of the document", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const subscribedUser = await buildUser({ teamId: document.teamId });
|
|
await buildSubscription({
|
|
userId: subscribedUser.id,
|
|
documentId: document.id,
|
|
deletedAt: subMinutes(new Date(), 1),
|
|
});
|
|
await buildSubscription({
|
|
userId: subscribedUser.id,
|
|
collectionId: document.collectionId!,
|
|
});
|
|
|
|
const recipients =
|
|
await NotificationHelper.getDocumentNotificationRecipients({
|
|
document,
|
|
notificationType: NotificationEventType.UpdateDocument,
|
|
actorId: documentAuthor.id,
|
|
});
|
|
|
|
expect(recipients.map((recipient) => recipient.id)).toEqual([
|
|
subscribedUser.id,
|
|
]);
|
|
});
|
|
|
|
it("should return users who have subscribed to the document", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const subscribedUser = await buildUser({ teamId: document.teamId });
|
|
await buildSubscription({
|
|
userId: subscribedUser.id,
|
|
documentId: document.id,
|
|
});
|
|
|
|
const deletedUser = await buildUser({ teamId: document.teamId });
|
|
await buildSubscription({
|
|
userId: deletedUser.id,
|
|
documentId: document.id,
|
|
});
|
|
await deletedUser.destroy({ hooks: false });
|
|
|
|
const recipients =
|
|
await NotificationHelper.getDocumentNotificationRecipients({
|
|
document,
|
|
notificationType: NotificationEventType.UpdateDocument,
|
|
actorId: documentAuthor.id,
|
|
});
|
|
|
|
expect(recipients.length).toEqual(1);
|
|
expect(recipients[0].id).toEqual(subscribedUser.id);
|
|
});
|
|
|
|
it("should return users who have subscribed to the collection", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const subscribedUser = await buildUser({ teamId: document.teamId });
|
|
await buildSubscription({
|
|
userId: subscribedUser.id,
|
|
collectionId: document.collectionId!,
|
|
});
|
|
await buildSubscription({
|
|
userId: subscribedUser.id,
|
|
documentId: document.id,
|
|
});
|
|
|
|
const recipients =
|
|
await NotificationHelper.getDocumentNotificationRecipients({
|
|
document,
|
|
notificationType: NotificationEventType.UpdateDocument,
|
|
actorId: documentAuthor.id,
|
|
});
|
|
|
|
expect(recipients.length).toEqual(1);
|
|
expect(recipients[0].id).toEqual(subscribedUser.id);
|
|
});
|
|
|
|
it("should return users who have subscribed to either the document or the containing collection", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const [documentSubscribedUser, collectionSubscribedUser] =
|
|
await Promise.all([
|
|
buildUser({
|
|
teamId: document.teamId,
|
|
}),
|
|
buildUser({
|
|
teamId: document.teamId,
|
|
}),
|
|
]);
|
|
await Promise.all([
|
|
buildSubscription({
|
|
userId: documentSubscribedUser.id,
|
|
documentId: document.id,
|
|
}),
|
|
buildSubscription({
|
|
userId: collectionSubscribedUser.id,
|
|
collectionId: document.collectionId!,
|
|
}),
|
|
]);
|
|
|
|
const recipients =
|
|
await NotificationHelper.getDocumentNotificationRecipients({
|
|
document,
|
|
notificationType: NotificationEventType.UpdateDocument,
|
|
actorId: documentAuthor.id,
|
|
});
|
|
|
|
expect(recipients.length).toEqual(2);
|
|
|
|
const recipientIds = recipients.map((u) => u.id);
|
|
expect(recipientIds).toContain(collectionSubscribedUser.id);
|
|
expect(recipientIds).toContain(documentSubscribedUser.id);
|
|
});
|
|
|
|
it("should not return suspended users", async () => {
|
|
const documentAuthor = await buildUser();
|
|
const document = await buildDocument({
|
|
userId: documentAuthor.id,
|
|
teamId: documentAuthor.teamId,
|
|
});
|
|
const notificationEnabledUser = await buildUser({
|
|
teamId: document.teamId,
|
|
notificationSettings: { [NotificationEventType.PublishDocument]: true },
|
|
});
|
|
// suspended user
|
|
await buildUser({
|
|
suspendedAt: new Date(),
|
|
teamId: document.teamId,
|
|
notificationSettings: { [NotificationEventType.PublishDocument]: true },
|
|
});
|
|
|
|
const recipients =
|
|
await NotificationHelper.getDocumentNotificationRecipients({
|
|
document,
|
|
notificationType: NotificationEventType.PublishDocument,
|
|
actorId: documentAuthor.id,
|
|
});
|
|
|
|
expect(recipients.length).toEqual(1);
|
|
expect(recipients[0].id).toEqual(notificationEnabledUser.id);
|
|
});
|
|
});
|
|
});
|