-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.go
127 lines (110 loc) · 3.09 KB
/
store.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
package slack
import (
"sync"
"github.com/nlopes/slack"
)
// Store is the interface to expect from adapter.Store
type Store interface {
// Load takes slack info and adds new users and channels from it
Load(*slack.Info)
// Update queries Slack's web API for users and channels
Update() error
// UserByID queries the store for a User by ID
UserByID(id string) (slack.User, bool)
// UserByName queries the store for a User by Name
UserByName(name string) (slack.User, bool)
// UserByEmail queries the store for a User by Name
UserByEmail(name string) (slack.User, bool)
// ChannelByID queries the store for a Channel by ID
ChannelByID(id string) (slack.Channel, bool)
// ChannelByName queries the store for a Channel by Name
ChannelByName(id string) (slack.Channel, bool)
// IMByID queries the store for a IM by ID
IMByID(id string) (slack.IM, bool)
// IMByUserID queries the store for a DM by User ID
IMByUserID(userID string) (slack.IM, bool)
}
type memoryStore struct {
mu sync.RWMutex
client *slack.Client
indices map[string]string
users map[string]slack.User
channels map[string]slack.Channel
ims map[string]slack.IM
}
func newMemoryStore(c *slack.Client) *memoryStore {
m := &memoryStore{
client: c,
indices: make(map[string]string),
users: make(map[string]slack.User),
channels: make(map[string]slack.Channel),
ims: make(map[string]slack.IM),
}
return m
}
func (s *memoryStore) Load(i *slack.Info) {
s.mu.Lock()
defer s.mu.Unlock()
for _, u := range i.Users {
s.users[u.ID] = u
s.indices["user:name:"+u.Name] = u.ID
s.indices["user:email:"+u.Profile.Email] = u.ID
}
for _, ch := range i.Channels {
s.channels[ch.ID] = ch
s.indices["channel:name:"+ch.Name] = ch.ID
}
for _, im := range i.IMs {
s.ims[im.ID] = im
s.indices["im:userID:"+im.User] = im.ID
}
}
func (s *memoryStore) Update() (err error) {
info := slack.Info{}
if info.Users, err = s.client.GetUsers(); err != nil {
return err
}
if info.Channels, err = s.client.GetChannels(true); err != nil {
return err
}
s.Load(&info)
return err
}
func (s *memoryStore) UserByID(id string) (slack.User, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
u, ok := s.users[id]
return u, ok
}
func (s *memoryStore) UserByName(name string) (slack.User, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
return s.UserByID(s.indices["user:name:"+name])
}
func (s *memoryStore) UserByEmail(name string) (slack.User, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
return s.UserByID(s.indices["user:email:"+name])
}
func (s *memoryStore) ChannelByID(id string) (slack.Channel, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
ch, ok := s.channels[id]
return ch, ok
}
func (s *memoryStore) ChannelByName(name string) (slack.Channel, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
return s.ChannelByID(s.indices["channel:name:"+name])
}
func (s *memoryStore) IMByID(id string) (slack.IM, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
dm, ok := s.ims[id]
return dm, ok
}
func (s *memoryStore) IMByUserID(userID string) (slack.IM, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
return s.IMByID(s.indices["im:userID:"+userID])
}