From d2d44de3bd662c0903db51cbfcb3c212b93dbfd4 Mon Sep 17 00:00:00 2001 From: mehm8128 Date: Tue, 16 Jan 2024 23:20:30 +0900 Subject: [PATCH 1/3] =?UTF-8?q?USER=5FACTIVATED=E3=82=A4=E3=83=99=E3=83=B3?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- event/topic.go | 4 ++ repository/gorm/user.go | 13 ++++- service/bot/event/events.go | 3 ++ .../bot/event/payload/ev_user_activated.go | 20 ++++++++ service/bot/handler/ev_user_activated.go | 33 +++++++++++++ service/bot/handler/ev_user_activated_test.go | 48 +++++++++++++++++++ service/bot/handlers.go | 1 + 7 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 service/bot/event/payload/ev_user_activated.go create mode 100644 service/bot/handler/ev_user_activated.go create mode 100644 service/bot/handler/ev_user_activated_test.go diff --git a/event/topic.go b/event/topic.go index cf70dd3be..c32e444d8 100644 --- a/event/topic.go +++ b/event/topic.go @@ -24,6 +24,10 @@ const ( // user_id: uuid.UUID // datetime: time.Time UserOffline = "user.offline" + // UserActivated ユーザーの凍結が解除された + // Fields: + // user: *model.User + UserActivated = "user.activated" // UserViewStateChanged ユーザーの閲覧状態が変化した // Fields: // user_id: uuid.UUID diff --git a/repository/gorm/user.go b/repository/gorm/user.go index ae9e89c30..7dd9b7240 100644 --- a/repository/gorm/user.go +++ b/repository/gorm/user.go @@ -217,13 +217,15 @@ func (r *userRepository) UpdateUser(id uuid.UUID, args repository.UpdateUserArgs if id == uuid.Nil { return repository.ErrNilID } + var u model.User + var ( + activate bool deactivate bool changed bool count int ) err := r.db.Transaction(func(tx *gorm.DB) error { - var u model.User if err := tx.Preload("Profile").First(&u, model.User{ID: id}).Error; err != nil { return convertError(err) } @@ -239,6 +241,8 @@ func (r *userRepository) UpdateUser(id uuid.UUID, args repository.UpdateUserArgs changes["status"] = args.UserState.V.Int() if args.UserState.V.Int() == model.UserAccountStatusDeactivated.Int() { deactivate = true + } else if args.UserState.V.Int() == model.UserAccountStatusActive.Int() { + activate = true } } if args.IconFileID.Valid { @@ -312,6 +316,13 @@ func (r *userRepository) UpdateUser(id uuid.UUID, args repository.UpdateUserArgs "file_id": args.IconFileID.V, }, }) + } else if activate { + r.hub.Publish(hub.Message{ + Name: event.UserActivated, + Fields: hub.Fields{ + "user": &u, + }, + }) } else { r.hub.Publish(hub.Message{ Name: event.UserUpdated, diff --git a/service/bot/event/events.go b/service/bot/event/events.go index 0055f24de..67606a960 100644 --- a/service/bot/event/events.go +++ b/service/bot/event/events.go @@ -31,6 +31,8 @@ const ( ChannelTopicChanged model.BotEventType = "CHANNEL_TOPIC_CHANGED" // UserCreated ユーザー作成イベント UserCreated model.BotEventType = "USER_CREATED" + // UserActivated ユーザー凍結解除イベント + UserActivated model.BotEventType = "USER_ACTIVATED" // StampCreated スタンプ作成イベント StampCreated model.BotEventType = "STAMP_CREATED" // TagAdded タグ追加イベント @@ -75,6 +77,7 @@ func init() { ChannelCreated, ChannelTopicChanged, UserCreated, + UserActivated, StampCreated, TagAdded, TagRemoved, diff --git a/service/bot/event/payload/ev_user_activated.go b/service/bot/event/payload/ev_user_activated.go new file mode 100644 index 000000000..8cedd199c --- /dev/null +++ b/service/bot/event/payload/ev_user_activated.go @@ -0,0 +1,20 @@ +package payload + +import ( + "time" + + "github.com/traPtitech/traQ/model" +) + +// UserActivated USER_ACTIVATEDイベントペイロード +type UserActivated struct { + Base + User User `json:"user"` +} + +func MakeUserActivated(et time.Time, user model.UserInfo) *UserActivated { + return &UserActivated{ + Base: MakeBase(et), + User: MakeUser(user), + } +} diff --git a/service/bot/handler/ev_user_activated.go b/service/bot/handler/ev_user_activated.go new file mode 100644 index 000000000..b6344ab78 --- /dev/null +++ b/service/bot/handler/ev_user_activated.go @@ -0,0 +1,33 @@ +package handler + +import ( + "fmt" + "time" + + "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 UserActivated(ctx Context, datetime time.Time, _ string, fields hub.Fields) error { + user := fields["user"].(model.UserInfo) + + bots, err := ctx.GetBots(event.UserActivated) + if err != nil { + return fmt.Errorf("failed to GetBots: %w", err) + } + if len(bots) == 0 { + return nil + } + + if err := ctx.Multicast( + event.UserActivated, + payload.MakeUserActivated(datetime, user), + bots, + ); err != nil { + return fmt.Errorf("failed to multicast: %w", err) + } + return nil +} diff --git a/service/bot/handler/ev_user_activated_test.go b/service/bot/handler/ev_user_activated_test.go new file mode 100644 index 000000000..96f84aa56 --- /dev/null +++ b/service/bot/handler/ev_user_activated_test.go @@ -0,0 +1,48 @@ +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 TestUserActivated(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.UserActivated.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) + + user := &model.User{ + ID: uuid.NewV3(uuid.Nil, "u"), + Name: "activated_user", + Status: model.UserAccountStatusActive, + Bot: false, + } + et := time.Now() + + expectMulticast(handlerCtx, event.UserActivated, payload.MakeUserActivated(et, user), []*model.Bot{b}) + assert.NoError(t, UserActivated(handlerCtx, et, intevent.UserActivated, hub.Fields{ + "user": user, + })) + }) +} diff --git a/service/bot/handlers.go b/service/bot/handlers.go index d3c8a161b..3676c247f 100644 --- a/service/bot/handlers.go +++ b/service/bot/handlers.go @@ -19,6 +19,7 @@ var eventHandlerSet = map[string]eventHandler{ intevent.MessageDeleted: handler.MessageDeleted, intevent.MessageUpdated: handler.MessageUpdated, intevent.UserCreated: handler.UserCreated, + intevent.UserActivated: handler.UserActivated, intevent.ChannelCreated: handler.ChannelCreated, intevent.ChannelTopicUpdated: handler.ChannelTopicUpdated, intevent.StampCreated: handler.StampCreated, From b47d3ae1e2d3efd9d6831fb954335b77aee4cbce Mon Sep 17 00:00:00 2001 From: mehm8128 Date: Tue, 23 Jan 2024 12:34:41 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF?= =?UTF-8?q?=E3=82=BF=E3=83=AA=E3=83=B3=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- repository/gorm/user.go | 60 ++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/repository/gorm/user.go b/repository/gorm/user.go index 7dd9b7240..645f69697 100644 --- a/repository/gorm/user.go +++ b/repository/gorm/user.go @@ -239,10 +239,11 @@ func (r *userRepository) UpdateUser(id uuid.UUID, args repository.UpdateUserArgs } if args.UserState.Valid { changes["status"] = args.UserState.V.Int() - if args.UserState.V.Int() == model.UserAccountStatusDeactivated.Int() { - deactivate = true - } else if args.UserState.V.Int() == model.UserAccountStatusActive.Int() { + switch args.UserState.V.Int() { + case model.UserAccountStatusActive.Int(): activate = true + case model.UserAccountStatusDeactivated.Int(): + deactivate = true } } if args.IconFileID.Valid { @@ -306,31 +307,34 @@ func (r *userRepository) UpdateUser(id uuid.UUID, args repository.UpdateUserArgs if args.LastOnline.Valid && count == 1 { return nil // 最終オンライン日時のみの更新の時はUserUpdatedイベントを発生させない } - if changed { - r.forgetCache(id) - if args.IconFileID.Valid && count == 1 { - r.hub.Publish(hub.Message{ - Name: event.UserIconUpdated, - Fields: hub.Fields{ - "user_id": id, - "file_id": args.IconFileID.V, - }, - }) - } else if activate { - r.hub.Publish(hub.Message{ - Name: event.UserActivated, - Fields: hub.Fields{ - "user": &u, - }, - }) - } else { - r.hub.Publish(hub.Message{ - Name: event.UserUpdated, - Fields: hub.Fields{ - "user_id": id, - }, - }) - } + if !changed { + return nil + } + + r.forgetCache(id) + switch { + case args.IconFileID.Valid && count == 1: + r.hub.Publish(hub.Message{ + Name: event.UserIconUpdated, + Fields: hub.Fields{ + "user_id": id, + "file_id": args.IconFileID.V, + }, + }) + case activate: + r.hub.Publish(hub.Message{ + Name: event.UserActivated, + Fields: hub.Fields{ + "user": &u, + }, + }) + default: + r.hub.Publish(hub.Message{ + Name: event.UserUpdated, + Fields: hub.Fields{ + "user_id": id, + }, + }) } return nil } From 00d504c98a650aa6108084e7c22b3a298a289b72 Mon Sep 17 00:00:00 2001 From: mehm8128 Date: Tue, 23 Jan 2024 15:41:42 +0900 Subject: [PATCH 3/3] =?UTF-8?q?activate=E3=81=AE=E3=81=A8=E3=81=8D?= =?UTF-8?q?=E3=82=82update=E3=82=A4=E3=83=99=E3=83=B3=E3=83=88=E3=82=92?= =?UTF-8?q?=E7=99=BA=E8=A1=8C=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- repository/gorm/user.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/repository/gorm/user.go b/repository/gorm/user.go index 645f69697..ae2c9ae8e 100644 --- a/repository/gorm/user.go +++ b/repository/gorm/user.go @@ -217,9 +217,9 @@ func (r *userRepository) UpdateUser(id uuid.UUID, args repository.UpdateUserArgs if id == uuid.Nil { return repository.ErrNilID } - var u model.User var ( + u model.User activate bool deactivate bool changed bool @@ -328,6 +328,7 @@ func (r *userRepository) UpdateUser(id uuid.UUID, args repository.UpdateUserArgs "user": &u, }, }) + fallthrough default: r.hub.Publish(hub.Message{ Name: event.UserUpdated,