Skip to content

Commit 79d051a

Browse files
committed
feat: add is_room field to messages
Also add TS definitions for `time` and `time_iso`.
1 parent ec9dc26 commit 79d051a

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

lib/bot.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class WickrBot extends EventEmitter {
4141
let splitArgs = (s) => s.trim().split(/\s+/)
4242

4343
if (match === null) {
44-
return { command: undefined, args: splitArgs(message)}
44+
return { command: undefined, args: splitArgs(message) }
4545
}
4646

4747
let command = match[1]
@@ -51,7 +51,7 @@ class WickrBot extends EventEmitter {
5151
args = splitArgs(match[2])
5252
}
5353

54-
return {command: command, args: args}
54+
return { command, args }
5555
}
5656

5757
_getUsername() {
@@ -94,6 +94,10 @@ class WickrBot extends EventEmitter {
9494
let handlerName = 'default'
9595
let args
9696

97+
// Set the `is_room` value for convenience
98+
// The `receiver` field is not present for room messages
99+
data.is_room = typeof data.receiver === 'undefined'
100+
97101
if (data.msgtype === MESSAGE_TYPE.FILE && this.fileHandler) {
98102
handler = this.fileHandler
99103
handlerName = 'file'
@@ -111,6 +115,7 @@ class WickrBot extends EventEmitter {
111115

112116
if (handler) {
113117
try {
118+
// TODO: we should await this handler to avoid unhandled rejections
114119
handler(data, args)
115120
} catch (error) {
116121
console.warn(`Error executing ${handlerName} handler:`, error)

test/test-wickr-bot.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('WickrBot', function() {
3131
bot.listen('foo', spyFn)
3232
bot.handleMessage()(fakeMsg)
3333

34-
sinon.assert.calledOnceWithExactly(spyFn, JSON.parse(fakeMsg), ['bar', 'baz'])
34+
sinon.assert.calledOnceWithMatch(spyFn, JSON.parse(fakeMsg), ['bar', 'baz'])
3535
})
3636

3737
describe('#handleMessage', function() {
@@ -53,6 +53,26 @@ describe('WickrBot', function() {
5353
bot.handleMessage()(fakeMsg)
5454
})
5555

56+
it('sets the `is_room` field to `true` on messages sent to a room', function() {
57+
let fakeMsg = '{"msgtype": 1000, "message": "/foo@fake-bot bar baz"}'
58+
let spyFn = sinon.spy()
59+
let bot = new WickrBot(this.wickr, 'foo')
60+
61+
bot.listen('foo', spyFn)
62+
bot.handleMessage()(fakeMsg)
63+
sinon.assert.calledOnceWithExactly(spyFn, { is_room: true, ...JSON.parse(fakeMsg) }, ['bar', 'baz'])
64+
})
65+
66+
it('sets the `is_room` field to `false` on messages sent in a 1:1', function() {
67+
let fakeMsg = '{"msgtype": 1000, "receiver": "bob", "message": "/foo@fake-bot bar baz"}'
68+
let spyFn = sinon.spy()
69+
let bot = new WickrBot(this.wickr, 'foo')
70+
71+
bot.listen('foo', spyFn)
72+
bot.handleMessage()(fakeMsg)
73+
sinon.assert.calledOnceWithExactly(spyFn, { is_room: false, ...JSON.parse(fakeMsg) }, ['bar', 'baz'])
74+
})
75+
5676
it('calls the default listener for non-slash command messages', function() {
5777
let fakeMsg = '{"msgtype": 1000, "message": "hey what\'s up?"}'
5878
let spyFn = sinon.spy()
@@ -61,7 +81,7 @@ describe('WickrBot', function() {
6181
bot.setDefaultHandler(spyFn)
6282
bot.handleMessage()(fakeMsg)
6383

64-
sinon.assert.calledOnceWithExactly(spyFn, JSON.parse(fakeMsg), ['hey', 'what\'s', 'up?'])
84+
sinon.assert.calledOnceWithMatch(spyFn, JSON.parse(fakeMsg), ['hey', 'what\'s', 'up?'])
6585
})
6686

6787
it('calls the file handler for files', function() {
@@ -72,7 +92,7 @@ describe('WickrBot', function() {
7292
bot.setFileHandler(spyFn)
7393
bot.handleMessage()(fakeMsg)
7494

75-
sinon.assert.calledOnceWithExactly(spyFn, JSON.parse(fakeMsg), undefined)
95+
sinon.assert.calledOnceWithMatch(spyFn, JSON.parse(fakeMsg), undefined)
7696
})
7797

7898
it('calls the default handler for any message type', function() {
@@ -83,7 +103,7 @@ describe('WickrBot', function() {
83103
bot.setDefaultHandler(spyFn)
84104
bot.handleMessage()(fakeMsg)
85105

86-
sinon.assert.calledOnceWithExactly(spyFn, JSON.parse(fakeMsg), undefined)
106+
sinon.assert.calledOnceWithMatch(spyFn, JSON.parse(fakeMsg), undefined)
87107
})
88108

89109
it('prefers the file handler over the default handler for file type messages', function() {
@@ -95,7 +115,7 @@ describe('WickrBot', function() {
95115
bot.setDefaultHandler(spyFnDefault)
96116
bot.handleMessage()(fakeMsg)
97117

98-
sinon.assert.calledOnceWithExactly(spyFn, JSON.parse(fakeMsg), undefined)
118+
sinon.assert.calledOnceWithMatch(spyFn, JSON.parse(fakeMsg), undefined)
99119
sinon.assert.notCalled(spyFnDefault)
100120
})
101121
})

types/bot.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export interface WickrMessage {
2727
*/
2828
message_id: string
2929

30+
/**
31+
* Indicates if the message is a part of a room (true) or a direct message (false)
32+
*/
33+
is_room: boolean
34+
3035
/**
3136
* Wickr ID of the user who sent the message
3237
*/
@@ -47,6 +52,16 @@ export interface WickrMessage {
4752
*/
4853
msg_ts: string
4954

55+
/**
56+
* Displayable time message was sent.
57+
*/
58+
time: string
59+
60+
/**
61+
* The time the message was sent in ISO format (YYYY-MM-DD hh:mm:ss.xxx)
62+
*/
63+
time_iso: string
64+
5065
/**
5166
* For file message types, this contains details about the file
5267
*/

0 commit comments

Comments
 (0)