Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
team-friendo
signalboost
Commits
f2a96239
Verified
Commit
f2a96239
authored
Sep 09, 2020
by
aguestuser
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[215] extract notifier module
parent
46ad6fe2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
15 deletions
+70
-15
app/notifier.js
app/notifier.js
+57
-0
test/unit/notifier.spec.js
test/unit/notifier.spec.js
+13
-15
No files found.
app/notifier.js
0 → 100644
View file @
f2a96239
const
channelRepository
=
require
(
'
./db/repositories/channel
'
)
const
signal
=
require
(
'
./signal
'
)
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
},
}
=
require
(
'
./config
'
)
const
notificationKeys
=
{
CHANNEL_DESTROYED
:
'
channelDestroyed
'
,
CHANNEL_RECYCLED
:
'
channelRecycled
'
,
CHANNEL_REDEEMED
:
'
channelRedeemed
'
,
}
// (Database, Socket, Channel, String, String) -> Promise<Array<string>>
const
notifyMembersExcept
=
async
(
channel
,
message
,
sender
)
=>
{
if
(
channel
==
null
)
return
const
memberPhoneNumbers
=
channelRepository
.
getMemberPhoneNumbersExcept
(
channel
,
[
sender
])
await
signal
.
broadcastMessage
(
memberPhoneNumbers
,
sdMessageOf
(
channel
,
message
))
}
// (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
))
}
// (string, string) -> Promise<Array<string>>
const
notifyAdmins
=
async
(
channel
,
notificationKey
)
=>
_notifyMany
(
channel
,
notificationKey
,
getAdminMemberships
(
channel
))
// (Channel, string) -> Promise<Array<string>>
const
notifyMembers
=
async
(
channel
,
notificationKey
)
=>
_notifyMany
(
channel
,
notificationKey
,
channel
.
memberships
)
// (Channel, string, Array<Member>) => Promise<Array<string>>
const
_notifyMany
=
(
channel
,
notificationKey
,
recipients
)
=>
Promise
.
all
(
recipients
.
map
(
recipient
=>
signal
.
sendMessage
(
recipient
.
memberPhoneNumber
,
sdMessageOf
(
channel
,
messagesIn
(
recipient
.
language
).
notifications
[
notificationKey
]),
),
),
)
module
.
exports
=
{
notifyAdmins
,
notifyMembers
,
notifyMaintainers
,
notifyMembersExcept
,
notificationKeys
,
}
test/unit/
registrar/phoneNumber/common
.spec.js
→
test/unit/
notifier
.spec.js
View file @
f2a96239
import
{
expect
}
from
'
chai
'
import
{
describe
,
it
,
beforeEach
,
afterEach
}
from
'
mocha
'
import
sinon
from
'
sinon
'
import
channelRepository
from
'
../../../../app/db/repositories/channel
'
import
signal
from
'
../../../../app/signal
'
import
{
deepChannelFactory
}
from
'
../../../support/factories/channel
'
import
{
times
}
from
'
lodash
'
import
channelRepository
from
'
../../app/db/repositories/channel
'
import
signal
from
'
../../app/signal
'
import
{
deepChannelFactory
}
from
'
../support/factories/channel
'
import
{
adminMembershipFactory
,
subscriberMembershipFactory
,
}
from
'
../../../support/factories/membership
'
import
common
,
{
notificationKeys
}
from
'
../../../../app/registrar/phoneNumber/common
'
import
{
sdMessageOf
}
from
'
../../../../app/signal/constants
'
import
{
messagesIn
}
from
'
../../../../app/dispatcher/strings/messages
'
import
{
defaultLanguage
}
from
'
../../../../app/config
'
}
from
'
../support/factories/membership
'
import
notifier
,
{
notificationKeys
}
from
'
../../app/notifier
'
import
{
sdMessageOf
}
from
'
../../app/signal/constants
'
import
{
messagesIn
}
from
'
../../app/dispatcher/strings/messages
'
describe
(
'
phone number registrar -- common
module
'
,
()
=>
{
describe
(
'
notifier
module
'
,
()
=>
{
const
channel
=
deepChannelFactory
({
memberships
:
[
adminMembershipFactory
({
language
:
'
DE
'
}),
...
...
@@ -23,10 +21,10 @@ describe('phone number registrar -- common module', () => {
subscriberMembershipFactory
({
language
:
'
DE
'
}),
],
})
let
findChannelStub
,
broadcastMessageStub
,
sendMessageStub
let
broadcastMessageStub
,
sendMessageStub
beforeEach
(()
=>
{
findChannelStub
=
sinon
.
stub
(
channelRepository
,
'
findDeep
'
).
returns
(
Promise
.
resolve
(
channel
))
sinon
.
stub
(
channelRepository
,
'
findDeep
'
).
returns
(
Promise
.
resolve
(
channel
))
broadcastMessageStub
=
sinon
.
stub
(
signal
,
'
broadcastMessage
'
)
.
callsFake
(
numbers
=>
numbers
.
map
(()
=>
'
42
'
))
...
...
@@ -36,7 +34,7 @@ describe('phone number registrar -- common module', () => {
describe
(
'
#notifyAdmins
'
,
()
=>
{
it
(
'
sends a notification to each admin in their language
'
,
async
()
=>
{
await
common
.
notifyAdmins
(
channel
,
notificationKeys
.
CHANNEL_RECYCLED
)
await
notifier
.
notifyAdmins
(
channel
,
notificationKeys
.
CHANNEL_RECYCLED
)
expect
(
sendMessageStub
.
callCount
).
to
.
eql
(
2
)
expect
(
sendMessageStub
.
getCalls
().
map
(
x
=>
x
.
args
)).
to
.
have
.
deep
.
members
([
[
...
...
@@ -53,7 +51,7 @@ describe('phone number registrar -- common module', () => {
describe
(
'
#notifyMembers
'
,
()
=>
{
it
(
'
sends a notification to each member in their language
'
,
async
()
=>
{
await
common
.
notifyMembers
(
channel
,
notificationKeys
.
CHANNEL_DESTROYED
)
await
notifier
.
notifyMembers
(
channel
,
notificationKeys
.
CHANNEL_DESTROYED
)
expect
(
sendMessageStub
.
callCount
).
to
.
eql
(
4
)
expect
(
sendMessageStub
.
getCalls
().
map
(
x
=>
x
.
args
)).
to
.
have
.
deep
.
members
([
[
...
...
@@ -78,7 +76,7 @@ describe('phone number registrar -- common module', () => {
describe
(
'
#notifyMaintainers
'
,
()
=>
{
it
(
'
sends an untranslated notification to sysadmins of the instance
'
,
async
()
=>
{
await
common
.
notifyMaintainers
(
'
foo
'
)
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
],
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment