Commit 815d4d18 authored by aguestuser's avatar aguestuser
Browse files

Merge branch 'austin/add-signup-channel' into 'master'

[102] add signup channel

Closes #102

See merge request team-friendo/signalboost!94
parents 78a59213 46e04eda
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!\n You will receive a welcome message on your new 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,44 @@ 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 }) => {
const notifications = messagesIn(defaultLanguage).notifications
const oneDay = 60 * 60 * 24
// set expiry
await Promise.all(
channel.publications.map(p =>
signal.setExpiration(sock, channel.phoneNumber, p.publisherPhoneNumber, oneDay),
),
)
// notify admins of signpu request
await notify({
sock,
channel,
notification: notifications.signupRequestReceived(sender.phoneNumber, sdMessage.messageBody),
recipients: channel.publications.map(p => p.publisherPhoneNumber),
})
// respond to signpu requester
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 +132,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),
})
}
}
......
......@@ -86,6 +86,7 @@ const messageTypes = {
VERIFICATION_SUCCESS: 'verification_succeeded',
VERIFICATION_ERROR: 'verification_error',
VERIFICATION_REQUIRED: 'verification_required',
SET_EXPIRATION: 'set_expiration',
}
const trustLevels = {
......@@ -218,6 +219,14 @@ const broadcastMessage = (sock, recipientNumbers, outboundMessage) =>
recipientNumbers.map(recipientNumber => sendMessage(sock, recipientNumber, outboundMessage)),
)
const setExpiration = (sock, channelPhoneNumber, memberPhoneNumber, expiresInSeconds) =>
write(sock, {
type: messageTypes.SET_EXPIRATION,
username: channelPhoneNumber,
recipientNumber: memberPhoneNumber,
expiresInSeconds,
})
// (Socket, String, String) -> Promise<Array<TrustResult>>
const trust = async (sock, channelPhoneNumber, memberPhoneNumber) => {
const id = await fetchMostRecentId(sock, channelPhoneNumber, memberPhoneNumber)
......@@ -356,4 +365,5 @@ module.exports = {
subscribe,
trust,
verify,
setExpiration,
}
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