-
Notifications
You must be signed in to change notification settings - Fork 129
/
client.go
187 lines (162 loc) · 3.92 KB
/
client.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
package slack
import (
"context"
"encoding/json"
sl "github.com/slack-go/slack"
"golang.org/x/time/rate"
)
type DeliveryPolicy int
const (
Post DeliveryPolicy = iota
PostAndUpdate
Update
)
func (p DeliveryPolicy) String() string {
switch p {
case Post:
return "Post"
case PostAndUpdate:
return "PostAndUpdate"
case Update:
return "Update"
}
return "Post"
}
func (p DeliveryPolicy) FromString(policy string) DeliveryPolicy {
switch policy {
case "Post":
return Post
case "PostAndUpdate":
return PostAndUpdate
case "Update":
return Update
}
return Post
}
func (p DeliveryPolicy) MarshalJSON() ([]byte, error) {
return json.Marshal(p.String())
}
func (p *DeliveryPolicy) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
*p = p.FromString(s)
return nil
}
//go:generate mockgen -destination=./mocks/client.go -source=$GOFILE -package=mocks SlackClient
type SlackClient interface {
SendMessageContext(ctx context.Context, channelID string, options ...sl.MsgOption) (string, string, string, error)
}
type timestampMap map[string]map[string]string
type channelMap map[string]string
type state struct {
Limiter *rate.Limiter
ThreadTSs timestampMap
ChannelIDs channelMap
}
func NewState(limiter *rate.Limiter) *state {
return &state{
Limiter: limiter,
ThreadTSs: make(timestampMap),
ChannelIDs: make(channelMap),
}
}
type threadedClient struct {
Client SlackClient
*state
}
func NewThreadedClient(client SlackClient, s *state) *threadedClient {
return &threadedClient{client, s}
}
func (c *threadedClient) getChannelID(recipient string) string {
if id, ok := c.ChannelIDs[recipient]; ok {
return id
}
return recipient
}
func (c *threadedClient) getThreadTimestamp(recipient string, groupingKey string) string {
thread, ok := c.ThreadTSs[recipient]
if !ok {
return ""
}
return thread[groupingKey]
}
func (c *threadedClient) setThreadTimestamp(recipient string, groupingKey string, ts string) {
thread, ok := c.ThreadTSs[recipient]
if !ok {
thread = map[string]string{}
c.ThreadTSs[recipient] = thread
}
thread[groupingKey] = ts
}
func (c *threadedClient) SendMessage(ctx context.Context, recipient string, groupingKey string, broadcast bool, policy DeliveryPolicy, options []sl.MsgOption) error {
ts := c.getThreadTimestamp(recipient, groupingKey)
if groupingKey != "" && ts != "" {
options = append(options, sl.MsgOptionTS(ts))
}
if ts == "" || policy == Post || policy == PostAndUpdate {
newTs, channelID, err := SendMessageRateLimited(
c.Client,
ctx,
c.Limiter,
recipient,
sl.MsgOptionPost(),
buildPostOptions(broadcast, options),
)
if err != nil {
return err
}
if groupingKey != "" && ts == "" {
c.setThreadTimestamp(recipient, groupingKey, newTs)
}
c.ChannelIDs[recipient] = channelID
}
if ts != "" && (policy == Update || policy == PostAndUpdate) {
_, _, err := SendMessageRateLimited(
c.Client,
ctx,
c.Limiter,
c.getChannelID(recipient),
sl.MsgOptionUpdate(ts),
sl.MsgOptionAsUser(true),
sl.MsgOptionCompose(options...),
)
if err != nil {
return err
}
}
return nil
}
func buildPostOptions(broadcast bool, options []sl.MsgOption) sl.MsgOption {
opt := sl.MsgOptionCompose(options...)
if broadcast {
opt = sl.MsgOptionCompose(sl.MsgOptionBroadcast(), opt)
}
return opt
}
func SendMessageRateLimited(client SlackClient, ctx context.Context, limiter *rate.Limiter, recipient string, options ...sl.MsgOption) (ts, channelID string, err error) {
for {
err = limiter.Wait(ctx)
if err != nil {
break
}
channelID, ts, _, err = client.SendMessageContext(
ctx,
recipient,
options...,
)
if err != nil {
if rateLimitedError, ok := err.(*sl.RateLimitedError); ok {
limiter.SetLimit(rate.Every(rateLimitedError.RetryAfter))
} else {
break
}
} else {
// No error, so remove rate limit
limiter.SetLimit(rate.Inf)
break
}
}
return
}