This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
member_list.go
147 lines (118 loc) · 2.84 KB
/
member_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
package ningen
import (
"github.com/diamondburned/arikawa/discord"
"github.com/diamondburned/arikawa/gateway"
"github.com/diamondburned/gtkcord3/internal/log"
"github.com/diamondburned/ningen/handler"
"github.com/sasha-s/go-deadlock"
)
type MemberListState struct {
mu deadlock.Mutex
state map[discord.Snowflake]*MemberList
// All mutex guarded
OnOP func(m *MemberList, guild discord.Snowflake, op gateway.GuildMemberListOp)
OnSync func(m *MemberList, guild discord.Snowflake)
}
type MemberList struct {
mu deadlock.Mutex
ID string
MemberCount uint64
OnlineCount uint64
Groups []gateway.GuildMemberListGroup
Items []*gateway.GuildMemberListOpItem
}
func NewMemberListState(h handler.AddHandler) *MemberListState {
state := &MemberListState{
state: map[discord.Snowflake]*MemberList{},
}
h.AddHandler(state.handle)
return state
}
func (m *MemberList) Acquire() func() {
m.mu.Lock()
return m.mu.Unlock
}
func (m *MemberListState) GetMemberList(guild discord.Snowflake) *MemberList {
m.mu.Lock()
defer m.mu.Unlock()
ml, ok := m.state[guild]
if !ok {
return nil
}
return ml
}
func (m *MemberListState) handle(ev *gateway.GuildMemberListUpdate) {
if ev.ID != "everyone" {
log.Errorln("Invalid member list ID:", ev.ID)
return
}
m.mu.Lock()
ml, ok := m.state[ev.GuildID]
if !ok {
ml = &MemberList{}
m.state[ev.GuildID] = ml
}
ml.mu.Lock()
defer ml.mu.Unlock()
m.mu.Unlock()
ml.ID = ev.ID
ml.MemberCount = ev.MemberCount
ml.OnlineCount = ev.OnlineCount
ml.Groups = ev.Groups
synced := false
for _, op := range ev.Ops {
if op.Op == "SYNC" {
start, end := op.Range[0], op.Range[1]
length := end + 1
growItems(&ml.Items, length)
for i := 0; i < length-start && i < len(op.Items); i++ {
ml.Items[start+i] = &op.Items[i]
}
synced = true
continue
}
// https://github.com/golang/go/wiki/SliceTricks
i := op.Index
// Bounds check
if len(ml.Items) > 0 && i != 0 {
var length = len(ml.Items)
if op.Op == "INSERT" {
length++
}
if length <= i {
log.Errorf(
"Member %s: index out of range: len(ml.Items)=%d <= op.Index=%d\n",
op.Op, len(ml.Items), i,
)
continue
}
}
switch op.Op {
case "UPDATE":
ml.Items[i] = &op.Item
case "DELETE":
if i < len(ml.Items)-1 {
copy(ml.Items[i:], ml.Items[i+1:])
}
ml.Items[len(ml.Items)-1] = nil
ml.Items = ml.Items[:len(ml.Items)-1]
case "INSERT":
ml.Items = append(ml.Items, nil)
copy(ml.Items[i+1:], ml.Items[i:])
ml.Items[i] = &op.Item
}
if m.OnOP != nil {
m.OnOP(ml, ev.GuildID, op)
}
}
if synced && m.OnSync != nil {
m.OnSync(ml, ev.GuildID)
}
}
func growItems(items *[]*gateway.GuildMemberListOpItem, maxLen int) {
if len(*items) >= maxLen {
return
}
delta := maxLen - len(*items)
*items = append(*items, make([]*gateway.GuildMemberListOpItem, delta)...)
}