Verified Commit 0c49349b authored by aguestuser's avatar aguestuser
Browse files

[102] add signup message forwarding / responding

TODO: unit tests
parent 78a59213
No preview for this file type
......@@ -10,6 +10,7 @@ const defaults = {
resendDelay: 3000, // 3 seconds
signaldStartupTime: 1000 * 60 * 5, // 5 minutes
welcomeDelay: 3000, // 3 sec
signupChannel: process.env.SIGNUP_CHANNEL,
}
const test = {
......@@ -23,10 +24,16 @@ const test = {
resendDelay: 40, // 40 millis
signaldStartupTime: 1, // 1 milli
welcomeDelay: 0.0001, // .0001 millis
signupChannel: '+15555555555',
}
const development = {
...defaults,
signupChannel: '+13343264237',
}
module.exports = {
development: defaults,
development,
test,
production: defaults,
}
......@@ -19,7 +19,8 @@ HOW IT WORKS
`
const notifications = {
publisherAdded: (commandIssuer, addedPublisher) => `New admin ${addedPublisher} added by ${commandIssuer}`,
publisherAdded: (commandIssuer, addedPublisher) =>
`New admin ${addedPublisher} added by ${commandIssuer}`,
broadcastResponseSent: channel => `Your message was forwarded to the admins of [${channel.name}]`,
deauthorization: publisherPhoneNumber => `
${publisherPhoneNumber} has been removed from this channel because their safety number changed.
......@@ -39,6 +40,10 @@ Until then, they will be unable to send messages to or read messages from this c
You were just made an admin of this signalboost channel by ${addingPublisher}. Welcome!
Reply with HELP for more information or GOODBYE to leave.`,
signupRequestReceived: (senderNumber, requestMsg) =>
`Signup request received from ${senderNumber}:\n ${requestMsg}`,
signupRequestResponse:
'Thank you for signing up for Signalboost! We are creating a channel for you!\n We will send you a welcome message on your channel shortly...',
}
const commandResponses = {
......
......@@ -7,7 +7,8 @@ const { values } = require('lodash')
const { commands, statuses } = require('./executor')
const { wait } = require('../util')
const {
signal: { resendDelay },
defaultLanguage,
signal: { signupChannel, resendDelay },
} = require('../../config')
/**
......@@ -19,9 +20,10 @@ const messageTypes = {
BROADCAST_RESPONSE: 'BROADCAST_RESPONSE',
COMMAND_RESULT: 'COMMAND_RESULT',
NEW_PUBLISHER_WELCOME: 'NEW_PUBLISHER_WELCOME',
SIGNUP_MESSAGE: 'SIGNUP_MESSAGE',
}
const { BROADCAST_MESSAGE, BROADCAST_RESPONSE, COMMAND_RESULT } = messageTypes
const { BROADCAST_MESSAGE, BROADCAST_RESPONSE, COMMAND_RESULT, SIGNUP_MESSAGE } = messageTypes
const { PUBLISHER } = memberTypes
......@@ -31,7 +33,7 @@ const { PUBLISHER } = memberTypes
// (CommandResult, Dispatchable) -> Promise<void>
const dispatch = async ({ commandResult, dispatchable }) => {
const messageType = parseMessageType(commandResult, dispatchable.sender)
const messageType = parseMessageType(commandResult, dispatchable)
switch (messageType) {
case BROADCAST_MESSAGE:
return broadcast(dispatchable)
......@@ -39,15 +41,36 @@ const dispatch = async ({ commandResult, dispatchable }) => {
return handleBroadcastResponse(dispatchable)
case COMMAND_RESULT:
return handleCommandResult({ commandResult, dispatchable })
case SIGNUP_MESSAGE:
return handleSignupMessage(dispatchable)
default:
return Promise.reject(`Invalid message. Must be one of: ${values(messageTypes)}`)
}
}
const handleSignupMessage = async ({ sock, channel, sender, sdMessage }) => {
// TODO: set disappearing messages here
const notifications = messagesIn(defaultLanguage).notifications
await notify({
sock,
channel,
notification: notifications.signupRequestReceived(sender.phoneNumber, sdMessage.messageBody),
recipients: channel.publications.map(p => p.publisherPhoneNumber),
})
return notify({
sock,
channel,
notification: notifications.signupRequestResponse,
recipients: [sender.phoneNumber],
})
}
// CommandResult -> [MessageType, NotificationType]
const parseMessageType = (commandResult, sender) => {
const parseMessageType = (commandResult, { sender, channel }) => {
if (commandResult.status === statuses.NOOP) {
return sender.type === PUBLISHER ? BROADCAST_MESSAGE : BROADCAST_RESPONSE
if (sender.type === PUBLISHER) return BROADCAST_MESSAGE
if (channel.phoneNumber === signupChannel) return SIGNUP_MESSAGE
return BROADCAST_RESPONSE
}
return COMMAND_RESULT
}
......@@ -101,7 +124,7 @@ const handleNotifications = async ({ commandResult, dispatchable }) => {
// don't send to newly added publisher, that would mess up safety number re-trusting!
recipients: channel.publications
.map(p => p.publisherPhoneNumber)
.filter(pNum => pNum !== payload),
.filter(pNum => pNum !== payload),
})
}
}
......
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