diff --git a/README.md b/README.md index 03cf299..0ebacbc 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,23 @@ -# TOPCODER NOTIFICATIONS SERIES - NOTIFICATIONS SERVER - +# TOPCODER NOTIFICATIONS + +## Description +This repository hosts the API and processors for enabling notifications in various topcoder apps. Currently it is limited to provide this facility to the [Connect](https://github.com/appirio-tech/connect-app) application. Theoretcially, it is a generic framework and application which could be used for sending and consuming notificaitons by any other topcoder app. In very simple words to send notifications using this application: + +1. Send an event to bus +2. Listen that event in tc-notifications +3. There is a config in tc-notifications for each event it want to listen and we can specify rules who are the target users to whom we should send notifications for this event +4. By default it saves all notifications which are generated after parsing the rules specified in step 3 and these are the treated web notifications which we show in the app directly +5. Then there is option to add notification handlers where we get all these notifications one by one and we can process them further for more channels e.g. we send notification emails for each of notification generated +6. When one wants to show the notifications, we use the notifications api (hosted inside the tc-notifications itself as separate service) to fetch notifications, mark notifications read or unread etc. + +tc-notifications (as a standard nodejs app) provides generic framework around notifications, it does not have config (used in step 3 of previous list) for specific apps. So, to have config for specific apps, we have to start the notification consumers per app e.g. for connect, we have a folder [`connect`](https://github.com/topcoder-platform/tc-notifications/blob/dev/connect) which hosts start script to start notification consumers with connect specific configuration ([`events-config.js`](https://github.com/topcoder-platform/tc-notifications/blob/dev/connect/events-config.js)). It also adds email notification service which sends emails for notifications as per notification settings (coming from common framework laid by tc-notifications) for the user. + +### Steps needed to enable other apps to use the notifications are: +1. Have a separate start up script (in a new folder at the root of the repo) for the concerned app. I would call this as notification consumer/processor. +2. Write [`events-config.js`](https://github.com/topcoder-platform/tc-notifications/blob/dev/connect/events-config.js) (name is not important, we have to read this file in the start up script written in step 1) for app specific notifications +3. Write additional notification services (eg. if you want to send email or slack or any other notification) and add them to startup script +4. Specify a node script in package.json to launch the start up script written in step 1 +5. Either add deployment for this new notification consumer/processor in existing deployment script (if you want to host the processor as separate service in the same ECS cluster) or write a new script if you want to keep the deployment separate. ## Dependencies - nodejs https://nodejs.org/en/ (v6+) diff --git a/connect/connectNotificationServer.js b/connect/connectNotificationServer.js index e6ed1f4..c315321 100644 --- a/connect/connectNotificationServer.js +++ b/connect/connectNotificationServer.js @@ -323,8 +323,9 @@ const handler = (topic, message, logger, callback) => { // if message has userId such messages will likely need userHandle and user full name // so let's get it + const ids = [message.initiatorUserId]; if (message.userId) { - const ids = [message.userId]; + ids.push(message.userId); return service.getUsersById(ids); } return []; @@ -335,10 +336,15 @@ const handler = (topic, message, logger, callback) => { notification.contents.timestamp = (new Date()).toISOString(); // if found a user then add user handle if (users.length) { - notification.contents.userHandle = users[0].handle; - notification.contents.userFullName = `${users[0].firstName} ${users[0].lastName}`; - notification.contents.userEmail = users[0].email; - notification.contents.photoURL = users[0].photoURL; + const affectedUser = _.find(users, u => u.userId === message.userId); + const initiatorUser = _.find(users, u => u.userId === message.initiatorUserId); + if (affectedUser) { + notification.contents.userHandle = affectedUser.handle; + notification.contents.userFullName = `${affectedUser.firstName} ${affectedUser.lastName}`; + notification.contents.userEmail = affectedUser.email; + notification.contents.photoURL = affectedUser.photoURL; + } + notification.contents.initiatorUser = initiatorUser; } }); callback(null, allNotifications); diff --git a/connect/constants.js b/connect/constants.js index fd46cf8..5625c62 100644 --- a/connect/constants.js +++ b/connect/constants.js @@ -28,6 +28,8 @@ module.exports = { MANAGER_JOINED: 'notifications.connect.project.member.managerJoined', COPILOT_JOINED: 'notifications.connect.project.member.copilotJoined', ASSIGNED_AS_OWNER: 'notifications.connect.project.member.assignedAsOwner', + INVITE_CREATED: 'notifications.connect.project.member.invite.created', + INVITE_UPDATED: 'notifications.connect.project.member.invite.updated', }, PROJECT: { ACTIVE: 'notifications.connect.project.active', diff --git a/connect/events-config.js b/connect/events-config.js index 142d8aa..4c442bd 100644 --- a/connect/events-config.js +++ b/connect/events-config.js @@ -103,6 +103,10 @@ const EVENTS = [ }, { type: BUS_API_EVENT.CONNECT.MEMBER.MANAGER_JOINED, projectRoles: [PROJECT_ROLE_OWNER, PROJECT_ROLE_COPILOT, PROJECT_ROLE_MANAGER], + }, { + type: BUS_API_EVENT.CONNECT.MEMBER.INVITE_CREATED, + projectRoles: [], + toUserHandle: true, }, // Project activity @@ -255,6 +259,7 @@ const EVENT_BUNDLES = { BUS_API_EVENT.CONNECT.MEMBER.LEFT, BUS_API_EVENT.CONNECT.MEMBER.MANAGER_JOINED, BUS_API_EVENT.CONNECT.MEMBER.REMOVED, + BUS_API_EVENT.CONNECT.MEMBER.INVITE_CREATED, ], }, PROJECT_PLAN: { diff --git a/connect/notificationServices/email.js b/connect/notificationServices/email.js index f7a20cc..27aa187 100644 --- a/connect/notificationServices/email.js +++ b/connect/notificationServices/email.js @@ -233,6 +233,7 @@ function handler(topicName, messageJSON, notification) { authorFullName: notification.contents.userFullName, photoURL: notification.contents.photoURL, type: notificationType, + emailToAffectedUser: notification.contents.userEmail === userEmail, }, recipients, version:"v3", @@ -291,6 +292,19 @@ function handler(topicName, messageJSON, notification) { bundlePeriod = bundlePeriod && bundlePeriod.trim().length > 0 ? bundlePeriod : null; // if bundling is not explicitly set and the event is not a messaging event, assume bundling enabled if (!bundlePeriod && !messagingEvent) { + // finds the event category for the notification type + let eventBundleCategory = _.findKey(EVENT_BUNDLES, b => b.types && b.types.indexOf(notificationType) !== -1); + if (eventBundleCategory) { + const eventBundle = EVENT_BUNDLES[eventBundleCategory]; + // if we find the event category for the notification, use the bundle settings from the first event + if (eventBundle && eventBundle.types && eventBundle.types.length) { + const firstEvtInBundle = eventBundle.types[0]; + const firstEvtBundleSettingPath = `notifications['${firstEvtInBundle}'].${SETTINGS_EMAIL_SERVICE_ID}.bundlePeriod` + let firstEvtBundlePeriod = _.get(settings, firstEvtBundleSettingPath); + bundlePeriod = firstEvtBundlePeriod + logger.debug('Assuming bundle period of first event in the event category=>', bundlePeriod); + } + } // if bundle period is not set, assume it to be daily for default case bundlePeriod = !bundlePeriod ? 'daily' : bundlePeriod; } diff --git a/emails/src/partials/invites.html b/emails/src/partials/invites.html new file mode 100644 index 0000000..8e0da62 --- /dev/null +++ b/emails/src/partials/invites.html @@ -0,0 +1,69 @@ +{{#if [EMAIL_INVITES]}} + + + + + + + + + + + + +
IMG + {{title}}. +
+ + + + + + +
+ + + + + + + + + + + +
You are invited to join the {{projectName}} on Topcoder Connect. To join the project, please register for a Topcoder account using Register button below.
+ + + + + + + + +
+ + + + + + + + + + +
+ + Register + +
+ + + + + + +
+ + +{{/if}} \ No newline at end of file diff --git a/emails/src/partials/project-team.html b/emails/src/partials/project-team.html index 62c169b..733ee86 100644 --- a/emails/src/partials/project-team.html +++ b/emails/src/partials/project-team.html @@ -45,7 +45,11 @@ {{userFullName}} joined the project as Manager {{/if}} {{#if [notifications.connect.project.member.removed]}} - {{userFullName}} left the project + {{#if [emailToAffectedUser]}} + You are removed from the project + {{else}} + {{userFullName}} left the project + {{/if}} {{/if}} {{#if [notifications.connect.project.member.left]}} {{userFullName}} left the project @@ -53,6 +57,9 @@ {{#if [notifications.connect.project.member.joined]}} {{userFullName}} joined the project {{/if}} + {{#if [notifications.connect.project.member.invite.created]}} + Hi {{userFullName}}, you are invited to join the project {{projectName}}. Please click on the button ("View project on Connect") below to join. + {{/if}}