Skip to content

Commit

Permalink
update webhook implement contents.
Browse files Browse the repository at this point in the history
  • Loading branch information
mo3et committed Jul 30, 2024
1 parent ba170ba commit a386110
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 65 deletions.
62 changes: 23 additions & 39 deletions internal/rpc/group/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,32 +101,7 @@ func (s *groupServer) webhookAfterCreateGroup(ctx context.Context, after *config
s.webhookClient.AsyncPost(ctx, cbReq.GetCallbackCommand(), cbReq, &callbackstruct.CallbackAfterCreateGroupResp{}, after)
}

func (s *groupServer) webhookBeforeMemberJoinGroup(ctx context.Context, before *config.BeforeConfig, groupMember *model.GroupMember, groupEx string) error {
return webhook.WithCondition(ctx, before, func(ctx context.Context) error {
cbReq := &callbackstruct.CallbackBeforeMemberJoinGroupReq{
CallbackCommand: callbackstruct.CallbackBeforeMemberJoinGroupCommand,
GroupID: groupMember.GroupID,
UserID: groupMember.UserID,
Ex: groupMember.Ex,
GroupEx: groupEx,
}
resp := &callbackstruct.CallbackBeforeMemberJoinGroupResp{}
if err := s.webhookClient.SyncPost(ctx, cbReq.GetCallbackCommand(), cbReq, resp, before); err != nil {
return err
}

if resp.MuteEndTime != nil {
groupMember.MuteEndTime = time.UnixMilli(*resp.MuteEndTime)
}
datautil.NotNilReplace(&groupMember.FaceURL, resp.FaceURL)
datautil.NotNilReplace(&groupMember.Ex, resp.Ex)
datautil.NotNilReplace(&groupMember.Nickname, resp.Nickname)
datautil.NotNilReplace(&groupMember.RoleLevel, resp.RoleLevel)
return nil
})
}

func (s *groupServer) webhookBeforeMemberJoinGroupBatch(ctx context.Context, before *config.BeforeConfig, groupMembers []*model.GroupMember, groupEx string) error {
func (s *groupServer) webhookBeforeMembersJoinGroup(ctx context.Context, before *config.BeforeConfig, groupMembers []*model.GroupMember, groupEx string) error {
return webhook.WithCondition(ctx, before, func(ctx context.Context) error {
groupMembersMap := datautil.SliceToMap(groupMembers, func(e *model.GroupMember) string {
return e.UserID
Expand All @@ -135,27 +110,33 @@ func (s *groupServer) webhookBeforeMemberJoinGroupBatch(ctx context.Context, bef

for _, member := range groupMembers {
groupMembersCallback = append(groupMembersCallback, &callbackstruct.CallbackGroupMember{
UserID: member.UserID,
Ex: member.Ex,
GroupEx: groupEx,
UserID: member.UserID,
Ex: member.Ex,
})
}

cbReq := &callbackstruct.CallbackBeforeMemberJoinGroupBatchReq{
cbReq := &callbackstruct.CallbackBeforeMembersJoinGroupReq{
CallbackCommand: callbackstruct.CallbackBeforeMemberJoinGroupBatchCommand,
MembersList: groupMembersCallback,
GroupEx: groupEx,
}
resp := &callbackstruct.CallbackBeforeMemberJoinGroupBatchResp{}
resp := &callbackstruct.CallbackBeforeMembersJoinGroupResp{}

if err := s.webhookClient.SyncPost(ctx, cbReq.GetCallbackCommand(), cbReq, resp, before); err != nil {
return err
}

for _, memberCallbackResp := range resp.MemberCallbacks {
datautil.NotNilReplace(&groupMembersMap[(*memberCallbackResp.UserID)].FaceURL, memberCallbackResp.FaceURL)
datautil.NotNilReplace(&groupMembersMap[(*memberCallbackResp.UserID)].Ex, memberCallbackResp.Ex)
datautil.NotNilReplace(&groupMembersMap[(*memberCallbackResp.UserID)].Nickname, memberCallbackResp.Nickname)
datautil.NotNilReplace(&groupMembersMap[(*memberCallbackResp.UserID)].RoleLevel, memberCallbackResp.RoleLevel)
if _, ok := groupMembersMap[(*memberCallbackResp.UserID)]; ok {
if memberCallbackResp.MuteEndTime != nil {
groupMembersMap[(*memberCallbackResp.UserID)].MuteEndTime = time.UnixMilli(*memberCallbackResp.MuteEndTime)
}

datautil.NotNilReplace(&groupMembersMap[(*memberCallbackResp.UserID)].FaceURL, memberCallbackResp.FaceURL)
datautil.NotNilReplace(&groupMembersMap[(*memberCallbackResp.UserID)].Ex, memberCallbackResp.Ex)
datautil.NotNilReplace(&groupMembersMap[(*memberCallbackResp.UserID)].Nickname, memberCallbackResp.Nickname)
datautil.NotNilReplace(&groupMembersMap[(*memberCallbackResp.UserID)].RoleLevel, memberCallbackResp.RoleLevel)
}
}

return nil
Expand Down Expand Up @@ -281,10 +262,13 @@ func (s *groupServer) webhookBeforeInviteUserToGroup(ctx context.Context, before
return err
}

if len(resp.RefusedMembersAccount) > 0 {
// Handle the scenario where certain members are refused
// You might want to update the req.Members list or handle it as per your business logic
}
// Handle the scenario where certain members are refused
// You might want to update the req.Members list or handle it as per your business logic

// if len(resp.RefusedMembersAccount) > 0 {
// implement members are refused
// }

return nil
})
}
Expand Down
9 changes: 5 additions & 4 deletions internal/rpc/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbgroup.CreateGroupR
joinGroupFunc(userID, constant.GroupOrdinaryUsers)
}

if err := s.webhookBeforeMemberJoinGroupBatch(ctx, &s.config.WebhooksConfig.BeforeMemberJoinGroup, groupMembers, group.Ex); err != nil && err != servererrs.ErrCallbackContinue {
if err := s.webhookBeforeMembersJoinGroup(ctx, &s.config.WebhooksConfig.BeforeMemberJoinGroup, groupMembers, group.Ex); err != nil && err != servererrs.ErrCallbackContinue {
return nil, err
}

Expand Down Expand Up @@ -443,7 +443,7 @@ func (s *groupServer) InviteUserToGroup(ctx context.Context, req *pbgroup.Invite
groupMembers = append(groupMembers, member)
}

if err := s.webhookBeforeMemberJoinGroupBatch(ctx, &s.config.WebhooksConfig.BeforeMemberJoinGroup, groupMembers, group.Ex); err != nil && err != servererrs.ErrCallbackContinue {
if err := s.webhookBeforeMembersJoinGroup(ctx, &s.config.WebhooksConfig.BeforeMemberJoinGroup, groupMembers, group.Ex); err != nil && err != servererrs.ErrCallbackContinue {
return nil, err
}

Expand Down Expand Up @@ -811,7 +811,8 @@ func (s *groupServer) GroupApplicationResponse(ctx context.Context, req *pbgroup
InviterUserID: groupRequest.InviterUserID,
OperatorUserID: mcontext.GetOpUserID(ctx),
}
if err := s.webhookBeforeMemberJoinGroup(ctx, &s.config.WebhooksConfig.BeforeMemberJoinGroup, member, group.Ex); err != nil && err != servererrs.ErrCallbackContinue {

if err := s.webhookBeforeMembersJoinGroup(ctx, &s.config.WebhooksConfig.BeforeMemberJoinGroup, []*model.GroupMember{member}, group.Ex); err != nil && err != servererrs.ErrCallbackContinue {
return nil, err
}
}
Expand Down Expand Up @@ -880,7 +881,7 @@ func (s *groupServer) JoinGroup(ctx context.Context, req *pbgroup.JoinGroupReq)
MuteEndTime: time.UnixMilli(0),
}

if err := s.webhookBeforeMemberJoinGroup(ctx, &s.config.WebhooksConfig.BeforeMemberJoinGroup, groupMember, group.Ex); err != nil && err != servererrs.ErrCallbackContinue {
if err := s.webhookBeforeMembersJoinGroup(ctx, &s.config.WebhooksConfig.BeforeMemberJoinGroup, []*model.GroupMember{groupMember}, group.Ex); err != nil && err != servererrs.ErrCallbackContinue {
return nil, err
}

Expand Down
28 changes: 6 additions & 22 deletions pkg/callbackstruct/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,15 @@ type CallbackAfterCreateGroupResp struct {
CommonCallbackResp
}

type CallbackBeforeMemberJoinGroupReq struct {
CallbackCommand `json:"callbackCommand"`
GroupID string `json:"groupID"`
UserID string `json:"userID"`
Ex string `json:"ex"`
GroupEx string `json:"groupEx"`
}

type CallbackGroupMember struct {
UserID string `json:"userID"`
Ex string `json:"ex"`
GroupEx string `json:"groupEx"`
UserID string `json:"userID"`
Ex string `json:"ex"`
}

type CallbackBeforeMemberJoinGroupBatchReq struct {
type CallbackBeforeMembersJoinGroupReq struct {
CallbackCommand `json:"callbackCommand"`
MembersList []*CallbackGroupMember
GroupEx string `json:"groupEx"`
}

type MemberJoinGroupCallBack struct {
Expand All @@ -86,18 +78,10 @@ type MemberJoinGroupCallBack struct {
MuteEndTime *int64 `json:"muteEndTime"`
Ex *string `json:"ex"`
}
type CallbackBeforeMemberJoinGroupBatchResp struct {
CommonCallbackResp
MemberCallbacks []*MemberJoinGroupCallBack
}

type CallbackBeforeMemberJoinGroupResp struct {
type CallbackBeforeMembersJoinGroupResp struct {
CommonCallbackResp
Nickname *string `json:"nickname"`
FaceURL *string `json:"faceURL"`
RoleLevel *int32 `json:"roleLevel"`
MuteEndTime *int64 `json:"muteEndTime"`
Ex *string `json:"ex"`
MemberCallbacks []*MemberJoinGroupCallBack
}

type CallbackBeforeSetGroupMemberInfoReq struct {
Expand Down

0 comments on commit a386110

Please sign in to comment.