Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
notifications-bot
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
archives
notifications-bot
Commits
32e9c00c
Commit
32e9c00c
authored
Jan 7, 2022
by
Jonah Husson
Browse files
Options
Downloads
Patches
Plain Diff
notify should accept gtnresult
parent
798d786a
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!32
Release
,
!25
Restructure notification code for batching
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
notifications/notifications.go
+16
-13
16 additions, 13 deletions
notifications/notifications.go
with
16 additions
and
13 deletions
notifications/notifications.go
+
16
−
13
View file @
32e9c00c
...
...
@@ -183,6 +183,8 @@ func NewImplementation(instance *Impl) *notificationBot.Implementation {
return
impl
}
// SendBatch accepts the map of ephemeralID:list[notifications.Data]
// It handles logic for building the CSV & sending to devices
func
(
nb
*
Impl
)
SendBatch
(
data
map
[
int64
][]
*
notifications
.
Data
)
([]
*
notifications
.
Data
,
error
)
{
csvs
:=
map
[
int64
]
string
{}
var
ephemerals
[]
int64
...
...
@@ -208,22 +210,23 @@ func (nb *Impl) SendBatch(data map[int64][]*notifications.Data) ([]*notification
return
nil
,
errors
.
WithMessage
(
err
,
"Failed to get list of tokens to notify"
)
}
for
_
,
n
:=
range
toNotify
{
nb
.
notify
(
n
.
Token
,
csvs
[
n
.
EphemeralId
],
n
.
EphemeralId
,
n
.
TransmissionRSAHash
)
nb
.
notify
(
csvs
[
n
.
EphemeralId
],
n
)
}
return
unsent
,
nil
}
func
(
nb
*
Impl
)
notify
(
token
,
csv
string
,
ephID
int64
,
transmissionRSAHash
[]
byte
)
{
isAPNS
:=
!
strings
.
Contains
(
token
,
":"
)
// notify is a helper function which handles sending notifications to either APNS or firebase
func
(
nb
*
Impl
)
notify
(
csv
string
,
toNotify
storage
.
GTNResult
)
{
isAPNS
:=
!
strings
.
Contains
(
toNotify
.
Token
,
":"
)
// mutableContent := 1
if
isAPNS
{
jww
.
INFO
.
Printf
(
"Notifying ephemeral ID %+v via APNS to token %+v"
,
ephID
,
t
oken
)
jww
.
INFO
.
Printf
(
"Notifying ephemeral ID %+v via APNS to token %+v"
,
toNotify
.
EphemeralId
,
toNotify
.
T
oken
)
notifPayload
:=
payload
.
NewPayload
()
.
AlertTitle
(
"Privacy: protected!"
)
.
AlertBody
(
"Some notifications are not for you to ensure privacy; we hope to remove this notification soon"
)
.
MutableContent
()
.
Custom
(
notificationsTag
,
csv
)
notif
:=
&
apns2
.
Notification
{
CollapseID
:
base64
.
StdEncoding
.
EncodeToString
(
transmissionRSAHash
),
DeviceToken
:
token
,
CollapseID
:
base64
.
StdEncoding
.
EncodeToString
(
t
oNotify
.
T
ransmissionRSAHash
),
DeviceToken
:
to
Notify
.
To
ken
,
Expiration
:
time
.
Now
()
.
Add
(
time
.
Hour
*
24
*
7
),
Priority
:
apns2
.
PriorityHigh
,
Payload
:
notifPayload
,
...
...
@@ -239,10 +242,10 @@ func (nb *Impl) notify(token, csv string, ephID int64, transmissionRSAHash []byt
// return errors.WithMessagef(err, "Failed to remove user registration tRSA hash: %+v", u.TransmissionRSAHash)
//}
}
else
{
jww
.
INFO
.
Printf
(
"Notified ephemeral ID %+v [%+v] and received response %+v"
,
ephID
,
t
oken
,
resp
)
jww
.
INFO
.
Printf
(
"Notified ephemeral ID %+v [%+v] and received response %+v"
,
toNotify
.
EphemeralId
,
toNotify
.
T
oken
,
resp
)
}
}
else
{
resp
,
err
:=
nb
.
fcm
.
SendNotification
(
nb
.
fcm
.
Client
,
token
,
csv
)
resp
,
err
:=
nb
.
fcm
.
SendNotification
(
nb
.
fcm
.
Client
,
to
Notify
.
To
ken
,
csv
)
if
err
!=
nil
{
// Catch firebase errors that we don't want to crash on
// 404 indicate that the token stored is incorrect
...
...
@@ -254,16 +257,16 @@ func (nb *Impl) notify(token, csv string, ephID int64, transmissionRSAHash []byt
invalidToken
:=
strings
.
Contains
(
err
.
Error
(),
"400"
)
&&
strings
.
Contains
(
err
.
Error
(),
"Invalid registration"
)
if
strings
.
Contains
(
err
.
Error
(),
"404"
)
||
invalidToken
{
jww
.
ERROR
.
Printf
(
"User with Transmission RSA hash %+v has invalid token, unregistering..."
,
transmissionRSAHash
)
err
:=
nb
.
Storage
.
DeleteUserByHash
(
transmissionRSAHash
)
jww
.
ERROR
.
Printf
(
"User with Transmission RSA hash %+v has invalid token, unregistering..."
,
t
oNotify
.
T
ransmissionRSAHash
)
err
:=
nb
.
Storage
.
DeleteUserByHash
(
t
oNotify
.
T
ransmissionRSAHash
)
if
err
!=
nil
{
jww
.
ERROR
.
Printf
(
"Failed to remove user registration tRSA hash %+v: %+v"
,
transmissionRSAHash
,
err
)
jww
.
ERROR
.
Printf
(
"Failed to remove user registration tRSA hash %+v: %+v"
,
t
oNotify
.
T
ransmissionRSAHash
,
err
)
}
}
else
{
jww
.
ERROR
.
Printf
(
"Failed to send notification to user with tRSA hash %+v: %+v"
,
transmissionRSAHash
,
err
)
jww
.
ERROR
.
Printf
(
"Failed to send notification to user with tRSA hash %+v: %+v"
,
t
oNotify
.
T
ransmissionRSAHash
,
err
)
}
}
else
{
jww
.
INFO
.
Printf
(
"Notified ephemeral ID %+v [%+v] and received response %+v"
,
ephID
,
t
oken
,
resp
)
jww
.
INFO
.
Printf
(
"Notified ephemeral ID %+v [%+v] and received response %+v"
,
toNotify
.
EphemeralId
,
toNotify
.
T
oken
,
resp
)
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment