-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2266 from traPtitech/feat/bot_event_user_activated
USER_ACTIVATEDイベントを追加
- Loading branch information
Showing
7 changed files
with
145 additions
and
20 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
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,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), | ||
} | ||
} |
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" | ||
"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 | ||
} |
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,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, | ||
})) | ||
}) | ||
} |
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