diff options
Diffstat (limited to 'vnext/server/common')
-rw-r--r-- | vnext/server/common/MessageUtils.js | 66 | ||||
-rw-r--r-- | vnext/server/common/MessageUtils.spec.js | 47 | ||||
-rw-r--r-- | vnext/server/common/__snapshots__/MessageUtils.spec.js.snap | 17 |
3 files changed, 130 insertions, 0 deletions
diff --git a/vnext/server/common/MessageUtils.js b/vnext/server/common/MessageUtils.js new file mode 100644 index 00000000..f766faf2 --- /dev/null +++ b/vnext/server/common/MessageUtils.js @@ -0,0 +1,66 @@ +/** + * check if message is PM + * + * @param {import('../../src/api').Message} msg message + */ + export function isPM(msg) { + return !msg.mid; +} + +/** + * check if message is reply + * + * @param {import('../../src/api').Message} msg message + */ +export function isReply(msg) { + return msg.rid && msg.rid > 0; +} + +/** + * check if message is service one + * + * @param {import('../../src/api').Message} msg message + */ +export function isService(msg) { + return msg.service && msg.service; +} + +/** + * format notification title + * + * @param {import('../../src/api').Message} msg message + * @returns {string} formatted title + */ +export function formatTitle(msg) { + if (isReply(msg)) { + return `Reply by ${msg.user.uname}:`; + } else if (isPM(msg)) { + return `Private message from ${msg.user.uname}:`; + } + return `${msg.user.uname}`; +} + +/** + * format notification quote + * + * @param { import('../../src/api').Message } msg message + * @returns {string} formatted quote line + */ +export function formatQuote(msg) { + if (isReply(msg)) { + return msg.replyQuote || ''; + } else if (isPM(msg)) { + return ''; + } + return (msg.tags || []).map(t => `*${t}`).join(', '); +} + +/** + * format notification body + * + * @param {import('../../src/api').Message} msg message + * @returns {string} formatted body + */ +export function formatMessage(msg) { + return msg.body || 'Sent an image'; +} diff --git a/vnext/server/common/MessageUtils.spec.js b/vnext/server/common/MessageUtils.spec.js new file mode 100644 index 00000000..766b1882 --- /dev/null +++ b/vnext/server/common/MessageUtils.spec.js @@ -0,0 +1,47 @@ +import { formatTitle, formatMessage, formatQuote } from './MessageUtils'; + +describe('Message formatting', () => { + it('Blog message', () => { + let msg = { + 'mid': 1, + 'user': { + 'uid': 1, + 'uname': 'ugnich' + }, + 'tags': [ + 'yo', + 'people' + ], + 'body': 'The message' + }; + expect(formatTitle(msg)).toMatchSnapshot(); + expect(formatQuote(msg)).toMatchSnapshot(); + expect(formatMessage(msg)).toMatchSnapshot(); + }); + it('Reply message', () => { + let msg = { + 'mid': 1, + 'rid': 1, + 'user': { + 'uid': 1, + 'uname': 'ugnich' + }, + 'replyQuote': '> The message', + 'body': 'The reply' + }; + expect(formatTitle(msg)).toMatchSnapshot(); + expect(formatQuote(msg)).toMatchSnapshot(); + expect(formatMessage(msg)).toMatchSnapshot(); + }); + it('PM', () => { + let msg = { + 'user': { + 'uid': 1, + 'uname': 'ugnich' + }, + 'body': 'The PM' + }; + expect(formatTitle(msg)).toMatchSnapshot(); + expect(formatMessage(msg)).toMatchSnapshot(); + }); +}); diff --git a/vnext/server/common/__snapshots__/MessageUtils.spec.js.snap b/vnext/server/common/__snapshots__/MessageUtils.spec.js.snap new file mode 100644 index 00000000..ea58aebc --- /dev/null +++ b/vnext/server/common/__snapshots__/MessageUtils.spec.js.snap @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Message formatting Blog message 1`] = `"ugnich"`; + +exports[`Message formatting Blog message 2`] = `"*yo, *people"`; + +exports[`Message formatting Blog message 3`] = `"The message"`; + +exports[`Message formatting PM 1`] = `"Private message from ugnich:"`; + +exports[`Message formatting PM 2`] = `"The PM"`; + +exports[`Message formatting Reply message 1`] = `"Reply by ugnich:"`; + +exports[`Message formatting Reply message 2`] = `"> The message"`; + +exports[`Message formatting Reply message 3`] = `"The reply"`; |