Skip to content

Commit 6ac02e9

Browse files
authored
Merge pull request #17 from passimm/handle_notification
Issue #1469 - User mentions in the posts
2 parents 92f76f4 + 406040a commit 6ac02e9

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

connect/connectNotificationServer.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,38 @@ const getTopCoderMembersNotifications = (eventConfig) => {
5252
});
5353
};
5454

55+
/**
56+
* Get notifications for mentioned users
57+
*
58+
* @param {Object} eventConfig event configuration
59+
* @param {Object} message content
60+
*
61+
* @return {Promise} resolves to a list of notifications
62+
*/
63+
const getNotificationsForMentionedUser = (eventConfig, content) => {
64+
if (!eventConfig.toMentionedUsers) {
65+
return Promise.resolve([]);
66+
}
67+
68+
let notifications = [];
69+
const regexUserId = /@([0-9]+)/g;
70+
let matches = regexUserId.exec(content);
71+
while (matches) {
72+
notifications.push({
73+
userId: matches[1].toString(),
74+
newType: 'notifications.connect.project.post.mention',
75+
contents: {
76+
toUserHandle: true,
77+
},
78+
});
79+
matches = regexUserId.exec(content);
80+
}
81+
82+
// only one per userId
83+
notifications = _.uniqBy(notifications, 'userId');
84+
return Promise.resolve(notifications);
85+
};
86+
5587
/**
5688
* Get project members notifications
5789
*
@@ -252,6 +284,7 @@ const handler = (topic, message, callback) => {
252284
// - check that event has everything required or throw error
253285
getNotificationsForTopicStarter(eventConfig, message.topicId),
254286
getNotificationsForUserId(eventConfig, message.userId),
287+
getNotificationsForMentionedUser(eventConfig, message.contents.postContent),
255288
getProjectMembersNotifications(eventConfig, project),
256289
getTopCoderMembersNotifications(eventConfig),
257290
]).then((notificationsPerSource) => (

connect/events-config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ const EVENTS = [
110110
version: 2,
111111
projectRoles: [PROJECT_ROLE_OWNER, PROJECT_ROLE_COPILOT, PROJECT_ROLE_MANAGER, PROJECT_ROLE_MEMBER],
112112
toTopicStarter: true,
113+
toMentionedUsers: true,
114+
}, {
115+
type: 'notifications.connect.project.post.mention',
113116
},
114117
{
115118
type: 'notifications.connect.project.linkCreated',

src/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function startKafkaConsumer(handlers) {
5252
// save notifications
5353
.then((notifications) => Promise.all(_.map(notifications, (notification) => models.Notification.create({
5454
userId: notification.userId,
55-
type: topicName,
55+
type: notification.newType || topicName,
5656
version: notification.version || null,
5757
contents: _.extend({}, messageJSON, notification.contents),
5858
read: false,

0 commit comments

Comments
 (0)