-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpush.go
119 lines (105 loc) · 3.38 KB
/
webpush.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package notella
import (
"encoding/json"
"strings"
"sync"
"git.inpt.fr/churros/notella/db"
"github.com/SherClockHolmes/webpush-go"
ll "github.com/gwennlbh/label-logger-go"
)
type WebPushNotification struct {
Title string `json:"title"`
Actions []webpushAction `json:"actions"`
Badge string `json:"badge"`
Icon string `json:"icon"`
Image string `json:"image"`
Body string `json:"body"`
Renotify bool `json:"renotify"`
RequireInteraction bool `json:"requireInteraction"`
Silent bool `json:"silent"`
Tag string `json:"tag"`
Timestamp int64 `json:"timestamp"`
Vibrate []int `json:"vibrate"`
Data webpushNotificationData `json:"data"`
}
type webpushAction struct {
Action string `json:"action"`
Label string `json:"label"`
Icon string `json:"icon"`
}
type webpushNotificationData struct {
Group string `json:"group"`
Channel db.NotificationChannel `json:"channel"`
SubscriptionName string `json:"subscriptionName"`
Goto string `json:"goto"`
}
func (msg Message) WebPush(groupId string) WebPushNotification {
actions := make([]webpushAction, len(msg.Actions))
for i, action := range msg.Actions {
actions[i] = webpushAction{
Action: action.Action,
Label: action.Label,
Icon: "",
}
}
return WebPushNotification{
Title: msg.Title,
Actions: actions,
Badge: "",
Icon: "",
Image: msg.Image,
Body: msg.Body,
Data: webpushNotificationData{
Group: groupId,
Channel: msg.Channel(),
SubscriptionName: "",
Goto: msg.Action,
},
}
}
func (msg Message) SendWebPush(groupId string, subs []Subscription) error {
jsoned, err := json.Marshal(msg.WebPush(groupId))
if err != nil {
ll.ErrorDisplay("could not marshal notification to json", err)
}
var wg sync.WaitGroup
wg.Add(len(subs))
for _, sub := range subs {
go func(wg *sync.WaitGroup, sub Subscription) {
if config.DryRunMode {
exempt := false
for _, username := range config.DryRunExceptions {
if username == sub.Owner.Uid {
exempt = true
}
}
if !exempt {
ll.Warn("dry run mode enabled, not sending webpush notification to %s", sub.Owner.Uid)
wg.Done()
return
}
}
resp, err := webpush.SendNotification(jsoned, &sub.Webpush, &webpush.Options{
TTL: 30,
Subscriber: config.ContactEmail,
VAPIDPublicKey: config.VapidPublicKey,
VAPIDPrivateKey: config.VapidPrivateKey,
})
wg.Done()
if err != nil {
ll.ErrorDisplay("could not send notification to %s", err, sub.Owner.Uid)
} else if resp.StatusCode == 410 {
ll.Log("Deleting", "yellow", "invalid webpush subscription %s", sub.Webpush.Endpoint)
sub.Destroy()
} else if resp.StatusCode >= 400 {
ll.Error("could not send notification to %s: HTTP %d", sub.Owner.Uid, resp.StatusCode)
}
}(&wg, sub)
}
wg.Wait()
return nil
}
func (sub Subscription) IsWebpush() bool {
// Native subscriptions don't use the https: protocol
return strings.HasPrefix(sub.Webpush.Endpoint, "https://")
}