Skip to content

Commit

Permalink
fix: streamline the notifications to prevent duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
drodil committed Oct 4, 2024
1 parent 39fc68c commit 92da2e4
Showing 1 changed file with 112 additions and 241 deletions.
353 changes: 112 additions & 241 deletions plugins/qeta-backend/src/service/NotificationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>([
...(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}`);
}
Expand All @@ -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<string>(question.comments?.map(c => c.author));

const commenters = new Set<string>(
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<string>([
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}`,
Expand All @@ -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<string>([
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}`);
}
Expand All @@ -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<string>(answer.comments?.map(c => c.author));

const commenters = new Set<string>(
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<string>([
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}`,
Expand All @@ -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<string>([
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}`);
}
Expand Down

0 comments on commit 92da2e4

Please sign in to comment.