/
state.go
94 lines (78 loc) · 1.62 KB
/
state.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
package mockslack
import (
"sync"
"time"
)
type state struct {
mx sync.Mutex
gen *idGen
flags struct {
autoCreateChannel bool
}
apps map[string]*appState
channels map[string]*channelState
tokens map[string]*AuthToken
users map[string]*userState
tokenCodes map[string]*tokenCode
teamID string
}
func newState() *state {
return &state{
gen: newIDGen(),
apps: make(map[string]*appState),
channels: make(map[string]*channelState),
tokens: make(map[string]*AuthToken),
users: make(map[string]*userState),
tokenCodes: make(map[string]*tokenCode),
teamID: genTeamID(),
}
}
type tokenCode struct {
ClientID string
*AuthToken
}
type appState struct {
App
}
type App struct {
ID string
Name string
Secret string
AuthToken *AuthToken
}
type channelState struct {
Channel
TS time.Time
Users []string
Messages []*Message
}
// SetAutoCreateChannel, if set to true, will cause messages sent to
// non-existent channels to succeed by creating the channel automatically.
func (st *state) SetAutoCreateChannel(value bool) {
st.mx.Lock()
defer st.mx.Unlock()
st.flags.autoCreateChannel = value
}
func (st *state) token(id string) *AuthToken {
st.mx.Lock()
defer st.mx.Unlock()
return st.tokens[id]
}
func (st *state) app(id string) *appState {
st.mx.Lock()
defer st.mx.Unlock()
return st.apps[id]
}
func (st *state) newToken(a AuthToken) *AuthToken {
st.mx.Lock()
defer st.mx.Unlock()
if a.ID == "" {
if a.IsBot {
a.ID = st.gen.BotAccessToken()
} else {
a.ID = st.gen.UserAccessToken()
}
}
st.tokens[a.ID] = &a
return &a
}