Skip to content

Commit

Permalink
Merge pull request #6 from atlassian/send-messages-as-markdown
Browse files Browse the repository at this point in the history
Send messages as markdown
  • Loading branch information
rudzon authored Apr 13, 2018
2 parents 1c49b63 + 5513668 commit 83fdd0d
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 267 deletions.
28 changes: 4 additions & 24 deletions __tests__/adapter/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const context = new Context()
beforeAll(() => context.begin())
afterAll(() => context.end())

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

context.app.adapter.reply({
room: {
Expand All @@ -19,26 +19,6 @@ test('Adapter.reply sends message to Stride with user mention prepended', async
}, 'test message')

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'
}],
'type': 'paragraph'
}],
'type': 'doc',
'version': 1
}
})
const res = await sendMessageMarkdownRequest
expect(res.body).toBe('test message')
})
68 changes: 0 additions & 68 deletions __tests__/adapter/replyLink.js

This file was deleted.

18 changes: 3 additions & 15 deletions __tests__/adapter/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ afterAll(() => context.end())

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

context.app.adapter.send({
room: {
Expand All @@ -16,18 +16,6 @@ test('Adapter.send sends message to Stride', async () => {
}, 'test message')

await getTokenRequest
const res = await sendMessageRequest
expect(res.body).toEqual({
'body': {
'content': [{
'content': [{
'text': 'test message',
'type': 'text'
}],
'type': 'paragraph'
}],
'type': 'doc',
'version': 1
}
})
const res = await sendMessageMarkdownRequest
expect(res.body).toEqual('test message')
})
35 changes: 18 additions & 17 deletions __tests__/adapter/sendFailed.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ afterAll(() => context.end())

test('Adapter.send emits error on failed Stride API calls', async (done) => {
const getTokenRequest = utils.waitForRequest(mocks.stride.listeners.getToken)
const sendMessage400Request = utils.waitForRequest(mocks.stride.listeners.sendMessage400)
const sendMessageMarkdown400Request =
utils.waitForRequest(mocks.stride.listeners.sendMessageMarkdown400)

context.app.adapter.send({
room: {
Expand All @@ -17,29 +18,29 @@ test('Adapter.send emits error on failed Stride API calls', async (done) => {

context.app.robot.once('error', res => {
expect(res).toMatchObject({
'code': 400,
'source': 'stride',
'state': {
'req': {
'method': 'POST',
'headers': {
'Content-Type': 'application/json',
'Authorization': 'Bearer kinda jwt'
code: 400,
source: 'stride',
state: {
req: {
method: 'POST',
headers: {
'Content-Type': 'text/markdown',
Authorization: 'Bearer kinda jwt'
},
'body': '{"body":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"test message"}]}],"version":1}}',
'url': 'https://api.atlassian.com/site/a436116f-02ce-4520-8fbb-7301462a1674/conversation/b1d1f2c9-d9c7-4de6-8cfd-e31029fb4ad0/message'
body: 'test message',
url: 'https://api.atlassian.com/site/a436116f-02ce-4520-8fbb-7301462a1674/conversation/b1d1f2c9-d9c7-4de6-8cfd-e31029fb4ad0/message'
},
'methodName': 'sendMessage',
'res': {
'headers': {},
'status': 400,
'body': ''
methodName: 'sendMessageMarkdown',
res: {
headers: {},
status: 400,
body: ''
}
}
})
done()
})

await getTokenRequest
await sendMessage400Request
await sendMessageMarkdown400Request
})
57 changes: 0 additions & 57 deletions __tests__/adapter/sendLink.js

This file was deleted.

20 changes: 20 additions & 0 deletions __tests__/helpers/mocks/stride/listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ function sendMessage (onReply, {
.reply(201, onReply())
}

function sendMessageMarkdown (onReply, {
cloudId = defaultCloudId,
conversationId = defaultConversationId
} = {}) {
return nock(apiUrl)
.post(`/site/${cloudId}/conversation/${conversationId}/message`)
.reply(201, onReply())
}

function sendMessageMarkdown400 (onReply, {
cloudId = defaultCloudId,
conversationId = defaultConversationId
} = {}) {
return nock(apiUrl)
.post(`/site/${cloudId}/conversation/${conversationId}/message`)
.reply(400, onReply())
}

function sendMessage400 (onReply, {
cloudId = defaultCloudId,
conversationId = defaultConversationId
Expand Down Expand Up @@ -52,6 +70,8 @@ module.exports = {
getToken,
sendMessage,
sendMessage400,
sendMessageMarkdown,
sendMessageMarkdown400,
uploadFile,
defaultCloudId,
defaultConversationId
Expand Down
68 changes: 12 additions & 56 deletions lib/StrideAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,66 +61,22 @@ class StrideAdapter extends Adapter {
await invokeReduce(stopOrder, 'stop', this.components)
}

send (envelope, ...messages) {
const doc = new Document()

messages.forEach(msg => {
const paragraph = doc.paragraph()
this._textToParsedParagraph(msg, paragraph)
async send (envelope, ...messages) {
await Promise.each(messages, msg => {
return sc.sendMessageMarkdown(envelope.room.cloudId, envelope.room.conversationId, msg)
.catch(error => {
this.robot.emit('error', error)
})
})

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

reply (envelope, ...messages) {
const userId = envelope.user.id
const doc = new Document()

messages.forEach(msg => {
const paragraph = doc.paragraph()
paragraph
.mention(userId)
.text(': ')
this._textToParsedParagraph(msg, paragraph)
async reply (envelope, ...messages) {
await Promise.each(messages, msg => {
return sc.sendMessageMarkdown(envelope.room.cloudId, envelope.room.conversationId, msg)
.catch(error => {
this.robot.emit('error', error)
})
})

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
}
}

Expand Down
Loading

0 comments on commit 83fdd0d

Please sign in to comment.