forked from tulir/whatsmeow
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathreceipt.go
More file actions
294 lines (277 loc) · 9.17 KB
/
receipt.go
File metadata and controls
294 lines (277 loc) · 9.17 KB
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
// Copyright (c) 2021 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package whatsmeow
import (
"context"
"fmt"
"time"
"github.com/rs/zerolog"
"go.mau.fi/util/ptr"
waBinary "go.mau.fi/whatsmeow/binary"
"go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
)
func (cli *Client) handleReceipt(ctx context.Context, node *waBinary.Node) {
var cancelled bool
defer cli.maybeDeferredAck(ctx, node)(&cancelled)
receipt, err := cli.parseReceipt(node)
if err != nil {
cli.Log.Warnf("Failed to parse receipt: %v", err)
} else if receipt != nil {
if receipt.Type == types.ReceiptTypeRetry {
go func() {
err := cli.handleRetryReceipt(ctx, receipt, node)
if err != nil {
cli.Log.Errorf("Failed to handle retry receipt for %s/%s from %s: %v", receipt.Chat, receipt.MessageIDs[0], receipt.Sender, err)
}
}()
}
cancelled = cli.dispatchEvent(receipt)
}
}
func (cli *Client) handleGroupedReceipt(partialReceipt events.Receipt, participants *waBinary.Node) {
pag := participants.AttrGetter()
partialReceipt.MessageIDs = []types.MessageID{pag.String("key")}
for _, child := range participants.GetChildren() {
if child.Tag != "user" {
cli.Log.Warnf("Unexpected node in grouped receipt participants: %s", child.XMLString())
continue
}
ag := child.AttrGetter()
receipt := partialReceipt
receipt.Timestamp = ag.UnixTime("t")
receipt.MessageSource.Sender = ag.JID("jid")
if !ag.OK() {
cli.Log.Warnf("Failed to parse user node %s in grouped receipt: %v", child.XMLString(), ag.Error())
continue
}
cli.dispatchEvent(&receipt)
}
}
func (cli *Client) parseReceipt(node *waBinary.Node) (*events.Receipt, error) {
ag := node.AttrGetter()
source, err := cli.parseMessageSource(node, false)
if err != nil {
return nil, err
}
receipt := events.Receipt{
MessageSource: source,
Timestamp: ag.UnixTime("t"),
Type: types.ReceiptType(ag.OptionalString("type")),
MessageSender: ag.OptionalJIDOrEmpty("recipient"),
}
if source.IsGroup && source.Sender.IsEmpty() {
participantTags := node.GetChildrenByTag("participants")
if len(participantTags) == 0 {
return nil, &ElementMissingError{Tag: "participants", In: "grouped receipt"}
}
for _, pcp := range participantTags {
cli.handleGroupedReceipt(receipt, &pcp)
}
return nil, nil
}
mainMessageID := ag.String("id")
if !ag.OK() {
return nil, fmt.Errorf("failed to parse read receipt attrs: %+v", ag.Errors)
}
receiptChildren := node.GetChildren()
if len(receiptChildren) == 1 && receiptChildren[0].Tag == "list" {
listChildren := receiptChildren[0].GetChildren()
receipt.MessageIDs = make([]string, 1, len(listChildren)+1)
receipt.MessageIDs[0] = mainMessageID
for _, item := range listChildren {
if id, ok := item.Attrs["id"].(string); ok && item.Tag == "item" {
receipt.MessageIDs = append(receipt.MessageIDs, id)
}
}
} else {
receipt.MessageIDs = []types.MessageID{mainMessageID}
}
return &receipt, nil
}
func (cli *Client) backgroundIfAsyncAck(fn func()) {
if cli.SynchronousAck {
fn()
} else {
go fn()
}
}
func (cli *Client) maybeDeferredAck(ctx context.Context, node *waBinary.Node) func(cancelled ...*bool) {
if cli.SynchronousAck {
return func(cancelled ...*bool) {
isCancelled := len(cancelled) > 0 && ptr.Val(cancelled[0])
if ctx.Err() != nil || isCancelled {
zerolog.Ctx(ctx).Debug().
AnErr("ctx_err", ctx.Err()).
Bool("cancelled", isCancelled).
Str("node_tag", node.Tag).
Msg("Not sending ack for node")
return
}
cli.sendAck(ctx, node, 0)
}
} else {
go cli.sendAck(ctx, node, 0)
return func(...*bool) {}
}
}
const (
NackParsingError = 487
NackUnrecognizedStanza = 488
NackUnrecognizedStanzaClass = 489
NackUnrecognizedStanzaType = 490
NackInvalidProtobuf = 491
NackInvalidHostedCompanionStanza = 493
NackMissingMessageSecret = 495
NackSignalErrorOldCounter = 496
NackMessageDeletedOnPeer = 499
NackUnhandledError = 500
NackUnsupportedAdminRevoke = 550
NackUnsupportedLIDGroup = 551
NackDBOperationFailed = 552
)
func (cli *Client) sendAck(ctx context.Context, node *waBinary.Node, error int) {
attrs := waBinary.Attrs{
"class": node.Tag,
"id": node.Attrs["id"],
}
attrs["to"] = node.Attrs["from"]
if participant, ok := node.Attrs["participant"]; ok {
attrs["participant"] = participant
}
if recipient, ok := node.Attrs["recipient"]; ok {
attrs["recipient"] = recipient
// TODO this hack probably needs to be removed at some point
recipientJID, ok := recipient.(types.JID)
if ok && recipientJID.Server == types.BotServer && node.Tag == "message" {
altRecipient, ok := types.BotJIDMap[recipientJID]
if ok {
attrs["recipient"] = altRecipient
}
}
}
if receiptType, ok := node.Attrs["type"]; node.Tag != "message" && ok {
attrs["type"] = receiptType
}
if error != 0 {
attrs["error"] = error
}
err := cli.sendNode(ctx, waBinary.Node{
Tag: "ack",
Attrs: attrs,
})
if err != nil {
cli.Log.Warnf("Failed to send acknowledgement for %s %s: %v", node.Tag, node.Attrs["id"], err)
}
}
// MarkRead sends a read receipt for the given message IDs including the given timestamp as the read at time.
//
// The first JID parameter (chat) must always be set to the chat ID (user ID in DMs and group ID in group chats).
// The second JID parameter (sender) must be set in group chats and must be the user ID who sent the message.
//
// You can mark multiple messages as read at the same time, but only if the messages were sent by the same user.
// To mark messages by different users as read, you must call MarkRead multiple times (once for each user).
//
// To mark a voice message as played, specify types.ReceiptTypePlayed as the last parameter.
// Providing more than one receipt type will panic: the parameter is only a vararg for backwards compatibility.
func (cli *Client) MarkRead(ctx context.Context, ids []types.MessageID, timestamp time.Time, chat, sender types.JID, receiptTypeExtra ...types.ReceiptType) error {
if len(ids) == 0 {
return fmt.Errorf("no message IDs specified")
}
receiptType := types.ReceiptTypeRead
if len(receiptTypeExtra) == 1 {
receiptType = receiptTypeExtra[0]
} else if len(receiptTypeExtra) > 1 {
panic(fmt.Errorf("too many receipt types specified"))
}
node := waBinary.Node{
Tag: "receipt",
Attrs: waBinary.Attrs{
"id": ids[0],
"type": string(receiptType),
"to": chat,
"t": timestamp.Unix(),
},
}
if chat.Server == types.NewsletterServer || cli.GetPrivacySettings(ctx).ReadReceipts == types.PrivacySettingNone {
switch receiptType {
case types.ReceiptTypeRead:
node.Attrs["type"] = string(types.ReceiptTypeReadSelf)
// TODO change played to played-self?
}
}
if !sender.IsEmpty() && chat.Server != types.DefaultUserServer && chat.Server != types.HiddenUserServer && chat.Server != types.MessengerServer {
node.Attrs["participant"] = sender.ToNonAD()
}
if len(ids) > 1 {
children := make([]waBinary.Node, len(ids)-1)
for i := 1; i < len(ids); i++ {
children[i-1].Tag = "item"
children[i-1].Attrs = waBinary.Attrs{"id": ids[i]}
}
node.Content = []waBinary.Node{{
Tag: "list",
Content: children,
}}
}
return cli.sendNode(ctx, node)
}
// SetForceActiveDeliveryReceipts will force the client to send normal delivery
// receipts (which will show up as the two gray ticks on WhatsApp), even if the
// client isn't marked as online.
//
// By default, clients that haven't been marked as online will send delivery
// receipts with type="inactive", which is transmitted to the sender, but not
// rendered in the official WhatsApp apps. This is consistent with how WhatsApp
// web works when it's not in the foreground.
//
// To mark the client as online, use
//
// cli.SendPresence(types.PresenceAvailable)
//
// Note that if you turn this off (i.e. call SetForceActiveDeliveryReceipts(false)),
// receipts will act like the client is offline until SendPresence is called again.
func (cli *Client) SetForceActiveDeliveryReceipts(active bool) {
if cli == nil {
return
}
if active {
cli.sendActiveReceipts.Store(2)
} else {
cli.sendActiveReceipts.Store(0)
}
}
func buildBaseReceipt(id string, node *waBinary.Node) waBinary.Attrs {
attrs := waBinary.Attrs{
"id": id,
"to": node.Attrs["from"],
}
if recipient, ok := node.Attrs["recipient"]; ok {
attrs["recipient"] = recipient
}
if participant, ok := node.Attrs["participant"]; ok {
attrs["participant"] = participant
}
return attrs
}
func (cli *Client) sendMessageReceipt(ctx context.Context, info *types.MessageInfo, node *waBinary.Node) {
attrs := buildBaseReceipt(info.ID, node)
if info.IsFromMe {
attrs["type"] = string(types.ReceiptTypeSender)
if info.Type == "peer_msg" {
attrs["type"] = string(types.ReceiptTypePeerMsg)
}
} else if cli.sendActiveReceipts.Load() == 0 {
attrs["type"] = string(types.ReceiptTypeInactive)
}
err := cli.sendNode(ctx, waBinary.Node{
Tag: "receipt",
Attrs: attrs,
})
if err != nil {
cli.Log.Warnf("Failed to send receipt for %s: %v", info.ID, err)
}
}