Skip to content

Commit def4988

Browse files
Prevent slack Update policy from posting new messages
Signed-off-by: Andrei Petrus <[email protected]>
1 parent 1f05776 commit def4988

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed

pkg/util/slack/client.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,37 +114,40 @@ func (c *threadedClient) SendMessage(ctx context.Context, recipient string, grou
114114
options = append(options, sl.MsgOptionTS(ts))
115115
}
116116

117-
if ts == "" || policy == Post || policy == PostAndUpdate {
118-
newTs, channelID, err := SendMessageRateLimited(
117+
// Updating an existing message
118+
if ts != "" && (policy == Update || policy == PostAndUpdate) {
119+
_, _, err := SendMessageRateLimited(
119120
c.Client,
120121
ctx,
121122
c.Limiter,
122-
recipient,
123-
sl.MsgOptionPost(),
124-
buildPostOptions(broadcast, options),
123+
c.getChannelID(recipient),
124+
sl.MsgOptionUpdate(ts),
125+
sl.MsgOptionAsUser(true),
126+
sl.MsgOptionCompose(options...),
125127
)
126128
if err != nil {
127129
return err
128130
}
129-
if groupingKey != "" && ts == "" {
130-
c.setThreadTimestamp(recipient, groupingKey, newTs)
131-
}
132-
c.ChannelIDs[recipient] = channelID
131+
return nil
133132
}
134133

135-
if ts != "" && (policy == Update || policy == PostAndUpdate) {
136-
_, _, err := SendMessageRateLimited(
134+
// Posting a new message
135+
if policy == Post || policy == PostAndUpdate {
136+
newTs, channelID, err := SendMessageRateLimited(
137137
c.Client,
138138
ctx,
139139
c.Limiter,
140-
c.getChannelID(recipient),
141-
sl.MsgOptionUpdate(ts),
142-
sl.MsgOptionAsUser(true),
143-
sl.MsgOptionCompose(options...),
140+
recipient,
141+
sl.MsgOptionPost(),
142+
buildPostOptions(broadcast, options),
144143
)
145144
if err != nil {
146145
return err
147146
}
147+
if groupingKey != "" && ts == "" {
148+
c.setThreadTimestamp(recipient, groupingKey, newTs)
149+
}
150+
c.ChannelIDs[recipient] = channelID
148151
}
149152

150153
return nil

pkg/util/slack/client_test.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,58 +103,56 @@ func TestThreadedClient(t *testing.T) {
103103
groupingKey string
104104
policy DeliveryPolicy
105105
wantPostType1 gomock.Matcher
106-
wantPostType2 gomock.Matcher
107-
wantthreadTSs timestampMap
106+
wantThreadTSs timestampMap
108107
}{
109108
"Post, basic": {
110109
threadTSs: timestampMap{},
111110
groupingKey: "",
112111
policy: Post,
113112
wantPostType1: EqChatPost(),
114-
wantthreadTSs: timestampMap{},
113+
wantThreadTSs: timestampMap{},
115114
},
116115
"Post, no parent, with grouping": {
117116
threadTSs: timestampMap{},
118117
groupingKey: groupingKey,
119118
policy: Post,
120119
wantPostType1: EqChatPost(),
121-
wantthreadTSs: timestampMap{channel: {groupingKey: ts1}},
120+
wantThreadTSs: timestampMap{channel: {groupingKey: ts1}},
122121
},
123122
"Post, with parent, with grouping": {
124123
threadTSs: timestampMap{channel: {groupingKey: ts2}},
125124
groupingKey: groupingKey,
126125
policy: Post,
127126
wantPostType1: EqChatPost(),
128-
wantthreadTSs: timestampMap{channel: {groupingKey: ts2}},
127+
wantThreadTSs: timestampMap{channel: {groupingKey: ts2}},
129128
},
130129
"PostAndUpdate, no parent. First post should not be updated": {
131130
threadTSs: timestampMap{},
132131
groupingKey: groupingKey,
133132
policy: PostAndUpdate,
134133
wantPostType1: EqChatPost(),
135-
wantthreadTSs: timestampMap{channel: {groupingKey: ts1}},
134+
wantThreadTSs: timestampMap{channel: {groupingKey: ts1}},
136135
},
137136
"PostAndUpdate, with parent. First post should be updated": {
138137
threadTSs: timestampMap{channel: {groupingKey: ts2}},
139138
groupingKey: groupingKey,
140139
policy: PostAndUpdate,
141-
wantPostType1: EqChatPost(),
142-
wantPostType2: EqChatUpdate(),
143-
wantthreadTSs: timestampMap{channel: {groupingKey: ts2}},
140+
wantPostType1: EqChatUpdate(),
141+
wantThreadTSs: timestampMap{channel: {groupingKey: ts2}},
144142
},
145-
"Update, no parent. Only call should be post": {
143+
"Update, no parent. There should be no call, no new thread": {
146144
threadTSs: timestampMap{},
147145
groupingKey: groupingKey,
148146
policy: Update,
149-
wantPostType1: EqChatPost(),
150-
wantthreadTSs: timestampMap{channel: {groupingKey: ts1}},
147+
wantPostType1: nil,
148+
wantThreadTSs: timestampMap{},
151149
},
152150
"Update, with parent. Only call should be update": {
153151
threadTSs: timestampMap{channel: {groupingKey: ts2}},
154152
groupingKey: groupingKey,
155153
policy: Update,
156154
wantPostType1: EqChatUpdate(),
157-
wantthreadTSs: timestampMap{channel: {groupingKey: ts2}},
155+
wantThreadTSs: timestampMap{channel: {groupingKey: ts2}},
158156
},
159157
}
160158
for name, tc := range tests {
@@ -163,19 +161,19 @@ func TestThreadedClient(t *testing.T) {
163161
defer ctrl.Finish()
164162
m := mocks.NewMockSlackClient(ctrl)
165163

166-
m.EXPECT().
167-
SendMessageContext(gomock.Any(), gomock.Eq(channel), tc.wantPostType1).
168-
Return(channelID, ts1, "", nil)
164+
expectedFunctionCall := m.EXPECT().
165+
SendMessageContext(gomock.Any(), gomock.Eq(channel), tc.wantPostType1)
169166

170-
if tc.wantPostType2 != nil {
171-
m.EXPECT().
172-
SendMessageContext(gomock.Any(), gomock.Eq(channelID), tc.wantPostType2)
167+
if tc.wantPostType1 != nil {
168+
expectedFunctionCall.Return(channelID, ts1, "", nil)
169+
} else {
170+
expectedFunctionCall.MaxTimes(0)
173171
}
174172

175173
client := NewThreadedClient(m, &state{rate.NewLimiter(rate.Inf, 1), tc.threadTSs, channelMap{}})
176174
err := client.SendMessage(context.TODO(), channel, tc.groupingKey, false, tc.policy, []slack.MsgOption{})
177175
assert.NoError(t, err)
178-
assert.Equal(t, tc.wantthreadTSs, client.ThreadTSs)
176+
assert.Equal(t, tc.wantThreadTSs, client.ThreadTSs)
179177
})
180178
}
181179
}

0 commit comments

Comments
 (0)