Verified Commit 7a0e5b40 authored by aguestuser's avatar aguestuser

[hotfix] notify maintainers on the diagnostics channel

* we used to notify ourselves on BOOST IT, now we tend to get
  notifications on `DIAGNOSTICS`. let's be consistent about that!
* while at it: send these notifications in sequence. if a big channel
  is destroyed or recycled, we don't want signald to shit the bed
* aaaand... let's make `_notifyMany` handle either a concrete message
  or a notification key so we can use it as a helper for all of our
  notify functions
parent 3bb1c598
Pipeline #45870 passed with stage
in 1 minute and 6 seconds
const channelRepository = require('./db/repositories/channel')
const signal = require('./signal')
const { sequence } = require('./util')
const { getAdminMemberships } = require('./db/repositories/channel')
const { getAdminPhoneNumbers } = require('./db/repositories/channel')
const { messagesIn } = require('./dispatcher/strings/messages')
const { sdMessageOf } = require('./signal/constants')
const {
signal: { supportPhoneNumber },
signal: { diagnosticsPhoneNumber },
} = require('./config')
const notificationKeys = {
......@@ -17,32 +17,36 @@ const notificationKeys = {
// (Channel, String, String) -> Promise<Array<string>>
const notifyMembersExcept = async (channel, sender, notificationKey) => {
const recipients = channelRepository.getMembersExcept(channel, [sender])
return _notifyMany(channel, notificationKey, recipients)
return _notifyMany({ channel, notificationKey, recipients })
}
// (string) -> Promise<Array<string>>
const notifyMaintainers = async message => {
if (!supportPhoneNumber) return Promise.resolve([])
const supportChannel = await channelRepository.findDeep(supportPhoneNumber)
const maintainerPhoneNumbers = getAdminPhoneNumbers(supportChannel)
await signal.broadcastMessage(maintainerPhoneNumbers, sdMessageOf(supportChannel, message))
if (!diagnosticsPhoneNumber) return Promise.resolve([])
const channel = await channelRepository.findDeep(diagnosticsPhoneNumber)
const recipients = getAdminMemberships(channel)
return _notifyMany({ channel, recipients, message })
}
// (string, string) -> Promise<Array<string>>
const notifyAdmins = async (channel, notificationKey) =>
_notifyMany(channel, notificationKey, getAdminMemberships(channel))
_notifyMany({ channel, notificationKey, recipients: getAdminMemberships(channel) })
// (Channel, string) -> Promise<Array<string>>
const notifyMembers = async (channel, notificationKey) =>
_notifyMany(channel, notificationKey, channel.memberships)
_notifyMany({ channel, notificationKey, recipients: channel.memberships })
// (Channel, string, Array<Member>) => Promise<Array<string>>
const _notifyMany = (channel, notificationKey, recipients) =>
Promise.all(
recipients.map(recipient =>
const _notifyMany = ({ channel, recipients, notificationKey, message }) =>
// TODO(aguestuser|2020-09-11):
// we sequence these to get around signald concurrency bugs, eventually use Promise.all here
sequence(
recipients.map(recipient => () =>
signal.sendMessage(
recipient.memberPhoneNumber,
sdMessageOf(channel, messagesIn(recipient.language).notifications[notificationKey]),
message
? sdMessageOf(channel, message)
: sdMessageOf(channel, messagesIn(recipient.language).notifications[notificationKey]),
),
),
)
......
......@@ -22,13 +22,10 @@ describe('notifier module', () => {
subscriberMembershipFactory({ language: 'DE' }),
],
})
let broadcastMessageStub, sendMessageStub
let sendMessageStub
beforeEach(() => {
sinon.stub(channelRepository, 'findDeep').returns(Promise.resolve(channel))
broadcastMessageStub = sinon
.stub(signal, 'broadcastMessage')
.callsFake(numbers => numbers.map(() => '42'))
sendMessageStub = sinon.stub(signal, 'sendMessage').returns(Promise.resolve('42'))
})
afterEach(() => sinon.restore())
......@@ -102,10 +99,9 @@ describe('notifier module', () => {
describe('#notifyMaintainers', () => {
it('sends an untranslated notification to sysadmins of the instance', async () => {
await notifier.notifyMaintainers('foo')
expect(broadcastMessageStub.callCount).to.eql(1)
expect(broadcastMessageStub.getCall(0).args).to.eql([
[channel.memberships[0].memberPhoneNumber, channel.memberships[1].memberPhoneNumber],
sdMessageOf(channel, 'foo'),
expect(map(sendMessageStub.getCalls(), 'args')).to.have.deep.members([
[channel.memberships[0].memberPhoneNumber, sdMessageOf(channel, 'foo')],
[channel.memberships[1].memberPhoneNumber, sdMessageOf(channel, 'foo')],
])
})
})
......
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