/
mark_read.go
112 lines (102 loc) · 3.25 KB
/
mark_read.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
package main
/*
#include "../c/constants.h"
*/
import "C"
import (
"go.mau.fi/whatsmeow/types"
"time"
)
/*
* Puts ID and sender of a received message in the store.
*
* Except if the purple setting is "never send receipts". Then it does nothing.
*/
func (handler *Handler) mark_read_defer(id types.MessageID, chat types.JID, sender types.JID) {
default_setting := C.GOWHATSAPP_SEND_RECEIPT_CHOICE_ON_ANSWER
never := C.GoString(C.GOWHATSAPP_SEND_RECEIPT_CHOICE_NEVER)
setting := purple_get_string(handler.account, C.GOWHATSAPP_SEND_RECEIPT_OPTION, default_setting)
if never == setting {
return
}
// append to map of map of ids
// I really could go for a python defaultdict here
senders, ok := handler.deferredReceipts[chat]
if !ok {
// first sender in this chat
senders = make(map[types.JID][]types.MessageID)
ids := []types.MessageID{id}
senders[sender] = ids
} else {
// chat already has senders
ids, ok := senders[sender]
if !ok {
// first id for this sender
ids = []types.MessageID{id}
} else {
ids = append(ids, id)
}
senders[sender] = ids
}
handler.deferredReceipts[chat] = senders
}
/*
* In case the purple setting is matching,
* ths will send all receipts stored for the given chat.
* mark_read_defer(…) should have been called before.
*/
func (handler *Handler) mark_read_if(chat types.JID, choice *C.char) {
default_setting := C.GOWHATSAPP_SEND_RECEIPT_CHOICE_ON_ANSWER
setting := purple_get_string(handler.account, C.GOWHATSAPP_SEND_RECEIPT_OPTION, default_setting)
if C.GoString(choice) == setting {
handler.log.Infof("Sending read receipt due to setting %s...", C.GoString(choice))
handler.mark_read_unconditionally(chat)
}
}
/*
* Shorthand for handler.mark_read_if(chat, C.GOWHATSAPP_SEND_RECEIPT_CHOICE_ON_ANSWER)
*/
func (handler *Handler) mark_read_if_on_answer(chat types.JID) {
handler.mark_read_if(chat, C.GOWHATSAPP_SEND_RECEIPT_CHOICE_ON_ANSWER)
}
/*
* Shorthand for handler.mark_read_if(chat, C.GOWHATSAPP_SEND_RECEIPT_CHOICE_IMMEDIATELY)
*/
func (handler *Handler) mark_read_if_on_receival(chat types.JID) {
handler.mark_read_if(chat, C.GOWHATSAPP_SEND_RECEIPT_CHOICE_IMMEDIATELY)
}
/*
* In case the purple setting is "send receipt when interacting with conversation",
* ths will send all receipts stored for the given chat.
* mark_read_defer(…) should have been called before.
*/
func (handler *Handler) mark_read_conversation(chat string) {
recipient, err := parseJID(chat)
if err != nil {
// fail silently
} else {
handler.mark_read_if(recipient, C.GOWHATSAPP_SEND_RECEIPT_CHOICE_ON_INTERACT)
}
}
/*
* This unconditionally sends all receipts stored in regard to the given chat.
* mark_read_defer(…) should have been called before.
*/
func (handler *Handler) mark_read_unconditionally(chat types.JID) {
senders, ok := handler.deferredReceipts[chat]
if ok {
for sender, ids := range senders {
handler.mark_read(ids, chat, sender)
}
}
}
/*
* This actually sends the receipts.
*/
func (handler *Handler) mark_read(ids []types.MessageID, chat types.JID, sender types.JID) {
handler.log.Infof("Sending read receipt to %s...", sender.ToNonAD().String())
err := handler.client.MarkRead(ids, time.Now(), chat, sender)
if err != nil {
handler.log.Warnf("Sending read receipt failed: %#v", err)
}
}