-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpublicchannels.go
56 lines (45 loc) · 1.4 KB
/
publicchannels.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
package main
import (
"sync"
plugin "github.com/gotify/plugin-api"
)
var publicChannels = new(PublicChannelListManager)
// PublicChannelListManager holds a registry of public channels at a server scope
type PublicChannelListManager struct {
mutex sync.RWMutex
channels []ChannelWithUserContext
}
// UpdateChannelsForUser replaces all public channels belonging to a user context with a new slice of channels
// the publicity of the channel is checked here so it is not necessary to check for it again
func (c *PublicChannelListManager) UpdateChannelsForUser(userCtx plugin.UserContext, channels []ChannelDef) {
c.mutex.Lock()
defer c.mutex.Unlock()
res := make([]ChannelWithUserContext, 0)
for _, ch := range c.channels {
if ch.UserContext.ID == userCtx.ID {
continue
}
res = append(res, ch)
}
for _, def := range channels {
if def.Public {
res = append(res, ChannelWithUserContext{def, userCtx})
}
}
c.channels = res
}
// GetAllChannels gets all public channels in the manager
func (c *PublicChannelListManager) GetAllChannels() []ChannelWithUserContext {
res := make([]ChannelWithUserContext, 0)
c.mutex.RLock()
defer c.mutex.RUnlock()
for _, ch := range c.channels {
res = append(res, ch)
}
return res
}
// ChannelWithUserContext wraps a ChannelDef with the user context that possesses it
type ChannelWithUserContext struct {
Channel ChannelDef
UserContext plugin.UserContext
}