File size: 1,849 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
	usergroups map[string]*usergroupState
	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),
		usergroups: make(map[string]*usergroupState),
		teamID:     genTeamID(),
	}
}

type tokenCode struct {
	ClientID string
	*AuthToken
}

type appState struct {
	App
}
type App struct {
	ID        string
	Name      string
	Secret    string
	AuthToken *AuthToken
	ActionURL string

	SigningSecret string
}

type channelState struct {
	Channel

	TS       time.Time
	Users    []string
	Messages []*Message
}

type usergroupState struct {
	UserGroup

	Users []string
}

// 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
}