Skip to content

Commit

Permalink
fix implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sho0126hiro committed Mar 19, 2023
1 parent 0ba9d0e commit a5856d2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 57 deletions.
4 changes: 0 additions & 4 deletions app/internal/domain/repository/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,4 @@ type SlackRepository interface {

// 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)
}
21 changes: 0 additions & 21 deletions app/internal/domain/service/slack_reaction_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ func (s *slackReactionUsersService) ListUsersEmailByReaction(ctx context.Context
if err != nil {
return nil, err
}
if s.isReactionRefetchNeeded(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 @@ -82,17 +76,6 @@ func (s *slackReactionUsersService) ListUsersEmailByReaction(ctx context.Context
return reactedUserEmails, nil
}

// isReactionRefetchNeeded returns true if more fetches is required
// reactions[*].Count may be greater than len(reactions[*].UserIDs), at which point a fetch is required.
func (s *slackReactionUsersService) isReactionRefetchNeeded(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 @@ -112,7 +95,3 @@ 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)
}
65 changes: 33 additions & 32 deletions app/internal/repository/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package repository

import (
"context"
"fmt"

slack2 "github.com/slack-go/slack"

"github.com/moneyforward/auriga/app/pkg/slack"

Expand Down Expand Up @@ -55,24 +56,40 @@ func (r *slackRepository) GetParentMessage(ctx context.Context, channelID, ts st
return nil, err
}
}
if len(msgs) <= 0 {
return nil, errors.New("number of messages is zero")
}
parentMessage := msgs[0]
var reactions []*model.SlackReaction
if r.isIncompleteReaction(parentMessage.Reactions) {
// get full reactions
parentMessage.Reactions, err = r.client.GetReaction(ctx, channelID, ts, true)
if err != nil {
return nil, err
}
}
for _, reaction := range parentMessage.Reactions {
reactions = append(reactions, &model.SlackReaction{
Name: reaction.Name,
UserIDs: reaction.Users,
Count: reaction.Count,
})
}
return &model.SlackMessage{
ChannelID: parentMessage.Channel,
Reactions: reactions,
}, nil
}

if len(msgs) > 0 {
fmt.Printf("%#v \n", msgs[0])
parentMessage := msgs[0]
var reactions []*model.SlackReaction
for _, reaction := range parentMessage.Reactions {
reactions = append(reactions, &model.SlackReaction{
Name: reaction.Name,
UserIDs: reaction.Users,
Count: reaction.Count,
})
// isIncompleteReaction returns true if more fetches is required
// reactions[*].Count may be greater than len(reactions[*].Users), at which point a fetch is required.
func (r *slackRepository) isIncompleteReaction(reactions []slack2.ItemReaction) bool {
for _, reaction := range reactions {
if reaction.Count > len(reaction.Users) {
return true
}
return &model.SlackMessage{
ChannelID: parentMessage.Channel,
Reactions: reactions,
}, nil
}
return nil, errors.New("number of messages is zero")
return false
}

func (r *slackRepository) ListUsersEmail(ctx context.Context, userID []string) ([]*model.SlackUserEmail, error) {
Expand All @@ -94,19 +111,3 @@ 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 a5856d2

Please sign in to comment.