@hubot-friends/hubot-slack
Advanced tools
Comparing version
@@ -1,3 +0,3 @@ | ||
const SlackBot = require('./src/bot'); | ||
require('./src/extensions'); | ||
const SlackBot = require('./src/bot.js').SlackBot; | ||
require('./src/extensions.js'); | ||
@@ -4,0 +4,0 @@ exports.use = function(robot) { |
{ | ||
"name": "@hubot-friends/hubot-slack", | ||
"version": "1.0.11", | ||
"version": "2.0.0", | ||
"description": "A new Slack adapter for Hubot", | ||
@@ -30,3 +30,3 @@ "homepage": "https://github.com/hubot-friends/hubot-slack#readme", | ||
"peerDependencies": { | ||
"hubot": ">= 7.0.0" | ||
"hubot": ">= 9.0.0" | ||
}, | ||
@@ -33,0 +33,0 @@ "engines": { |
122
src/bot.js
@@ -23,3 +23,3 @@ const {Adapter, TextMessage, EnterMessage, LeaveMessage, TopicMessage, CatchAllMessage, User} = require.main.require("hubot/es2015.js"); | ||
this.robot.logger.debug(`SocketModeClient initialized with options: ${JSON.stringify(options.socketModeOptions)}`); | ||
this.robot.logger.debug(`SocketModeClient initialized with options: ${JSON.stringify(options.socketModeOptions) ?? ''}`); | ||
@@ -62,19 +62,17 @@ // Map to convert bot user IDs (BXXXXXXXX) to user representations for events from custom | ||
setTopic(conversationId, topic) { | ||
async setTopic(conversationId, topic) { | ||
this.robot.logger.debug(`SlackClient#setTopic() with topic ${topic}`); | ||
return this.web.conversations.info({channel: conversationId}) | ||
.then(res => { | ||
const conversation = res.channel; | ||
if (!conversation.is_im && !conversation.is_mpim) { | ||
return this.web.conversations.setTopic({channel: conversationId, topic}); | ||
} else { | ||
return this.robot.logger.debug(`Conversation ${conversationId} is a DM or MPDM. ` + | ||
"These conversation types do not have topics." | ||
); | ||
} | ||
}).catch(error => { | ||
return this.robot.logger.error(error, `Error setting topic in conversation ${conversationId}: ${error.message}`); | ||
}); | ||
try { | ||
const res = await this.web.conversations.info({channel: conversationId}) | ||
const conversation = res.channel; | ||
if (!conversation.is_im && !conversation.is_mpim) { | ||
return this.web.conversations.setTopic({channel: conversationId, topic}); | ||
} else { | ||
return this.robot.logger.debug(`Conversation ${conversationId} is a DM or MPDM. These conversation types do not have topics.`); | ||
} | ||
} catch (error) { | ||
this.robot.logger.error(error, `Error setting topic in conversation ${conversationId}: ${error.message}`); | ||
} | ||
} | ||
send(envelope, message) { | ||
async send(envelope, message) { | ||
const room = envelope.room || envelope.id; | ||
@@ -88,9 +86,15 @@ if (room == null) { | ||
message.channel = room | ||
return this.web.chat.postMessage(message).then(result => { | ||
try { | ||
const result = await this.web.chat.postMessage(message) | ||
this.robot.logger.debug(`Successfully sent message to ${room}`) | ||
}).catch(e => this.robot.logger.error(e, `SlackClient#send(message) error: ${e.message}`)) | ||
} catch (e) { | ||
this.robot.logger.error(e, `SlackClient#send(message) error: ${e.message}`) | ||
} | ||
} else { | ||
return this.web.chat.postMessage({ channel: room, text: message }).then(result => { | ||
try { | ||
const result = await this.web.chat.postMessage({ channel: room, text: message }) | ||
this.robot.logger.debug(`Successfully sent message (string) to ${room}`) | ||
}).catch(e => this.robot.logger.error(e, `SlackClient#send(string) error: ${e.message}`)) | ||
} catch (e) { | ||
this.robot.logger.error(e, `SlackClient#send(string) error: ${e.message}`) | ||
} | ||
} | ||
@@ -127,3 +131,3 @@ } | ||
} | ||
fetchConversation(conversationId) { | ||
async fetchConversation(conversationId) { | ||
const expiration = Date.now() - SlackClient.CONVERSATION_CACHE_TTL_MS; | ||
@@ -133,11 +137,10 @@ if (((this.channelData[conversationId] != null ? this.channelData[conversationId].channel : undefined) != null) && | ||
if (this.channelData[conversationId] != null) { delete this.channelData[conversationId]; } | ||
return this.web.conversations.info({channel: conversationId}).then(r => { | ||
if (r.channel != null) { | ||
this.channelData[conversationId] = { | ||
channel: r.channel, | ||
updated: Date.now() | ||
}; | ||
} | ||
return r.channel; | ||
}); | ||
const r = await this.web.conversations.info({channel: conversationId}) | ||
if (r.channel != null) { | ||
this.channelData[conversationId] = { | ||
channel: r.channel, | ||
updated: Date.now() | ||
}; | ||
} | ||
return r.channel; | ||
} | ||
@@ -254,3 +257,3 @@ updateUserInBrain(event_or_user) { | ||
*/ | ||
send(envelope, ...messages) { | ||
async send(envelope, ...messages) { | ||
this.robot.logger.debug('Sending message to Slack'); | ||
@@ -266,3 +269,11 @@ let callback = function() {}; | ||
}); | ||
return Promise.all(messagePromises).then(callback.bind(null, null), callback); | ||
let results = []; | ||
try { | ||
results = await Promise.all(messagePromises) | ||
callback(null, null) | ||
} catch (e) { | ||
this.robot.logger.error(e); | ||
callback(e, null); | ||
} | ||
return results; | ||
} | ||
@@ -276,3 +287,3 @@ | ||
*/ | ||
reply(envelope, ...messages) { | ||
async reply(envelope, ...messages) { | ||
this.robot.logger.debug('replying to message'); | ||
@@ -294,3 +305,11 @@ let callback = function() {}; | ||
}); | ||
return Promise.all(messagePromises).then(callback.bind(null, null), callback); | ||
let results = []; | ||
try { | ||
results = await Promise.all(messagePromises) | ||
callback(null, null) | ||
} catch (e) { | ||
this.robot.logger.error(e); | ||
callback(e, null); | ||
} | ||
return results; | ||
} | ||
@@ -304,6 +323,4 @@ | ||
*/ | ||
setTopic(envelope, ...strings) { | ||
// TODO: if the sender is interested in the completion, the last item in `messages` will be a function | ||
// TODO: this will fail if sending an object as a value in strings | ||
return this.client.setTopic(envelope.room, strings.join("\n")); | ||
async setTopic(envelope, ...strings) { | ||
return await this.client.setTopic(envelope.room, strings.join("\n")); | ||
} | ||
@@ -470,7 +487,3 @@ | ||
from.room = channel ?? ''; | ||
// looks like sometimes the profile is not set | ||
if (from.profile) { | ||
from.name = from.profile.display_name; | ||
} | ||
from.name = from?.profile?.display_name ?? null; | ||
@@ -481,2 +494,3 @@ // add the bot id to the message if it's a direct message | ||
this.robot.logger.debug(`Text = ${message.body.event.text}`); | ||
this.robot.logger.debug(`Event subtype = ${message.body.event?.subtype}`); | ||
try { | ||
@@ -489,3 +503,3 @@ switch (message.event.type) { | ||
msg.ts = message.event.ts; | ||
this.receive(msg); | ||
await this.receive(msg); | ||
break; | ||
@@ -496,3 +510,3 @@ case "member_left_channel": | ||
msg.ts = message.ts; | ||
this.receive(msg); | ||
await this.receive(msg); | ||
break; | ||
@@ -511,14 +525,16 @@ case "reaction_added": case "reaction_removed": | ||
this.robot.logger.debug(`Received reaction message from: ${from.id}, reaction: ${message.body.event.reaction}, item type: ${message.body.event.item.type}`); | ||
this.receive(new ReactionMessage(message.body.event.type, from, message.body.event.reaction, item_user, message.body.event.item, message.body.event.event_ts)); | ||
await this.receive(new ReactionMessage(message.body.event.type, from, message.body.event.reaction, item_user, message.body.event.item, message.body.event.event_ts)); | ||
break; | ||
case "file_shared": | ||
this.robot.logger.debug(`Received file_shared message from: ${message.body.event.user_id}, file_id: ${message.body.event.file_id}`); | ||
this.receive(new FileSharedMessage(from, message.body.event.file_id, message.body.event.event_ts)); | ||
await this.receive(new FileSharedMessage(from, message.body.event.file_id, message.body.event.event_ts)); | ||
break; | ||
default: | ||
this.robot.logger.debug(`Received generic message: ${message.event.type}`); | ||
SlackTextMessage.makeSlackTextMessage(from, null, message?.body?.event.text, message?.body?.event, channel, this.robot.name, this.robot.alias, this.client, (error, message) => { | ||
if (error) { return this.robot.logger.error(error, `Dropping message due to error ${error.message}`); } | ||
return this.receive(message); | ||
}); | ||
try { | ||
const msg = await SlackTextMessage.makeSlackTextMessage(from, null, message?.body?.event.text, message?.body?.event, channel, this.robot.name, this.robot.alias, this.client) | ||
await this.receive(msg); | ||
} catch (error) { | ||
this.robot.logger.error(error, `Dropping message due to error ${error.message}`); | ||
} | ||
break; | ||
@@ -550,3 +566,3 @@ } | ||
} | ||
module.exports = SlackBot; | ||
module.exports.SlackClient = SlackClient; | ||
module.exports.SlackBot = SlackBot; |
@@ -1,3 +0,3 @@ | ||
const {Message, TextMessage} = require.main.require("hubot/es2015.js"); | ||
const SlackClient = require("./client"); | ||
const { Message, TextMessage, TopicMessage } = require.main.require("hubot/es2015.js"); | ||
const SlackClient = require("./bot.js"); | ||
const SlackMention = require("./mention"); | ||
@@ -117,6 +117,5 @@ | ||
*/ | ||
buildText(client, cb) { | ||
async buildText(client) { | ||
// base text | ||
let text = (this.rawMessage.text != null) ? this.rawMessage.text : ""; | ||
// flatten any attachments into text | ||
@@ -132,15 +131,14 @@ if (this.rawMessage.attachments) { | ||
const fetchingConversationInfo = client.fetchConversation(this._channel_id); | ||
return Promise.all([mentionFormatting, fetchingConversationInfo]) | ||
.then(results => { | ||
const [ replacedText, conversationInfo ] = results; | ||
text = replacedText; | ||
text = text.replace(/</g, "<"); | ||
text = text.replace(/>/g, ">"); | ||
text = text.replace(/&/g, "&"); | ||
this.text = text; | ||
return cb(); | ||
}).catch(error => { | ||
client.robot.logger.error(error, `An error occurred while building text: ${error.message}`); | ||
return cb(error); | ||
}); | ||
let results = []; | ||
try { | ||
results = await Promise.all([mentionFormatting, fetchingConversationInfo]); | ||
const [ replacedText, conversationInfo ] = results; | ||
text = replacedText; | ||
text = text.replace(/</g, "<"); | ||
text = text.replace(/>/g, ">"); | ||
text = text.replace(/&/g, "&"); | ||
} catch (e) { | ||
client.robot.logger.error(e, `An error occurred while building text: ${e.message}`); | ||
} | ||
return text; | ||
} | ||
@@ -276,18 +274,10 @@ | ||
*/ | ||
static makeSlackTextMessage(user, text, rawText, rawMessage, channel_id, robot_name, robot_alias, client, cb) { | ||
static async makeSlackTextMessage(user, text, rawText, rawMessage, channel_id, robot_name, robot_alias, client) { | ||
if(rawMessage?.subtype) { | ||
return new TopicMessage(user, rawMessage.text, rawMessage.event_ts) | ||
} | ||
const message = new SlackTextMessage(user, text, rawText, rawMessage, channel_id, robot_name, robot_alias); | ||
// creates a completion function that consistently calls the callback after this function has returned | ||
const done = message => setImmediate(() => cb(null, message)); | ||
if ((message.text == null)) { | ||
return message.buildText(client, function(error) { | ||
if (error) { | ||
return cb(error); | ||
} | ||
return done(message); | ||
}); | ||
} else { | ||
return done(message); | ||
} | ||
if (message.text !== null) return message; | ||
message.text = await message.buildText(client); | ||
return message; | ||
} | ||
@@ -294,0 +284,0 @@ } |
const {describe, it, beforeEach, before, after} = require('node:test'); | ||
const assert = require('node:assert/strict'); | ||
const Module = require('module'); | ||
const SlackBot = require('../src/bot.js'); | ||
const SlackBot = require('../src/bot.js').SlackBot; | ||
@@ -211,19 +211,2 @@ const hookModuleToReturnMockFromRequire = (module, mock) => { | ||
describe('Client sending message', function() { | ||
let stubs, client; | ||
beforeEach(function() { | ||
({stubs, client} = require('./stubs.js')()); | ||
}); | ||
it('Should append as_user = true', function() { | ||
client.send({room: stubs.channel.id}, {text: 'foo', user: stubs.user, channel: stubs.channel.id}); | ||
assert.ok(stubs._opts.as_user); | ||
}); | ||
it('Should append as_user = true only as a default', function() { | ||
client.send({room: stubs.channel.id}, {text: 'foo', user: stubs.user, channel: stubs.channel.id, as_user: false}); | ||
assert.deepEqual(stubs._opts.as_user, true); | ||
}); | ||
}); | ||
describe('Reply to Messages', function() { | ||
@@ -294,12 +277,14 @@ let stubs, slackbot; | ||
it('Should set the topic in channels', function(t, done) { | ||
it('Should set the topic in channels', async () => { | ||
let wasCalled = false; | ||
stubs.receiveMock.onTopic = function(topic) { | ||
assert.deepEqual(topic, 'channel'); | ||
done(); | ||
wasCalled = true; | ||
}; | ||
slackbot.setTopic({room: stubs.channel.id}, 'channel'); | ||
await slackbot.setTopic({room: stubs.channel.id}, 'channel'); | ||
assert.deepEqual(wasCalled, true); | ||
}); | ||
it('Should NOT set the topic in DMs', function() { | ||
slackbot.setTopic({room: 'D1232'}, 'DM'); | ||
it('Should NOT set the topic in DMs', async () => { | ||
await slackbot.setTopic({room: 'D1232'}, 'DM'); | ||
assert.equal(stubs._topic, undefined); | ||
@@ -306,0 +291,0 @@ }); |
const { describe, it, beforeEach } = require('node:test'); | ||
const assert = require('node:assert/strict'); | ||
const Module = require('module'); | ||
const SlackTextMessage = require('../src/message.js').SlackTextMessage; | ||
const TopicMessage = require('hubot/src/message.js').TopicMessage; | ||
const hookModuleToReturnMockFromRequire = (module, mock) => { | ||
@@ -38,84 +39,66 @@ const originalRequire = Module.prototype.require; | ||
it('Should decode entities', function(t, done) { | ||
const message = slacktextmessage; | ||
it('Should decode entities', async () => { | ||
let message = slacktextmessage; | ||
message.rawMessage.text = 'foo > & < >&<'; | ||
message.buildText(client, () => { | ||
assert.deepEqual(message.text, 'foo > & < >&<'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo > & < >&<'); | ||
}); | ||
it('Should remove formatting around <http> links', function(t, done) { | ||
const message = slacktextmessage; | ||
it('Should remove formatting around <http> links', async () => { | ||
let message = slacktextmessage; | ||
message.rawMessage.text = 'foo <http://www.example.com> bar'; | ||
message.buildText(client, () => { | ||
assert.deepEqual(message.text, 'foo http://www.example.com bar'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo http://www.example.com bar'); | ||
}); | ||
it('Should remove formatting around <https> links', function(t, done) { | ||
const message = slacktextmessage; | ||
it('Should remove formatting around <https> links', async () => { | ||
let message = slacktextmessage; | ||
message.rawMessage.text = 'foo <https://www.example.com> bar'; | ||
message.buildText(client, () => { | ||
assert.deepEqual(message.text, 'foo https://www.example.com bar'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo https://www.example.com bar'); | ||
}); | ||
it('Should remove formatting around <skype> links', function(t, done) { | ||
it('Should remove formatting around <skype> links', async () => { | ||
const message = slacktextmessage; | ||
message.rawMessage.text = 'foo <skype:echo123?call> bar'; | ||
message.buildText(client, () => { | ||
assert.deepEqual(message.text, 'foo skype:echo123?call bar'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo skype:echo123?call bar'); | ||
}); | ||
it('Should remove formatting around <https> links with a label', function(t, done) { | ||
it('Should remove formatting around <https> links with a label', async () => { | ||
const message = slacktextmessage; | ||
message.rawMessage.text = 'foo <https://www.example.com|label> bar'; | ||
message.buildText(client, () => { | ||
assert.deepEqual(message.text, 'foo label (https://www.example.com) bar'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo label (https://www.example.com) bar'); | ||
}); | ||
it('Should remove formatting around <https> links with a substring label', function(t, done) { | ||
it('Should remove formatting around <https> links with a substring label', async () => { | ||
const message = slacktextmessage; | ||
message.rawMessage.text = 'foo <https://www.example.com|example.com> bar'; | ||
message.buildText(client, () => { | ||
assert.deepEqual(message.text, 'foo https://www.example.com bar'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo https://www.example.com bar'); | ||
}); | ||
it('Should remove formatting around <https> links with a label containing entities', function(t, done) { | ||
it('Should remove formatting around <https> links with a label containing entities', async () => { | ||
const message = slacktextmessage; | ||
message.rawMessage.text = 'foo <https://www.example.com|label > & <> bar'; | ||
message.buildText(client, () => { | ||
assert.deepEqual(message.text, 'foo label > & < (https://www.example.com) bar'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo label > & < (https://www.example.com) bar'); | ||
}); | ||
it('Should remove formatting around <mailto> links', function(t, done) { | ||
it('Should remove formatting around <mailto> links', async () => { | ||
const message = slacktextmessage; | ||
message.rawMessage.text = 'foo <mailto:name@example.com> bar'; | ||
message.buildText(client, () => { | ||
assert.deepEqual(message.text, 'foo name@example.com bar'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo name@example.com bar'); | ||
}); | ||
it('Should remove formatting around <mailto> links with an email label', function(t, done) { | ||
it('Should remove formatting around <mailto> links with an email label', async () => { | ||
const message = slacktextmessage; | ||
message.rawMessage.text = 'foo <mailto:name@example.com|name@example.com> bar'; | ||
message.buildText(client, () => { | ||
assert.deepEqual(message.text, 'foo name@example.com bar'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo name@example.com bar'); | ||
}); | ||
it('Should handle empty text with attachments', function(t, done) { | ||
it('Should handle empty text with attachments', async () => { | ||
const message = slacktextmessage; | ||
@@ -126,88 +109,72 @@ message.rawMessage.text = undefined; | ||
]; | ||
message.buildText(client, () => { | ||
assert.deepEqual(message.text, '\nfirst'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, '\nfirst'); | ||
}); | ||
it('Should handle an empty set of attachments', function(t, done) { | ||
it('Should handle an empty set of attachments', async () => { | ||
const message = slacktextmessage; | ||
message.rawMessage.text = 'foo'; | ||
message.rawMessage.attachments = []; | ||
message.buildText(client, () => { | ||
assert.deepEqual(message.text, 'foo'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo'); | ||
}); | ||
it('Should change multiple links at once', function(t, done) { | ||
it('Should change multiple links at once', async () => { | ||
const message = slacktextmessage; | ||
message.rawMessage.text = 'foo <@U123|label> bar <#C123> <!channel> <https://www.example.com|label>'; | ||
message.buildText(client, () => { | ||
assert.deepEqual(message.text, 'foo @label bar #general @channel label (https://www.example.com)'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo @label bar #general @channel label (https://www.example.com)'); | ||
}); | ||
it('Should populate mentions with simple SlackMention object', function(t, done) { | ||
it('Should populate mentions with simple SlackMention object', async () => { | ||
const message = slacktextmessage; | ||
message.rawMessage.text = 'foo <@U123> bar'; | ||
message.buildText(client, function() { | ||
assert.deepEqual(message.mentions.length, 1); | ||
assert.deepEqual(message.mentions[0].type, 'user'); | ||
assert.deepEqual(message.mentions[0].id, 'U123'); | ||
assert.deepEqual((message.mentions[0] instanceof SlackMention), true); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.mentions.length, 1); | ||
assert.deepEqual(message.mentions[0].type, 'user'); | ||
assert.deepEqual(message.mentions[0].id, 'U123'); | ||
assert.deepEqual((message.mentions[0] instanceof SlackMention), true); | ||
}); | ||
it('Should populate mentions with simple SlackMention object with label', function(t, done) { | ||
it('Should populate mentions with simple SlackMention object with label', async () => { | ||
const message = slacktextmessage; | ||
message.rawMessage.text = 'foo <@U123|label> bar'; | ||
message.buildText(client, function() { | ||
assert.deepEqual(message.mentions.length, 1); | ||
assert.deepEqual(message.mentions[0].type, 'user'); | ||
assert.deepEqual(message.mentions[0].id, 'U123'); | ||
assert.deepEqual(message.mentions[0].info, undefined); | ||
assert.deepEqual((message.mentions[0] instanceof SlackMention), true); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.mentions.length, 1); | ||
assert.deepEqual(message.mentions[0].type, 'user'); | ||
assert.deepEqual(message.mentions[0].id, 'U123'); | ||
assert.deepEqual(message.mentions[0].info, undefined); | ||
assert.deepEqual((message.mentions[0] instanceof SlackMention), true); | ||
}); | ||
it('Should populate mentions with multiple SlackMention objects', function(t, done) { | ||
it('Should populate mentions with multiple SlackMention objects', async () => { | ||
const message = slacktextmessage; | ||
message.rawMessage.text = 'foo <@U123> bar <#C123> baz <@U123|label> qux'; | ||
message.buildText(client, function() { | ||
assert.deepEqual(message.mentions.length, 3); | ||
assert.deepEqual((message.mentions[0] instanceof SlackMention), true); | ||
assert.deepEqual((message.mentions[1] instanceof SlackMention), true); | ||
assert.deepEqual((message.mentions[2] instanceof SlackMention), true); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.mentions.length, 3); | ||
assert.deepEqual((message.mentions[0] instanceof SlackMention), true); | ||
assert.deepEqual((message.mentions[1] instanceof SlackMention), true); | ||
assert.deepEqual((message.mentions[2] instanceof SlackMention), true); | ||
}); | ||
it('Should populate mentions with simple SlackMention object if user in brain', function(t, done) { | ||
it('Should populate mentions with simple SlackMention object if user in brain', async () => { | ||
client.updateUserInBrain(stubs.user); | ||
const message = slacktextmessage; | ||
message.rawMessage.text = 'foo <@U123> bar'; | ||
message.buildText(client, function() { | ||
assert.deepEqual(message.mentions.length, 1); | ||
assert.deepEqual(message.mentions[0].type, 'user'); | ||
assert.deepEqual(message.mentions[0].id, 'U123'); | ||
assert.deepEqual((message.mentions[0] instanceof SlackMention), true); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.mentions.length, 1); | ||
assert.deepEqual(message.mentions[0].type, 'user'); | ||
assert.deepEqual(message.mentions[0].id, 'U123'); | ||
assert.deepEqual((message.mentions[0] instanceof SlackMention), true); | ||
}); | ||
it('Should add conversation to cache', function(t, done) { | ||
it('Should add conversation to cache', async () => { | ||
const message = slacktextmessage; | ||
message.rawMessage.text = 'foo bar'; | ||
message.buildText(client, function() { | ||
assert.deepEqual(message.text, 'foo bar'); | ||
assert.ok(Object.keys(client.channelData).includes('C123')); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo bar'); | ||
assert.ok(Object.keys(client.channelData).includes('C123')); | ||
}); | ||
it('Should not modify conversation if it is not expired', function(t, done) { | ||
it('Should not modify conversation if it is not expired', async () => { | ||
const message = slacktextmessage; | ||
@@ -219,20 +186,16 @@ client.channelData[stubs.channel.id] = { | ||
message.rawMessage.text = 'foo bar'; | ||
message.buildText(client, function() { | ||
assert.deepEqual(message.text, 'foo bar'); | ||
assert.ok(Object.keys(client.channelData).includes('C123')); | ||
assert.deepEqual(client.channelData['C123'].channel.name, 'baz'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo bar'); | ||
assert.ok(Object.keys(client.channelData).includes('C123')); | ||
assert.deepEqual(client.channelData['C123'].channel.name, 'baz'); | ||
}); | ||
it('Should handle conversation errors', function(t, done) { | ||
it('Should handle conversation errors', async () => { | ||
const message = slacktextmessage_invalid_conversation; | ||
message.rawMessage.text = 'foo bar'; | ||
message.buildText(client, () => { | ||
client.robot.logger.logs != null ? assert.deepEqual(client.robot.logger.logs.error.length, 1) : undefined; | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
client.robot.logger.logs != null ? assert.deepEqual(client.robot.logger.logs.error.length, 1) : undefined; | ||
}); | ||
it('Should flatten attachments', function(t, done) { | ||
it('Should flatten attachments', async () => { | ||
const message = slacktextmessage; | ||
@@ -244,17 +207,23 @@ message.rawMessage.text = 'foo bar'; | ||
]; | ||
message.buildText(client, () => { | ||
assert.deepEqual(message.text, 'foo bar\nfirst\nsecond'); | ||
done(); | ||
}); | ||
message.text = await message.buildText(client); | ||
assert.deepEqual(message.text, 'foo bar\nfirst\nsecond'); | ||
}); | ||
it('Should make a TopicMessage if subtype is channel_topic', async () => { | ||
const message = await SlackTextMessage.makeSlackTextMessage({}, null, null, { | ||
subtype: 'channel_topic', | ||
topic: 'foo' | ||
}, 'test', 'test-bot', null, null); | ||
assert.deepEqual(message instanceof TopicMessage, true); | ||
}) | ||
}); | ||
describe('replaceLinks()', function() { | ||
describe('replaceLinks()', () => { | ||
let stubs, client, slacktextmessage, slacktextmessage_invalid_conversation; | ||
beforeEach(function() { | ||
beforeEach(() => { | ||
({ stubs, client, slacktextmessage, slacktextmessage_invalid_conversation } = require('./stubs.js')()); | ||
}); | ||
it('Should change <@U123> links to @name', async function() { | ||
it('Should change <@U123> links to @name', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <@U123> bar'); | ||
@@ -264,3 +233,3 @@ assert.deepEqual(text, 'foo @name bar'); | ||
it('Should change <@U123|label> links to @label', async function() { | ||
it('Should change <@U123|label> links to @label', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <@U123|label> bar'); | ||
@@ -270,3 +239,3 @@ assert.deepEqual(text, 'foo @label bar'); | ||
it('Should handle invalid User ID gracefully', async function() { | ||
it('Should handle invalid User ID gracefully', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <@U555> bar'); | ||
@@ -276,3 +245,3 @@ assert.deepEqual(text, 'foo <@U555> bar'); | ||
it('Should handle empty User API response', async function() { | ||
it('Should handle empty User API response', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <@U789> bar'); | ||
@@ -282,3 +251,3 @@ assert.deepEqual(text, 'foo <@U789> bar'); | ||
it('Should change <#C123> links to #general', async function() { | ||
it('Should change <#C123> links to #general', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <#C123> bar'); | ||
@@ -288,3 +257,3 @@ assert.deepEqual(text, 'foo #general bar'); | ||
it('Should change <#C123|label> links to #label', async function() { | ||
it('Should change <#C123|label> links to #label', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <#C123|label> bar'); | ||
@@ -294,3 +263,3 @@ assert.deepEqual(text, 'foo #label bar'); | ||
it('Should handle invalid Conversation ID gracefully', async function() { | ||
it('Should handle invalid Conversation ID gracefully', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <#C555> bar'); | ||
@@ -300,3 +269,3 @@ assert.deepEqual(text, 'foo <#C555> bar'); | ||
it('Should handle empty Conversation API response', async function() { | ||
it('Should handle empty Conversation API response', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <#C789> bar'); | ||
@@ -306,3 +275,3 @@ assert.deepEqual(text, 'foo <#C789> bar'); | ||
it('Should change <!everyone> links to @everyone', async function() { | ||
it('Should change <!everyone> links to @everyone', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <!everyone> bar'); | ||
@@ -312,3 +281,3 @@ assert.deepEqual(text, 'foo @everyone bar'); | ||
it('Should change <!channel> links to @channel', async function() { | ||
it('Should change <!channel> links to @channel', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <!channel> bar'); | ||
@@ -318,3 +287,3 @@ assert.deepEqual(text, 'foo @channel bar'); | ||
it('Should change <!group> links to @group', async function() { | ||
it('Should change <!group> links to @group', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <!group> bar'); | ||
@@ -324,3 +293,3 @@ assert.deepEqual(text, 'foo @group bar'); | ||
it('Should change <!here> links to @here', async function() { | ||
it('Should change <!here> links to @here', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <!here> bar'); | ||
@@ -330,3 +299,3 @@ assert.deepEqual(text, 'foo @here bar'); | ||
it('Should change <!subteam^S123|@subteam> links to @subteam', async function() { | ||
it('Should change <!subteam^S123|@subteam> links to @subteam', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <!subteam^S123|@subteam> bar'); | ||
@@ -336,3 +305,3 @@ assert.deepEqual(text, 'foo @subteam bar'); | ||
it('Should change <!foobar|hello> links to hello', async function() { | ||
it('Should change <!foobar|hello> links to hello', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <!foobar|hello> bar'); | ||
@@ -342,3 +311,3 @@ assert.deepEqual(text, 'foo hello bar'); | ||
it('Should leave <!foobar> links as-is when no label is provided', async function() { | ||
it('Should leave <!foobar> links as-is when no label is provided', async () => { | ||
const text = await slacktextmessage.replaceLinks(client, 'foo <!foobar> bar'); | ||
@@ -345,0 +314,0 @@ assert.deepEqual(text, 'foo <!foobar> bar'); |
@@ -10,6 +10,6 @@ /* | ||
const SlackBot = require('../src/bot'); | ||
const SlackClient = require('../src/client'); | ||
const SlackBot = require('../src/bot.js').SlackBot; | ||
const SlackClient = require('../src/bot.js').SlackClient; | ||
const {EventEmitter} = require('events'); | ||
const { SlackTextMessage } = require('../src/message'); | ||
const { SlackTextMessage } = require('../src/message.js'); | ||
// Use Hubot's brain in our stubs | ||
@@ -243,3 +243,3 @@ const {Brain, Robot} = require('hubot'); | ||
stubs.conversationsMock = { | ||
setTopic: ({channel, topic}) => { | ||
setTopic: async ({channel, topic}) => { | ||
stubs._topic = topic; | ||
@@ -246,0 +246,0 @@ if (stubs.receiveMock.onTopic != null) { |
25
-10.71%177616
-16.17%71
-2.74%2901
-22.58%