Commit 5f02fb3a authored by aguestuser's avatar aguestuser
Browse files

Merge branch '154-dont-send-empty-messages-on-disappearing-message-changes' into 'master'

[#154] don't send empty messages on disappearing message changes

Closes #154

See merge request team-friendo/signalboost!121
parents 5ae3a965 bb769b34
......@@ -7,7 +7,7 @@ const messenger = require('./messenger')
const logger = require('./logger')
const safetyNumberService = require('../registrar/safetyNumbers')
const { messagesIn } = require('./strings/messages')
const { get } = require('lodash')
const { get, isEmpty } = require('lodash')
const { defaultLanguage } = require('../../config')
/**
......@@ -66,6 +66,7 @@ const listenForInboundMessages = async (db, sock, channels) =>
*******************/
const dispatch = async (db, sock, inboundMsg) => {
const should = shouldRelay(inboundMsg)
if (shouldRelay(inboundMsg)) return relay(db, sock, inboundMsg)
if (shouldUpdateSafetyNumber(inboundMsg)) return updateSafetyNumber(db, sock, inboundMsg)
return Promise.resolve()
......@@ -122,9 +123,15 @@ const parseMessage = inboundMsg => {
}
}
const shouldRelay = inboundMsg =>
const shouldRelay = inboundMsg => _isMessage(inboundMsg) && !_isEmpty(inboundMsg)
const _isMessage = inboundMsg =>
inboundMsg.type === signal.messageTypes.MESSAGE && get(inboundMsg, 'data.dataMessage')
const _isEmpty = inboundMsg =>
get(inboundMsg, 'data.dataMessage.message') === '' &&
isEmpty(get(inboundMsg, 'data.dataMessage.attachments'))
const shouldUpdateSafetyNumber = inboundMsg =>
inboundMsg.type === signal.messageTypes.ERROR &&
get(inboundMsg, 'data.request.type') === signal.messageTypes.SEND &&
......
......@@ -117,7 +117,92 @@ describe('dispatcher service', () => {
})
describe('handling an incoming message', () => {
describe('when message is not relayable or a failed send attempt', () => {
describe('deciding whether to dispatch a message', () => {
describe('when message is not of type "message"', () => {
beforeEach(async () => {
sock.emit(
'data',
JSON.stringify({
type: 'list_groups',
data: {
username: '+12223334444',
},
}),
)
await wait(socketDelay)
})
it('ignores the message', () => {
expect(processCommandStub.callCount).to.eql(0)
expect(dispatchStub.callCount).to.eql(0)
})
})
describe('when message is of type "message"', () => {
describe('when message has a body', () => {
beforeEach(async () => {
sock.emit(
'data',
JSON.stringify({
type: 'message',
data: {
dataMessage: {
message: 'hi',
attachments: [],
},
},
}),
)
await wait(socketDelay)
})
it('dispatches the message', () => {
expect(processCommandStub.callCount).to.be.above(0)
expect(dispatchStub.callCount).to.be.above(0)
})
})
describe('when message lacks a body but contains an attachment', () => {
beforeEach(async () => {
sock.emit(
'data',
JSON.stringify({
type: 'message',
data: {
dataMessage: {
message: '',
attachments: ['cool pix!'],
},
},
}),
)
await wait(socketDelay)
})
it('dispatches the message', () => {
expect(processCommandStub.callCount).to.be.above(0)
expect(dispatchStub.callCount).to.be.above(0)
})
})
describe('when message lacks a body AND an attachment', () => {
beforeEach(async () => {
sock.emit(
'data',
JSON.stringify({ type: 'message', data: { receipt: { type: 'READ' } } }),
)
await wait(socketDelay)
})
it('ignores the message', () => {
expect(processCommandStub.callCount).to.eql(0)
expect(dispatchStub.callCount).to.eql(0)
})
})
})
})
describe('when message lacks a body AND an attachment', () => {
beforeEach(async () => {
sock.emit('data', JSON.stringify({ type: 'message', data: { receipt: { type: 'READ' } } }))
wait(socketDelay)
......@@ -129,7 +214,7 @@ describe('dispatcher service', () => {
})
})
describe('when message is dispatchable', () => {
describe('dispatching a message', () => {
beforeEach(async () => {
sock.emit('data', JSON.stringify(sdInMessage))
await wait(socketDelay)
......@@ -273,7 +358,6 @@ describe('dispatcher service', () => {
})
describe('when message is a welcome message from an admin', () => {
const welcomeMessage = `[foo]\n\n${messagesIn(defaultLanguage).notifications.welcome(
genPhoneNumber(),
channel.phoneNumber,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment