-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package payload | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/traPtitech/traQ/model" | ||
) | ||
|
||
// UserGroupUpdated USER_GROUP_UPDATEDイベントペイロード | ||
type UserGroupUpdated struct { | ||
Base | ||
Group model.UserGroup `json:"group"` | ||
} | ||
|
||
func MakeUserGroupUpdated(eventTime time.Time, group model.UserGroup) *UserGroupUpdated { | ||
return &UserGroupUpdated{ | ||
Base: MakeBase(eventTime), | ||
Group: group, | ||
} | ||
} |
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,32 @@ | ||
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 UserGroupUpdated(ctx Context, datetime time.Time, _ string, fields hub.Fields) error { | ||
group := fields["group"].(model.UserGroup) | ||
bots, err := ctx.GetBots(event.UserGroupUpdated) | ||
if err != nil { | ||
return fmt.Errorf("failed to GetBots: %w", err) | ||
} | ||
if len(bots) == 0 { | ||
return nil | ||
} | ||
|
||
if err := ctx.Multicast( | ||
event.UserGroupUpdated, | ||
payload.MakeUserGroupUpdated(datetime, group), | ||
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,57 @@ | ||
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 TestUserGroupUpdated(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.UserGroupUpdated.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: "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}) | ||
et := time.Now() | ||
|
||
expectMulticast(handlerCtx, event.UserGroupUpdated, payload.MakeUserGroupUpdated(et, group), []*model.Bot{b}) | ||
assert.NoError(t, UserGroupUpdated(handlerCtx, et, intevent.UserGroupUpdated, hub.Fields{ | ||
"group_id": group.ID, | ||
"group": group, | ||
})) | ||
}) | ||
} |
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