Add type filter to parseMentions

This commit is contained in:
Tom Moor committed 2024-06-09 15:26:03 -04:00
1 parent f439293a7b
commit d10149ccb3
7 files changed
+18 -10

No files matched your search

+4 -2
View File
@@ -32,7 +32,8 @@ export default async function commentUpdater({
transaction,
}: Props): Promise<Comment> {
const mentionIdsBefore = ProsemirrorHelper.parseMentions(
ProsemirrorHelper.toProsemirror(comment.data)
ProsemirrorHelper.toProsemirror(comment.data),
"user"
).map((mention) => mention.id);
if (resolvedBy !== undefined) {
@@ -43,7 +44,8 @@ export default async function commentUpdater({
}
const mentionsAfter = ProsemirrorHelper.parseMentions(
ProsemirrorHelper.toProsemirror(comment.data)
ProsemirrorHelper.toProsemirror(comment.data),
"user"
);
const newMentionIds = mentionsAfter
+2 -2
View File
@@ -203,9 +203,9 @@ export class DocumentHelper {
* @param document Document or Revision
* @returns An array of mentions in passed document or revision
*/
static parseMentions(document: Document | Revision) {
static parseMentions(document: Document | Revision, type?: string) {
const node = DocumentHelper.toProsemirror(document);
return ProsemirrorHelper.parseMentions(node);
return ProsemirrorHelper.parseMentions(node, type);
}
/**
+3 -1
View File
@@ -128,14 +128,16 @@ export class ProsemirrorHelper {
* Returns an array of attributes of all mentions in the node.
*
* @param node The node to parse mentions from
* @param type An optional mention type to filter by
* @returns An array of mention attributes
*/
static parseMentions(doc: Node) {
static parseMentions(doc: Node, type?: string) {
const mentions: MentionAttrs[] = [];
doc.descendants((node: Node) => {
if (
node.type.name === "mention" &&
(!type || node.attrs.type === type) &&
!mentions.some((m) => m.id === node.attrs.id)
) {
mentions.push(node.attrs as MentionAttrs);
@@ -35,7 +35,8 @@ export default class CommentCreatedNotificationsTask extends BaseTask<CommentEve
});
const mentions = ProsemirrorHelper.parseMentions(
ProsemirrorHelper.toProsemirror(comment.data)
ProsemirrorHelper.toProsemirror(comment.data),
"user"
);
const userIdsMentioned: string[] = [];
@@ -19,7 +19,8 @@ export default class CommentUpdatedNotificationsTask extends BaseTask<CommentEve
}
const mentions = ProsemirrorHelper.parseMentions(
ProsemirrorHelper.toProsemirror(comment.data)
ProsemirrorHelper.toProsemirror(comment.data),
"user"
).filter((mention) => event.data.newMentionIds.includes(mention.id));
const userIdsMentioned: string[] = [];
@@ -18,7 +18,7 @@ export default class DocumentPublishedNotificationsTask extends BaseTask<Documen
await createSubscriptionsForDocument(document, event);
// Send notifications to mentioned users first
const mentions = DocumentHelper.parseMentions(document);
const mentions = DocumentHelper.parseMentions(document, "user");
const userIdsMentioned: string[] = [];
for (const mention of mentions) {
@@ -26,8 +26,10 @@ export default class RevisionCreatedNotificationsTask extends BaseTask<RevisionE
// Send notifications to mentioned users first
const before = await revision.before();
const oldMentions = before ? DocumentHelper.parseMentions(before) : [];
const newMentions = DocumentHelper.parseMentions(document);
const oldMentions = before
? DocumentHelper.parseMentions(before, "user")
: [];
const newMentions = DocumentHelper.parseMentions(document, "user");
const mentions = differenceBy(newMentions, oldMentions, "id");
const userIdsMentioned: string[] = [];