Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse links into adf nodes #4

Merged
merged 2 commits into from
Mar 20, 2018
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
68 changes: 68 additions & 0 deletions __tests__/adapter/replyLink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const {Context, mocks, utils} = require('../helpers')
const context = new Context()

beforeAll(() => context.begin())
afterAll(() => context.end())

test('Adapter.reply sends message with parsed links to Stride and with user mention prepended', async () => {
const getTokenRequest = utils.waitForRequest(mocks.stride.listeners.getToken)
const sendMessageRequest = utils.waitForRequest(mocks.stride.listeners.sendMessage)

context.app.adapter.reply({
room: {
cloudId: mocks.stride.listeners.defaultCloudId,
conversationId: mocks.stride.listeners.defaultConversationId
},
user: {
id: 'dummy-user-id'
}
}, ' test message https://media.giphy.com/media/dTJd5ygpxkzWo/giphy.gif test https://media.giphy.com/media/dTJd5ygpxkzWo/giphy.gif test ')

await getTokenRequest
const res = await sendMessageRequest
expect(res.body).toEqual({
'body': {
'content': [{
'content': [{
'attrs': {
'id': 'dummy-user-id'
},
'type': 'mention'
}, {
'text': ': ',
'type': 'text'
}, {
'text': ' test message ',
'type': 'text'
}, {
'marks': [{
'attrs': {
'href': 'https://media.giphy.com/media/dTJd5ygpxkzWo/giphy.gif'
},
'type': 'link'
}],
'text': 'https://media.giphy.com/media/dTJd5ygpxkzWo/giphy.gif',
'type': 'text'
}, {
'text': ' test ',
'type': 'text'
}, {
'marks': [{
'attrs': {
'href': 'https://media.giphy.com/media/dTJd5ygpxkzWo/giphy.gif'
},
'type': 'link'
}],
'text': 'https://media.giphy.com/media/dTJd5ygpxkzWo/giphy.gif',
'type': 'text'
}, {
'text': ' test ',
'type': 'text'
}],
'type': 'paragraph'
}],
'type': 'doc',
'version': 1
}
})
})
57 changes: 57 additions & 0 deletions __tests__/adapter/sendLink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const {Context, mocks, utils} = require('../helpers')
const context = new Context()

beforeAll(() => context.begin())
afterAll(() => context.end())

test('Adapter.send sends message with parsed links to Stride', async () => {
const getTokenRequest = utils.waitForRequest(mocks.stride.listeners.getToken)
const sendMessageRequest = utils.waitForRequest(mocks.stride.listeners.sendMessage)

context.app.adapter.send({
room: {
cloudId: mocks.stride.listeners.defaultCloudId,
conversationId: mocks.stride.listeners.defaultConversationId
}
}, ' test message https://media.giphy.com/media/dTJd5ygpxkzWo/giphy.gif test https://media.giphy.com/media/dTJd5ygpxkzWo/giphy.gif test ')

await getTokenRequest
const res = await sendMessageRequest
expect(res.body).toEqual({
'body': {
'content': [{
'content': [{
'text': ' test message ',
'type': 'text'
}, {
'marks': [{
'attrs': {
'href': 'https://media.giphy.com/media/dTJd5ygpxkzWo/giphy.gif'
},
'type': 'link'
}],
'text': 'https://media.giphy.com/media/dTJd5ygpxkzWo/giphy.gif',
'type': 'text'
}, {
'text': ' test ',
'type': 'text'
}, {
'marks': [{
'attrs': {
'href': 'https://media.giphy.com/media/dTJd5ygpxkzWo/giphy.gif'
},
'type': 'link'
}],
'text': 'https://media.giphy.com/media/dTJd5ygpxkzWo/giphy.gif',
'type': 'text'
}, {
'text': ' test ',
'type': 'text'
}],
'type': 'paragraph'
}],
'type': 'doc',
'version': 1
}
})
})
40 changes: 36 additions & 4 deletions lib/StrideAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const config = require('./common/config')
const components = require('./components')
const { Document } = require('adf-builder')
const sc = require('./common/net/Stride')()
const { extractUrlsWithIndices } = require('twitter-text')

class StrideAdapter extends Adapter {
constructor (robot, options) {
Expand Down Expand Up @@ -64,8 +65,8 @@ class StrideAdapter extends Adapter {
const doc = new Document()

messages.forEach(msg => {
doc.paragraph()
.text(msg)
const paragraph = doc.paragraph()
this._textToParsedParagraph(msg, paragraph)
})

sc.sendMessage(envelope.room.cloudId, envelope.room.conversationId, doc)
Expand All @@ -79,17 +80,48 @@ class StrideAdapter extends Adapter {
const doc = new Document()

messages.forEach(msg => {
doc.paragraph()
const paragraph = doc.paragraph()
paragraph
.mention(userId)
.text(': ')
.text(msg)
this._textToParsedParagraph(msg, paragraph)
})

sc.sendMessage(envelope.room.cloudId, envelope.room.conversationId, doc)
.catch(error => {
this.robot.emit('error', error)
})
}

_textToParsedParagraph (text, paragraph) {
const urlsWithIndices = extractUrlsWithIndices(text)

const plainIndices = urlsWithIndices.reduce((acc, urlIndex) => {
acc.push(urlIndex.indices[0])
acc.push(urlIndex.indices[1])
return acc
}, [0])
plainIndices.push(text.length)

plainIndices.forEach((item, currIndex) => {
const nextItem = plainIndices[currIndex + 1]
if (nextItem) {
if (currIndex % 2) {
const nodeText = text.slice(item, nextItem)
if (nodeText) {
paragraph.link(nodeText, nodeText)
}
} else {
const nodeText = text.slice(item, nextItem)
if (nodeText) {
paragraph.text(nodeText)
}
}
}
})

return paragraph
}
}

function invokeReduce (order, funcName, acc) {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hubot-stride",
"version": "1.0.4",
"version": "1.0.5",
"description": "Hubot adapter for Atlassian Stride",
"main": "lib/web.js",
"directories": {
Expand Down Expand Up @@ -55,7 +55,8 @@
"jsonwebtoken": "^8.1.1",
"lodash": "^4.17.5",
"node-fetch": "^2.0.0",
"require-directory": "^2.1.1"
"require-directory": "^2.1.1",
"twitter-text": "^2.0.4"
},
"jest": {
"testPathIgnorePatterns": [
Expand Down