Skip to content

Commit

Permalink
Merge pull request #9 from WickrInc/default-handler-receive-everything
Browse files Browse the repository at this point in the history
feat: default handler now accepts _every_ message type
  • Loading branch information
dwickr committed Oct 26, 2023
2 parents 014446b + 56bea88 commit 4443d84
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 44 deletions.
37 changes: 17 additions & 20 deletions lib/bot.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
const fs = require('fs')
const EventEmitter = require('events')
const mergician = require('mergician')
const WickrBrain = require('./brain')

const MESSAGE_TYPE = {
TEXT: 1000,
VERIFICATION: 3000,
CREATE_ROOM: 4001,
MODIFY_ROOM_MEMBERS: 4002,
LEAVE_ROOM: 4003,
MODIFY_ROOM_PARAMS: 4004,
DELETE_ROOM: 4005,
DELETE_MESSAGE: 4009,
FILE: 6000,
CALL: 7000,
}
const WickrBrain = require('./brain')
const { MESSAGE_TYPE } = require('./constants')

class WickrBot extends EventEmitter {
constructor(wickr, username) {
Expand Down Expand Up @@ -99,18 +88,25 @@ class WickrBot extends EventEmitter {
handleMessage() {
// return as an arrow function to maintain `this` binding
return (message) => {
let data = JSON.parse(message)
let handler, handlerName, args
const data = JSON.parse(message)

let handler = this.defaultHandler
let handlerName = 'default'
let args

if (data.msgtype === MESSAGE_TYPE.FILE) {
if (data.msgtype === MESSAGE_TYPE.FILE && this.fileHandler) {
handler = this.fileHandler
handlerName = 'file'
} else if (data.msgtype === MESSAGE_TYPE.TEXT) {
const msg = this._parseMessage(data.message)

// text type messages receive an array of the words of the message as the second argument
args = msg.args
handler = this.handlers[msg.command]?.fn || this.defaultHandler
handlerName = msg.command || 'default'

// A slash command exists for this message, call its handler instead of the default handler
if (msg.command in this.handlers) {
handler = this.handlers[msg.command].fn
handlerName = msg.command
}
}

if (handler) {
Expand Down Expand Up @@ -302,7 +298,7 @@ class WickrBot extends EventEmitter {
}

/**
* setDefaultHandler defines the function which will be called when a text message is received
* setDefaultHandler defines the function which will be called when a message of any type is received
* which does not match any slash command
*
* @param {function (msg, args)} fn The function to call
Expand Down Expand Up @@ -348,3 +344,4 @@ class WickrBot extends EventEmitter {
}

module.exports = WickrBot
module.exports.MESSAGE_TYPE = MESSAGE_TYPE
27 changes: 27 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const MESSAGE_TYPE = {
TEXT: 1000,
KEY_VERIFICATION: 3000,
CREATE_ROOM: 4001,
MODIFY_ROOM_MEMBERS: 4002,
LEAVE_ROOM: 4003,
MODIFY_ROOM_PARAMS: 4004,
DELETE_ROOM: 4005,
DELETE_MESSAGE: 4011,
MESSAGE_ATTRIBUTES_MESSAGE: 4012,
MESSAGE_ATTRIBUTES_SYNC_REQUEST: 4013,
MODIFY_PRIVATE_PROPERTY: 4014,
ROOM_KEY_REQUEST: 4015,
ROOM_KEY_RESPONSE: 4016,
FILE: 6000,
CALL: 7000,
CALL_START: 7001,
CALL_END: 7002,
CALL_SYNC: 7003,
CALL_MISSED: 7004,
CALL_DECLINED: 7005,
LOCATION: 8000,
EDIT: 9000,
EDIT_REACTION: 9100,
}

module.exports = { MESSAGE_TYPE }
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"lib": "lib",
"test": "test"
},
"files": [
"lib/**"
],
"scripts": {
"test": "nyc mocha test -c",
"lint": "eslint . --ext .js",
Expand Down
73 changes: 49 additions & 24 deletions test/test-wickr-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ describe('WickrBot', function() {
sinon.spy(this.wickr, 'clientInit')
let bot = new WickrBot(this.wickr, 'foo')
bot.start()
expect(this.wickr.clientInit.calledWith('foo')).to.be.true

sinon.assert.calledOnceWithExactly(this.wickr.clientInit, 'foo')
})

it('creates a listener and calls its handler on a message', function() {
Expand All @@ -30,29 +31,7 @@ describe('WickrBot', function() {
bot.listen('foo', spyFn)
bot.handleMessage()(fakeMsg)

expect(spyFn.calledWith(JSON.parse(fakeMsg), ['bar', 'baz'])).to.be.true
})

it('sets a default listener', function() {
let fakeMsg = '{"msgtype": 1000, "message": "hey what\'s up?"}'
let spyFn = sinon.spy()
let bot = new WickrBot(this.wickr, 'foo')

bot.setDefaultHandler(spyFn)
bot.handleMessage()(fakeMsg)

expect(spyFn.calledWith(JSON.parse(fakeMsg), ['hey', 'what\'s', 'up?'])).to.be.true
})

it('sets a file listener', function() {
let fakeMsg = '{"msgtype": 6000}'
let spyFn = sinon.spy()
let bot = new WickrBot(this.wickr, 'foo')

bot.setFileHandler(spyFn)
bot.handleMessage()(fakeMsg)

expect(spyFn.calledWith(JSON.parse(fakeMsg))).to.be.true
sinon.assert.calledOnceWithExactly(spyFn, JSON.parse(fakeMsg), ['bar', 'baz'])
})

describe('#handleMessage', function() {
Expand All @@ -73,6 +52,52 @@ describe('WickrBot', function() {

bot.handleMessage()(fakeMsg)
})

it('calls the default listener for non-slash command messages', function() {
let fakeMsg = '{"msgtype": 1000, "message": "hey what\'s up?"}'
let spyFn = sinon.spy()
let bot = new WickrBot(this.wickr, 'foo')

bot.setDefaultHandler(spyFn)
bot.handleMessage()(fakeMsg)

sinon.assert.calledOnceWithExactly(spyFn, JSON.parse(fakeMsg), ['hey', 'what\'s', 'up?'])
})

it('calls the file handler for files', function() {
let fakeMsg = '{"msgtype": 6000}'
let spyFn = sinon.spy()
let bot = new WickrBot(this.wickr, 'foo')

bot.setFileHandler(spyFn)
bot.handleMessage()(fakeMsg)

sinon.assert.calledOnceWithExactly(spyFn, JSON.parse(fakeMsg), undefined)
})

it('calls the default handler for any message type', function() {
let fakeMsg = '{"msgtype": 666}'
let spyFn = sinon.spy()
let bot = new WickrBot(this.wickr, 'foo')

bot.setDefaultHandler(spyFn)
bot.handleMessage()(fakeMsg)

sinon.assert.calledOnceWithExactly(spyFn, JSON.parse(fakeMsg), undefined)
})

it('prefers the file handler over the default handler for file type messages', function() {
let fakeMsg = '{"msgtype": 6000}'
let [spyFn, spyFnDefault] = [sinon.spy(), sinon.spy()]
let bot = new WickrBot(this.wickr, 'foo')

bot.setFileHandler(spyFn)
bot.setDefaultHandler(spyFnDefault)
bot.handleMessage()(fakeMsg)

sinon.assert.calledOnceWithExactly(spyFn, JSON.parse(fakeMsg), undefined)
sinon.assert.notCalled(spyFnDefault)
})
})

describe('#sendHelp', function() {
Expand Down

0 comments on commit 4443d84

Please sign in to comment.