forked from tulir/whatsmeow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcast.go
147 lines (135 loc) · 3.76 KB
/
broadcast.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
// Copyright (c) 2022 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 (
"errors"
"fmt"
waBinary "go.mau.fi/whatsmeow/binary"
"go.mau.fi/whatsmeow/types"
)
func (cli *Client) getBroadcastListParticipants(jid types.JID) ([]types.JID, error) {
var list []types.JID
var err error
if jid == types.StatusBroadcastJID {
list, err = cli.getStatusBroadcastRecipients()
} else {
return nil, ErrBroadcastListUnsupported
}
if err != nil {
return nil, err
}
ownID := cli.getOwnID().ToNonAD()
if ownID.IsEmpty() {
return nil, ErrNotLoggedIn
}
selfIndex := -1
for i, participant := range list {
if participant.User == ownID.User {
selfIndex = i
break
}
}
if selfIndex >= 0 {
if cli.DontSendSelfBroadcast {
list[selfIndex] = list[len(list)-1]
list = list[:len(list)-1]
}
} else if !cli.DontSendSelfBroadcast {
list = append(list, ownID)
}
return list, nil
}
func (cli *Client) getStatusBroadcastRecipients() ([]types.JID, error) {
statusPrivacyOptions, err := cli.GetStatusPrivacy()
if err != nil {
return nil, fmt.Errorf("failed to get status privacy: %w", err)
}
statusPrivacy := statusPrivacyOptions[0]
if statusPrivacy.Type == types.StatusPrivacyTypeWhitelist {
// Whitelist mode, just return the list
return statusPrivacy.List, nil
}
// Blacklist or all contacts mode. Find all contacts from database, then filter them appropriately.
contacts, err := cli.Store.Contacts.GetAllContacts()
if err != nil {
return nil, fmt.Errorf("failed to get contact list from db: %w", err)
}
blacklist := make(map[types.JID]struct{})
if statusPrivacy.Type == types.StatusPrivacyTypeBlacklist {
for _, jid := range statusPrivacy.List {
blacklist[jid] = struct{}{}
}
}
var contactsArray []types.JID
for jid, contact := range contacts {
_, isBlacklisted := blacklist[jid]
if isBlacklisted {
continue
}
// TODO should there be a better way to separate contacts and found push names in the db?
if len(contact.FullName) > 0 {
contactsArray = append(contactsArray, jid)
}
}
return contactsArray, nil
}
var DefaultStatusPrivacy = []types.StatusPrivacy{{
Type: types.StatusPrivacyTypeContacts,
IsDefault: true,
}}
// GetStatusPrivacy gets the user's status privacy settings (who to send status broadcasts to).
//
// There can be multiple different stored settings, the first one is always the default.
func (cli *Client) GetStatusPrivacy() ([]types.StatusPrivacy, error) {
resp, err := cli.sendIQ(infoQuery{
Namespace: "status",
Type: iqGet,
To: types.ServerJID,
Content: []waBinary.Node{{
Tag: "privacy",
}},
})
if err != nil {
if errors.Is(err, ErrIQNotFound) {
return DefaultStatusPrivacy, nil
}
return nil, err
}
privacyLists := resp.GetChildByTag("privacy")
var outputs []types.StatusPrivacy
for _, list := range privacyLists.GetChildren() {
if list.Tag != "list" {
continue
}
ag := list.AttrGetter()
var out types.StatusPrivacy
out.IsDefault = ag.OptionalBool("default")
out.Type = types.StatusPrivacyType(ag.String("type"))
children := list.GetChildren()
if len(children) > 0 {
out.List = make([]types.JID, 0, len(children))
for _, child := range children {
jid, ok := child.Attrs["jid"].(types.JID)
if child.Tag == "user" && ok {
out.List = append(out.List, jid)
}
}
}
outputs = append(outputs, out)
if out.IsDefault {
// Move default to always be first in the list
outputs[len(outputs)-1] = outputs[0]
outputs[0] = out
}
if len(ag.Errors) > 0 {
return nil, ag.Error()
}
}
if len(outputs) == 0 {
return DefaultStatusPrivacy, nil
}
return outputs, nil
}