diff --git a/plugins/qeta-backend/src/service/NotificationManager.ts b/plugins/qeta-backend/src/service/NotificationManager.ts index f8eea301..1d5881d8 100644 --- a/plugins/qeta-backend/src/service/NotificationManager.ts +++ b/plugins/qeta-backend/src/service/NotificationManager.ts @@ -22,42 +22,27 @@ export class NotificationManager { return; } - try { - if (question.entities && question.entities.length > 0) { - await this.notifications.send({ - recipients: { - type: 'entity', - entityRef: question.entities, - excludeEntityRef: username, - }, - payload: { - title: `New question about your entity`, - description: this.formatDescription( - `${username} asked a question about your entity: ${question.title}`, - ), - link: `/qeta/questions/${question.id}`, - topic: 'New question about entity', - }, - }); - } + const notificationReceivers = new Set([ + ...(question?.entities ?? []), + ...followingUsers, + ]); - if (followingUsers.length > 0) { - await this.notifications.send({ - recipients: { - type: 'entity', - entityRef: [...new Set(followingUsers)], - excludeEntityRef: username, - }, - payload: { - title: `New question for tag/entity you follow`, - description: this.formatDescription( - `${username} asked a question: ${question.title}`, - ), - link: `/qeta/questions/${question.id}`, - topic: 'New question with tag', - }, - }); - } + try { + await this.notifications.send({ + recipients: { + type: 'entity', + entityRef: [...notificationReceivers], + excludeEntityRef: username, + }, + payload: { + title: `New question`, + description: this.formatDescription( + `${username} asked a question: ${question.title}`, + ), + link: `/qeta/questions/${question.id}`, + topic: 'New question about entity', + }, + }); } catch (e) { this.logger.error(`Failed to send notification for new question: ${e}`); } @@ -73,65 +58,32 @@ export class NotificationManager { return; } - try { - if (question.author !== username) { - await this.notifications.send({ - recipients: { - type: 'entity', - entityRef: question.author, - excludeEntityRef: username, - }, - payload: { - title: `New comment on your question`, - description: this.formatDescription( - `${username} commented on your question: ${comment}`, - ), - link: `/qeta/questions/${question.id}`, - topic: 'New question comment', - scope: `question:comment:${question.id}`, - }, - }); - } + const commenters = new Set(question.comments?.map(c => c.author)); - const commenters = new Set( - question.comments?.map(c => c.author).filter(a => a !== username), - ); - if (commenters.size > 0) { - await this.notifications.send({ - recipients: { - type: 'entity', - entityRef: Array.from(commenters), - excludeEntityRef: username, - }, - payload: { - title: `New comment on question you commented`, - description: this.formatDescription( - `${username} commented on a question you commented: ${comment}`, - ), - link: `/qeta/questions/${question.id}`, - topic: 'New question comment', - scope: `question:comment:${question.id}`, - }, - }); - } + const notificationReceivers = new Set([ + question.author, + ...(question?.entities ?? []), + ...commenters, + ...followingUsers, + ]); - if (followingUsers.length > 0) { - await this.notifications.send({ - recipients: { - type: 'entity', - entityRef: [...new Set(followingUsers)], - excludeEntityRef: username, - }, - payload: { - title: `New comment for tag/entity you follow`, - description: this.formatDescription( - `${username} commented on a question: ${comment}`, - ), - link: `/qeta/questions/${question.id}`, - topic: 'New question comment with tag', - }, - }); - } + try { + await this.notifications.send({ + recipients: { + type: 'entity', + entityRef: [...notificationReceivers], + excludeEntityRef: username, + }, + payload: { + title: `New comment on question`, + description: this.formatDescription( + `${username} commented on question: ${comment}`, + ), + link: `/qeta/questions/${question.id}`, + topic: 'New question comment', + scope: `question:comment:${question.id}`, + }, + }); } catch (e) { this.logger.error( `Failed to send notification for new question comment: ${e}`, @@ -149,62 +101,29 @@ export class NotificationManager { return; } - try { - if (question.author !== username) { - await this.notifications.send({ - recipients: { - type: 'entity', - entityRef: question.author, - excludeEntityRef: username, - }, - payload: { - title: `New answer on your question`, - description: this.formatDescription( - `${username} answered your question: ${answer.content}`, - ), - link: `/qeta/questions/${question.id}#answer_${answer.id}`, - topic: 'New answer on your question', - scope: `question:answer:${question.id}:author`, - }, - }); - } - - if (question.entities && question.entities.length > 0) { - await this.notifications.send({ - recipients: { - type: 'entity', - entityRef: question.entities, - excludeEntityRef: username, - }, - payload: { - title: `New answer on question about your entity`, - description: this.formatDescription( - `${username} answered a question about your entity: ${answer.content}`, - ), - link: `/qeta/questions/${question.id}#answer_${answer.id}`, - topic: 'New answer on entity question', - scope: `question:answer:${question.id}:entity`, - }, - }); - } + const notificationReceivers = new Set([ + question.author, + ...(question?.entities ?? []), + ...followingUsers, + ]); - if (followingUsers.length > 0) { - await this.notifications.send({ - recipients: { - type: 'entity', - entityRef: [...new Set(followingUsers)], - excludeEntityRef: username, - }, - payload: { - title: `New answer for tag/entity you follow`, - description: this.formatDescription( - `${username} answered a question: ${answer.content}`, - ), - link: `/qeta/questions/${question.id}#answer_${answer.id}`, - topic: 'New answer with tag', - }, - }); - } + try { + await this.notifications.send({ + recipients: { + type: 'entity', + entityRef: [...notificationReceivers], + excludeEntityRef: username, + }, + payload: { + title: `New answer on question`, + description: this.formatDescription( + `${username} answered question: ${answer.content}`, + ), + link: `/qeta/questions/${question.id}#answer_${answer.id}`, + topic: 'New answer on question', + scope: `question:answer:${question.id}:author`, + }, + }); } catch (e) { this.logger.error(`Failed to send notification for new answer: ${e}`); } @@ -221,65 +140,32 @@ export class NotificationManager { return; } - try { - if (answer.author !== username) { - await this.notifications.send({ - recipients: { - type: 'entity', - entityRef: answer.author, - excludeEntityRef: username, - }, - payload: { - title: `New comment on your answer`, - description: this.formatDescription( - `${username} commented your answer: ${comment}`, - ), - link: `/qeta/questions/${question.id}#answer_${answer.id}`, - topic: 'New answer comment', - scope: `answer:comment:${answer.id}`, - }, - }); - } + const commenters = new Set(answer.comments?.map(c => c.author)); - const commenters = new Set( - answer.comments?.map(c => c.author).filter(a => a !== username), - ); - if (commenters.size > 0) { - await this.notifications.send({ - recipients: { - type: 'entity', - entityRef: Array.from(commenters), - excludeEntityRef: username, - }, - payload: { - title: `New comment on answer you commented`, - description: this.formatDescription( - `${username} commented on an answer you commented: ${comment}`, - ), - link: `/qeta/questions/${question.id}#answer_${answer.id}`, - topic: 'New answer comment', - scope: `answer:comment:${answer.id}`, - }, - }); - } + const notificationReceivers = new Set([ + answer.author, + ...commenters, + ...(question?.entities ?? []), + ...followingUsers, + ]); - if (followingUsers.length > 0) { - await this.notifications.send({ - recipients: { - type: 'entity', - entityRef: followingUsers, - excludeEntityRef: username, - }, - payload: { - title: `New comment for tag/entity you follow`, - description: this.formatDescription( - `${username} commented an answer: ${comment}`, - ), - link: `/qeta/questions/${question.id}#answer_${answer.id}`, - topic: 'New answer comment with tag', - }, - }); - } + try { + await this.notifications.send({ + recipients: { + type: 'entity', + entityRef: [...notificationReceivers], + excludeEntityRef: username, + }, + payload: { + title: `New comment on answer`, + description: this.formatDescription( + `${username} commented answer: ${comment}`, + ), + link: `/qeta/questions/${question.id}#answer_${answer.id}`, + topic: 'New answer comment', + scope: `answer:comment:${answer.id}`, + }, + }); } catch (e) { this.logger.error( `Failed to send notification for new answer comment: ${e}`, @@ -292,44 +178,29 @@ export class NotificationManager { return; } - try { - if (answer.author !== username) { - await this.notifications.send({ - recipients: { - type: 'entity', - entityRef: answer.author, - excludeEntityRef: username, - }, - payload: { - title: `Correct answer on question`, - description: this.formatDescription( - `${username} marked your answer as correct: ${answer.content}`, - ), - link: `/qeta/questions/${question.id}#answer_${answer.id}`, - topic: 'Correct answer on question', - scope: `question:correct:${question.id}:answer`, - }, - }); - } + const notificationReceivers = new Set([ + answer.author, + question.author, + ...(question?.entities ?? []), + ]); - if (question.entities && question.entities.length > 0) { - await this.notifications.send({ - recipients: { - type: 'entity', - entityRef: question.entities, - excludeEntityRef: username, - }, - payload: { - title: `Correct answer on question about your entity`, - description: this.formatDescription( - `${username} marked answer correct on question about your entity: ${answer.content}`, - ), - link: `/qeta/questions/${question.id}#answer_${answer.id}`, - topic: 'Correct answer on entity question', - scope: `question:correct:${question.id}:entity`, - }, - }); - } + try { + await this.notifications.send({ + recipients: { + type: 'entity', + entityRef: [...notificationReceivers], + excludeEntityRef: username, + }, + payload: { + title: `Correct answer on question`, + description: this.formatDescription( + `${username} marked answer as correct: ${answer.content}`, + ), + link: `/qeta/questions/${question.id}#answer_${answer.id}`, + topic: 'Correct answer on question', + scope: `question:correct:${question.id}:answer`, + }, + }); } catch (e) { this.logger.error(`Failed to send notification for correct answer: ${e}`); }