Skip to content

Commit 05d64e5

Browse files
committed
feat: send notifications on post edit
this includes notification for new tags, entities and mentions. closes #313
1 parent 6bb9564 commit 05d64e5

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

plugins/qeta-backend/src/service/NotificationManager.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,51 @@ export class NotificationManager {
131131
return notificationReceivers;
132132
}
133133

134+
async onPostEdit(
135+
username: string,
136+
post: Post,
137+
followingUsers: string[],
138+
): Promise<string[]> {
139+
if (!this.notifications || !this.enabled) {
140+
return [];
141+
}
142+
143+
const notificationReceivers = [
144+
...new Set<string>([post.author, ...followingUsers]),
145+
];
146+
147+
if (notificationReceivers.length === 0) {
148+
return [];
149+
}
150+
151+
try {
152+
const user = await this.getUserDisplayName(username);
153+
154+
await this.notifications.send({
155+
recipients: {
156+
type: 'entity',
157+
entityRef: notificationReceivers,
158+
excludeEntityRef: username,
159+
},
160+
payload: {
161+
title: `Edit for ${post.type}`,
162+
description: this.formatDescription(
163+
`${user} edited ${post.type}: ${post.title}`,
164+
),
165+
link:
166+
post.type === 'question'
167+
? `/qeta/questions/${post.id}`
168+
: `/qeta/articles/${post.id}`,
169+
topic: `${post.type} edited`,
170+
scope: `${post.type}:edit:${post.id}`,
171+
},
172+
});
173+
} catch (e) {
174+
this.logger.error(`Failed to send notification for post edit: ${e}`);
175+
}
176+
return notificationReceivers;
177+
}
178+
134179
async onNewAnswer(
135180
username: string,
136181
question: Post,

plugins/qeta-backend/src/service/routes/posts.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,32 @@ export const postsRoutes = (router: Router, options: RouteOptions) => {
702702
return;
703703
}
704704

705+
wrapAsync(async () => {
706+
const newTags = tags.filter(t => !originalPost.tags?.includes(t));
707+
const newEntities = entities.filter(
708+
e => !originalPost.entities?.includes(e),
709+
);
710+
711+
console.log(newTags, newEntities);
712+
const followingUsers = await Promise.all([
713+
database.getUsersForTags(newTags),
714+
database.getUsersForEntities(newEntities),
715+
]);
716+
717+
const sent = await notificationMgr.onPostEdit(
718+
username,
719+
post,
720+
followingUsers.flat(),
721+
);
722+
const originalMentions = findEntityMentions(originalPost.content);
723+
const mentions = findEntityMentions(request.body.content);
724+
const newMentions = mentions.filter(m => !originalMentions.includes(m));
725+
726+
if (newMentions.length > 0) {
727+
await notificationMgr.onMention(username, post, newMentions, sent);
728+
}
729+
});
730+
705731
events?.publish({
706732
topic: 'qeta',
707733
eventPayload: {

plugins/qeta-backend/src/service/routes/routeUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const getTags = async (
3636
return tags.slice(0, maxTags);
3737
};
3838

39-
export const getEntities = (request: Request, config: Config) => {
39+
export const getEntities = (request: Request, config: Config): string[] => {
4040
const maxEntities = config.getOptionalNumber('qeta.entities.max') ?? 3;
4141
let entities = request.body.entities;
4242
if (Array.isArray(entities)) {

0 commit comments

Comments
 (0)