Skip to content

Commit d2d44de

Browse files
committed
USER_ACTIVATEDイベントを追加
1 parent 36da236 commit d2d44de

File tree

7 files changed

+121
-1
lines changed

7 files changed

+121
-1
lines changed

event/topic.go

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const (
2424
// user_id: uuid.UUID
2525
// datetime: time.Time
2626
UserOffline = "user.offline"
27+
// UserActivated ユーザーの凍結が解除された
28+
// Fields:
29+
// user: *model.User
30+
UserActivated = "user.activated"
2731
// UserViewStateChanged ユーザーの閲覧状態が変化した
2832
// Fields:
2933
// user_id: uuid.UUID

repository/gorm/user.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,15 @@ func (r *userRepository) UpdateUser(id uuid.UUID, args repository.UpdateUserArgs
217217
if id == uuid.Nil {
218218
return repository.ErrNilID
219219
}
220+
var u model.User
221+
220222
var (
223+
activate bool
221224
deactivate bool
222225
changed bool
223226
count int
224227
)
225228
err := r.db.Transaction(func(tx *gorm.DB) error {
226-
var u model.User
227229
if err := tx.Preload("Profile").First(&u, model.User{ID: id}).Error; err != nil {
228230
return convertError(err)
229231
}
@@ -239,6 +241,8 @@ func (r *userRepository) UpdateUser(id uuid.UUID, args repository.UpdateUserArgs
239241
changes["status"] = args.UserState.V.Int()
240242
if args.UserState.V.Int() == model.UserAccountStatusDeactivated.Int() {
241243
deactivate = true
244+
} else if args.UserState.V.Int() == model.UserAccountStatusActive.Int() {
245+
activate = true
242246
}
243247
}
244248
if args.IconFileID.Valid {
@@ -312,6 +316,13 @@ func (r *userRepository) UpdateUser(id uuid.UUID, args repository.UpdateUserArgs
312316
"file_id": args.IconFileID.V,
313317
},
314318
})
319+
} else if activate {
320+
r.hub.Publish(hub.Message{
321+
Name: event.UserActivated,
322+
Fields: hub.Fields{
323+
"user": &u,
324+
},
325+
})
315326
} else {
316327
r.hub.Publish(hub.Message{
317328
Name: event.UserUpdated,

service/bot/event/events.go

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const (
3131
ChannelTopicChanged model.BotEventType = "CHANNEL_TOPIC_CHANGED"
3232
// UserCreated ユーザー作成イベント
3333
UserCreated model.BotEventType = "USER_CREATED"
34+
// UserActivated ユーザー凍結解除イベント
35+
UserActivated model.BotEventType = "USER_ACTIVATED"
3436
// StampCreated スタンプ作成イベント
3537
StampCreated model.BotEventType = "STAMP_CREATED"
3638
// TagAdded タグ追加イベント
@@ -75,6 +77,7 @@ func init() {
7577
ChannelCreated,
7678
ChannelTopicChanged,
7779
UserCreated,
80+
UserActivated,
7881
StampCreated,
7982
TagAdded,
8083
TagRemoved,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package payload
2+
3+
import (
4+
"time"
5+
6+
"github.com/traPtitech/traQ/model"
7+
)
8+
9+
// UserActivated USER_ACTIVATEDイベントペイロード
10+
type UserActivated struct {
11+
Base
12+
User User `json:"user"`
13+
}
14+
15+
func MakeUserActivated(et time.Time, user model.UserInfo) *UserActivated {
16+
return &UserActivated{
17+
Base: MakeBase(et),
18+
User: MakeUser(user),
19+
}
20+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package handler
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/leandro-lugaresi/hub"
8+
9+
"github.com/traPtitech/traQ/model"
10+
"github.com/traPtitech/traQ/service/bot/event"
11+
"github.com/traPtitech/traQ/service/bot/event/payload"
12+
)
13+
14+
func UserActivated(ctx Context, datetime time.Time, _ string, fields hub.Fields) error {
15+
user := fields["user"].(model.UserInfo)
16+
17+
bots, err := ctx.GetBots(event.UserActivated)
18+
if err != nil {
19+
return fmt.Errorf("failed to GetBots: %w", err)
20+
}
21+
if len(bots) == 0 {
22+
return nil
23+
}
24+
25+
if err := ctx.Multicast(
26+
event.UserActivated,
27+
payload.MakeUserActivated(datetime, user),
28+
bots,
29+
); err != nil {
30+
return fmt.Errorf("failed to multicast: %w", err)
31+
}
32+
return nil
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package handler
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/gofrs/uuid"
8+
"github.com/golang/mock/gomock"
9+
"github.com/leandro-lugaresi/hub"
10+
"github.com/stretchr/testify/assert"
11+
12+
intevent "github.com/traPtitech/traQ/event"
13+
"github.com/traPtitech/traQ/model"
14+
"github.com/traPtitech/traQ/service/bot/event"
15+
"github.com/traPtitech/traQ/service/bot/event/payload"
16+
"github.com/traPtitech/traQ/service/bot/handler/mock_handler"
17+
)
18+
19+
func TestUserActivated(t *testing.T) {
20+
t.Parallel()
21+
22+
b := &model.Bot{
23+
ID: uuid.NewV3(uuid.Nil, "b"),
24+
BotUserID: uuid.NewV3(uuid.Nil, "bu"),
25+
SubscribeEvents: model.BotEventTypesFromArray([]string{event.UserActivated.String()}),
26+
State: model.BotActive,
27+
}
28+
29+
t.Run("success", func(t *testing.T) {
30+
t.Parallel()
31+
ctrl := gomock.NewController(t)
32+
handlerCtx := mock_handler.NewMockContext(ctrl)
33+
registerBot(t, handlerCtx, b)
34+
35+
user := &model.User{
36+
ID: uuid.NewV3(uuid.Nil, "u"),
37+
Name: "activated_user",
38+
Status: model.UserAccountStatusActive,
39+
Bot: false,
40+
}
41+
et := time.Now()
42+
43+
expectMulticast(handlerCtx, event.UserActivated, payload.MakeUserActivated(et, user), []*model.Bot{b})
44+
assert.NoError(t, UserActivated(handlerCtx, et, intevent.UserActivated, hub.Fields{
45+
"user": user,
46+
}))
47+
})
48+
}

service/bot/handlers.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var eventHandlerSet = map[string]eventHandler{
1919
intevent.MessageDeleted: handler.MessageDeleted,
2020
intevent.MessageUpdated: handler.MessageUpdated,
2121
intevent.UserCreated: handler.UserCreated,
22+
intevent.UserActivated: handler.UserActivated,
2223
intevent.ChannelCreated: handler.ChannelCreated,
2324
intevent.ChannelTopicUpdated: handler.ChannelTopicUpdated,
2425
intevent.StampCreated: handler.StampCreated,

0 commit comments

Comments
 (0)