diff --git a/connect/connectNotificationServer.js b/connect/connectNotificationServer.js index 249a1df..05cab2a 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(); + const handle = matches[1] ? matches[1].toString() : matches[2].toString(); notifications.push({ userHandle: handle, - newType: 'notifications.connect.project.post.mention', + newType: BUS_API_EVENT.CONNECT.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/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 6738344..d0f4d91 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'); @@ -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, };