-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
USER_GROUP_MEMBER_ADDED
bot event
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package payload | ||
|
||
import ( | ||
"github.com/gofrs/uuid" | ||
"time" | ||
) | ||
|
||
// UserGroupMemberAdded USER_GROUP_MEMBER_ADDEDイベントペイロード | ||
type UserGroupMemberAdded struct { | ||
Base | ||
GroupMember struct { | ||
GroupID uuid.UUID `json:"groupId"` | ||
UserID uuid.UUID `json:"userId"` | ||
} `json:"groupMember"` | ||
} | ||
|
||
func MakeUserGroupMemberAdded(eventTime time.Time, groupID, userID uuid.UUID) *UserGroupMemberAdded { | ||
return &UserGroupMemberAdded{ | ||
Base: MakeBase(eventTime), | ||
GroupMember: struct { | ||
GroupID uuid.UUID `json:"groupId"` | ||
UserID uuid.UUID `json:"userId"` | ||
}{ | ||
GroupID: groupID, | ||
UserID: userID, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package handler | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gofrs/uuid" | ||
"time" | ||
|
||
"github.com/leandro-lugaresi/hub" | ||
|
||
"github.com/traPtitech/traQ/service/bot/event" | ||
"github.com/traPtitech/traQ/service/bot/event/payload" | ||
) | ||
|
||
func UserGroupMemberAdded(ctx Context, datetime time.Time, _ string, fields hub.Fields) error { | ||
groupID := fields["group_id"].(uuid.UUID) | ||
userID := fields["user_id"].(uuid.UUID) | ||
bots, err := ctx.GetBots(event.UserGroupMemberAdded) | ||
if err != nil { | ||
return fmt.Errorf("failed to GetBots: %w", err) | ||
} | ||
if len(bots) == 0 { | ||
return nil | ||
} | ||
|
||
if err := ctx.Multicast( | ||
event.UserGroupMemberAdded, | ||
payload.MakeUserGroupMemberAdded(datetime, groupID, userID), | ||
bots, | ||
); err != nil { | ||
return fmt.Errorf("failed to multicast: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package handler | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/gofrs/uuid" | ||
"github.com/golang/mock/gomock" | ||
"github.com/leandro-lugaresi/hub" | ||
"github.com/stretchr/testify/assert" | ||
|
||
intevent "github.com/traPtitech/traQ/event" | ||
"github.com/traPtitech/traQ/model" | ||
"github.com/traPtitech/traQ/service/bot/event" | ||
"github.com/traPtitech/traQ/service/bot/event/payload" | ||
"github.com/traPtitech/traQ/service/bot/handler/mock_handler" | ||
) | ||
|
||
func TestUserGroupMemberAdded(t *testing.T) { | ||
t.Parallel() | ||
|
||
b := &model.Bot{ | ||
ID: uuid.NewV3(uuid.Nil, "b"), | ||
BotUserID: uuid.NewV3(uuid.Nil, "bu"), | ||
SubscribeEvents: model.BotEventTypesFromArray([]string{event.UserGroupMemberAdded.String()}), | ||
State: model.BotActive, | ||
} | ||
|
||
t.Run("success", func(t *testing.T) { | ||
t.Parallel() | ||
ctrl := gomock.NewController(t) | ||
handlerCtx := mock_handler.NewMockContext(ctrl) | ||
registerBot(t, handlerCtx, b) | ||
|
||
userID := uuid.NewV3(uuid.Nil, "u") | ||
groupID := uuid.NewV3(uuid.Nil, "g") | ||
et := time.Now() | ||
|
||
expectMulticast(handlerCtx, event.UserGroupMemberAdded, payload.MakeUserGroupMemberAdded(et, groupID, userID), []*model.Bot{b}) | ||
assert.NoError(t, UserGroupMemberAdded(handlerCtx, et, intevent.UserGroupMemberAdded, hub.Fields{ | ||
"group_id": groupID, | ||
"user_id": userID, | ||
})) | ||
}) | ||
|
||
t.Run("not subscribe USER_GROUP_MEMBER_ADDED", func(t *testing.T) { | ||
t.Parallel() | ||
ctrl := gomock.NewController(t) | ||
handlerCtx := mock_handler.NewMockContext(ctrl) | ||
b2 := &model.Bot{ | ||
ID: uuid.NewV3(uuid.Nil, "b2"), | ||
BotUserID: uuid.NewV3(uuid.Nil, "bu2"), | ||
SubscribeEvents: model.BotEventTypesFromArray([]string{event.MessageCreated.String()}), | ||
State: model.BotActive, | ||
} | ||
registerBot(t, handlerCtx, b) | ||
registerBot(t, handlerCtx, b2) | ||
|
||
userID := uuid.NewV3(uuid.Nil, "u") | ||
groupID := uuid.NewV3(uuid.Nil, "g") | ||
et := time.Now() | ||
|
||
expectMulticast(handlerCtx, event.UserGroupMemberAdded, payload.MakeUserGroupMemberAdded(et, groupID, userID), []*model.Bot{b}) | ||
assert.NoError(t, UserGroupMemberAdded(handlerCtx, et, intevent.UserGroupMemberAdded, hub.Fields{ | ||
"group_id": groupID, | ||
"user_id": userID, | ||
})) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters