Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/get all reacted users #18

Merged
merged 14 commits into from
Mar 21, 2023
7 changes: 7 additions & 0 deletions app/internal/domain/repository/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ 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)
}
7 changes: 4 additions & 3 deletions app/internal/domain/service/slack_reaction_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,19 @@ func (s *slackReactionUsersService) ListUsersEmailByReaction(ctx context.Context
if err != nil {
return nil, err
}
inviteUserIDs := s.getReactionUserIDs(ctx, msg.Reactions, reactionName)
inviteUserEmails, err := s.chunkedListUsersEmail(ctx, inviteUserIDs)
reactedUserIDs := s.getReactionUserIDs(ctx, msg.Reactions, reactionName)
reactedUserEmails, err := s.chunkedListUsersEmail(ctx, reactedUserIDs)
if err != nil {
return nil, err
}
return inviteUserEmails, nil
return reactedUserEmails, nil
}

// getReactionUserIDs get reaction users by reactionName
func (s *slackReactionUsersService) getReactionUserIDs(ctx context.Context, reactions []*model.SlackReaction, reactionName string) []string {
var userIDs []string
var targetReactions []*model.SlackReaction
// filtered reactions by reactionName
for _, reaction := range reactions {
rn := slack.ExtractReactionName(reaction.Name)
if slack.RemoveSkinToneFromReaction(rn) == reactionName {
Expand Down
2 changes: 2 additions & 0 deletions app/internal/domain/service/slack_reaction_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ func Test_slackReactionUsersService_ListUsersEmailByReaction(t *testing.T) {
{
Name: "join",
UserIDs: []string{"user01", "user02"},
Count: 2,
},
{
Name: "reactionSample",
UserIDs: []string{"user02", "user03"},
Count: 2,
},
},
}
Expand Down
1 change: 1 addition & 0 deletions app/internal/model/slack_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type SlackMessage struct {

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

Expand Down
48 changes: 33 additions & 15 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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こっち pkgslack とかにしとく?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Expand Down Expand Up @@ -55,23 +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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

変数宣言と使用箇所が無駄に離れてる

Suggested change
}
for _, reaction := range parentMessage.Reactions {
}
var reactions []*model.SlackReaction
for _, reaction := range parentMessage.Reactions {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

対応しておきました move reactions(var)

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,
})
// 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 Down
10 changes: 10 additions & 0 deletions app/pkg/slack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Client interface {
PostEphemeral(ctx context.Context, channelID, userID, ts, message string) error
GetConversationReplies(ctx context.Context, channelID, ts string) ([]slack.Message, error)
GetUsersInfo(ctx context.Context, userID ...string) (*[]slack.User, error)
GetReaction(ctx context.Context, channelID, ts string, full bool) ([]slack.ItemReaction, error)

GetClient() *slack.Client
GetAppUserID() string
Expand Down Expand Up @@ -118,6 +119,15 @@ func (c *client) GetUsersInfo(ctx context.Context, userID ...string) (*[]slack.U
return users, nil
}

func (c *client) GetReaction(ctx context.Context, channelID, ts string, full bool) ([]slack.ItemReaction, error) {
return c.Client.GetReactionsContext(ctx, slack.ItemRef{
Channel: channelID,
Timestamp: ts,
}, slack.GetReactionsParameters{
Full: full,
})
}

func (c *client) GetClient() *slack.Client {
return c.Client
}
Expand Down