From 9a068a6ee88269c637bb3b84a72987caceebe946 Mon Sep 17 00:00:00 2001 From: Vikas Agarwal Date: Wed, 25 Apr 2018 15:03:06 +0530 Subject: [PATCH 1/4] Using constants for events name in connectNotificationServer Updated regular expression to detect user mention --- connect/connectNotificationServer.js | 7 ++++--- src/app.js | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/connect/connectNotificationServer.js b/connect/connectNotificationServer.js index 249a1df..45cb871 100644 --- a/connect/connectNotificationServer.js +++ b/connect/connectNotificationServer.js @@ -9,6 +9,7 @@ const config = require('./config'); const notificationServer = require('../index'); const _ = require('lodash'); const service = require('./service'); +const { BUS_API_EVENT } = require('../src/constants') const EVENTS = require('./events-config').EVENTS; const TOPCODER_ROLE_RULES = require('./events-config').TOPCODER_ROLE_RULES; const PROJECT_ROLE_RULES = require('./events-config').PROJECT_ROLE_RULES; @@ -67,14 +68,14 @@ const getNotificationsForMentionedUser = (eventConfig, content) => { let notifications = []; // eslint-disable-next-line - const regexUserHandle = /title=\"@([a-zA-Z0-9-_.{}\[\]]+)\"/g; + const regexUserHandle = /title=\"@([a-zA-Z0-9-_.{}\[\]]+)\"|\[(.*)\]\(.*\"\@(.*)\"\)/g; const handles = []; let matches = regexUserHandle.exec(content); while (matches) { const handle = matches[1].toString(); notifications.push({ userHandle: handle, - newType: 'notifications.connect.project.post.mention', + newType: BUS_API_EVENT.EMAIL.MENTIONED_IN_POST, contents: { toUserHandle: true, }, @@ -279,7 +280,7 @@ const handler = (topic, message, callback) => { // filter out `notifications.connect.project.topic.created` events send by bot // because they create too much clutter and duplicate info - if (topic === 'notifications.connect.project.topic.created' && message.userId.toString() === config.TCWEBSERVICE_ID) { + if (topic === BUS_API_EVENT.CONNECT.TOPIC_CREATED && message.userId.toString() === config.TCWEBSERVICE_ID) { return callback(null, []); } diff --git a/src/app.js b/src/app.js index 6738344..aa22efd 100644 --- a/src/app.js +++ b/src/app.js @@ -11,7 +11,7 @@ const jwt = require('jsonwebtoken'); const _ = require('lodash'); const cors = require('cors'); const bodyParser = require('body-parser'); -const { BUS_API_EVENT } = require('./constants') +const { BUS_API_EVENT } = require('./constants'); const helper = require('./common/helper'); const helperService = require('./services/helper'); const logger = require('./common/logger'); From ef98d1e581cd3a3760ae3bc5b360ebbf5258724a Mon Sep 17 00:00:00 2001 From: Vikas Agarwal Date: Wed, 25 Apr 2018 15:03:27 +0530 Subject: [PATCH 2/4] Converting markdown to html before sending to email service --- package.json | 3 ++- src/app.js | 2 +- src/services/helper.js | 24 +++++++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 48e720c..7782c6e 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "sequelize": "^4.21.0", "superagent": "^3.8.0", "tc-core-library-js": "appirio-tech/tc-core-library-js.git#v2.2", - "winston": "^2.2.0" + "winston": "^2.2.0", + "remarkable": "^1.7.1" }, "engines": { "node": "6.x" diff --git a/src/app.js b/src/app.js index aa22efd..d0f4d91 100644 --- a/src/app.js +++ b/src/app.js @@ -113,7 +113,7 @@ function startKafkaConsumer(handlers) { name: user.firstName + ' ' + user.lastName, handle: user.handle, topicTitle: connectTopic.title || '', - post: messageJSON.postContent, + post: helperService.markdownToHTML(messageJSON.postContent), date: (new Date()).toISOString(), projectName: notification.contents.projectName, projectId: messageJSON.projectId, diff --git a/src/services/helper.js b/src/services/helper.js index 0fe0311..b3584af 100644 --- a/src/services/helper.js +++ b/src/services/helper.js @@ -4,7 +4,7 @@ const request = require('superagent'); const config = require('config'); const _ = require('lodash'); - +const Remarkable = require('remarkable') /** * Get users details by ids @@ -63,7 +63,29 @@ const getTopic = (topicId, logger) => request }); + +/** + * Convert markdown into raw draftjs state + * + * @param {String} markdown - markdown to convert into raw draftjs object + * @param {Object} options - optional additional data + * + * @return {Object} ContentState +**/ +const markdownToHTML = (markdown) => { + const md = new Remarkable('full', { + html: true, + linkify: true, + // typographer: true, + }) + // Replace the BBCode [u][/u] to markdown '++' for underline style + const _markdown = markdown.replace(new RegExp('\\[/?u\\]', 'g'), '++') + return md.render(_markdown, {}) // remarkable js takes markdown and makes it an array of style objects for us to easily parse +} + + module.exports = { getUsersById, getTopic, + markdownToHTML, }; From f016b6688e9b2b3773b934990dd3d2c3b80c001e Mon Sep 17 00:00:00 2001 From: Vikas Agarwal Date: Wed, 25 Apr 2018 15:20:50 +0530 Subject: [PATCH 3/4] Fixed event name for user mention --- connect/connectNotificationServer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connect/connectNotificationServer.js b/connect/connectNotificationServer.js index 45cb871..aeed76d 100644 --- a/connect/connectNotificationServer.js +++ b/connect/connectNotificationServer.js @@ -75,7 +75,7 @@ const getNotificationsForMentionedUser = (eventConfig, content) => { const handle = matches[1].toString(); notifications.push({ userHandle: handle, - newType: BUS_API_EVENT.EMAIL.MENTIONED_IN_POST, + newType: BUS_API_EVENT.CONNECT.MENTIONED_IN_POST, contents: { toUserHandle: true, }, From 06ff1e19ead4f554bee9c57c489133c5b6582d24 Mon Sep 17 00:00:00 2001 From: Vikas Agarwal Date: Wed, 25 Apr 2018 17:30:40 +0530 Subject: [PATCH 4/4] Fixed accessing wrong match group --- connect/connectNotificationServer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/connect/connectNotificationServer.js b/connect/connectNotificationServer.js index aeed76d..05cab2a 100644 --- a/connect/connectNotificationServer.js +++ b/connect/connectNotificationServer.js @@ -68,11 +68,11 @@ const getNotificationsForMentionedUser = (eventConfig, content) => { let notifications = []; // eslint-disable-next-line - const regexUserHandle = /title=\"@([a-zA-Z0-9-_.{}\[\]]+)\"|\[(.*)\]\(.*\"\@(.*)\"\)/g; + const regexUserHandle = /title=\"@([a-zA-Z0-9-_.{}\[\]]+)\"|\[.*\]\(.*\"\@(.*)\"\)/g; const handles = []; let matches = regexUserHandle.exec(content); while (matches) { - const handle = matches[1].toString(); + const handle = matches[1] ? matches[1].toString() : matches[2].toString(); notifications.push({ userHandle: handle, newType: BUS_API_EVENT.CONNECT.MENTIONED_IN_POST,