From bfa2796a30bf42d48325ccc96f64f1f1294df320 Mon Sep 17 00:00:00 2001 From: logica0419 <84759675+logica0419@users.noreply.github.com> Date: Mon, 20 Nov 2023 19:28:53 +0900 Subject: [PATCH 1/2] fix group_create type cast --- service/bot/event/payload/common.go | 2 +- service/bot/event/payload/ev_user_group_created.go | 2 +- service/bot/handler/ev_user_group_created.go | 2 +- service/bot/handler/ev_user_group_created_test.go | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/service/bot/event/payload/common.go b/service/bot/event/payload/common.go index d2dd1ede7..8912f8254 100644 --- a/service/bot/event/payload/common.go +++ b/service/bot/event/payload/common.go @@ -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) diff --git a/service/bot/event/payload/ev_user_group_created.go b/service/bot/event/payload/ev_user_group_created.go index 8ad53d79b..e35996afa 100644 --- a/service/bot/event/payload/ev_user_group_created.go +++ b/service/bot/event/payload/ev_user_group_created.go @@ -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), diff --git a/service/bot/handler/ev_user_group_created.go b/service/bot/handler/ev_user_group_created.go index f7eeb4a3d..c2b1bd5cb 100644 --- a/service/bot/handler/ev_user_group_created.go +++ b/service/bot/handler/ev_user_group_created.go @@ -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) diff --git a/service/bot/handler/ev_user_group_created_test.go b/service/bot/handler/ev_user_group_created_test.go index 7dab84dd5..e1a3a43ff 100644 --- a/service/bot/handler/ev_user_group_created_test.go +++ b/service/bot/handler/ev_user_group_created_test.go @@ -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", @@ -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", From a3870e8fd20b1feb509622554e16ba28e70c4401 Mon Sep 17 00:00:00 2001 From: logica0419 <84759675+logica0419@users.noreply.github.com> Date: Mon, 20 Nov 2023 19:32:55 +0900 Subject: [PATCH 2/2] fix group_deleted type cast --- .../event/payload/ev_user_group_deleted.go | 5 +-- service/bot/handler/ev_user_group_deleted.go | 6 +-- .../bot/handler/ev_user_group_deleted_test.go | 40 +++---------------- 3 files changed, 11 insertions(+), 40 deletions(-) diff --git a/service/bot/event/payload/ev_user_group_deleted.go b/service/bot/event/payload/ev_user_group_deleted.go index a554b064b..927cf02c1 100644 --- a/service/bot/event/payload/ev_user_group_deleted.go +++ b/service/bot/event/payload/ev_user_group_deleted.go @@ -4,7 +4,6 @@ import ( "time" "github.com/gofrs/uuid" - "github.com/traPtitech/traQ/model" ) // UserGroupDeleted USER_GROUP_DELETEDイベントペイロード @@ -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, } } diff --git a/service/bot/handler/ev_user_group_deleted.go b/service/bot/handler/ev_user_group_deleted.go index 927e17cf9..4d4ff671b 100644 --- a/service/bot/handler/ev_user_group_deleted.go +++ b/service/bot/handler/ev_user_group_deleted.go @@ -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) @@ -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) diff --git a/service/bot/handler/ev_user_group_deleted_test.go b/service/bot/handler/ev_user_group_deleted_test.go index cd51ce3d7..c38a46380 100644 --- a/service/bot/handler/ev_user_group_deleted_test.go +++ b/service/bot/handler/ev_user_group_deleted_test.go @@ -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, })) }) @@ -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, })) }) }