Skip to content

Commit

Permalink
fix slackReactionUsersService.ListUsersEmailByReaction
Browse files Browse the repository at this point in the history
  • Loading branch information
sho0126hiro committed Mar 17, 2023
1 parent e12b7c5 commit e6cd1c8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/internal/domain/repository/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,19 @@ import (
)

type SlackRepository interface {
// PostMessage send a message to a channel
PostMessage(ctx context.Context, channelID, message, ts string) error

// PostEphemeral sends an ephemeral message to user in a channel
PostEphemeral(ctx context.Context, channelID, message, ts, userID string) error

// GetParentMessage gets Slack message that started the thread
GetParentMessage(ctx context.Context, channelID, ts string) (*model.SlackMessage, error)

// ListUsersEmail fetches users email
ListUsersEmail(ctx context.Context, userID []string) ([]*model.SlackUserEmail, error)

// GetReactions fetches reactions
// full: if true always return the complete reaction list
GetReactions(ctx context.Context, channelID, ts string, full bool) ([]*model.SlackReaction, error)
}
20 changes: 20 additions & 0 deletions app/internal/domain/service/slack_reaction_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func (s *slackReactionUsersService) ListUsersEmailByReaction(ctx context.Context
if err != nil {
return nil, err
}
if s.IsNeededMoreFetches(msg.Reactions) {
msg.Reactions, err = s.getFullReactions(ctx, channelID, ts)
if err != nil {
return nil, err
}
}
reactedUserIDs := s.getReactionUserIDs(ctx, msg.Reactions, reactionName)
reactedUserEmails, err := s.chunkedListUsersEmail(ctx, reactedUserIDs)
if err != nil {
Expand All @@ -76,6 +82,16 @@ func (s *slackReactionUsersService) ListUsersEmailByReaction(ctx context.Context
return reactedUserEmails, nil
}

// IsNeededMoreFetches returns true if more fetches is required
func (s *slackReactionUsersService) IsNeededMoreFetches(reactions []*model.SlackReaction) bool {
for _, r := range reactions {
if r.Count != len(r.UserIDs) {
return true
}
}
return false
}

// getReactionUserIDs get reaction users by reactionName
func (s *slackReactionUsersService) getReactionUserIDs(ctx context.Context, reactions []*model.SlackReaction, reactionName string) []string {
var userIDs []string
Expand All @@ -95,3 +111,7 @@ func (s *slackReactionUsersService) getReactionUserIDs(ctx context.Context, reac
}
return slice.ToStringSet(userIDs)
}

func (s *slackReactionUsersService) getFullReactions(ctx context.Context, channelID, ts string) ([]*model.SlackReaction, error) {
return s.slackRepository.GetReactions(ctx, channelID, ts, true)
}
2 changes: 2 additions & 0 deletions app/internal/model/slack_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package model

type SlackMessage struct {
ChannelID string
Timestamp string
Reactions []*SlackReaction
}

type SlackReaction struct {
Name string
Count int
UserIDs []string
}

Expand Down
18 changes: 18 additions & 0 deletions app/internal/repository/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ func (r *slackRepository) GetParentMessage(ctx context.Context, channelID, ts st
reactions = append(reactions, &model.SlackReaction{
Name: reaction.Name,
UserIDs: reaction.Users,
Count: reaction.Count,
})
}
return &model.SlackMessage{
ChannelID: parentMessage.Channel,
Timestamp: parentMessage.Timestamp,
Reactions: reactions,
}, nil
}
Expand All @@ -93,3 +95,19 @@ func (r *slackRepository) ListUsersEmail(ctx context.Context, userID []string) (
}
return slackUsers, nil
}

func (r *slackRepository) GetReactions(ctx context.Context, channelID, ts string, full bool) ([]*model.SlackReaction, error) {
reactions, err := r.client.GetReaction(ctx, channelID, ts, full)
if err != nil {
return nil, err
}
ret := make([]*model.SlackReaction, 0, len(reactions))
for _, reaction := range reactions {
ret = append(ret, &model.SlackReaction{
Name: reaction.Name,
UserIDs: reaction.Users,
Count: reaction.Count,
})
}
return ret, nil
}

0 comments on commit e6cd1c8

Please sign in to comment.