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 29
/
privates.go
258 lines (206 loc) · 5.13 KB
/
privates.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
package channel
import (
"strings"
"sync"
"github.com/diamondburned/arikawa/discord"
"github.com/diamondburned/arikawa/gateway"
"github.com/diamondburned/gtkcord3/gtkcord/gtkutils"
"github.com/diamondburned/gtkcord3/gtkcord/ningen"
"github.com/diamondburned/gtkcord3/gtkcord/semaphore"
"github.com/diamondburned/gtkcord3/internal/log"
"github.com/gotk3/gotk3/gtk"
)
type PrivateChannels struct {
*gtk.Box
List *gtk.ListBox
Scroll *gtk.ScrolledWindow
Search *gtk.Entry
search string
// Channels map[discord.Snowflake]*PrivateChannel
Channels map[string]*PrivateChannel
busy sync.RWMutex
state *ningen.State
OnSelect func(pm *PrivateChannel)
}
// thread-safe
func NewPrivateChannels(s *ningen.State) (pcs *PrivateChannels) {
semaphore.IdleMust(func() {
l, _ := gtk.ListBoxNew()
l.Show()
gtkutils.InjectCSSUnsafe(l, "dmchannels", "")
cs, _ := gtk.ScrolledWindowNew(nil, nil)
cs.Show()
cs.SetVExpand(true)
cs.Add(l)
e, _ := gtk.EntryNew()
e.Show()
e.SetPlaceholderText("Find conversation...")
b, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
b.Show()
b.Add(e)
b.Add(cs)
pcs = &PrivateChannels{
Box: b,
List: l,
Scroll: cs,
Search: e,
state: s,
}
e.Connect("changed", func() {
t, err := e.GetText()
if err != nil {
log.Errorln("Failed to get text from dmchannels entry:", err)
return
}
pcs.search = strings.ToLower(t)
pcs.List.InvalidateFilter()
})
l.SetFilterFunc(pcs.filter, 0)
l.Connect("row-activated", func(l *gtk.ListBox, r *gtk.ListBoxRow) {
if len(pcs.Channels) == 0 || pcs.OnSelect == nil || r == nil {
return
}
rw, ok := pcs.Channels[_ChIDFromRow(r)]
if !ok {
log.Errorln("Failed to find channel")
return
}
go pcs.OnSelect(rw)
})
})
s.AddReadChange(pcs.TraverseReadState)
return
}
func (pcs *PrivateChannels) Cleanup() {
pcs.busy.Lock()
defer pcs.busy.Unlock()
if pcs.Channels != nil {
semaphore.IdleMust(func() {
for _, ch := range pcs.Channels {
pcs.List.Remove(ch)
}
})
pcs.Channels = nil
}
}
// thread-safe
func (pcs *PrivateChannels) LoadChannels() error {
pcs.busy.Lock()
// defer at the end of the bottom goroutine.
channels, err := pcs.state.PrivateChannels()
if err != nil {
return err
}
pcs.Channels = make(map[string]*PrivateChannel, len(channels))
semaphore.IdleMust(func() {
for _, channel := range channels {
w := newPrivateChannel(channel)
if channel.Type == discord.DirectMessage && len(channel.DMRecipients) == 1 {
user := channel.DMRecipients[0]
w.updateAvatar(user.AvatarURL())
if p, _ := pcs.state.Presence(0, user.ID); p != nil {
var game = p.Game
if game == nil && len(p.Activities) > 0 {
game = &p.Activities[0]
}
w.updateStatus(p.Status)
w.updateActivity(game)
}
} else if channel.Icon != "" {
w.updateAvatar(channel.IconURL())
}
pcs.Channels[channel.ID.String()] = w
pcs.List.Insert(w, -1)
}
})
go func() {
// defer here last:
defer pcs.busy.Unlock()
for _, channel := range channels {
rs := pcs.state.FindLastRead(channel.ID)
if rs == nil {
continue
}
// Snowflakes have timestamps, which allow us to do this:
if channel.LastMessageID.Time().After(rs.LastMessageID.Time()) {
ch := pcs.Channels[channel.ID.String()]
semaphore.Async(ch.setUnread, true)
}
}
}()
return nil
}
func (pcs *PrivateChannels) Selected() *PrivateChannel {
pcs.busy.RLock()
defer pcs.busy.RUnlock()
if len(pcs.Channels) == 0 {
return nil
}
r := pcs.List.GetSelectedRow()
if r == nil {
r = pcs.List.GetRowAtIndex(0)
pcs.List.SelectRow(r)
}
rw, ok := pcs.Channels[_ChIDFromRow(r)]
if !ok || rw == nil {
log.Errorln("Failed to find channel row")
}
return rw
}
func (pcs *PrivateChannels) filter(r *gtk.ListBoxRow, _ ...interface{}) bool {
if pcs.search == "" {
return true
}
pc, ok := pcs.Channels[_ChIDFromRow(r)]
if !ok {
log.Errorln("Failed to get channel for filter")
return false
}
return strings.Contains(strings.ToLower(pc.Name), pcs.search)
}
func (pcs *PrivateChannels) TraverseReadState(_ *ningen.State, rs *gateway.ReadState, unread bool) {
// Read lock is used, as the size of the slice isn't directly modified.
pcs.busy.RLock()
defer pcs.busy.RUnlock()
if len(pcs.Channels) == 0 {
return
}
pc, ok := pcs.Channels[rs.ChannelID.String()]
if !ok {
return
}
// Prepend/move to top.
semaphore.IdleMust(func() {
pcs.List.Remove(pc)
pcs.List.Prepend(pc)
})
pc.setUnread(unread)
}
func (pcs *PrivateChannels) FindByID(id discord.Snowflake) *PrivateChannel {
ch, _ := pcs.Channels[id.String()]
return ch
}
// func (pcs *PrivateChannels) updatePresence(p discord.Presence) {
// for _, ch := range pcs.Channels {
// if ch.Recp == p.User.ID && !ch.Group {
// ch.UpdateStatus(p.Status)
// ch.UpdateActivity(p.Game)
// break
// }
// }
// }
// func (pcs *PrivateChannels) setUnread(unread bool) {
// if !unread {
// for _, ch := range pcs.Channels {
// if ch.stateClass == "pinged" {
// unread = true
// break
// }
// }
// }
// if unread {
// pcs.setButtonClass("pinged")
// } else {
// pcs.setButtonClass("")
// }
// }