Skip to content

Issue #1469 - User mentions in the posts #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions connect/connectNotificationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,38 @@ const getTopCoderMembersNotifications = (eventConfig) => {
});
};

/**
* Get notifications for mentioned users
*
* @param {Object} eventConfig event configuration
* @param {Object} message content
*
* @return {Promise} resolves to a list of notifications
*/
const getNotificationsForMentionedUser = (eventConfig, content) => {
if (!eventConfig.toMentionedUsers) {
return Promise.resolve([]);
}

let notifications = [];
const regexUserId = /@([0-9]+)/g;
let matches = regexUserId.exec(content);
while (matches) {
notifications.push({
userId: matches[1].toString(),
newType: 'notifications.connect.project.post.mention',
contents: {
toUserHandle: true,
},
});
matches = regexUserId.exec(content);
}

// only one per userId
notifications = _.uniqBy(notifications, 'userId');
return Promise.resolve(notifications);
};

/**
* Get project members notifications
*
Expand Down Expand Up @@ -252,6 +284,7 @@ const handler = (topic, message, callback) => {
// - check that event has everything required or throw error
getNotificationsForTopicStarter(eventConfig, message.topicId),
getNotificationsForUserId(eventConfig, message.userId),
getNotificationsForMentionedUser(eventConfig, message.contents.postContent),
getProjectMembersNotifications(eventConfig, project),
getTopCoderMembersNotifications(eventConfig),
]).then((notificationsPerSource) => (
Expand Down
3 changes: 3 additions & 0 deletions connect/events-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ const EVENTS = [
version: 2,
projectRoles: [PROJECT_ROLE_OWNER, PROJECT_ROLE_COPILOT, PROJECT_ROLE_MANAGER, PROJECT_ROLE_MEMBER],
toTopicStarter: true,
toMentionedUsers: true,
}, {
type: 'notifications.connect.project.post.mention',
},
{
type: 'notifications.connect.project.linkCreated',
Expand Down
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function startKafkaConsumer(handlers) {
// save notifications
.then((notifications) => Promise.all(_.map(notifications, (notification) => models.Notification.create({
userId: notification.userId,
type: topicName,
type: notification.newType || topicName,
version: notification.version || null,
contents: _.extend({}, messageJSON, notification.contents),
read: false,
Expand Down