Skip to content

Commit

Permalink
Support replies
Browse files Browse the repository at this point in the history
  • Loading branch information
jybp committed Feb 6, 2024
1 parent 609a993 commit 0616407
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/gser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {

func main() {
if err := run(); err != nil {
fmt.Printf("%+v", err)
fmt.Printf("%+v\n", err)
os.Exit(1)
}
}
Expand Down
49 changes: 31 additions & 18 deletions internal/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,40 @@ func (api API) SetEmojis(ctx context.Context, match string, channelIDs []string,
}
log.Printf("%d messages found in channel %s\n", len(resp.Messages), channelID)
for _, msg := range resp.Messages {
idx := strings.LastIndex(msg.Text, match)
if idx == -1 {
continue
msgWithReplies := []slack.Message{msg}
if msg.ReplyCount > 0 {
replies, _, _, _ := api.client.GetConversationRepliesContext(ctx, &slack.GetConversationRepliesParameters{
ChannelID: channelID,
Timestamp: msg.Timestamp,
Limit: 100,
})
msgWithReplies = append(msgWithReplies, replies...)
}
rest := msg.Text[idx+len(match):]
if len(rest) > 0 && unicode.IsDigit(rune(rest[0])) {
continue
}
ref := slack.NewRefToMessage(channelID, msg.Timestamp)
log.Printf("message %+v adding emojis %+v\n", ref, emojis)
for emoji, set := range emojis {
if set {
if err := api.client.AddReactionContext(ctx, emoji, ref); err != nil &&
err.Error() != "already_reacted" {
return fmt.Errorf("AddReactionContext(ctx,%s,%+v): %w", emoji, ref, err)
}

for _, msg := range msgWithReplies {
idx := strings.LastIndex(msg.Text, match)
if idx == -1 {
continue
}
rest := msg.Text[idx+len(match):]
if len(rest) > 0 && unicode.IsDigit(rune(rest[0])) {
continue
}
if err := api.client.RemoveReactionContext(ctx, emoji, ref); err != nil &&
err.Error() != "no_reaction" {
return fmt.Errorf("RemoveReactionContext(ctx,%s,%+v): %w", emoji, ref, err)
ref := slack.NewRefToMessage(channelID, msg.Timestamp)

log.Printf("message %+v adding emojis %+v\n", ref, emojis)
for emoji, set := range emojis {
if set {
if err := api.client.AddReactionContext(ctx, emoji, ref); err != nil &&
err.Error() != "already_reacted" {
return fmt.Errorf("AddReactionContext(ctx,%s,%+v): %w", emoji, ref, err)
}
continue
}
if err := api.client.RemoveReactionContext(ctx, emoji, ref); err != nil &&
err.Error() != "no_reaction" {
return fmt.Errorf("RemoveReactionContext(ctx,%s,%+v): %w", emoji, ref, err)
}
}
}
}
Expand Down

0 comments on commit 0616407

Please sign in to comment.