Files
outline/server/queues/tasks/CommentUpdatedNotificationsTask.ts
infinite-persistence e8bddbe104 Notification for resolved comment (#8045)
* fix: probably copy-pasted function description

* fix: userIdsMentioned was always empty

* add: NotificationEventType.ResolveComment

* move: split handler for "mentioned" vs. "resolved"

The recipients for "resolved" will include more people (creator, repliers, mentioned), so it's easier to just split the handler than trying to augment it.

* implement: handleResolvedComment

* clone: CommentMentionedEmail as CommentResolvedEmail

Changes coming up in next commit...

* implement: CommentResolvedEmail

* Fix "New Comment↓" incorrectly showing in Resolved

## Repro 1 (with production code)
1. In a list of long resolved comments, scroll up and select the first one.
2. From another account, resolve another comment. The hint appears.

## Repro 2 (with production code)
1. Select Most-Recent, then Resolved.
2. F5. It's scrolled all the way to the bottom.

## Repro 3 (after this PR)
1. Click on the notification when someone resolved a comment. The screen jumps to "Resolved" + showing hint unnecessarily.

## Fix
The scrolling and hint was meant for Most Recent only, but missed out this case since "Resolve" is not part of the enum.

* Better sentences

* Refactor "mentions + author" calculation

* Remove unnecessary check

The resolver is already added to `userIdsNotified` from the start, so no point checking it again here.
2024-12-04 15:10:03 -08:00

142 lines
4.1 KiB
TypeScript

import invariant from "invariant";
import { Op } from "sequelize";
import { NotificationEventType } from "@shared/types";
import { Comment, Document, Notification, User } from "@server/models";
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
import { CommentEvent, CommentUpdateEvent } from "@server/types";
import { canUserAccessDocument } from "@server/utils/policies";
import BaseTask, { TaskPriority } from "./BaseTask";
export default class CommentUpdatedNotificationsTask extends BaseTask<CommentEvent> {
public async perform(event: CommentUpdateEvent) {
const isResolving =
event.changes?.previous?.resolvedAt === null &&
event.changes?.attributes?.resolvedAt !== null;
return isResolving
? await this.handleResolvedComment(event)
: await this.handleMentionedComment(event);
}
private async handleMentionedComment(event: CommentUpdateEvent) {
const newMentionIds = event.data?.newMentionIds;
if (!newMentionIds) {
return;
}
const [document, comment] = await Promise.all([
Document.scope("withCollection").findOne({
where: {
id: event.documentId,
},
}),
Comment.findByPk(event.modelId),
]);
if (!document || !comment) {
return;
}
const mentions = ProsemirrorHelper.parseMentions(
ProsemirrorHelper.toProsemirror(comment.data)
).filter((mention) => newMentionIds.includes(mention.id));
const userIdsMentioned: string[] = [];
for (const mention of mentions) {
if (userIdsMentioned.includes(mention.modelId)) {
continue;
}
const recipient = await User.findByPk(mention.modelId);
if (
mention.actorId &&
recipient &&
recipient.id !== mention.actorId &&
recipient.subscribedToEventType(
NotificationEventType.MentionedInComment
) &&
(await canUserAccessDocument(recipient, document.id))
) {
await Notification.create({
event: NotificationEventType.MentionedInComment,
userId: recipient.id,
actorId: mention.actorId,
teamId: document.teamId,
documentId: document.id,
commentId: comment.id,
});
userIdsMentioned.push(mention.modelId);
}
}
}
private async handleResolvedComment(event: CommentUpdateEvent) {
invariant(
!event.data?.newMentionIds,
"newMentionIds should not be present in resolved comment"
);
const [document, commentsAndReplies] = await Promise.all([
Document.scope("withCollection").findOne({
where: { id: event.documentId },
}),
Comment.findAll({
where: {
[Op.or]: [{ id: event.modelId }, { parentCommentId: event.modelId }],
},
}),
]);
if (!document || !commentsAndReplies) {
return;
}
const userIdsNotified: string[] = [];
// Don't notify resolver
userIdsNotified.push(event.actorId);
for (const item of commentsAndReplies) {
// Mentions:
const proseCommentData = ProsemirrorHelper.toProsemirror(item.data);
const mentions = ProsemirrorHelper.parseMentions(proseCommentData);
const userIds = mentions.map((mention) => mention.modelId);
// Comment author:
userIds.push(item.createdById);
for (const userId of userIds) {
if (userIdsNotified.includes(userId)) {
continue;
}
const user = await User.findByPk(userId);
if (
user &&
user.subscribedToEventType(NotificationEventType.ResolveComment) &&
(await canUserAccessDocument(user, document.id))
) {
await Notification.create({
event: NotificationEventType.ResolveComment,
userId: user.id,
actorId: event.actorId,
teamId: document.teamId,
documentId: document.id,
commentId: event.modelId,
});
userIdsNotified.push(userId);
}
}
}
}
public get options() {
return {
attempts: 1,
priority: TaskPriority.Background,
};
}
}