-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload.go
382 lines (341 loc) · 11.4 KB
/
payload.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package fcm
import (
"encoding/json"
"errors"
"strconv"
"time"
fcmv1 "google.golang.org/api/fcm/v1"
"github.com/tinode/chat/server/drafty"
"github.com/tinode/chat/server/logs"
"github.com/tinode/chat/server/push"
"github.com/tinode/chat/server/push/common"
"github.com/tinode/chat/server/store"
t "github.com/tinode/chat/server/store/types"
)
const (
// TTL of a VOIP push notification in seconds.
voipTimeToLive = 10
// TTL of a regular push notification in seconds.
defaultTimeToLive = 3600
)
func payloadToData(pl *push.Payload) (map[string]string, error) {
if pl == nil {
return nil, errors.New("empty push payload")
}
data := make(map[string]string)
var err error
data["what"] = pl.What
if pl.Silent {
data["silent"] = "true"
}
data["topic"] = pl.Topic
data["ts"] = pl.Timestamp.Format(time.RFC3339Nano)
// Must use "xfrom" because "from" is a reserved word. Google did not bother to document it anywhere.
data["xfrom"] = pl.From
if pl.What == push.ActMsg {
data["seq"] = strconv.Itoa(pl.SeqId)
if pl.ContentType != "" {
data["mime"] = pl.ContentType
}
// Convert Drafty content to plain text (clients 0.16 and below).
data["content"], err = drafty.PlainText(pl.Content)
if err != nil {
return nil, err
}
// Trim long strings to 128 runes.
// Check byte length first and don't waste time converting short strings.
if len(data["content"]) > push.MaxPayloadLength {
runes := []rune(data["content"])
if len(runes) > push.MaxPayloadLength {
data["content"] = string(runes[:push.MaxPayloadLength]) + "…"
}
}
// Rich content for clients version 0.17 and above.
data["rc"], err = drafty.Preview(pl.Content, push.MaxPayloadLength)
if pl.Webrtc != "" {
data["webrtc"] = pl.Webrtc
if pl.AudioOnly {
data["aonly"] = "true"
}
// Video call push notifications are silent.
data["silent"] = "true"
}
if pl.Replace != "" {
// Notification of a message edit should be silent too.
data["silent"] = "true"
data["replace"] = pl.Replace
}
if err != nil {
return nil, err
}
} else if pl.What == push.ActSub {
data["modeWant"] = pl.ModeWant.String()
data["modeGiven"] = pl.ModeGiven.String()
} else if pl.What == push.ActRead {
data["seq"] = strconv.Itoa(pl.SeqId)
data["silent"] = "true"
} else {
return nil, errors.New("unknown push type")
}
return data, nil
}
func clonePayload(src map[string]string) map[string]string {
dst := make(map[string]string, len(src))
for key, val := range src {
dst[key] = val
}
return dst
}
// PrepareV1Notifications creates notification payloads ready to be posted
// to push notification server for the provided receipt.
func PrepareV1Notifications(rcpt *push.Receipt, config *configType) ([]*fcmv1.Message, []t.Uid) {
data, err := payloadToData(&rcpt.Payload)
if err != nil {
logs.Warn.Println("fcm push: could not parse payload:", err)
return nil, nil
}
// Device IDs to send pushes to.
var devices map[t.Uid][]t.DeviceDef
// Count of device IDs to push to.
var count int
// Devices which were online in the topic when the message was sent.
skipDevices := make(map[string]struct{})
if len(rcpt.To) > 0 {
// List of UIDs for querying the database
uids := make([]t.Uid, len(rcpt.To))
i := 0
for uid, to := range rcpt.To {
uids[i] = uid
i++
// Some devices were online and received the message. Skip them.
for _, deviceID := range to.Devices {
skipDevices[deviceID] = struct{}{}
}
}
devices, count, err = store.Devices.GetAll(uids...)
if err != nil {
logs.Warn.Println("fcm push: db error", err)
return nil, nil
}
}
if count == 0 && rcpt.Channel == "" {
return nil, nil
}
if config == nil {
// config is nil when called from tnpg adapter; provide a blank one for simplicity.
config = &configType{}
}
var messages []*fcmv1.Message
var uids []t.Uid
for uid, devList := range devices {
topic := rcpt.Payload.Topic
userData := data
tcat := t.GetTopicCat(topic)
if rcpt.To[uid].Delivered > 0 || tcat == t.TopicCatP2P {
userData = clonePayload(data)
// Fix topic name for P2P pushes.
if tcat == t.TopicCatP2P {
topic, _ = t.P2PNameForUser(uid, topic)
userData["topic"] = topic
}
// Silence the push for user who have received the data interactively.
if rcpt.To[uid].Delivered > 0 {
userData["silent"] = "true"
}
}
for i := range devList {
d := &devList[i]
if _, ok := skipDevices[d.DeviceId]; !ok && d.DeviceId != "" {
msg := fcmv1.Message{
Token: d.DeviceId,
Data: userData,
}
switch d.Platform {
case "android":
msg.Android = androidNotificationConfig(rcpt.Payload.What, topic, userData, config)
case "ios":
msg.Apns = apnsNotificationConfig(rcpt.Payload.What, topic, userData, rcpt.To[uid].Unread, config)
case "web":
if config != nil && config.Webpush != nil && config.Webpush.Enabled {
msg.Webpush = &fcmv1.WebpushConfig{}
}
case "":
// ignore
default:
logs.Warn.Println("fcm: unknown device platform", d.Platform)
}
uids = append(uids, uid)
messages = append(messages, &msg)
}
}
}
if rcpt.Channel != "" {
topic := rcpt.Channel
userData := clonePayload(data)
userData["topic"] = topic
// Channel receiver should not know the ID of the message sender.
delete(userData, "xfrom")
msg := fcmv1.Message{
Topic: topic,
Data: userData,
}
// We don't know the platform of the receiver, must provide payload for all platforms.
msg.Android = androidNotificationConfig(rcpt.Payload.What, topic, userData, config)
msg.Apns = apnsNotificationConfig(rcpt.Payload.What, topic, userData, 0, config)
// TODO: add webpush payload.
messages = append(messages, &msg)
// UID is not used in handling Topic pushes, but should keep the same count as messages.
uids = append(uids, t.ZeroUid)
}
return messages, uids
}
// DevicesForUser loads device IDs of the given user.
func DevicesForUser(uid t.Uid) []string {
ddef, count, err := store.Devices.GetAll(uid)
if err != nil {
logs.Warn.Println("fcm devices for user: db error", err)
return nil
}
if count == 0 {
return nil
}
devices := make([]string, count)
for i, dd := range ddef[uid] {
devices[i] = dd.DeviceId
}
return devices
}
// ChannelsForUser loads user's channel subscriptions with P permission.
func ChannelsForUser(uid t.Uid) []string {
channels, err := store.Users.GetChannels(uid)
if err != nil {
logs.Warn.Println("fcm channels for user: db error", err)
return nil
}
return channels
}
func androidNotificationConfig(what, topic string, data map[string]string, config *configType) *fcmv1.AndroidConfig {
timeToLive := strconv.Itoa(defaultTimeToLive) + "s"
if config != nil && config.TimeToLive > 0 {
timeToLive = strconv.Itoa(config.TimeToLive) + "s"
}
if what == push.ActRead {
return &fcmv1.AndroidConfig{
Priority: string(common.AndroidPriorityNormal),
Notification: nil,
Ttl: timeToLive,
}
}
_, videoCall := data["webrtc"]
if videoCall {
timeToLive = "0s"
}
// Sending priority.
priority := string(common.AndroidPriorityHigh)
ac := &fcmv1.AndroidConfig{
Priority: priority,
Ttl: timeToLive,
}
// When this notification type is included and the app is not in the foreground
// Android won't wake up the app and won't call FirebaseMessagingService:onMessageReceived.
// See dicussion: https://github.com/firebase/quickstart-js/issues/71
if config.Android == nil || !config.Android.Enabled {
return ac
}
body := config.Android.GetStringField(what, "Body")
if body == "$content" {
body = data["content"]
}
// Client-side display priority.
priority = string(common.AndroidNotificationPriorityHigh)
if videoCall {
priority = string(common.AndroidNotificationPriorityMax)
}
ac.Notification = &fcmv1.AndroidNotification{
// Android uses Tag value to group notifications together:
// show just one notification per topic.
Tag: topic,
NotificationPriority: priority,
Visibility: string(common.AndroidVisibilityPrivate),
TitleLocKey: config.Android.GetStringField(what, "TitleLocKey"),
Title: config.Android.GetStringField(what, "Title"),
BodyLocKey: config.Android.GetStringField(what, "BodyLocKey"),
Body: body,
Icon: config.Android.GetStringField(what, "Icon"),
Color: config.Android.GetStringField(what, "Color"),
ClickAction: config.Android.GetStringField(what, "ClickAction"),
}
return ac
}
func apnsShouldPresentAlert(what, callStatus, isSilent string, config *configType) bool {
return config.Apns != nil && config.Apns.Enabled && what != push.ActRead && callStatus == "" && isSilent == ""
}
func apnsNotificationConfig(what, topic string, data map[string]string, unread int, config *configType) *fcmv1.ApnsConfig {
callStatus := data["webrtc"]
expires := time.Now().UTC().Add(time.Duration(defaultTimeToLive) * time.Second)
if config.TimeToLive > 0 {
expires = time.Now().UTC().Add(time.Duration(config.TimeToLive) * time.Second)
}
bundleId := config.ApnsBundleID
pushType := common.ApnsPushTypeAlert
priority := 10
interruptionLevel := common.InterruptionLevelTimeSensitive
if callStatus == "started" {
// Send VOIP push only when a new call is started, otherwise send normal alert.
interruptionLevel = common.InterruptionLevelCritical
// FIXME: PushKit notifications do not work with the current FCM adapter.
// Using normal pushes as a poor-man's replacement for VOIP pushes.
// Uncomment the following two lines when FCM fixes its problem or when we switch to
// a different adapter.
// pushType = common.ApnsPushTypeVoip
// bundleId += ".voip"
expires = time.Now().UTC().Add(time.Duration(voipTimeToLive) * time.Second)
} else if what == push.ActRead {
priority = 5
interruptionLevel = common.InterruptionLevelPassive
pushType = common.ApnsPushTypeBackground
}
apsPayload := common.Aps{
Badge: unread,
ContentAvailable: 1,
MutableContent: 1,
InterruptionLevel: interruptionLevel,
Sound: "default",
ThreadID: topic,
}
// Do not present alert for read notifications and video calls.
if apnsShouldPresentAlert(what, callStatus, data["silent"], config) {
body := config.Apns.GetStringField(what, "Body")
if body == "$content" {
body = data["content"]
}
apsPayload.Alert = &common.ApsAlert{
Action: config.Apns.GetStringField(what, "Action"),
ActionLocKey: config.Apns.GetStringField(what, "ActionLocKey"),
Body: body,
LaunchImage: config.Apns.GetStringField(what, "LaunchImage"),
LocKey: config.Apns.GetStringField(what, "LocKey"),
Title: config.Apns.GetStringField(what, "Title"),
Subtitle: config.Apns.GetStringField(what, "Subtitle"),
TitleLocKey: config.Apns.GetStringField(what, "TitleLocKey"),
SummaryArg: config.Apns.GetStringField(what, "SummaryArg"),
SummaryArgCount: config.Apns.GetIntField(what, "SummaryArgCount"),
}
}
payload, err := json.Marshal(map[string]interface{}{"aps": apsPayload})
if err != nil {
return nil
}
headers := map[string]string{
common.HeaderApnsExpiration: strconv.FormatInt(expires.Unix(), 10),
common.HeaderApnsPriority: strconv.Itoa(priority),
common.HeaderApnsTopic: bundleId,
common.HeaderApnsCollapseID: topic,
common.HeaderApnsPushType: string(pushType),
}
ac := &fcmv1.ApnsConfig{
Headers: headers,
Payload: payload,
}
return ac
}