Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.
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: 22 additions & 11 deletions lib/SymMessageParser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,23 @@ var SymBotAuth = require('../SymBotAuth')

var SymMessageParser = {}

const regex = /(<div.+?>)(.*)(<\/div>)/g
const regexTagStart = /<\w+>/g
const regexTagEnd = /<\/\w+>/g

const HASHTAG_TYPE = 'org.symphonyoss.taxonomy.hashtag'
const CASHTAG_TYPE = 'org.symphonyoss.fin.security.id.ticker'
const MENTION_TYPE = 'com.symphony.user.userId'
const EntityRegex = 'id=\"\\d+\">(.*?)<'
Comment thread
symphony-elias marked this conversation as resolved.

SymMessageParser.parse = events => {
const arrParsedMessages = []

try {
events.forEach(event => {
if (event.type === 'MESSAGESENT' && event.initiator.user.userId !== SymBotAuth.botUser.id) {
let match
const message = event.payload.messageSent.message
while ((match = regex.exec(message.message)) !== null) {
if (match.index === regex.lastIndex) {
regex.lastIndex++
}
let textMessage = match[2].replace(regexTagStart, '')
textMessage = textMessage.replace(regexTagEnd, '')
message.messageText = textMessage
}

message.messageText = extractTextFromPresentationML(message.message)

arrParsedMessages.push(message)
}
})
Expand All @@ -43,6 +35,25 @@ SymMessageParser.parse = events => {
return arrParsedMessages
}

function extractTextFromPresentationML(presentationMl) {
const trimmedPresentationMl = presentationMl.trim()

let indexOfFirstDivTag = trimmedPresentationMl.indexOf('<div')
if (indexOfFirstDivTag == -1) {
return undefined;
}

let firstIndex = trimmedPresentationMl.indexOf('>', indexOfFirstDivTag) // index of first <div> tag

let lastIndex = trimmedPresentationMl.lastIndexOf('</div>') // index of last </div> tag
if(lastIndex == -1) {
return undefined
}

let insideDiv = trimmedPresentationMl.substring(firstIndex + 1, lastIndex) // extract what is inside <div></div>
return insideDiv.replace(regexTagStart, '').replace(regexTagEnd, '') //remove inside tags and extract text
}

function getTags (message, type) {
const arrTags = []

Expand Down
96 changes: 96 additions & 0 deletions tests/SymMessageParser/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const SymMessageParser = require('../../lib/SymMessageParser');

describe('Message parsing', () => {
beforeAll(() => {
const SymBotAuth = require('../../lib/SymBotAuth')
SymBotAuth.botUser = { id: -1};
});

test('invalid opening tag', () => {
assertMessageParsed('<divsa data-format="PresentationML" data-version="2.0">Hello, World</div>',
'Hello, World');
});

test('no opening tag', () => {
assertMessageParsed(' data-format="PresentationML" data-version="2.0">Hello, World</>',
undefined);
});

test('no closing tag', () => {
assertMessageParsed('<div data-format="PresentationML" data-version="2.0">Hello, World',
undefined);
});

test('simple message', () => {
assertMessageParsed('<div data-format="PresentationML" data-version="2.0">Hello, World</div>',
'Hello, World');
});

test('simple message with spaces around div', () => {
assertMessageParsed(' <div data-format="PresentationML" data-version="2.0">Hello, World</div> ',
'Hello, World');
});

test('message enclosed in markups', () => {
assertMessageParsed('<div data-format="PresentationML" data-version="2.0"><b>Hello, World</b></div>',
'Hello, World');
});

test('message enclosed in div markups', () => {
assertMessageParsed('<div data-format="PresentationML" data-version="2.0"><div>Hello, World</div></div>',
'Hello, World');
});

test('message enclosed in markups with spaces around div', () => {
assertMessageParsed(' <div data-format="PresentationML" data-version="2.0"><b>Hello, World</b></div> ',
'Hello, World');
});

test('message partially enclosed in markups', () => {
assertMessageParsed('<div data-format="PresentationML" data-version="2.0">Hello <b>wonderful</b> world</div>',
'Hello wonderful world');
});

test('message partially enclosed in div markups', () => {
assertMessageParsed('<div data-format="PresentationML" data-version="2.0">Hello <div>wonderful</div> world</div>',
'Hello wonderful world');
});

test('message partially enclosed in markups with spaces around div', () => {
assertMessageParsed(' <div data-format="PresentationML" data-version="2.0">Hello <b>wonderful</b> world</div> ',
'Hello wonderful world');
});

test('message with several levels of markups', () => {
assertMessageParsed(
'<div data-format="PresentationML" data-version="2.0">Hello <b>wonderful <c>and</c> beautiful</b> world</div>',
'Hello wonderful and beautiful world');
});

test('message with several levels of markups, innermost div', () => {
assertMessageParsed(
'<div data-format="PresentationML" data-version="2.0">Hello <b>wonderful <div>and</div> beautiful</b> world</div>',
'Hello wonderful and beautiful world');
});

test('message with several levels of markups with spaces around div', () => {
Comment thread
symphony-elias marked this conversation as resolved.
assertMessageParsed(
' <div data-format="PresentationML" data-version="2.0">Hello <b>wonderful <c>and</c> beautiful</b> world</div> ',
'Hello wonderful and beautiful world');
});
});

function assertMessageParsed(incomingMessage, expectedParsedMessage) {
const event = {
type: 'MESSAGESENT',
initiator: {user: {id: 1234}},
payload: {messageSent: {message: {message: incomingMessage}}}
};

let parsedMessages = SymMessageParser.parse([event]);

expect(Array.isArray(parsedMessages)).toBeTruthy();
expect(parsedMessages.length).toBe(1);

expect(parsedMessages[0].messageText).toBe(expectedParsedMessage);
}