-
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.
Add
USER_GROUP_ADMIN_REMOVED
bot event
- Loading branch information
Showing
5 changed files
with
134 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,28 @@ | ||
package payload | ||
|
||
import ( | ||
"github.com/gofrs/uuid" | ||
"time" | ||
) | ||
|
||
// UserGroupAdminRemoved USER_GROUP_ADMIN_REMOVEDイベントペイロード | ||
type UserGroupAdminRemoved struct { | ||
Base | ||
GroupMember struct { | ||
GroupID uuid.UUID `json:"groupId"` | ||
UserID uuid.UUID `json:"userId"` | ||
} `json:"groupMember"` | ||
} | ||
|
||
func MakeUserGroupAdminRemoved(eventTime time.Time, groupID, userID uuid.UUID) *UserGroupAdminRemoved { | ||
return &UserGroupAdminRemoved{ | ||
Base: MakeBase(eventTime), | ||
GroupMember: struct { | ||
GroupID uuid.UUID `json:"groupId"` | ||
UserID uuid.UUID `json:"userId"` | ||
}{ | ||
GroupID: groupID, | ||
UserID: userID, | ||
}, | ||
} | ||
} |
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" | ||
"github.com/gofrs/uuid" | ||
"time" | ||
|
||
"github.com/leandro-lugaresi/hub" | ||
|
||
"github.com/traPtitech/traQ/service/bot/event" | ||
"github.com/traPtitech/traQ/service/bot/event/payload" | ||
) | ||
|
||
func UserGroupAdminRemoved(ctx Context, datetime time.Time, _ string, fields hub.Fields) error { | ||
groupID := fields["group_id"].(uuid.UUID) | ||
userID := fields["user_id"].(uuid.UUID) | ||
bots, err := ctx.GetBots(event.UserGroupAdminRemoved) | ||
if err != nil { | ||
return fmt.Errorf("failed to GetBots: %w", err) | ||
} | ||
if len(bots) == 0 { | ||
return nil | ||
} | ||
|
||
if err := ctx.Multicast( | ||
event.UserGroupAdminRemoved, | ||
payload.MakeUserGroupAdminRemoved(datetime, groupID, userID), | ||
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,69 @@ | ||
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 TestUserGroupAdminRemoved(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.UserGroupAdminRemoved.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) | ||
|
||
userID := uuid.NewV3(uuid.Nil, "u") | ||
groupID := uuid.NewV3(uuid.Nil, "g") | ||
et := time.Now() | ||
|
||
expectMulticast(handlerCtx, event.UserGroupAdminRemoved, payload.MakeUserGroupAdminRemoved(et, groupID, userID), []*model.Bot{b}) | ||
assert.NoError(t, UserGroupAdminRemoved(handlerCtx, et, intevent.UserGroupAdminRemoved, hub.Fields{ | ||
"group_id": groupID, | ||
"user_id": userID, | ||
})) | ||
}) | ||
|
||
t.Run("not subscribe USER_GROUP_ADMIN_REMOVED", func(t *testing.T) { | ||
t.Parallel() | ||
ctrl := gomock.NewController(t) | ||
handlerCtx := mock_handler.NewMockContext(ctrl) | ||
b2 := &model.Bot{ | ||
ID: uuid.NewV3(uuid.Nil, "b2"), | ||
BotUserID: uuid.NewV3(uuid.Nil, "bu2"), | ||
SubscribeEvents: model.BotEventTypesFromArray([]string{event.MessageCreated.String()}), | ||
State: model.BotActive, | ||
} | ||
registerBot(t, handlerCtx, b) | ||
registerBot(t, handlerCtx, b2) | ||
|
||
userID := uuid.NewV3(uuid.Nil, "u") | ||
groupID := uuid.NewV3(uuid.Nil, "g") | ||
et := time.Now() | ||
|
||
expectMulticast(handlerCtx, event.UserGroupAdminRemoved, payload.MakeUserGroupAdminRemoved(et, groupID, userID), []*model.Bot{b}) | ||
assert.NoError(t, UserGroupAdminRemoved(handlerCtx, et, intevent.UserGroupAdminRemoved, hub.Fields{ | ||
"group_id": groupID, | ||
"user_id": userID, | ||
})) | ||
}) | ||
} |
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