-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
51 lines (41 loc) · 1.27 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package notifications
import (
"google.golang.org/grpc/codes"
"github.com/desmos-labs/caerus/utils"
)
type Handler struct {
firebase Firebase
db Database
}
// NewHandler allows to build a new Handler instance
func NewHandler(firebase Firebase, db Database) *Handler {
return &Handler{
firebase: firebase,
db: db,
}
}
// HandleSendNotificationRequest handles the request to send a new notification
func (h *Handler) HandleSendNotificationRequest(req *SendAppNotificationRequest) error {
// Get the application details
app, found, err := h.db.GetApp(req.AppID)
if err != nil {
return err
}
if !found {
return utils.WrapErr(codes.FailedPrecondition, "application not found")
}
// Make sure the app has not reached the rate limit
notificationsRateLimit, err := h.db.GetAppNotificationsRateLimit(req.AppID)
if err != nil {
return err
}
notificationsCount, err := h.db.GetAppNotificationsCount(req.AppID)
if err != nil {
return err
}
if notificationsRateLimit > 0 && notificationsCount >= notificationsRateLimit {
return utils.NewTooManyRequestsError("notifications rate limit reached")
}
// Send the notification if there are some tokens to which to send it
return h.firebase.SendNotificationToUsers(app, req.UserAddresses, req.Notification)
}