Commit e5c09686 authored by aguestuser's avatar aguestuser
Browse files

Merge branch '102-cleanup-signups' into 'master'

Resolve "add signup channels"

Closes #103

See merge request team-friendo/signalboost!98
parents ffea3fca 663cddd4
No preview for this file type
......@@ -10,7 +10,7 @@ const defaults = {
resendDelay: 3000, // 3 seconds
signaldStartupTime: 1000 * 60 * 5, // 5 minutes
welcomeDelay: 3000, // 3 sec
signupChannel: process.env.SIGNUP_CHANNEL_NUMBER,
signupPhoneNumber: process.env.SIGNUP_CHANNEL_NUMBER,
}
const test = {
......@@ -24,12 +24,12 @@ const test = {
resendDelay: 40, // 40 millis
signaldStartupTime: 1, // 1 milli
welcomeDelay: 0.0001, // .0001 millis
signupChannel: '+15555555555',
signupPhoneNumber: '+15555555555',
}
const development = {
...defaults,
signupChannel: '+13343264237',
signupPhoneNumber: process.env.SIGNUP_CHANNEL_NUMBER_DEV,
}
module.exports = {
......
......@@ -5,6 +5,9 @@ const { messagesIn } = require('./messages')
const { memberTypes } = channelRepository
const { PUBLISHER, SUBSCRIBER, NONE } = memberTypes
const { lowerCase } = require('lodash')
const {
signal: { signupPhoneNumber },
} = require('../../config')
/**
* type Executable = {
......@@ -48,8 +51,9 @@ const commands = {
******************/
// Dispatchable -> Promise<{dispatchable: Dispatchable, commandResult: CommandResult}>
const processCommand = dispatchable =>
execute(parseCommand(dispatchable.sdMessage.messageBody), dispatchable)
const processCommand = dispatchable => {
return execute(parseCommand(dispatchable.sdMessage.messageBody), dispatchable)
}
// string -> Executable
const parseCommand = msg => {
......@@ -73,6 +77,8 @@ const parseCommand = msg => {
const execute = async (executable, dispatchable) => {
const { command, payload } = executable
const { db, channel, sender } = dispatchable
// don't allow command execution on the signup channel for non-admins
if (channel.phoneNumber === signupPhoneNumber && sender.type !== PUBLISHER) return noop()
const result = await ({
[commands.ADD]: () => maybeAddPublisher(db, channel, sender, payload),
[commands.HELP]: () => maybeShowHelp(db, channel, sender),
......@@ -82,8 +88,8 @@ const execute = async (executable, dispatchable) => {
[commands.RENAME]: () => maybeRenameChannel(db, channel, sender, payload),
[commands.REMOVE]: () => maybeRemovePublisher(db, channel, sender, payload),
[commands.TOGGLE_RESPONSES]: () => maybeToggleResponses(db, channel, sender, payload),
}[command] || (() => noop(sender)))()
return { ...result, command }
}[command] || (() => noop()))()
return { command, ...result }
}
/********************
......@@ -235,11 +241,7 @@ const toggleResponses = (db, channel, newSetting, sender, cr) =>
.catch(err => logAndReturn(err, { status: statuses.ERROR, message: cr.dbError(newSetting) }))
// NOOP
const noop = sender =>
Promise.resolve({
status: statuses.NOOP,
message: messagesIn(sender.language).notifications.noop,
})
const noop = () => Promise.resolve({ command: commands.NOOP, status: statuses.NOOP, message: '' })
/**********
* HELPERS
......
......@@ -8,7 +8,7 @@ const { commands, statuses } = require('./executor')
const { wait } = require('../util')
const {
defaultLanguage,
signal: { signupChannel, resendDelay },
signal: { signupPhoneNumber, resendDelay },
} = require('../../config')
/**
......@@ -52,7 +52,7 @@ const dispatch = async ({ commandResult, dispatchable }) => {
const parseMessageType = (commandResult, { sender, channel }) => {
if (commandResult.status === statuses.NOOP) {
if (sender.type === PUBLISHER) return BROADCAST_MESSAGE
if (channel.phoneNumber === signupChannel) return SIGNUP_MESSAGE
if (channel.phoneNumber === signupPhoneNumber) return SIGNUP_MESSAGE
return BROADCAST_RESPONSE
}
return COMMAND_RESULT
......@@ -61,7 +61,7 @@ const parseMessageType = (commandResult, { sender, channel }) => {
const handleSignupMessage = async ({ sock, channel, sender, sdMessage }) => {
const notifications = messagesIn(defaultLanguage).notifications
// TODO(aguestuser|2019-11-09): send this as a disappearing message
// notify admins of signpu request
// notify admins of signup request
await notify({
sock,
channel,
......@@ -93,7 +93,6 @@ const handleBroadcastResponse = dispatchable => {
return relayBroadcastResponse(dispatchable)
}
// TODO: rename this handleCommandResult
const handleCommandResult = async ({ commandResult, dispatchable }) => {
const { message, command, status } = commandResult
await respond({ ...dispatchable, message, command, status })
......
......@@ -173,6 +173,7 @@ const verify = (sock, phoneNumber, code) =>
write(sock, { type: messageTypes.VERIFY, username: phoneNumber, code })
const awaitVerificationResult = async (sock, phoneNumber) => {
//TODO(aguestuser|2019-11-09): use signald message ids to make this await call simpler
return new Promise((resolve, reject) => {
sock.on('data', function handle(msg) {
const { type, data } = safeJsonParse(msg, reject)
......@@ -278,6 +279,7 @@ const fetchIdentities = async (sock, channelPhoneNumber, memberPhoneNumber) => {
}
const awaitIdentitiesOf = (sock, memberPhoneNumber) => {
//TODO(aguestuser|2019-11-09): use signald message ids to make this await call simpler
return new Promise((resolve, reject) => {
// create handler
const handle = msg => {
......
......@@ -16,8 +16,10 @@ import validator from '../../../../app/db/validations/phoneNumber'
import { subscriptionFactory } from '../../../support/factories/subscription'
import { genPhoneNumber } from '../../../support/factories/phoneNumber'
import { publicationFactory } from '../../../support/factories/publication'
import { messagesIn } from '../../../../app/services/dispatcher/messages'
import { sdMessageOf } from '../../../../app/services/signal'
const {
signal: { signupPhoneNumber },
} = require('../../../../app/config')
describe('executor service', () => {
describe('parsing commands', () => {
......@@ -199,6 +201,11 @@ describe('executor service', () => {
subscriptions: times(2, subscriptionFactory({ channelPhoneNumber: '+13333333333' })),
messageCount: { broadcastIn: 42 },
}
const signupChannel = {
name: 'SB_SIGNUP',
phoneNumber: signupPhoneNumber,
publications: channel.publications,
}
const publisher = {
phoneNumber: '+11111111111',
type: memberTypes.PUBLISHER,
......@@ -820,6 +827,22 @@ describe('executor service', () => {
})
})
describe('new user attempting to JOIN the signup channel', () => {
it('returns NOOP', async () => {
const dispatchable = {
db,
channel: signupChannel,
sender: randomPerson,
sdMessage: sdMessageOf(signupChannel, 'HELLO'),
}
expect(await processCommand(dispatchable)).to.eql({
command: commands.NOOP,
status: statuses.NOOP,
message: '',
})
})
})
describe('invalid command', () => {
it('returns NOOP status/message', async () => {
const dispatchable = {
......@@ -831,7 +854,7 @@ describe('executor service', () => {
expect(await processCommand(dispatchable)).to.eql({
command: commands.NOOP,
status: statuses.NOOP,
message: messagesIn('EN').notifications.noop,
message: '',
})
})
})
......
......@@ -11,8 +11,14 @@ import messages from '../../../../app/services/dispatcher/messages/EN'
import { statuses, commands } from '../../../../app/services/dispatcher/executor'
import { genPhoneNumber } from '../../../support/factories/phoneNumber'
import { sdMessageOf } from '../../../../app/services/signal'
import { messagesIn } from '../../../../app/services/dispatcher/messages'
import { defaultLanguage } from '../../../../app/config'
const {
signal: { signupPhoneNumber },
} = require('../../../../app/config')
describe('messenger service', () => {
const notifications = messagesIn(defaultLanguage).notifications
const [db, sock] = [{}, { write: () => {} }]
const channelPhoneNumber = genPhoneNumber()
const subscriberNumbers = times(2, genPhoneNumber)
......@@ -31,6 +37,11 @@ describe('messenger service', () => {
messageCount: { broadcastIn: 42 },
}
const responseEnabledChannel = { ...channel, responsesEnabled: true }
const signupChannel = {
name: 'SB_SIGNUP',
phoneNumber: signupPhoneNumber,
publications: channel.publications,
}
const attachments = [{ filename: 'some/path', width: 42, height: 42 }]
const sdMessage = {
......@@ -59,17 +70,13 @@ describe('messenger service', () => {
it('parses a broadcast message', () => {
const msg = { command: 'foo', status: statuses.NOOP }
const dispatchable = { channel, sender: publisherSender }
expect(messenger.parseMessageType(msg, dispatchable)).to.eql(
messageTypes.BROADCAST_MESSAGE,
)
expect(messenger.parseMessageType(msg, dispatchable)).to.eql(messageTypes.BROADCAST_MESSAGE)
})
it('parses a broadcast response from a subscriber', () => {
const msg = { command: 'foo', status: statuses.NOOP }
const dispatchable = { channel, sender: subscriberSender }
expect(messenger.parseMessageType(msg, dispatchable)).to.eql(
messageTypes.BROADCAST_RESPONSE,
)
expect(messenger.parseMessageType(msg, dispatchable)).to.eql(messageTypes.BROADCAST_RESPONSE)
})
it('parses a broadcast response from a random person', () => {
......@@ -233,6 +240,44 @@ describe('messenger service', () => {
})
})
describe('when message is a signup request', () => {
beforeEach(async () => {
const dispatchable = {
db,
sock,
channel: signupChannel,
sender: randomSender,
sdMessage: sdMessageOf(signupChannel, 'gimme a channel'),
}
const commandResult = { status: commands.NOOP, message: '' }
await messenger.dispatch({ dispatchable, commandResult })
})
it('forwards request to channel admins and appends phone number', () => {
expect(broadcastMessageStub.getCall(0).args).to.eql([
sock,
signupChannel.publications.map(p => p.publisherPhoneNumber),
sdMessageOf(
signupChannel,
`[${signupChannel.name}]\n${notifications.signupRequestReceived(
randomSender.phoneNumber,
'gimme a channel',
)}`,
),
])
})
it('responds to requester', () => {
expect(broadcastMessageStub.getCall(1).args).to.eql([
sock,
[randomSender.phoneNumber],
sdMessageOf(
signupChannel,
`[${signupChannel.name}]\n${notifications.signupRequestResponse}`,
),
])
})
})
describe('when message is a command response', () => {
beforeEach(async () => {
await messenger.dispatch({
......@@ -267,7 +312,10 @@ describe('messenger service', () => {
const newPublisher = genPhoneNumber()
const sdMessage = `${commands.ADD} ${newPublisher}`
const response = messages.commandResponses.publisher.add.success(newPublisher)
const welcome = messages.notifications.welcome(publisherSender.phoneNumber, channel.phoneNumber)
const welcome = messages.notifications.welcome(
publisherSender.phoneNumber,
channel.phoneNumber,
)
const alert = messages.notifications.publisherAdded(
publisherSender.phoneNumber,
newPublisher,
......
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