-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_manager.go
86 lines (77 loc) · 2.44 KB
/
channel_manager.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
package main
import (
_ "errors" // debug
"log"
)
type ChannelIdRequest struct {
Name string
Reply chan string
}
type ChannelNameRequest struct {
Id string
Reply chan string
}
type PutChannelInfo struct {
Id string
Name string
}
func GetChannelId(name string, getChannelIdChan chan<- ChannelIdRequest) string {
replyChan := make(chan string)
getChannelIdChan <- ChannelIdRequest{name, replyChan}
return <-replyChan
}
func GetChannelName(id string, getChannelNameChan chan<- ChannelNameRequest) string {
replyChan := make(chan string)
getChannelNameChan <- ChannelNameRequest{id, replyChan}
return <-replyChan
}
// TODO(remove, and write to chan directly?)
func PutChannelId(name, id string, putChannelIdChan chan<- PutChannelInfo) {
putChannelIdChan <- PutChannelInfo{name, id}
}
// channelIdManager acts like a CSP map, with put and get operations via channels
// TODO(add terminate channel, to safely shut down the manager)
// TODO(load group and personal channels)
func channelIdManager(token string, put <-chan PutChannelInfo, get <-chan ChannelIdRequest, getName <-chan ChannelNameRequest) {
// TODO(create name and id types?)
channels := make(map[string]string) // map[name]id
channelNames := make(map[string]string) // map[id]name
for {
select {
case p := <-put:
channels[p.Name] = p.Id
channelNames[p.Id] = p.Name
case g := <-get:
id, ok := channels[g.Name]
if !ok {
g.Reply <- "not found" // debug
continue
// Shouldn't be possible to have an name without an id.
// If it was, we could iterate names via channels.list
// log.Panicln(errors.New("channel id requested for name that doesn't exist")) // debug
}
g.Reply <- id
case gn := <-getName:
name, ok := channelNames[gn.Id]
if !ok {
newName, err := GetSlackChannel(token, gn.Id)
if err != nil {
log.Println("channelIdManager error getting channel: " + err.Error())
gn.Reply <- "" // TODO(fix to get group and private channels, and resume panicing)
continue
}
name = newName.Name
channels[name] = gn.Id
channelNames[gn.Id] = name
}
gn.Reply <- name
}
}
}
func StartChannelIdManager(token string) (chan<- PutChannelInfo, chan<- ChannelIdRequest, chan<- ChannelNameRequest) {
putChan := make(chan PutChannelInfo)
getChan := make(chan ChannelIdRequest)
getNameChan := make(chan ChannelNameRequest)
go channelIdManager(token, putChan, getChan, getNameChan)
return putChan, getChan, getNameChan
}