MCPcopy
hub / github.com/tinode/chat / pushForData

Method pushForData

server/push.go:27–85  ·  view source on GitHub ↗

Prepares a payload to be delivered to a mobile device as a push notification in response to a {data} message.

(fromUid types.Uid, data *MsgServerData, msgMarkedAsReadBySender bool)

Source from the content-addressed store, hash-verified

25
26// Prepares a payload to be delivered to a mobile device as a push notification in response to a {data} message.
27func (t *Topic) pushForData(fromUid types.Uid, data *MsgServerData, msgMarkedAsReadBySender bool) *push.Receipt {
28 // Passing `Topic` as `t.name` for group topics and P2P topics. The p2p topic name is later rewritten for
29 // each recipient then the payload is created: p2p recipient sees the topic as the ID of the other user.
30
31 // Initialize the push receipt.
32 contentType, _ := data.Head["mime"].(string)
33 receipt := push.Receipt{
34 To: make(map[types.Uid]push.Recipient, t.subsCount()),
35 Payload: push.Payload{
36 What: push.ActMsg,
37 Silent: false,
38 Topic: t.name,
39 From: data.From,
40 Timestamp: data.Timestamp,
41 SeqId: data.SeqId,
42 ContentType: contentType,
43 Content: data.Content,
44 },
45 }
46 if webrtc, found := data.Head["webrtc"].(string); found {
47 receipt.Payload.Webrtc = webrtc
48 audioOnly, _ := data.Head["aonly"].(bool)
49 receipt.Payload.AudioOnly = audioOnly
50 }
51 if replace, found := data.Head["replace"].(string); found {
52 receipt.Payload.Replace = replace
53 }
54
55 if t.isChan {
56 // Channel readers should get a push on a channel name (as an FCM topic push).
57 receipt.Channel = types.GrpToChn(t.name)
58 }
59
60 for uid, pud := range t.perUser {
61 online := pud.online
62 if uid == fromUid && online == 0 {
63 // Make sure the sender's devices receive a silent push.
64 online = 1
65 }
66
67 // Send only to those who have notifications enabled.
68 mode := pud.modeWant & pud.modeGiven
69 if mode.IsPresencer() && mode.IsReader() && !pud.deleted && !pud.isChan {
70 receipt.To[uid] = push.Recipient{
71 // Number of attached sessions the data message will be delivered to.
72 // Push notifications sent to users with non-zero online sessions will be marked silent.
73 Delivered: online,
74 // Unread counts are incremented for all recipients,
75 // and for sender only if the message wasnt't marked 'read' by the sender
76 ShouldIncrementUnreadCountInCache: uid != fromUid || !msgMarkedAsReadBySender,
77 }
78 }
79 }
80 if len(receipt.To) > 0 || receipt.Channel != "" {
81 return &receipt
82 }
83 // If there are no recipient there is no need to send the push notification.
84 return nil

Callers 1

Calls 4

subsCountMethod · 0.95
GrpToChnFunction · 0.92
IsPresencerMethod · 0.80
IsReaderMethod · 0.80

Tested by

no test coverage detected