Files
outline/server/queues/tasks/CommentUpdatedNotificationsTask.ts
T
7e2d2542b0 Split group mention notifications into separate background task (#11040)
* Initial plan

* Split group notifications into separate background task

- Created GroupMentionedInCommentNotificationsTask for handling group mention notifications
- Updated CommentCreatedNotificationsTask to delegate group notifications
- Updated CommentUpdatedNotificationsTask to delegate group notifications
- Added comprehensive tests for the new task
- Improved scalability with batch processing (10 members per batch)
- Enhanced resilience by isolating group notification failures

Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>

* Optimize database queries in GroupMentionedInCommentNotificationsTask

- Batch fetch users instead of individual queries for better performance
- Add defensive comment explaining duplicate mentions check
- Create user map for O(1) lookups within batch

Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>

* Add comment explaining concurrency control in batch processing

Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>

* Remove unnecessary document loading - use event properties directly

- Removed Document import and document loading query
- Use event.documentId and event.teamId directly
- Reduces database queries and improves performance
- canUserAccessDocument loads document internally when needed

Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>

* Fix TypeScript errors and test failures

- Fixed test: buildComment requires documentId parameter
- Fixed type error: use nullish coalescing for group.actorId ?? event.actorId
- Fixed type definition: use Omit<CommentEvent, "data"> to avoid type conflicts
- All TypeScript errors resolved

Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>

* test

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>
Co-authored-by: Tom Moor <tom@getoutline.com>
2026-01-03 10:14:56 -05:00

181 lines
5.1 KiB
TypeScript

import invariant from "invariant";
import { Op } from "sequelize";
import { MentionType, NotificationEventType } from "@shared/types";
import {
Comment,
Document,
Group,
Notification,
User,
} from "@server/models";
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
import type { CommentEvent, CommentUpdateEvent } from "@server/types";
import { canUserAccessDocument } from "@server/utils/permissions";
import { BaseTask, TaskPriority } from "./base/BaseTask";
import GroupMentionedInCommentNotificationsTask from "./GroupMentionedInCommentNotificationsTask";
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),
{ type: MentionType.User }
).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);
}
}
const groupMentions = ProsemirrorHelper.parseMentions(
ProsemirrorHelper.toProsemirror(comment.data),
{ type: MentionType.Group }
).filter((mention) => newMentionIds.includes(mention.id));
const mentionedGroup: string[] = [];
for (const group of groupMentions) {
if (mentionedGroup.includes(group.modelId)) {
continue;
}
// Check if the group has mentions disabled
const groupModel = await Group.findByPk(group.modelId);
if (groupModel?.disableMentions) {
continue;
}
// Schedule a separate task to handle group member notifications
await new GroupMentionedInCommentNotificationsTask().schedule({
...event,
data: {
groupId: group.modelId,
actorId: group.actorId ?? event.actorId,
},
});
mentionedGroup.push(group.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, {
type: MentionType.User,
});
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,
};
}
}