-
Notifications
You must be signed in to change notification settings - Fork 14
/
context.go
224 lines (194 loc) · 5.52 KB
/
context.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package slacker
import (
"context"
"github.com/slack-go/slack"
"github.com/slack-io/proper"
)
// newCommandContext creates a new command context
func newCommandContext(
ctx context.Context,
logger Logger,
slackClient *slack.Client,
event *MessageEvent,
definition *CommandDefinition,
parameters *proper.Properties,
) *CommandContext {
request := newRequest(parameters)
writer := newWriter(ctx, logger, slackClient)
replier := newReplier(event.ChannelID, event.UserID, event.InThread(), event.TimeStamp, writer)
response := newResponseReplier(writer, replier)
return &CommandContext{
ctx: ctx,
event: event,
slackClient: slackClient,
definition: definition,
request: request,
response: response,
logger: logger,
}
}
// CommandContext contains information relevant to the executed command
type CommandContext struct {
ctx context.Context
event *MessageEvent
slackClient *slack.Client
definition *CommandDefinition
request *Request
response *ResponseReplier
logger Logger
}
// Context returns the context
func (r *CommandContext) Context() context.Context {
return r.ctx
}
// Definition returns the command definition
func (r *CommandContext) Definition() *CommandDefinition {
return r.definition
}
// Event returns the slack message event
func (r *CommandContext) Event() *MessageEvent {
return r.event
}
// SlackClient returns the slack API client
func (r *CommandContext) SlackClient() *slack.Client {
return r.slackClient
}
// Request returns the command request
func (r *CommandContext) Request() *Request {
return r.request
}
// Response returns the response writer
func (r *CommandContext) Response() *ResponseReplier {
return r.response
}
// Logger returns the logger
func (r *CommandContext) Logger() Logger {
return r.logger
}
// WithContext returns a shallow copy of r with its context changed
// to ctx. The provided ctx must be non-nil.
func (r *CommandContext) WithContext(ctx context.Context) *CommandContext {
if ctx == nil {
panic("nil context")
}
r2 := new(CommandContext)
*r2 = *r
r.ctx = ctx
return r
}
// newInteractionContext creates a new interaction context
func newInteractionContext(
ctx context.Context,
logger Logger,
slackClient *slack.Client,
callback *slack.InteractionCallback,
definition *InteractionDefinition,
) *InteractionContext {
inThread := isMessageInThread(callback.OriginalMessage.ThreadTimestamp, callback.OriginalMessage.Timestamp)
writer := newWriter(ctx, logger, slackClient)
replier := newReplier(callback.Channel.ID, callback.User.ID, inThread, callback.MessageTs, writer)
response := newResponseReplier(writer, replier)
return &InteractionContext{
ctx: ctx,
definition: definition,
callback: callback,
slackClient: slackClient,
response: response,
logger: logger,
}
}
// InteractionContext contains information relevant to the executed interaction
type InteractionContext struct {
ctx context.Context
definition *InteractionDefinition
callback *slack.InteractionCallback
slackClient *slack.Client
response *ResponseReplier
logger Logger
}
// Context returns the context
func (r *InteractionContext) Context() context.Context {
return r.ctx
}
// Definition returns the interaction definition
func (r *InteractionContext) Definition() *InteractionDefinition {
return r.definition
}
// Callback returns the interaction callback
func (r *InteractionContext) Callback() *slack.InteractionCallback {
return r.callback
}
// Response returns the response writer
func (r *InteractionContext) Response() *ResponseReplier {
return r.response
}
// SlackClient returns the slack API client
func (r *InteractionContext) SlackClient() *slack.Client {
return r.slackClient
}
// Logger returns the logger
func (r *InteractionContext) Logger() Logger {
return r.logger
}
// WithContext returns a shallow copy of r with its context changed
// to ctx. The provided ctx must be non-nil.
func (r *InteractionContext) WithContext(ctx context.Context) *InteractionContext {
if ctx == nil {
panic("nil context")
}
r2 := new(InteractionContext)
*r2 = *r
r.ctx = ctx
return r
}
// newJobContext creates a new bot context
func newJobContext(ctx context.Context, logger Logger, slackClient *slack.Client, definition *JobDefinition) *JobContext {
writer := newWriter(ctx, logger, slackClient)
response := newWriterResponse(writer)
return &JobContext{
ctx: ctx,
definition: definition,
slackClient: slackClient,
response: response,
logger: logger,
}
}
// JobContext contains information relevant to the executed job
type JobContext struct {
ctx context.Context
definition *JobDefinition
slackClient *slack.Client
response *ResponseWriter
logger Logger
}
// Context returns the context
func (r *JobContext) Context() context.Context {
return r.ctx
}
// Definition returns the job definition
func (r *JobContext) Definition() *JobDefinition {
return r.definition
}
// Response returns the response writer
func (r *JobContext) Response() *ResponseWriter {
return r.response
}
// SlackClient returns the slack API client
func (r *JobContext) SlackClient() *slack.Client {
return r.slackClient
}
// Logger returns the logger
func (r *JobContext) Logger() Logger {
return r.logger
}
// WithContext returns a shallow copy of r with its context changed
// to ctx. The provided ctx must be non-nil.
func (r *JobContext) WithContext(ctx context.Context) *JobContext {
if ctx == nil {
panic("nil context")
}
r2 := new(JobContext)
*r2 = *r
r.ctx = ctx
return r
}