forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
276 lines (222 loc) · 6.87 KB
/
list.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
package roster
import (
"sort"
"sync"
"github.com/twstrike/coyim/xmpp/utils"
)
// List represent a list of peers. It takes care of both roster and presence information
// transparently and presents a unified view of this information to any UI
// List is not ordered, but can be asked to present its information in various orders
// depending on what policy is in use
// It also contains information about pending subscribes
// One invariant is that the list will only ever contain one Peer for each bare jid.
type List struct {
peers map[string]*Peer
peersLock sync.RWMutex
}
// New returns a new List
func New() *List {
return &List{
peers: make(map[string]*Peer),
}
}
// Get returns the peer if it's known and false otherwise
func (l *List) Get(jid string) (*Peer, bool) {
l.peersLock.RLock()
defer l.peersLock.RUnlock()
v, ok := l.peers[utils.RemoveResourceFromJid(jid)]
return v, ok
}
// Clear removes all current entries in the list
func (l *List) Clear() {
l.peersLock.Lock()
defer l.peersLock.Unlock()
l.peers = make(map[string]*Peer)
}
// Remove returns the Peer with the jid from the List - it will first turn the jid into a bare jid.
// It returns true if it could remove the entry and false otherwise. It also returns the removed entry.
func (l *List) Remove(jid string) (*Peer, bool) {
j := utils.RemoveResourceFromJid(jid)
l.peersLock.Lock()
defer l.peersLock.Unlock()
if v, ok := l.peers[j]; ok {
delete(l.peers, j)
return v, true
}
return nil, false
}
// AddOrMerge will add a new entry or merge with an existing entry the information from the given Peer
// It returns true if it added the entry and false otherwise
func (l *List) AddOrMerge(p *Peer) bool {
l.peersLock.Lock()
defer l.peersLock.Unlock()
if v, existed := l.peers[p.Jid]; existed {
l.peers[p.Jid] = v.MergeWith(p)
return false
}
l.peers[p.Jid] = p
return true
}
// AddOrReplace will add a new entry or replace an existing entry with the information from the given Peer
// It returns true if it added the entry and false otherwise
func (l *List) AddOrReplace(p *Peer) bool {
_, existed := l.Get(p.Jid)
l.peersLock.Lock()
defer l.peersLock.Unlock()
l.peers[p.Jid] = p
return !existed
}
// PeerBecameUnavailable marks the peer as unavailable if they exist
// Returns true if they existed, otherwise false
func (l *List) PeerBecameUnavailable(jid string) bool {
if p, exist := l.Get(jid); exist {
oldOnline := p.Online
resource := utils.ResourceFromJid(jid)
if resource != "" {
p.RemoveResource(resource)
p.Online = p.HasResources()
} else {
p.ClearResources()
p.Online = false
}
return oldOnline != p.Online
}
return false
}
// PeerPresenceUpdate updates the status for the peer
// It returns true if it actually updated the status of the user
func (l *List) PeerPresenceUpdate(jid, status, statusMsg, belongsTo string) bool {
resource := utils.ResourceFromJid(jid)
if p, ok := l.Get(jid); ok {
oldOnline := p.Online
p.Online = true
p.AddResource(resource)
if p.Status != status || p.StatusMsg != statusMsg {
p.Status = status
p.StatusMsg = statusMsg
return true
}
return !oldOnline
}
l.AddOrMerge(PeerWithState(jid, status, statusMsg, belongsTo, resource))
return true
}
// StateOf returns the status and status msg of the peer if it exists. It returns not ok if the peer doesn't exist.
func (l *List) StateOf(jid string) (status, statusMsg string, ok bool) {
if p, existed := l.Get(jid); existed {
return p.Status, p.StatusMsg, true
}
return "", "", false
}
// SubscribeRequest adds a new pending subscribe request
func (l *List) SubscribeRequest(jid, id, belongsTo string) {
l.AddOrMerge(peerWithPendingSubscribe(jid, id, belongsTo))
}
// RemovePendingSubscribe will return a subscribe id and remove the pending subscribe if it exists
// It will return false if no such subscribe is in flight
func (l *List) RemovePendingSubscribe(jid string) (string, bool) {
if p, existed := l.Get(jid); existed {
s := p.PendingSubscribeID
p.PendingSubscribeID = ""
return s, s != ""
}
return "", false
}
// GetPendingSubscribe will return a subscribe id without removing it
func (l *List) GetPendingSubscribe(jid string) (string, bool) {
if p, existed := l.Get(jid); existed {
return p.PendingSubscribeID, p.PendingSubscribeID != ""
}
return "", false
}
// Subscribed will mark the jid as subscribed
func (l *List) Subscribed(jid string) {
if p, existed := l.Get(jid); existed {
switch p.Subscription {
case "from":
p.Subscription = "both"
case "none", "":
p.Subscription = "to"
}
p.PendingSubscribeID = ""
p.Asked = false
}
}
// LatestError will set the latest error on the jid in question
func (l *List) LatestError(jid string, code, tp, more string) {
if p, existed := l.Get(jid); existed {
p.SetLatestError(code, tp, more)
}
}
// Unsubscribed will mark the jid as unsubscribed
func (l *List) Unsubscribed(jid string) {
if p, existed := l.Get(jid); existed {
switch p.Subscription {
case "both":
p.Subscription = "from"
case "to":
p.Subscription = "none"
}
p.PendingSubscribeID = ""
p.Asked = false
}
}
type byJidAlphabetic []*Peer
func (s byJidAlphabetic) Len() int { return len(s) }
func (s byJidAlphabetic) Less(i, j int) bool { return s[i].Jid < s[j].Jid }
func (s byJidAlphabetic) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type byNameForPresentation []*Peer
func (s byNameForPresentation) Len() int { return len(s) }
func (s byNameForPresentation) Less(i, j int) bool {
return s[i].NameForPresentation() < s[j].NameForPresentation()
}
func (s byNameForPresentation) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// this function expects that peers has already acquired the read lock for the peers
func (l *List) intoSlice(res []*Peer) []*Peer {
for _, v := range l.peers {
res = append(res, v)
}
return res
}
// ToSlice returns a slice of all the peers in this roster list
func (l *List) ToSlice() []*Peer {
l.peersLock.RLock()
defer l.peersLock.RUnlock()
res := l.intoSlice(make([]*Peer, 0, len(l.peers)))
sort.Sort(byJidAlphabetic(res))
return res
}
// Iter calls the cb function once for each peer in the list
func (l *List) Iter(cb func(int, *Peer)) {
for ix, pr := range l.ToSlice() {
cb(ix, pr)
}
}
// IterAll calls the cb function once for each peer in all the lists
func IterAll(cb func(int, *Peer), ls ...*List) {
res := make([]*Peer, 0, 20)
for _, l := range ls {
func() {
l.peersLock.RLock()
defer l.peersLock.RUnlock()
res = l.intoSlice(res)
}()
}
sort.Sort(byJidAlphabetic(res))
for ix, pr := range res {
cb(ix, pr)
}
}
// GetGroupNames return all group names for this peer list.
func (l *List) GetGroupNames() map[string]bool {
l.peersLock.RLock()
defer l.peersLock.RUnlock()
names := map[string]bool{}
//TODO: Should not group separator be part of a Peer List?
for _, peer := range l.peers {
for group := range peer.Groups {
names[group] = true
}
}
return names
}