Skip to content

Commit

Permalink
Merge pull request #2095 from traPtitech/fix/hub-payload-panic
Browse files Browse the repository at this point in the history
  • Loading branch information
logica0419 authored Nov 20, 2023
2 parents f9b2909 + a3870e8 commit acfda5c
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 45 deletions.
2 changes: 1 addition & 1 deletion service/bot/event/payload/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type UserGroup struct {
UpdatedAt time.Time `json:"updatedAt"`
}

func MakeUserGroup(group model.UserGroup) UserGroup {
func MakeUserGroup(group *model.UserGroup) UserGroup {
admins := make([]*UserGroupAdmin, len(group.Admins))
for i, admin := range group.Admins {
a := MakeUserGroupAdmin(admin.GroupID, admin.UserID)
Expand Down
2 changes: 1 addition & 1 deletion service/bot/event/payload/ev_user_group_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type UserGroupCreated struct {
Group UserGroup `json:"group"`
}

func MakeUserGroupCreated(eventTime time.Time, group model.UserGroup) *UserGroupCreated {
func MakeUserGroupCreated(eventTime time.Time, group *model.UserGroup) *UserGroupCreated {
return &UserGroupCreated{
Base: MakeBase(eventTime),
Group: MakeUserGroup(group),
Expand Down
5 changes: 2 additions & 3 deletions service/bot/event/payload/ev_user_group_deleted.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"time"

"github.com/gofrs/uuid"
"github.com/traPtitech/traQ/model"
)

// UserGroupDeleted USER_GROUP_DELETEDイベントペイロード
Expand All @@ -13,9 +12,9 @@ type UserGroupDeleted struct {
GroupID uuid.UUID `json:"groupId"`
}

func MakeUserGroupDeleted(eventTime time.Time, group model.UserGroup) *UserGroupDeleted {
func MakeUserGroupDeleted(eventTime time.Time, groupID uuid.UUID) *UserGroupDeleted {
return &UserGroupDeleted{
Base: MakeBase(eventTime),
GroupID: group.ID,
GroupID: groupID,
}
}
2 changes: 1 addition & 1 deletion service/bot/handler/ev_user_group_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func UserGroupCreated(ctx Context, datetime time.Time, _ string, fields hub.Fields) error {
group := fields["group"].(model.UserGroup)
group := fields["group"].(*model.UserGroup)
bots, err := ctx.GetBots(event.UserGroupCreated)
if err != nil {
return fmt.Errorf("failed to GetBots: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions service/bot/handler/ev_user_group_created_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestUserGroupCreated(t *testing.T) {
Status: model.UserAccountStatusActive,
Bot: false,
}
group := model.UserGroup{
group := &model.UserGroup{
ID: uuid.NewV3(uuid.Nil, "g"),
Name: "new_group",
Description: "new_group_description",
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestUserGroupCreated(t *testing.T) {
Status: model.UserAccountStatusActive,
Bot: false,
}
group := model.UserGroup{
group := &model.UserGroup{
ID: uuid.NewV3(uuid.Nil, "g"),
Name: "new_group",
Description: "new_group_description",
Expand Down
6 changes: 3 additions & 3 deletions service/bot/handler/ev_user_group_deleted.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"fmt"
"time"

"github.com/gofrs/uuid"
"github.com/leandro-lugaresi/hub"

"github.com/traPtitech/traQ/model"
"github.com/traPtitech/traQ/service/bot/event"
"github.com/traPtitech/traQ/service/bot/event/payload"
)

func UserGroupDeleted(ctx Context, datetime time.Time, _ string, fields hub.Fields) error {
group := fields["group"].(model.UserGroup)
groupID := fields["group_id"].(uuid.UUID)
bots, err := ctx.GetBots(event.UserGroupDeleted)
if err != nil {
return fmt.Errorf("failed to GetBots: %w", err)
Expand All @@ -23,7 +23,7 @@ func UserGroupDeleted(ctx Context, datetime time.Time, _ string, fields hub.Fiel

if err := ctx.Multicast(
event.UserGroupDeleted,
payload.MakeUserGroupDeleted(datetime, group),
payload.MakeUserGroupDeleted(datetime, groupID),
bots,
); err != nil {
return fmt.Errorf("failed to multicast: %w", err)
Expand Down
40 changes: 6 additions & 34 deletions service/bot/handler/ev_user_group_deleted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,12 @@ func TestUserGroupDeleted(t *testing.T) {
handlerCtx := mock_handler.NewMockContext(ctrl)
registerBot(t, handlerCtx, b)

user := &model.User{
ID: uuid.NewV3(uuid.Nil, "u"),
Name: "new_user",
Status: model.UserAccountStatusActive,
Bot: false,
}
group := model.UserGroup{
ID: uuid.NewV3(uuid.Nil, "g"),
Name: "new_group",
Description: "new_group_description",
Type: "new_group_type",
}
group.Admins = append(group.Admins, &model.UserGroupAdmin{GroupID: group.ID, UserID: user.ID})
group.Members = append(group.Members, &model.UserGroupMember{GroupID: group.ID, UserID: user.ID})
groupID := uuid.NewV3(uuid.Nil, "g")
et := time.Now()

expectMulticast(handlerCtx, event.UserGroupDeleted, payload.MakeUserGroupDeleted(et, group), []*model.Bot{b})
expectMulticast(handlerCtx, event.UserGroupDeleted, payload.MakeUserGroupDeleted(et, groupID), []*model.Bot{b})
assert.NoError(t, UserGroupDeleted(handlerCtx, et, intevent.UserGroupDeleted, hub.Fields{
"group_id": group.ID,
"group": group,
"group_id": groupID,
}))
})

Expand All @@ -68,26 +54,12 @@ func TestUserGroupDeleted(t *testing.T) {
registerBot(t, handlerCtx, b)
registerBot(t, handlerCtx, b2)

user := &model.User{
ID: uuid.NewV3(uuid.Nil, "u"),
Name: "new_user",
Status: model.UserAccountStatusActive,
Bot: false,
}
group := model.UserGroup{
ID: uuid.NewV3(uuid.Nil, "g"),
Name: "new_group",
Description: "new_group_description",
Type: "new_group_type",
}
group.Admins = append(group.Admins, &model.UserGroupAdmin{GroupID: group.ID, UserID: user.ID})
group.Members = append(group.Members, &model.UserGroupMember{GroupID: group.ID, UserID: user.ID})
groupID := uuid.NewV3(uuid.Nil, "g")
et := time.Now()

expectMulticast(handlerCtx, event.UserGroupDeleted, payload.MakeUserGroupDeleted(et, group), []*model.Bot{b})
expectMulticast(handlerCtx, event.UserGroupDeleted, payload.MakeUserGroupDeleted(et, groupID), []*model.Bot{b})
assert.NoError(t, UserGroupDeleted(handlerCtx, et, intevent.UserGroupDeleted, hub.Fields{
"group_id": group.ID,
"group": group,
"group_id": groupID,
}))
})
}

0 comments on commit acfda5c

Please sign in to comment.