mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode
Conflicts: internal/rpc/group/group.go pkg/common/db/cache/conversation.go pkg/common/db/cache/group.go pkg/common/db/controller/group.go
This commit is contained in:
commit
62f0d1043d
@ -6,8 +6,8 @@ import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"Open_IM/pkg/common/db/controller"
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
relationTb "Open_IM/pkg/common/db/table/relation"
|
||||
relation_conn "Open_IM/pkg/common/db/relation"
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/db/unrelation"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/common/middleware"
|
||||
@ -38,7 +38,7 @@ type groupServer struct {
|
||||
rpcRegisterName string
|
||||
etcdSchema string
|
||||
etcdAddr []string
|
||||
controller.GroupInterface
|
||||
GroupInterface controller.GroupInterface
|
||||
|
||||
etcdConn *getcdv3.EtcdConn
|
||||
//userRpc pbUser.UserClient
|
||||
@ -71,9 +71,9 @@ func NewGroupServer(port int) *groupServer {
|
||||
//g.conversationRpc = pbConversation.NewConversationClient(conn)
|
||||
|
||||
//mysql init
|
||||
var mysql relation.Mysql
|
||||
var mysql relation_conn.Mysql
|
||||
var mongo unrelation.Mongo
|
||||
var groupModel relationTb.GroupModel
|
||||
var groupModel relation.GroupModel
|
||||
var redis cache.RedisClient
|
||||
err = mysql.InitConn().AutoMigrateModel(&groupModel)
|
||||
if err != nil {
|
||||
@ -98,7 +98,7 @@ func NewGroupServer(port int) *groupServer {
|
||||
}
|
||||
var registerCenter discoveryRegistry.SvcDiscoveryRegistry = zkClient
|
||||
conns, err := registerCenter.GetConns(config.Config.RpcRegisterName.OpenImConversationName)
|
||||
g.GroupInterface = controller.NewGroupController(groupModel.DB, redis.GetClient(), mongo.GetClient())
|
||||
g.GroupInterface = controller.NewGroupInterface(controller.NewGroupDatabase(mysql.GormConn(), redis.GetClient(), mongo.GetClient()))
|
||||
return &g
|
||||
}
|
||||
|
||||
@ -200,7 +200,7 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR
|
||||
if err := callbackBeforeCreateGroup(ctx, req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var groupMembers []*relationTb.GroupMemberModel
|
||||
var groupMembers []*relation.GroupMemberModel
|
||||
group := PbToDBGroupInfo(req.GroupInfo)
|
||||
group.GroupID = genGroupID(ctx, req.GroupInfo.GroupID)
|
||||
joinGroup := func(userID string, roleLevel int32) error {
|
||||
@ -235,7 +235,7 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := s.GroupInterface.CreateGroup(ctx, []*relationTb.GroupModel{group}, groupMembers); err != nil {
|
||||
if err := s.GroupInterface.CreateGroup(ctx, []*relation.GroupModel{group}, groupMembers); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.GroupInfo = DbToPbGroupInfo(group, req.OwnerUserID, uint32(len(userIDs)))
|
||||
@ -265,7 +265,7 @@ func (s *groupServer) GetJoinedGroupList(ctx context.Context, req *pbGroup.GetJo
|
||||
if len(members) == 0 {
|
||||
return resp, nil
|
||||
}
|
||||
groupIDs := utils.Slice(members, func(e *relationTb.GroupMemberModel) string {
|
||||
groupIDs := utils.Slice(members, func(e *relation.GroupMemberModel) string {
|
||||
return e.GroupID
|
||||
})
|
||||
groups, err := s.GroupInterface.FindGroup(ctx, groupIDs)
|
||||
@ -280,12 +280,12 @@ func (s *groupServer) GetJoinedGroupList(ctx context.Context, req *pbGroup.GetJo
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ownerMap := utils.SliceToMap(owners, func(e *relationTb.GroupMemberModel) string {
|
||||
ownerMap := utils.SliceToMap(owners, func(e *relation.GroupMemberModel) string {
|
||||
return e.GroupID
|
||||
})
|
||||
resp.Groups = utils.Slice(utils.Order(groupIDs, groups, func(group *relationTb.GroupModel) string {
|
||||
resp.Groups = utils.Slice(utils.Order(groupIDs, groups, func(group *relation.GroupModel) string {
|
||||
return group.GroupID
|
||||
}), func(group *relationTb.GroupModel) *open_im_sdk.GroupInfo {
|
||||
}), func(group *relation.GroupModel) *open_im_sdk.GroupInfo {
|
||||
return DbToPbGroupInfo(group, ownerMap[group.GroupID].UserID, uint32(groupMemberNum[group.GroupID]))
|
||||
})
|
||||
return resp, nil
|
||||
@ -310,7 +310,7 @@ func (s *groupServer) InviteUserToGroup(ctx context.Context, req *pbGroup.Invite
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
memberMap := utils.SliceToMap(members, func(e *relationTb.GroupMemberModel) string {
|
||||
memberMap := utils.SliceToMap(members, func(e *relation.GroupMemberModel) string {
|
||||
return e.UserID
|
||||
})
|
||||
if ids := utils.Single(req.InvitedUserIDs, utils.Keys(memberMap)); len(ids) > 0 {
|
||||
@ -331,9 +331,9 @@ func (s *groupServer) InviteUserToGroup(ctx context.Context, req *pbGroup.Invite
|
||||
return nil, constant.ErrNoPermission.Wrap("not in group")
|
||||
}
|
||||
if !(member.RoleLevel == constant.GroupOwner || member.RoleLevel == constant.GroupAdmin) {
|
||||
var requests []*relationTb.GroupRequestModel
|
||||
var requests []*relation.GroupRequestModel
|
||||
for _, userID := range req.InvitedUserIDs {
|
||||
requests = append(requests, &relationTb.GroupRequestModel{
|
||||
requests = append(requests, &relation.GroupRequestModel{
|
||||
UserID: userID,
|
||||
GroupID: req.GroupID,
|
||||
JoinSource: constant.JoinByInvitation,
|
||||
@ -364,7 +364,7 @@ func (s *groupServer) InviteUserToGroup(ctx context.Context, req *pbGroup.Invite
|
||||
}
|
||||
} else {
|
||||
opUserID := tracelog.GetOpUserID(ctx)
|
||||
var groupMembers []*relationTb.GroupMemberModel
|
||||
var groupMembers []*relation.GroupMemberModel
|
||||
for _, userID := range req.InvitedUserIDs {
|
||||
member := PbToDbGroupMember(userMap[userID])
|
||||
member.GroupID = req.GroupID
|
||||
@ -398,7 +398,7 @@ func (s *groupServer) GetGroupAllMember(ctx context.Context, req *pbGroup.GetGro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.Members = utils.Slice(members, func(e *relationTb.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
|
||||
resp.Members = utils.Slice(members, func(e *relation.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
|
||||
return DbToPbGroupMembersCMSResp(e)
|
||||
})
|
||||
return resp, nil
|
||||
@ -411,7 +411,7 @@ func (s *groupServer) GetGroupMemberList(ctx context.Context, req *pbGroup.GetGr
|
||||
return nil, err
|
||||
}
|
||||
resp.Total = total
|
||||
resp.Members = utils.Slice(members, func(e *relationTb.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
|
||||
resp.Members = utils.Slice(members, func(e *relation.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
|
||||
return DbToPbGroupMembersCMSResp(e)
|
||||
})
|
||||
return resp, nil
|
||||
@ -447,7 +447,7 @@ func (s *groupServer) KickGroupMember(ctx context.Context, req *pbGroup.KickGrou
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
memberMap := make(map[string]*relationTb.GroupMemberModel)
|
||||
memberMap := make(map[string]*relation.GroupMemberModel)
|
||||
for i, member := range members {
|
||||
memberMap[member.UserID] = members[i]
|
||||
}
|
||||
@ -496,7 +496,7 @@ func (s *groupServer) GetGroupMembersInfo(ctx context.Context, req *pbGroup.GetG
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.Members = utils.Slice(members, func(e *relationTb.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
|
||||
resp.Members = utils.Slice(members, func(e *relation.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
|
||||
return DbToPbGroupMembersCMSResp(e)
|
||||
})
|
||||
return resp, nil
|
||||
@ -533,7 +533,7 @@ func (s *groupServer) GetGroupApplicationList(ctx context.Context, req *pbGroup.
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groupMap := utils.SliceToMap(groups, func(e *relationTb.GroupModel) string {
|
||||
groupMap := utils.SliceToMap(groups, func(e *relation.GroupModel) string {
|
||||
return e.GroupID
|
||||
})
|
||||
if ids := utils.Single(utils.Keys(groupMap), groupIDs); len(ids) > 0 {
|
||||
@ -547,10 +547,10 @@ func (s *groupServer) GetGroupApplicationList(ctx context.Context, req *pbGroup.
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ownerMap := utils.SliceToMap(owners, func(e *relationTb.GroupMemberModel) string {
|
||||
ownerMap := utils.SliceToMap(owners, func(e *relation.GroupMemberModel) string {
|
||||
return e.GroupID
|
||||
})
|
||||
resp.GroupRequests = utils.Slice(groupRequests, func(e *relationTb.GroupRequestModel) *open_im_sdk.GroupRequest {
|
||||
resp.GroupRequests = utils.Slice(groupRequests, func(e *relation.GroupRequestModel) *open_im_sdk.GroupRequest {
|
||||
return DbToPbGroupRequest(e, userMap[e.UserID], DbToPbGroupInfo(groupMap[e.GroupID], ownerMap[e.GroupID].UserID, uint32(groupMemberNumMap[e.GroupID])))
|
||||
})
|
||||
return resp, nil
|
||||
@ -573,10 +573,10 @@ func (s *groupServer) GetGroupsInfo(ctx context.Context, req *pbGroup.GetGroupsI
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ownerMap := utils.SliceToMap(owners, func(e *relationTb.GroupMemberModel) string {
|
||||
ownerMap := utils.SliceToMap(owners, func(e *relation.GroupMemberModel) string {
|
||||
return e.GroupID
|
||||
})
|
||||
resp.GroupInfos = utils.Slice(groups, func(e *relationTb.GroupModel) *open_im_sdk.GroupInfo {
|
||||
resp.GroupInfos = utils.Slice(groups, func(e *relation.GroupModel) *open_im_sdk.GroupInfo {
|
||||
return DbToPbGroupInfo(e, ownerMap[e.GroupID].UserID, uint32(groupMemberNumMap[e.GroupID]))
|
||||
})
|
||||
return resp, nil
|
||||
@ -618,9 +618,9 @@ func (s *groupServer) GroupApplicationResponse(ctx context.Context, req *pbGroup
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var member *relationTb.GroupMemberModel
|
||||
var member *relation.GroupMemberModel
|
||||
if req.HandleResult == constant.GroupResponseAgree {
|
||||
member = &relationTb.GroupMemberModel{
|
||||
member = &relation.GroupMemberModel{
|
||||
GroupID: req.GroupID,
|
||||
UserID: user.UserID,
|
||||
Nickname: user.Nickname,
|
||||
@ -664,7 +664,7 @@ func (s *groupServer) JoinGroup(ctx context.Context, req *pbGroup.JoinGroupReq)
|
||||
if group.GroupType == constant.SuperGroup {
|
||||
return nil, constant.ErrGroupTypeNotSupport.Wrap()
|
||||
}
|
||||
user, err := relation.GetUserByUserID(tracelog.GetOpUserID(ctx))
|
||||
user, err := relation_conn.GetUserByUserID(tracelog.GetOpUserID(ctx))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -677,20 +677,20 @@ func (s *groupServer) JoinGroup(ctx context.Context, req *pbGroup.JoinGroupReq)
|
||||
if err := CallbackBeforeMemberJoinGroup(ctx, tracelog.GetOperationID(ctx), groupMember, group.Ex); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.GroupInterface.CreateGroup(ctx, nil, []*relationTb.GroupMemberModel{groupMember}); err != nil {
|
||||
if err := s.GroupInterface.CreateGroup(ctx, nil, []*relation.GroupMemberModel{groupMember}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
chat.MemberEnterDirectlyNotification(req.GroupID, tracelog.GetOpUserID(ctx), tracelog.GetOperationID(ctx))
|
||||
return resp, nil
|
||||
}
|
||||
groupRequest := relationTb.GroupRequestModel{
|
||||
groupRequest := relation.GroupRequestModel{
|
||||
UserID: tracelog.GetOpUserID(ctx),
|
||||
ReqMsg: req.ReqMessage,
|
||||
GroupID: req.GroupID,
|
||||
JoinSource: req.JoinSource,
|
||||
ReqTime: time.Now(),
|
||||
}
|
||||
if err := s.GroupInterface.CreateGroupRequest(ctx, []*relationTb.GroupRequestModel{&groupRequest}); err != nil {
|
||||
if err := s.GroupInterface.CreateGroupRequest(ctx, []*relation.GroupRequestModel{&groupRequest}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
chat.JoinGroupApplicationNotification(ctx, req)
|
||||
@ -770,7 +770,7 @@ func (s *groupServer) TransferGroupOwner(ctx context.Context, req *pbGroup.Trans
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
memberMap := utils.SliceToMap(members, func(e *relationTb.GroupMemberModel) string { return e.UserID })
|
||||
memberMap := utils.SliceToMap(members, func(e *relation.GroupMemberModel) string { return e.UserID })
|
||||
if ids := utils.Single([]string{req.OldOwnerUserID, req.NewOwnerUserID}, utils.Keys(memberMap)); len(ids) > 0 {
|
||||
return nil, constant.ErrArgs.Wrap("user not in group " + strings.Join(ids, ","))
|
||||
}
|
||||
@ -804,26 +804,26 @@ func (s *groupServer) TransferGroupOwner(ctx context.Context, req *pbGroup.Trans
|
||||
func (s *groupServer) GetGroups(ctx context.Context, req *pbGroup.GetGroupsReq) (*pbGroup.GetGroupsResp, error) {
|
||||
resp := &pbGroup.GetGroupsResp{}
|
||||
var (
|
||||
groups []*relationTb.GroupModel
|
||||
groups []*relation.GroupModel
|
||||
err error
|
||||
)
|
||||
if req.GroupID != "" {
|
||||
groups, err = s.GroupInterface.FindGroup(ctx, []string{req.GroupID})
|
||||
resp.GroupNum = int32(len(groups))
|
||||
resp.Total = uint32(len(groups))
|
||||
} else {
|
||||
resp.GroupNum, groups, err = s.GroupInterface.SearchGroup(ctx, req.GroupName, req.Pagination.PageNumber, req.Pagination.ShowNumber)
|
||||
resp.Total, groups, err = s.GroupInterface.SearchGroup(ctx, req.GroupName, req.Pagination.PageNumber, req.Pagination.ShowNumber)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groupIDs := utils.Slice(groups, func(e *relationTb.GroupModel) string {
|
||||
groupIDs := utils.Slice(groups, func(e *relation.GroupModel) string {
|
||||
return e.GroupID
|
||||
})
|
||||
ownerMembers, err := s.GroupInterface.FindGroupMember(ctx, groupIDs, nil, []int32{constant.GroupOwner})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ownerMemberMap := utils.SliceToMap(ownerMembers, func(e *relationTb.GroupMemberModel) string {
|
||||
ownerMemberMap := utils.SliceToMap(ownerMembers, func(e *relation.GroupMemberModel) string {
|
||||
return e.GroupID
|
||||
})
|
||||
if ids := utils.Single(groupIDs, utils.Keys(ownerMemberMap)); len(ids) > 0 {
|
||||
@ -833,7 +833,7 @@ func (s *groupServer) GetGroups(ctx context.Context, req *pbGroup.GetGroupsReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.Groups = utils.Slice(groups, func(group *relationTb.GroupModel) *pbGroup.CMSGroup {
|
||||
resp.Groups = utils.Slice(groups, func(group *relation.GroupModel) *pbGroup.CMSGroup {
|
||||
member := ownerMemberMap[group.GroupID]
|
||||
return DbToPbCMSGroup(group, member.UserID, member.Nickname, uint32(groupMemberNumMap[group.GroupID]))
|
||||
})
|
||||
@ -846,8 +846,8 @@ func (s *groupServer) GetGroupMembersCMS(ctx context.Context, req *pbGroup.GetGr
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.MemberNums = total
|
||||
resp.Members = utils.Slice(members, func(e *relationTb.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
|
||||
resp.Total = total
|
||||
resp.Members = utils.Slice(members, func(e *relation.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
|
||||
return DbToPbGroupMembersCMSResp(e)
|
||||
})
|
||||
return resp, nil
|
||||
@ -867,14 +867,14 @@ func (s *groupServer) GetUserReqApplicationList(ctx context.Context, req *pbGrou
|
||||
if len(requests) == 0 {
|
||||
return resp, nil
|
||||
}
|
||||
groupIDs := utils.Distinct(utils.Slice(requests, func(e *relationTb.GroupRequestModel) string {
|
||||
groupIDs := utils.Distinct(utils.Slice(requests, func(e *relation.GroupRequestModel) string {
|
||||
return e.GroupID
|
||||
}))
|
||||
groups, err := s.GroupInterface.FindGroup(ctx, groupIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groupMap := utils.SliceToMap(groups, func(e *relationTb.GroupModel) string {
|
||||
groupMap := utils.SliceToMap(groups, func(e *relation.GroupModel) string {
|
||||
return e.GroupID
|
||||
})
|
||||
if ids := utils.Single(groupIDs, utils.Keys(groupMap)); len(ids) > 0 {
|
||||
@ -884,7 +884,7 @@ func (s *groupServer) GetUserReqApplicationList(ctx context.Context, req *pbGrou
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ownerMap := utils.SliceToMap(owners, func(e *relationTb.GroupMemberModel) string {
|
||||
ownerMap := utils.SliceToMap(owners, func(e *relation.GroupMemberModel) string {
|
||||
return e.GroupID
|
||||
})
|
||||
if ids := utils.Single(groupIDs, utils.Keys(ownerMap)); len(ids) > 0 {
|
||||
@ -894,7 +894,7 @@ func (s *groupServer) GetUserReqApplicationList(ctx context.Context, req *pbGrou
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.GroupRequests = utils.Slice(requests, func(e *relationTb.GroupRequestModel) *open_im_sdk.GroupRequest {
|
||||
resp.GroupRequests = utils.Slice(requests, func(e *relation.GroupRequestModel) *open_im_sdk.GroupRequest {
|
||||
return DbToPbGroupRequest(e, user, DbToPbGroupInfo(groupMap[e.GroupID], ownerMap[e.GroupID].UserID, uint32(groupMemberNum[e.GroupID])))
|
||||
})
|
||||
return resp, nil
|
||||
@ -1058,7 +1058,7 @@ func (s *groupServer) GetGroupAbstractInfo(ctx context.Context, req *pbGroup.Get
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.GroupAbstractInfos = utils.Slice(groups, func(e *relationTb.GroupModel) *pbGroup.GroupAbstractInfo {
|
||||
resp.GroupAbstractInfos = utils.Slice(groups, func(e *relation.GroupModel) *pbGroup.GroupAbstractInfo {
|
||||
userIDs := groupUserMap[e.GroupID]
|
||||
utils.Sort(userIDs, true)
|
||||
bi := big.NewInt(0)
|
||||
@ -1077,7 +1077,7 @@ func (s *groupServer) GetUserInGroupMembers(ctx context.Context, req *pbGroup.Ge
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.Members = utils.Slice(members, func(e *relationTb.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
|
||||
resp.Members = utils.Slice(members, func(e *relation.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
|
||||
return DbToPbGroupMembersCMSResp(e)
|
||||
})
|
||||
return resp, nil
|
||||
|
@ -3,42 +3,53 @@ package group
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/db/table/unrelation"
|
||||
pbGroup "Open_IM/pkg/proto/group"
|
||||
sdk_ws "Open_IM/pkg/proto/sdk_ws"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *groupServer) GetJoinedSuperGroupList(ctx context.Context, req *pbGroup.GetJoinedSuperGroupListReq) (*pbGroup.GetJoinedSuperGroupListResp, error) {
|
||||
resp := &pbGroup.GetJoinedSuperGroupListResp{}
|
||||
total, groupIDs, err := s.GroupInterface.FindJoinSuperGroup(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
|
||||
joinSuperGroup, err := s.GroupInterface.FindJoinSuperGroup(ctx, req.UserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.Total = total
|
||||
if len(groupIDs) == 0 {
|
||||
if len(joinSuperGroup.GroupIDs) == 0 {
|
||||
return resp, nil
|
||||
}
|
||||
numMap, err := s.GroupInterface.MapSuperGroupMemberNum(ctx, groupIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
owners, err := s.GroupInterface.FindGroupMember(ctx, groupIDs, nil, []int32{constant.GroupOwner})
|
||||
owners, err := s.GroupInterface.FindGroupMember(ctx, joinSuperGroup.GroupIDs, nil, []int32{constant.GroupOwner})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ownerMap := utils.SliceToMap(owners, func(e *relation.GroupMemberModel) string {
|
||||
return e.GroupID
|
||||
})
|
||||
groups, err := s.GroupInterface.FindGroup(ctx, groupIDs)
|
||||
if ids := utils.Single(joinSuperGroup.GroupIDs, utils.Keys(ownerMap)); len(ids) > 0 {
|
||||
return nil, constant.ErrData.Wrap(fmt.Sprintf("super group %s not owner", strings.Join(ids, ",")))
|
||||
}
|
||||
groups, err := s.GroupInterface.FindGroup(ctx, joinSuperGroup.GroupIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groupMap := utils.SliceToMap(groups, func(e *relation.GroupModel) string {
|
||||
return e.GroupID
|
||||
})
|
||||
resp.Groups = utils.Slice(groupIDs, func(groupID string) *sdk_ws.GroupInfo {
|
||||
return DbToPbGroupInfo(groupMap[groupID], ownerMap[groupID].UserID, numMap[groupID])
|
||||
if ids := utils.Single(joinSuperGroup.GroupIDs, utils.Keys(groupMap)); len(ids) > 0 {
|
||||
return nil, constant.ErrData.Wrap(fmt.Sprintf("super group info %s not found", strings.Join(ids, ",")))
|
||||
}
|
||||
superGroupMembers, err := s.GroupInterface.FindSuperGroup(ctx, joinSuperGroup.GroupIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
superGroupMemberMap := utils.SliceToMapAny(superGroupMembers, func(e *unrelation.SuperGroupModel) (string, []string) {
|
||||
return e.GroupID, e.MemberIDs
|
||||
})
|
||||
resp.Groups = utils.Slice(joinSuperGroup.GroupIDs, func(groupID string) *sdk_ws.GroupInfo {
|
||||
return DbToPbGroupInfo(groupMap[groupID], ownerMap[groupID].UserID, uint32(len(superGroupMemberMap)))
|
||||
})
|
||||
return resp, nil
|
||||
}
|
||||
@ -52,10 +63,13 @@ func (s *groupServer) GetSuperGroupsInfo(ctx context.Context, req *pbGroup.GetSu
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
numMap, err := s.GroupInterface.MapSuperGroupMemberNum(ctx, req.GroupIDs)
|
||||
superGroupMembers, err := s.GroupInterface.FindSuperGroup(ctx, req.GroupIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
superGroupMemberMap := utils.SliceToMapAny(superGroupMembers, func(e *unrelation.SuperGroupModel) (string, []string) {
|
||||
return e.GroupID, e.MemberIDs
|
||||
})
|
||||
owners, err := s.GroupInterface.FindGroupMember(ctx, req.GroupIDs, nil, []int32{constant.GroupOwner})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -64,7 +78,7 @@ func (s *groupServer) GetSuperGroupsInfo(ctx context.Context, req *pbGroup.GetSu
|
||||
return e.GroupID
|
||||
})
|
||||
resp.GroupInfos = utils.Slice(groups, func(e *relation.GroupModel) *sdk_ws.GroupInfo {
|
||||
return DbToPbGroupInfo(e, ownerMap[e.GroupID].UserID, numMap[e.GroupID])
|
||||
return DbToPbGroupInfo(e, ownerMap[e.GroupID].UserID, uint32(len(superGroupMemberMap[e.GroupID])))
|
||||
})
|
||||
return resp, nil
|
||||
}
|
||||
|
50
pkg/common/db/cache/conversation.go
vendored
50
pkg/common/db/cache/conversation.go
vendored
@ -95,7 +95,7 @@ func (c *ConversationRedis) GetUserConversationIDs(ctx context.Context, ownerUse
|
||||
// return nil, utils.Wrap(err, "")
|
||||
//}
|
||||
//return conversationIDs, nil
|
||||
return GetCache(c.rcClient, c.getConversationIDsKey(ownerUserID), time.Second*30*60, func() ([]string, error) {
|
||||
return GetCache(ctx, c.rcClient, c.getConversationIDsKey(ownerUserID), time.Second*30*60, func(ctx context.Context) ([]string, error) {
|
||||
return f(ownerUserID)
|
||||
})
|
||||
}
|
||||
@ -124,32 +124,32 @@ func (c *ConversationRedis) GetUserConversationIDs1(ctx context.Context, ownerUs
|
||||
return GetCache1[[]string](c.rcClient, c.getConversationIDsKey(ownerUserID), time.Second*30*60, fn)
|
||||
}
|
||||
|
||||
func GetCache1[T any](rcClient *rockscache.Client, key string, expire time.Duration, fn func() (any, error)) (T, error) {
|
||||
v, err := rcClient.Fetch(key, expire, func() (string, error) {
|
||||
v, err := fn()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
bs, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
return string(bs), nil
|
||||
})
|
||||
var t T
|
||||
if err != nil {
|
||||
return t, err
|
||||
}
|
||||
err = json.Unmarshal([]byte(v), &t)
|
||||
if err != nil {
|
||||
return t, utils.Wrap(err, "")
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
//func GetCache1[T any](rcClient *rockscache.Client, key string, expire time.Duration, fn func() (any, error)) (T, error) {
|
||||
// v, err := rcClient.Fetch(key, expire, func() (string, error) {
|
||||
// v, err := fn()
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
// bs, err := json.Marshal(v)
|
||||
// if err != nil {
|
||||
// return "", utils.Wrap(err, "")
|
||||
// }
|
||||
// return string(bs), nil
|
||||
// })
|
||||
// var t T
|
||||
// if err != nil {
|
||||
// return t, err
|
||||
// }
|
||||
// err = json.Unmarshal([]byte(v), &t)
|
||||
// if err != nil {
|
||||
// return t, utils.Wrap(err, "")
|
||||
// }
|
||||
// return t, nil
|
||||
//}
|
||||
|
||||
func GetCache[T any](rcClient *rockscache.Client, key string, expire time.Duration, fn func() (T, error)) (T, error) {
|
||||
func GetCache[T any](ctx context.Context, rcClient *rockscache.Client, key string, expire time.Duration, fn func(ctx context.Context) (T, error)) (T, error) {
|
||||
v, err := rcClient.Fetch(key, expire, func() (string, error) {
|
||||
v, err := fn()
|
||||
v, err := fn(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -4,8 +4,8 @@ import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
relationTb "Open_IM/pkg/common/db/table/relation"
|
||||
unRelationTb "Open_IM/pkg/common/db/table/unrelation"
|
||||
relation2 "Open_IM/pkg/common/db/table/relation"
|
||||
unrelation2 "Open_IM/pkg/common/db/table/unrelation"
|
||||
"Open_IM/pkg/common/db/unrelation"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
@ -20,57 +20,60 @@ import (
|
||||
//type GroupInterface GroupDataBaseInterface
|
||||
|
||||
type GroupInterface interface {
|
||||
CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error
|
||||
TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error)
|
||||
FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error)
|
||||
SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (int32, []*relationTb.GroupModel, error)
|
||||
CreateGroup(ctx context.Context, groups []*relation2.GroupModel, groupMembers []*relation2.GroupMemberModel) error
|
||||
TakeGroup(ctx context.Context, groupID string) (group *relation2.GroupModel, err error)
|
||||
FindGroup(ctx context.Context, groupIDs []string) (groups []*relation2.GroupModel, err error)
|
||||
SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (uint32, []*relation2.GroupModel, error)
|
||||
UpdateGroup(ctx context.Context, groupID string, data map[string]any) error
|
||||
DismissGroup(ctx context.Context, groupID string) error // 解散群,并删除群成员
|
||||
// GroupMember
|
||||
TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relationTb.GroupMemberModel, err error)
|
||||
TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error)
|
||||
FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relationTb.GroupMemberModel, error)
|
||||
PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (int32, []*relationTb.GroupMemberModel, error)
|
||||
SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (int32, []*relationTb.GroupMemberModel, error)
|
||||
HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error
|
||||
TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relation2.GroupMemberModel, err error)
|
||||
TakeGroupOwner(ctx context.Context, groupID string) (*relation2.GroupMemberModel, error)
|
||||
FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relation2.GroupMemberModel, error)
|
||||
PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relation2.GroupMemberModel, error)
|
||||
SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relation2.GroupMemberModel, error)
|
||||
HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relation2.GroupMemberModel) error
|
||||
DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string][]string, error)
|
||||
MapGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]uint32, error)
|
||||
TransferGroupOwner(ctx context.Context, groupID string, oldOwnerUserID, newOwnerUserID string, roleLevel int32) error // 转让群
|
||||
UpdateGroupMember(ctx context.Context, groupID, userID string, data map[string]any) error
|
||||
// GroupRequest
|
||||
CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error
|
||||
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error)
|
||||
PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (int32, []*relationTb.GroupRequestModel, error)
|
||||
CreateGroupRequest(ctx context.Context, requests []*relation2.GroupRequestModel) error
|
||||
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relation2.GroupRequestModel, error)
|
||||
PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (uint32, []*relation2.GroupRequestModel, error)
|
||||
// SuperGroup
|
||||
TakeSuperGroup(ctx context.Context, groupID string) (superGroup *unRelationTb.SuperGroupModel, err error)
|
||||
FindJoinSuperGroup(ctx context.Context, userID string, pageNumber, showNumber int32) (total int32, groupIDs []string, err error)
|
||||
FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unrelation2.SuperGroupModel, error)
|
||||
FindJoinSuperGroup(ctx context.Context, userID string) (superGroup *unrelation2.UserToSuperGroupModel, err error)
|
||||
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error
|
||||
DeleteSuperGroup(ctx context.Context, groupID string) error
|
||||
DeleteSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
MapSuperGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]uint32, error)
|
||||
}
|
||||
|
||||
var _ GroupInterface = (*GroupController)(nil)
|
||||
|
||||
func NewGroupInterface(database GroupDataBaseInterface) GroupInterface {
|
||||
return &GroupController{database: database}
|
||||
}
|
||||
|
||||
type GroupController struct {
|
||||
database GroupDataBaseInterface
|
||||
}
|
||||
|
||||
func (g *GroupController) CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error {
|
||||
func (g *GroupController) CreateGroup(ctx context.Context, groups []*relation2.GroupModel, groupMembers []*relation2.GroupMemberModel) error {
|
||||
return g.database.CreateGroup(ctx, groups, groupMembers)
|
||||
}
|
||||
|
||||
func (g *GroupController) TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error) {
|
||||
func (g *GroupController) TakeGroup(ctx context.Context, groupID string) (group *relation2.GroupModel, err error) {
|
||||
return g.TakeGroup(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupController) FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error) {
|
||||
func (g *GroupController) FindGroup(ctx context.Context, groupIDs []string) (groups []*relation2.GroupModel, err error) {
|
||||
return g.database.FindGroup(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupController) SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (int32, []*relationTb.GroupModel, error) {
|
||||
func (g *GroupController) SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (uint32, []*relation2.GroupModel, error) {
|
||||
return g.database.SearchGroup(ctx, keyword, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
@ -82,27 +85,27 @@ func (g *GroupController) DismissGroup(ctx context.Context, groupID string) erro
|
||||
return g.database.DismissGroup(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupController) TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relationTb.GroupMemberModel, err error) {
|
||||
func (g *GroupController) TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relation2.GroupMemberModel, err error) {
|
||||
return g.database.TakeGroupMember(ctx, groupID, userID)
|
||||
}
|
||||
|
||||
func (g *GroupController) TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error) {
|
||||
func (g *GroupController) TakeGroupOwner(ctx context.Context, groupID string) (*relation2.GroupMemberModel, error) {
|
||||
return g.database.TakeGroupOwner(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupController) FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relationTb.GroupMemberModel, error) {
|
||||
func (g *GroupController) FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relation2.GroupMemberModel, error) {
|
||||
return g.database.FindGroupMember(ctx, groupIDs, userIDs, roleLevels)
|
||||
}
|
||||
|
||||
func (g *GroupController) PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (int32, []*relationTb.GroupMemberModel, error) {
|
||||
func (g *GroupController) PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relation2.GroupMemberModel, error) {
|
||||
return g.database.PageGroupMember(ctx, groupIDs, userIDs, roleLevels, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupController) SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (int32, []*relationTb.GroupMemberModel, error) {
|
||||
func (g *GroupController) SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relation2.GroupMemberModel, error) {
|
||||
return g.database.SearchGroupMember(ctx, keyword, groupIDs, userIDs, roleLevels, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupController) HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error {
|
||||
func (g *GroupController) HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relation2.GroupMemberModel) error {
|
||||
return g.database.HandlerGroupRequest(ctx, groupID, userID, handledMsg, handleResult, member)
|
||||
}
|
||||
|
||||
@ -126,24 +129,27 @@ func (g *GroupController) UpdateGroupMember(ctx context.Context, groupID, userID
|
||||
return g.database.UpdateGroupMember(ctx, groupID, userID, data)
|
||||
}
|
||||
|
||||
func (g *GroupController) CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error {
|
||||
func (g *GroupController) CreateGroupRequest(ctx context.Context, requests []*relation2.GroupRequestModel) error {
|
||||
return g.database.CreateGroupRequest(ctx, requests)
|
||||
}
|
||||
|
||||
func (g *GroupController) TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error) {
|
||||
func (g *GroupController) TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relation2.GroupRequestModel, error) {
|
||||
return g.database.TakeGroupRequest(ctx, groupID, userID)
|
||||
}
|
||||
|
||||
func (g *GroupController) PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (int32, []*relationTb.GroupRequestModel, error) {
|
||||
func (g *GroupController) PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (uint32, []*relation2.GroupRequestModel, error) {
|
||||
return g.database.PageGroupRequestUser(ctx, userID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupController) TakeSuperGroup(ctx context.Context, groupID string) (superGroup *unRelationTb.SuperGroupModel, err error) {
|
||||
return g.database.TakeSuperGroup(ctx, groupID)
|
||||
// func (g *GroupController) TakeSuperGroup(ctx context.Context, groupID string) (superGroup *unrelation2.SuperGroupModel, err error) {
|
||||
// return g.database.TakeSuperGroup(ctx, groupID)
|
||||
// }
|
||||
func (g *GroupController) FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unrelation2.SuperGroupModel, error) {
|
||||
return g.database.FindSuperGroup(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupController) FindJoinSuperGroup(ctx context.Context, userID string, pageNumber, showNumber int32) (total int32, groupIDs []string, err error) {
|
||||
return g.database.FindJoinSuperGroup(ctx, userID, pageNumber, showNumber)
|
||||
func (g *GroupController) FindJoinSuperGroup(ctx context.Context, userID string) (*unrelation2.UserToSuperGroupModel, error) {
|
||||
return g.database.FindJoinSuperGroup(ctx, userID)
|
||||
}
|
||||
|
||||
func (g *GroupController) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error {
|
||||
@ -162,44 +168,39 @@ func (g *GroupController) CreateSuperGroupMember(ctx context.Context, groupID st
|
||||
return g.database.CreateSuperGroupMember(ctx, groupID, userIDs)
|
||||
}
|
||||
|
||||
func (g *GroupController) MapSuperGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]uint32, error) {
|
||||
return g.database.MapSuperGroupMemberNum(ctx, groupIDs)
|
||||
}
|
||||
|
||||
type GroupDataBaseInterface interface {
|
||||
CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error
|
||||
TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error)
|
||||
FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error)
|
||||
SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (int32, []*relationTb.GroupModel, error)
|
||||
CreateGroup(ctx context.Context, groups []*relation2.GroupModel, groupMembers []*relation2.GroupMemberModel) error
|
||||
TakeGroup(ctx context.Context, groupID string) (group *relation2.GroupModel, err error)
|
||||
FindGroup(ctx context.Context, groupIDs []string) (groups []*relation2.GroupModel, err error)
|
||||
SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (uint32, []*relation2.GroupModel, error)
|
||||
UpdateGroup(ctx context.Context, groupID string, data map[string]any) error
|
||||
DismissGroup(ctx context.Context, groupID string) error // 解散群,并删除群成员
|
||||
// GroupMember
|
||||
TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relationTb.GroupMemberModel, err error)
|
||||
TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error)
|
||||
FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relationTb.GroupMemberModel, error)
|
||||
PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (int32, []*relationTb.GroupMemberModel, error)
|
||||
SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (int32, []*relationTb.GroupMemberModel, error)
|
||||
HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error
|
||||
TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relation2.GroupMemberModel, err error)
|
||||
TakeGroupOwner(ctx context.Context, groupID string) (*relation2.GroupMemberModel, error)
|
||||
FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relation2.GroupMemberModel, error)
|
||||
PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relation2.GroupMemberModel, error)
|
||||
SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relation2.GroupMemberModel, error)
|
||||
HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relation2.GroupMemberModel) error
|
||||
DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string][]string, error)
|
||||
MapGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]uint32, error)
|
||||
TransferGroupOwner(ctx context.Context, groupID string, oldOwnerUserID, newOwnerUserID string, roleLevel int32) error // 转让群
|
||||
UpdateGroupMember(ctx context.Context, groupID, userID string, data map[string]any) error
|
||||
// GroupRequest
|
||||
CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error
|
||||
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error)
|
||||
PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (int32, []*relationTb.GroupRequestModel, error)
|
||||
CreateGroupRequest(ctx context.Context, requests []*relation2.GroupRequestModel) error
|
||||
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relation2.GroupRequestModel, error)
|
||||
PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (uint32, []*relation2.GroupRequestModel, error)
|
||||
// SuperGroup
|
||||
TakeSuperGroup(ctx context.Context, groupID string) (superGroup *unRelationTb.SuperGroupModel, err error)
|
||||
FindJoinSuperGroup(ctx context.Context, userID string, pageNumber, showNumber int32) (total int32, groupIDs []string, err error)
|
||||
FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unrelation2.SuperGroupModel, error)
|
||||
FindJoinSuperGroup(ctx context.Context, userID string) (*unrelation2.UserToSuperGroupModel, error)
|
||||
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error
|
||||
DeleteSuperGroup(ctx context.Context, groupID string) error
|
||||
DeleteSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
MapSuperGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]uint32, error)
|
||||
}
|
||||
|
||||
func newGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, mgoClient *mongo.Client) GroupDataBaseInterface {
|
||||
func NewGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, mgoClient *mongo.Client) GroupDataBaseInterface {
|
||||
groupDB := relation.NewGroupDB(db)
|
||||
groupMemberDB := relation.NewGroupMemberDB(db)
|
||||
groupRequestDB := relation.NewGroupRequest(db)
|
||||
@ -224,16 +225,16 @@ func newGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, mgoClient *mongo.C
|
||||
var _ GroupDataBaseInterface = (*GroupDataBase)(nil)
|
||||
|
||||
type GroupDataBase struct {
|
||||
groupDB *relation.GroupGorm
|
||||
groupMemberDB *relation.GroupMemberGorm
|
||||
groupRequestDB *relation.GroupRequestGorm
|
||||
groupDB relation2.GroupModelInterface
|
||||
groupMemberDB relation2.GroupMemberModelInterface
|
||||
groupRequestDB relation2.GroupRequestModelInterface
|
||||
db *gorm.DB
|
||||
|
||||
cache *cache.GroupCache
|
||||
mongoDB *unrelation.SuperGroupMongoDriver
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error {
|
||||
func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relation2.GroupModel, groupMembers []*relation2.GroupMemberModel) error {
|
||||
if len(groups) > 0 && len(groupMembers) > 0 {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.groupDB.Create(ctx, groups, tx); err != nil {
|
||||
@ -251,15 +252,15 @@ func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relationTb.Gr
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error) {
|
||||
func (g *GroupDataBase) TakeGroup(ctx context.Context, groupID string) (group *relation2.GroupModel, err error) {
|
||||
return g.groupDB.Take(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error) {
|
||||
func (g *GroupDataBase) FindGroup(ctx context.Context, groupIDs []string) (groups []*relation2.GroupModel, err error) {
|
||||
return g.groupDB.Find(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (int32, []*relationTb.GroupModel, error) {
|
||||
func (g *GroupDataBase) SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (uint32, []*relation2.GroupModel, error) {
|
||||
return g.groupDB.Search(ctx, keyword, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
@ -276,27 +277,27 @@ func (g *GroupDataBase) DismissGroup(ctx context.Context, groupID string) error
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relationTb.GroupMemberModel, err error) {
|
||||
func (g *GroupDataBase) TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relation2.GroupMemberModel, err error) {
|
||||
return g.groupMemberDB.Take(ctx, groupID, userID)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error) {
|
||||
func (g *GroupDataBase) TakeGroupOwner(ctx context.Context, groupID string) (*relation2.GroupMemberModel, error) {
|
||||
return g.groupMemberDB.TakeOwner(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relationTb.GroupMemberModel, error) {
|
||||
func (g *GroupDataBase) FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relation2.GroupMemberModel, error) {
|
||||
return g.groupMemberDB.Find(ctx, groupIDs, userIDs, roleLevels)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (int32, []*relationTb.GroupMemberModel, error) {
|
||||
func (g *GroupDataBase) PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relation2.GroupMemberModel, error) {
|
||||
return g.groupMemberDB.SearchMember(ctx, "", groupIDs, userIDs, roleLevels, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (int32, []*relationTb.GroupMemberModel, error) {
|
||||
func (g *GroupDataBase) SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relation2.GroupMemberModel, error) {
|
||||
return g.groupMemberDB.SearchMember(ctx, keyword, groupIDs, userIDs, roleLevels, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error {
|
||||
func (g *GroupDataBase) HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relation2.GroupMemberModel) error {
|
||||
if member == nil {
|
||||
return g.groupRequestDB.UpdateHandler(ctx, groupID, userID, handledMsg, handleResult)
|
||||
}
|
||||
@ -304,7 +305,7 @@ func (g *GroupDataBase) HandlerGroupRequest(ctx context.Context, groupID string,
|
||||
if err := g.groupRequestDB.UpdateHandler(ctx, groupID, userID, handledMsg, handleResult, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.groupMemberDB.Create(ctx, []*relationTb.GroupMemberModel{member}, tx)
|
||||
return g.groupMemberDB.Create(ctx, []*relation2.GroupMemberModel{member}, tx)
|
||||
})
|
||||
}
|
||||
|
||||
@ -344,47 +345,46 @@ func (g *GroupDataBase) UpdateGroupMember(ctx context.Context, groupID, userID s
|
||||
return g.groupMemberDB.Update(ctx, groupID, userID, data)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error {
|
||||
func (g *GroupDataBase) CreateGroupRequest(ctx context.Context, requests []*relation2.GroupRequestModel) error {
|
||||
return g.groupRequestDB.Create(ctx, requests)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error) {
|
||||
func (g *GroupDataBase) TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relation2.GroupRequestModel, error) {
|
||||
return g.groupRequestDB.Take(ctx, groupID, userID)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (int32, []*relationTb.GroupRequestModel, error) {
|
||||
func (g *GroupDataBase) PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (uint32, []*relation2.GroupRequestModel, error) {
|
||||
return g.groupRequestDB.Page(ctx, userID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TakeSuperGroup(ctx context.Context, groupID string) (superGroup *unRelationTb.SuperGroupModel, err error) {
|
||||
return g.mongoDB.GetSuperGroup(ctx, groupID)
|
||||
func (g *GroupDataBase) FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unrelation2.SuperGroupModel, error) {
|
||||
return g.mongoDB.FindSuperGroup(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindJoinSuperGroup(ctx context.Context, userID string, pageNumber, showNumber int32) (total int32, groupIDs []string, err error) {
|
||||
return g.mongoDB.GetJoinGroup(ctx, userID, pageNumber, showNumber)
|
||||
func (g *GroupDataBase) FindJoinSuperGroup(ctx context.Context, userID string) (*unrelation2.UserToSuperGroupModel, error) {
|
||||
return g.mongoDB.GetSuperGroupByUserID(ctx, userID)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error {
|
||||
return unrelation.MongoTransaction(ctx, g.mongoDB.MgoClient, func(sctx mongo.SessionContext) error {
|
||||
if err := g.mongoDB.CreateSuperGroup(ctx, groupID, initMemberIDList, sctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.cache.BatchDelJoinedSuperGroupIDs(ctx, initMemberIDList)
|
||||
return g.mongoDB.Transaction(ctx, func(s unrelation2.SuperGroupModelInterface, tx any) error {
|
||||
return s.CreateSuperGroup(ctx, groupID, initMemberIDList, tx)
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) DeleteSuperGroup(ctx context.Context, groupID string) error {
|
||||
return g.mongoDB.DeleteSuperGroup(ctx, groupID)
|
||||
return g.mongoDB.Transaction(ctx, func(s unrelation2.SuperGroupModelInterface, tx any) error {
|
||||
return s.DeleteSuperGroup(ctx, groupID, tx)
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) DeleteSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
||||
return g.mongoDB.RemoverUserFromSuperGroup(ctx, groupID, userIDs)
|
||||
return g.mongoDB.Transaction(ctx, func(s unrelation2.SuperGroupModelInterface, tx any) error {
|
||||
return s.RemoverUserFromSuperGroup(ctx, groupID, userIDs, tx)
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
||||
return g.mongoDB.AddUserToSuperGroup(ctx, groupID, userIDs)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) MapSuperGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]uint32, error) {
|
||||
return g.mongoDB.MapGroupMemberCount(ctx, groupIDs)
|
||||
return g.mongoDB.Transaction(ctx, func(s unrelation2.SuperGroupModelInterface, tx any) error {
|
||||
return s.AddUserToSuperGroup(ctx, groupID, userIDs, tx)
|
||||
})
|
||||
}
|
||||
|
@ -1,234 +1,151 @@
|
||||
package relation
|
||||
|
||||
//type GroupMember struct {
|
||||
// GroupID string `gorm:"column:group_id;primaryKey;"`
|
||||
// UserID string `gorm:"column:user_id;primaryKey;"`
|
||||
// NickName string `gorm:"column:nickname"`
|
||||
// FaceUrl string `gorm:"user_group_face_url"`
|
||||
// RoleLevel int32 `gorm:"column:role_level"`
|
||||
// JoinTime time.Time `gorm:"column:join_time"`
|
||||
// JoinSource int32 `gorm:"column:join_source"`
|
||||
// OperatorUserID string `gorm:"column:operator_user_id"`
|
||||
// Ex string `gorm:"column:ex"`
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var _ relation.GroupMemberModelInterface = (*GroupMemberGorm)(nil)
|
||||
|
||||
type GroupMemberGorm struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func NewGroupMemberDB(db *gorm.DB) relation.GroupMemberModelInterface {
|
||||
return &GroupMemberGorm{DB: db}
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) Create(ctx context.Context, groupMemberList []*relation.GroupMemberModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupMemberList", groupMemberList)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Create(&groupMemberList).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) Delete(ctx context.Context, groupID string, userIDs []string, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userIDs", userIDs)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id in (?)", groupID, userIDs).Delete(&relation.GroupMemberModel{}).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) DeleteGroup(ctx context.Context, groupIDs []string, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.GroupMemberModel{}).Error, "")
|
||||
}
|
||||
|
||||
//func (g *GroupMemberGorm) UpdateByMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...any) (err error) {
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "args", args)
|
||||
// }()
|
||||
// return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(args).Error, "")
|
||||
//}
|
||||
|
||||
//func InsertIntoGroupMember(toInsertInfo GroupMember) error {
|
||||
// toInsertInfo.JoinTime = time.Now()
|
||||
// if toInsertInfo.RoleLevel == 0 {
|
||||
// toInsertInfo.RoleLevel = constant.GroupOrdinaryUsers
|
||||
func (g *GroupMemberGorm) Update(ctx context.Context, groupID string, userID string, data map[string]any, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "data", data)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(data).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) UpdateRoleLevel(ctx context.Context, groupID string, userID string, roleLevel int32, tx ...any) (rowsAffected int64, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "roleLevel", roleLevel)
|
||||
}()
|
||||
db := getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(map[string]any{
|
||||
"role_level": roleLevel,
|
||||
})
|
||||
return db.RowsAffected, utils.Wrap(db.Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) Find(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, tx ...any) (groupList []*relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "userIDs", userIDs, "groupList", groupList)
|
||||
}()
|
||||
db := getDBConn(g.DB, tx)
|
||||
if len(groupIDs) > 0 {
|
||||
db = db.Where("group_id in (?)", groupIDs)
|
||||
}
|
||||
if len(userIDs) > 0 {
|
||||
db = db.Where("user_id in (?)", userIDs)
|
||||
}
|
||||
if len(roleLevels) > 0 {
|
||||
db = db.Where("role_level in (?)", roleLevels)
|
||||
}
|
||||
return groupList, utils.Wrap(db.Find(&groupList).Error, "")
|
||||
}
|
||||
|
||||
//func (g *GroupMemberGorm) FindGroupUser(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, tx ...any) (groupList []*relation.GroupMemberModel, err error) {
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "userIDs", userIDs, "groupList", groupList)
|
||||
// }()
|
||||
// db := getDBConn(g.DB, tx)
|
||||
// if len(groupList) > 0 {
|
||||
// db = db.Where("group_id in (?)", groupIDs)
|
||||
// }
|
||||
// toInsertInfo.MuteEndTime = time.Unix(int64(time.Now().Second()), 0)
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Create(toInsertInfo).Error
|
||||
// if err != nil {
|
||||
// return err
|
||||
// if len(userIDs) > 0 {
|
||||
// db = db.Where("user_id in (?)", userIDs)
|
||||
// }
|
||||
// return nil
|
||||
// if len(roleLevels) > 0 {
|
||||
// db = db.Where("role_level in (?)", roleLevels)
|
||||
// }
|
||||
// return groupList, utils.Wrap(db.Find(&groupList).Error, "")
|
||||
//}
|
||||
|
||||
//func BatchInsertIntoGroupMember(toInsertInfoList []*GroupMember) error {
|
||||
// for _, toInsertInfo := range toInsertInfoList {
|
||||
// toInsertInfo.JoinTime = time.Now()
|
||||
// if toInsertInfo.RoleLevel == 0 {
|
||||
// toInsertInfo.RoleLevel = constant.GroupOrdinaryUsers
|
||||
// }
|
||||
// toInsertInfo.MuteEndTime = time.Unix(int64(time.Now().Second()), 0)
|
||||
// }
|
||||
// return DB.DB.MysqlDB.DefaultGormDB().Create(toInsertInfoList).Error
|
||||
//
|
||||
//}
|
||||
//
|
||||
//func FindGroupMember(userID string) ([]GroupMember, error) {
|
||||
// var groupMemberList []GroupMember
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("user_id=?", userID).Find(&groupMemberList).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return groupMemberList, nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupMemberListByGroupID(groupID string) ([]GroupMember, error) {
|
||||
// var groupMemberList []GroupMember
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=?", groupID).Find(&groupMemberList).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return groupMemberList, nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupMemberIDListByGroupID(groupID string) ([]string, error) {
|
||||
// var groupMemberIDList []string
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=?", groupID).Pluck("user_id", &groupMemberIDList).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return groupMemberIDList, nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupMemberListByGroupIDAndRoleLevel(groupID string, roleLevel int32) ([]GroupMember, error) {
|
||||
// var groupMemberList []GroupMember
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? and role_level=?", groupID, roleLevel).Find(&groupMemberList).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return groupMemberList, nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupMemberInfoByGroupIDAndUserID(groupID, userID string) (*GroupMember, error) {
|
||||
// var groupMember GroupMember
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? and user_id=? ", groupID, userID).Limit(1).Take(&groupMember).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return &groupMember, nil
|
||||
//}
|
||||
//
|
||||
//func DeleteGroupMemberByGroupIDAndUserID(groupID, userID string) error {
|
||||
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? and user_id=? ", groupID, userID).Delete(GroupMember{}).Error
|
||||
//}
|
||||
//
|
||||
//func DeleteGroupMemberByGroupID(groupID string) error {
|
||||
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? ", groupID).Delete(GroupMember{}).Error
|
||||
//}
|
||||
//
|
||||
//func UpdateGroupMemberInfo(groupMemberInfo GroupMember) error {
|
||||
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? and user_id=?", groupMemberInfo.GroupID, groupMemberInfo.UserID).Updates(&groupMemberInfo).Error
|
||||
//}
|
||||
//
|
||||
//func UpdateGroupMemberInfoByMap(groupMemberInfo GroupMember, m map[string]interface{}) error {
|
||||
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? and user_id=?", groupMemberInfo.GroupID, groupMemberInfo.UserID).Updates(m).Error
|
||||
//}
|
||||
//
|
||||
//func GetOwnerManagerByGroupID(groupID string) ([]GroupMember, error) {
|
||||
// var groupMemberList []GroupMember
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? and role_level>?", groupID, constant.GroupOrdinaryUsers).Find(&groupMemberList).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return groupMemberList, nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupMemberNumByGroupID(groupID string) (int64, error) {
|
||||
// var number int64
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=?", groupID).Count(&number).Error
|
||||
// if err != nil {
|
||||
// return 0, utils.Wrap(err, "")
|
||||
// }
|
||||
// return number, nil
|
||||
//}
|
||||
func (g *GroupMemberGorm) Take(ctx context.Context, groupID string, userID string, tx ...any) (groupMember *relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "groupMember", *groupMember)
|
||||
}()
|
||||
groupMember = &relation.GroupMemberModel{}
|
||||
return groupMember, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id = ?", groupID, userID).Take(groupMember).Error, "")
|
||||
}
|
||||
|
||||
//func GetGroupOwnerInfoByGroupID(groupID string) (*GroupMember, error) {
|
||||
// omList, err := GetOwnerManagerByGroupID(groupID)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// for _, v := range omList {
|
||||
// if v.RoleLevel == constant.GroupOwner {
|
||||
// return &v, nil
|
||||
// }
|
||||
// }
|
||||
// return nil, utils.Wrap(constant.ErrNoGroupOwner, "")
|
||||
//}
|
||||
//
|
||||
//func IsExistGroupMember(groupID, userID string) bool {
|
||||
// var number int64
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id = ? and user_id = ?", groupID, userID).Count(&number).Error
|
||||
// if err != nil {
|
||||
// return false
|
||||
// }
|
||||
// if number != 1 {
|
||||
// return false
|
||||
// }
|
||||
// return true
|
||||
//}
|
||||
//
|
||||
//func GetGroupMemberByGroupID(groupID string, filter int32, begin int32, maxNumber int32) ([]GroupMember, error) {
|
||||
// var memberList []GroupMember
|
||||
// var err error
|
||||
// if filter >= 0 {
|
||||
// memberList, err = GetGroupMemberListByGroupIDAndRoleLevel(groupID, filter) //sorted by join time
|
||||
// } else {
|
||||
// memberList, err = GetGroupMemberListByGroupID(groupID)
|
||||
// }
|
||||
//
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// if begin >= int32(len(memberList)) {
|
||||
// return nil, nil
|
||||
// }
|
||||
//
|
||||
// var end int32
|
||||
// if begin+int32(maxNumber) < int32(len(memberList)) {
|
||||
// end = begin + maxNumber
|
||||
// } else {
|
||||
// end = int32(len(memberList))
|
||||
// }
|
||||
// return memberList[begin:end], nil
|
||||
//}
|
||||
//
|
||||
//func GetJoinedGroupIDListByUserID(userID string) ([]string, error) {
|
||||
// memberList, err := FindGroupMember(userID)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// var groupIDList []string
|
||||
// for _, v := range memberList {
|
||||
// groupIDList = append(groupIDList, v.GroupID)
|
||||
// }
|
||||
// return groupIDList, nil
|
||||
//}
|
||||
//
|
||||
//func IsGroupOwnerAdmin(groupID, UserID string) bool {
|
||||
// groupMemberList, err := GetOwnerManagerByGroupID(groupID)
|
||||
// if err != nil {
|
||||
// return false
|
||||
// }
|
||||
// for _, v := range groupMemberList {
|
||||
// if v.UserID == UserID && v.RoleLevel > constant.GroupOrdinaryUsers {
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
// return false
|
||||
//}
|
||||
//
|
||||
//func GetGroupMembersByGroupIdCMS(groupId string, userName string, showNumber, pageNumber int32) ([]GroupMember, error) {
|
||||
// var groupMembers []GroupMember
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=?", groupId).Where(fmt.Sprintf(" nickname like '%%%s%%' ", userName)).Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&groupMembers).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return groupMembers, nil
|
||||
//}
|
||||
//
|
||||
//func GetGroupMembersCount(groupID, userName string) (int64, error) {
|
||||
// var count int64
|
||||
// if err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=?", groupID).Where(fmt.Sprintf(" nickname like '%%%s%%' ", userName)).Count(&count).Error; err != nil {
|
||||
// return count, err
|
||||
// }
|
||||
// return count, nil
|
||||
//}
|
||||
//
|
||||
//func UpdateGroupMemberInfoDefaultZero(groupMemberInfo GroupMember, args map[string]interface{}) error {
|
||||
// return DB.DB.MysqlDB.DefaultGormDB().Model(groupMemberInfo).Updates(args).Error
|
||||
//}
|
||||
func (g *GroupMemberGorm) TakeOwner(ctx context.Context, groupID string, tx ...any) (groupMember *relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "groupMember", *groupMember)
|
||||
}()
|
||||
groupMember = &relation.GroupMemberModel{}
|
||||
return groupMember, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and role_level = ?", groupID, constant.GroupOwner).Take(groupMember).Error, "")
|
||||
}
|
||||
|
||||
//
|
||||
//func SelectGroupList(groupID string) ([]string, error) {
|
||||
// var groupUserID string
|
||||
// var groupList []string
|
||||
// dbConn, err := DB.DB.MysqlDB.DefaultGormDB()
|
||||
// if err != nil {
|
||||
// return groupList, err
|
||||
// }
|
||||
//
|
||||
// rows, err := dbConn.Model(&GroupMember{}).Where("group_id = ?", groupID).Select("user_id").Rows()
|
||||
// if err != nil {
|
||||
// return groupList, err
|
||||
// }
|
||||
// defer rows.Close()
|
||||
// for rows.Next() {
|
||||
// rows.Scan(&groupUserID)
|
||||
// groupList = append(groupList, groupUserID)
|
||||
// }
|
||||
// return groupList, nil
|
||||
//}
|
||||
func (g *GroupMemberGorm) SearchMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32, tx ...any) (total uint32, groupList []*relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "keyword", keyword, "groupIDs", groupIDs, "userIDs", userIDs, "roleLevels", roleLevels, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groupList", groupList)
|
||||
}()
|
||||
db := getDBConn(g.DB, tx)
|
||||
gormIn(&db, "group_id", groupIDs)
|
||||
gormIn(&db, "user_id", userIDs)
|
||||
gormIn(&db, "role_level", roleLevels)
|
||||
return gormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) MapGroupMemberNum(ctx context.Context, groupIDs []string, tx ...any) (count map[string]uint32, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "count", count)
|
||||
}()
|
||||
return mapCount(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs), "group_id")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) FindJoinUserID(ctx context.Context, groupIDs []string, tx ...any) (groupUsers map[string][]string, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groupUsers", groupUsers)
|
||||
}()
|
||||
var items []struct {
|
||||
GroupID string `gorm:"group_id"`
|
||||
UserID string `gorm:"user_id"`
|
||||
}
|
||||
if err := getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id in (?)", groupIDs).Find(&items).Error; err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
groupUsers = make(map[string][]string)
|
||||
for _, item := range items {
|
||||
groupUsers[item.GroupID] = append(groupUsers[item.GroupID], item.UserID)
|
||||
}
|
||||
return groupUsers, nil
|
||||
}
|
||||
|
@ -1,156 +0,0 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GroupMemberGorm struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func NewGroupMemberDB(db *gorm.DB) *GroupMemberGorm {
|
||||
return &GroupMemberGorm{DB: db}
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) Create(ctx context.Context, groupMemberList []*relation.GroupMemberModel, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupMemberList", groupMemberList)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Create(&groupMemberList).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) Delete(ctx context.Context, groupID string, userIDs []string, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userIDs", userIDs)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id in (?)", groupID, userIDs).Delete(&relation.GroupMemberModel{}).Error, "")
|
||||
}
|
||||
|
||||
//func (g *GroupMemberGorm) Delete(ctx context.Context, groupMembers []*relation.GroupMemberModel, tx ...*gorm.DB) (err error) {
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupMembers", groupMembers)
|
||||
// }()
|
||||
// return utils.Wrap(getDBConn(g.DB, tx).Delete(groupMembers).Error, "")
|
||||
//}
|
||||
|
||||
func (g *GroupMemberGorm) DeleteGroup(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.GroupMemberModel{}).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) UpdateByMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "args", args)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) Update(ctx context.Context, groupID string, userID string, data map[string]any, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "data", data)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(data).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) UpdateRoleLevel(ctx context.Context, groupID string, userID string, roleLevel int32, tx ...*gorm.DB) (rowsAffected int64, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "roleLevel", roleLevel)
|
||||
}()
|
||||
db := getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(map[string]any{
|
||||
"role_level": roleLevel,
|
||||
})
|
||||
return db.RowsAffected, utils.Wrap(db.Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) Find(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, tx ...*gorm.DB) (groupList []*relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "userIDs", userIDs, "groupList", groupList)
|
||||
}()
|
||||
db := getDBConn(g.DB, tx)
|
||||
if len(groupIDs) > 0 {
|
||||
db = db.Where("group_id in (?)", groupIDs)
|
||||
}
|
||||
if len(userIDs) > 0 {
|
||||
db = db.Where("user_id in (?)", userIDs)
|
||||
}
|
||||
if len(roleLevels) > 0 {
|
||||
db = db.Where("role_level in (?)", roleLevels)
|
||||
}
|
||||
return groupList, utils.Wrap(db.Find(&groupList).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) FindGroupUser(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, tx ...*gorm.DB) (groupList []*relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "userIDs", userIDs, "groupList", groupList)
|
||||
}()
|
||||
db := getDBConn(g.DB, tx)
|
||||
if len(groupList) > 0 {
|
||||
db = db.Where("group_id in (?)", groupIDs)
|
||||
}
|
||||
if len(userIDs) > 0 {
|
||||
db = db.Where("user_id in (?)", userIDs)
|
||||
}
|
||||
if len(roleLevels) > 0 {
|
||||
db = db.Where("role_level in (?)", roleLevels)
|
||||
}
|
||||
return groupList, utils.Wrap(db.Find(&groupList).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) Take(ctx context.Context, groupID string, userID string, tx ...*gorm.DB) (groupMember *relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "groupMember", *groupMember)
|
||||
}()
|
||||
groupMember = &relation.GroupMemberModel{}
|
||||
return groupMember, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id = ?", groupID, userID).Take(groupMember).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) TakeOwner(ctx context.Context, groupID string, tx ...*gorm.DB) (groupMember *relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "groupMember", *groupMember)
|
||||
}()
|
||||
groupMember = &relation.GroupMemberModel{}
|
||||
return groupMember, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and role_level = ?", groupID, constant.GroupOwner).Take(groupMember).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) SearchMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32, tx ...*gorm.DB) (total int32, groupList []*relation.GroupMemberModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "keyword", keyword, "groupIDs", groupIDs, "userIDs", userIDs, "roleLevels", roleLevels, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groupList", groupList)
|
||||
}()
|
||||
db := getDBConn(g.DB, tx)
|
||||
gormIn(&db, "group_id", groupIDs)
|
||||
gormIn(&db, "user_id", userIDs)
|
||||
gormIn(&db, "role_level", roleLevels)
|
||||
return gormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) MapGroupMemberNum(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (count map[string]uint32, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "count", count)
|
||||
}()
|
||||
return mapCount(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs), "group_id")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) FindJoinUserID(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (groupUsers map[string][]string, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groupUsers", groupUsers)
|
||||
}()
|
||||
var items []struct {
|
||||
GroupID string `gorm:"group_id"`
|
||||
UserID string `gorm:"user_id"`
|
||||
}
|
||||
if err := getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id in (?)", groupIDs).Find(&items).Error; err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
groupUsers = make(map[string][]string)
|
||||
for _, item := range items {
|
||||
groupUsers[item.GroupID] = append(groupUsers[item.GroupID], item.UserID)
|
||||
}
|
||||
return groupUsers, nil
|
||||
}
|
@ -1,98 +1,69 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"fmt"
|
||||
|
||||
"time"
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func InsertIntoGroup(groupInfo Group) error {
|
||||
if groupInfo.GroupName == "" {
|
||||
groupInfo.GroupName = "Group Chat"
|
||||
}
|
||||
groupInfo.CreateTime = time.Now()
|
||||
var _ relation.GroupModelInterface = (*GroupGorm)(nil)
|
||||
|
||||
if groupInfo.NotificationUpdateTime.Unix() < 0 {
|
||||
groupInfo.NotificationUpdateTime = utils.UnixSecondToTime(0)
|
||||
}
|
||||
err := GroupDB.Create(groupInfo).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
type GroupGorm struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func TakeGroupInfoByGroupID(groupID string) (*Group, error) {
|
||||
var groupInfo Group
|
||||
err := GroupDB.Where("group_id=?", groupID).Take(&groupInfo).Error
|
||||
return &groupInfo, err
|
||||
func NewGroupDB(db *gorm.DB) relation.GroupModelInterface {
|
||||
return &GroupGorm{DB: db}
|
||||
}
|
||||
|
||||
func GetGroupInfoByGroupID(groupID string) (*Group, error) {
|
||||
var groupInfo Group
|
||||
err := GroupDB.Where("group_id=?", groupID).Take(&groupInfo).Error
|
||||
return &groupInfo, err
|
||||
func (g *GroupGorm) Create(ctx context.Context, groups []*relation.GroupModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Create(&groups).Error, "")
|
||||
}
|
||||
|
||||
func SetGroupInfo(groupInfo Group) error {
|
||||
return GroupDB.Where("group_id=?", groupInfo.GroupID).Updates(&groupInfo).Error
|
||||
//func (g *GroupGorm) Delete(ctx context.Context, groupIDs []string, tx ...any) (err error) {
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs)
|
||||
// }()
|
||||
// return utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.GroupModel{}).Error, "")
|
||||
//}
|
||||
|
||||
func (g *GroupGorm) UpdateMap(ctx context.Context, groupID string, args map[string]interface{}, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "args", args)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
type GroupWithNum struct {
|
||||
Group
|
||||
MemberCount int `gorm:"column:num"`
|
||||
func (g *GroupGorm) UpdateStatus(ctx context.Context, groupID string, status int32, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "status", status)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(map[string]any{"status": status}).Error, "")
|
||||
}
|
||||
|
||||
func GetGroupsByName(groupName string, pageNumber, showNumber int32) ([]GroupWithNum, int64, error) {
|
||||
var groups []GroupWithNum
|
||||
var count int64
|
||||
sql := GroupDB.Select("groups.*, (select count(*) from group_members where group_members.group_id=groups.group_id) as num").
|
||||
Where(" name like ? and status != ?", fmt.Sprintf("%%%s%%", groupName), constant.GroupStatusDismissed)
|
||||
if err := sql.Count(&count).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
err := sql.Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&groups).Error
|
||||
return groups, count, err
|
||||
func (g *GroupGorm) Find(ctx context.Context, groupIDs []string, tx ...any) (groups []*relation.GroupModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groups", groups)
|
||||
}()
|
||||
return groups, utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Find(&groups).Error, "")
|
||||
}
|
||||
|
||||
func GetGroups(pageNumber, showNumber int) ([]GroupWithNum, error) {
|
||||
var groups []GroupWithNum
|
||||
if err := GroupDB.Select("groups.*, (select count(*) from group_members where group_members.group_id=groups.group_id) as num").
|
||||
Limit(showNumber).Offset(showNumber * (pageNumber - 1)).Find(&groups).Error; err != nil {
|
||||
return groups, err
|
||||
}
|
||||
return groups, nil
|
||||
func (g *GroupGorm) Take(ctx context.Context, groupID string, tx ...any) (group *relation.GroupModel, err error) {
|
||||
group = &relation.GroupModel{}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", group)
|
||||
}()
|
||||
return group, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Take(group).Error, "")
|
||||
}
|
||||
|
||||
func OperateGroupStatus(groupId string, groupStatus int32) error {
|
||||
group := Group{
|
||||
GroupID: groupId,
|
||||
Status: groupStatus,
|
||||
}
|
||||
if err := SetGroupInfo(group); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetGroupsCountNum(group Group) (int32, error) {
|
||||
var count int64
|
||||
if err := GroupDB.Where(" name like ? ", fmt.Sprintf("%%%s%%", group.GroupName)).Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int32(count), nil
|
||||
}
|
||||
|
||||
func UpdateGroupInfoDefaultZero(groupID string, args map[string]interface{}) error {
|
||||
return GroupDB.Where("group_id = ? ", groupID).Updates(args).Error
|
||||
}
|
||||
|
||||
func GetGroupIDListByGroupType(groupType int) ([]string, error) {
|
||||
var groupIDList []string
|
||||
if err := GroupDB.Where("group_type = ? ", groupType).Pluck("group_id", &groupIDList).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return groupIDList, nil
|
||||
func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32, tx ...any) (total uint32, groups []*relation.GroupModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "keyword", keyword, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groups", groups)
|
||||
}()
|
||||
return gormSearch[relation.GroupModel](getDBConn(g.DB, tx), []string{"name"}, keyword, pageNumber, showNumber)
|
||||
}
|
||||
|
@ -1,74 +0,0 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GroupGorm struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func NewGroupDB(db *gorm.DB) *GroupGorm {
|
||||
return &GroupGorm{DB: db}
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Create(ctx context.Context, groups []*relation.GroupModel, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Create(&groups).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Delete(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.GroupModel{}).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupGorm) UpdateMap(ctx context.Context, groupID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "args", args)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupGorm) UpdateStatus(ctx context.Context, groupID string, status int32, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "status", status)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(map[string]any{"status": status}).Error, "")
|
||||
}
|
||||
|
||||
//func (g *GroupGorm) Update(ctx context.Context, groups []*relation.GroupModel, tx ...*gorm.DB) (err error) {
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
|
||||
// }()
|
||||
// return utils.Wrap(getDBConn(g.DB, tx).Updates(&groups).Error, "")
|
||||
//}
|
||||
|
||||
func (g *GroupGorm) Find(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (groups []*relation.GroupModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groups", groups)
|
||||
}()
|
||||
return groups, utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Find(&groups).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Take(ctx context.Context, groupID string, tx ...*gorm.DB) (group *relation.GroupModel, err error) {
|
||||
group = &relation.GroupModel{}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", group)
|
||||
}()
|
||||
return group, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Take(group).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32, tx ...*gorm.DB) (total int32, groups []*relation.GroupModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "keyword", keyword, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groups", groups)
|
||||
}()
|
||||
return gormSearch[relation.GroupModel](getDBConn(g.DB, tx), []string{"name"}, keyword, pageNumber, showNumber)
|
||||
}
|
@ -1,158 +1,85 @@
|
||||
package relation
|
||||
|
||||
//
|
||||
//func UpdateGroupRequest(groupRequest GroupRequest) error {
|
||||
// if groupRequest.HandledTime.Unix() < 0 {
|
||||
// groupRequest.HandledTime = utils.UnixSecondToTime(0)
|
||||
// }
|
||||
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("group_id=? and user_id=?", groupRequest.GroupID, groupRequest.UserID).Updates(&groupRequest).Error
|
||||
import (
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var _ relation.GroupRequestModelInterface = (*GroupRequestGorm)(nil)
|
||||
|
||||
type GroupRequestGorm struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func NewGroupRequest(db *gorm.DB) relation.GroupRequestModelInterface {
|
||||
return &GroupRequestGorm{
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) Create(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Create(&groupRequests).Error, utils.GetSelfFuncName())
|
||||
}
|
||||
|
||||
//func (g *GroupRequestGorm) Delete(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...any) (err error) {
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests)
|
||||
// }()
|
||||
// return utils.Wrap(getDBConn(g.DB, tx).Delete(&groupRequests).Error, utils.GetSelfFuncName())
|
||||
//}
|
||||
//
|
||||
//func InsertIntoGroupRequest(toInsertInfo GroupRequest) error {
|
||||
// DelGroupRequestByGroupIDAndUserID(toInsertInfo.GroupID, toInsertInfo.UserID)
|
||||
// if toInsertInfo.HandledTime.Unix() < 0 {
|
||||
// toInsertInfo.HandledTime = utils.UnixSecondToTime(0)
|
||||
// }
|
||||
// u := DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("group_id=? and user_id=?", toInsertInfo.GroupID, toInsertInfo.UserID).Updates(&toInsertInfo)
|
||||
// if u.RowsAffected != 0 {
|
||||
// return nil
|
||||
// }
|
||||
//
|
||||
// toInsertInfo.ReqTime = time.Now()
|
||||
// if toInsertInfo.HandledTime.Unix() < 0 {
|
||||
// toInsertInfo.HandledTime = utils.UnixSecondToTime(0)
|
||||
// }
|
||||
//
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Create(&toInsertInfo).Error
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return nil
|
||||
|
||||
//func (g *GroupRequestGorm) UpdateMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...any) (err error) {
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "args", args)
|
||||
// }()
|
||||
// return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupRequestModel{}).Where("group_id = ? and user_id = ? ", groupID, userID).Updates(args).Error, utils.GetSelfFuncName())
|
||||
//}
|
||||
//
|
||||
//func GetGroupRequestByGroupIDAndUserID(groupID, userID string) (*GroupRequest, error) {
|
||||
// var groupRequest GroupRequest
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("user_id=? and group_id=?", userID, groupID).Take(&groupRequest).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return &groupRequest, nil
|
||||
|
||||
func (g *GroupRequestGorm) UpdateHandler(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "handledMsg", handledMsg, "handleResult", handleResult)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupRequestModel{}).Where("group_id = ? and user_id = ? ", groupID, userID).Updates(map[string]any{
|
||||
"handle_msg": handledMsg,
|
||||
"handle_result": handleResult,
|
||||
}).Error, utils.GetSelfFuncName())
|
||||
}
|
||||
|
||||
//func (g *GroupRequestGorm) Update(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...any) (err error) {
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests)
|
||||
// }()
|
||||
// return utils.Wrap(getDBConn(g.DB, tx).Updates(&groupRequests).Error, utils.GetSelfFuncName())
|
||||
//}
|
||||
//
|
||||
//func DelGroupRequestByGroupIDAndUserID(groupID, userID string) error {
|
||||
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("group_id=? and user_id=?", groupID, userID).Delete(GroupRequest{}).Error
|
||||
//}
|
||||
//
|
||||
//func GetGroupRequestByGroupID(groupID string) ([]GroupRequest, error) {
|
||||
// var groupRequestList []GroupRequest
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("group_id=?", groupID).Find(&groupRequestList).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return groupRequestList, nil
|
||||
//}
|
||||
//
|
||||
//received
|
||||
//func GetRecvGroupApplicationList(userID string) ([]GroupRequest, error) {
|
||||
// var groupRequestList []GroupRequest
|
||||
// memberList, err := FindGroupMember(userID)
|
||||
// if err != nil {
|
||||
// return nil, utils.Wrap(err, utils.GetSelfFuncName())
|
||||
// }
|
||||
// for _, v := range memberList {
|
||||
// if v.RoleLevel > constant.GroupOrdinaryUsers {
|
||||
// list, err := GetGroupRequestByGroupID(v.GroupID)
|
||||
// if err != nil {
|
||||
// continue
|
||||
// }
|
||||
// groupRequestList = append(groupRequestList, list...)
|
||||
// }
|
||||
// }
|
||||
// return groupRequestList, nil
|
||||
//}
|
||||
//
|
||||
//func GetUserReqGroupByUserID(userID string) ([]GroupRequest, error) {
|
||||
// var groupRequestList []GroupRequest
|
||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("user_id=?", userID).Find(&groupRequestList).Error
|
||||
// return groupRequestList, err
|
||||
//}
|
||||
//
|
||||
//
|
||||
//func GroupApplicationResponse(pb *group.GroupApplicationResponseReq) (*group.CommonResp, error) {
|
||||
//
|
||||
// ownerUser, err := FindGroupMemberInfoByGroupIdAndUserId(pb.GroupID, pb.OwnerID)
|
||||
// if err != nil {
|
||||
// log.ErrorByKv("FindGroupMemberInfoByGroupIdAndUserId failed", pb.OperationID, "groupId", pb.GroupID, "ownerID", pb.OwnerID)
|
||||
// return nil, err
|
||||
// }
|
||||
// if ownerUser.AdministratorLevel <= 0 {
|
||||
// return nil, errors.New("insufficient permissions")
|
||||
// }
|
||||
//
|
||||
// dbConn, err := DB.DB.MysqlDB.DefaultGormDB()
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// var groupRequest GroupRequest
|
||||
// err = dbConn.Raw("select * from `group_request` where handled_user = ? and group_id = ? and from_user_id = ? and to_user_id = ?",
|
||||
// "", pb.GroupID, pb.FromUserID, pb.ToUserID).Scan(&groupRequest).Error
|
||||
// if err != nil {
|
||||
// log.ErrorByKv("find group_request info failed", pb.OperationID, "groupId", pb.GroupID, "fromUserId", pb.FromUserID, "toUserId", pb.OwnerID)
|
||||
// return nil, err
|
||||
// }
|
||||
//
|
||||
// if groupRequest.Flag != 0 {
|
||||
// return nil, errors.New("application has already handle")
|
||||
// }
|
||||
//
|
||||
// var saveFlag int
|
||||
// if pb.HandleResult == 0 {
|
||||
// saveFlag = -1
|
||||
// } else if pb.HandleResult == 1 {
|
||||
// saveFlag = 1
|
||||
// } else {
|
||||
// return nil, errors.New("parma HandleResult error")
|
||||
// }
|
||||
// err = dbConn.Exec("update `group_request` set flag = ?, handled_msg = ?, handled_user = ? where group_id = ? and from_user_id = ? and to_user_id = ?",
|
||||
// saveFlag, pb.HandledMsg, pb.OwnerID, groupRequest.GroupID, groupRequest.FromUserID, groupRequest.ToUserID).Error
|
||||
// if err != nil {
|
||||
// log.ErrorByKv("update group request failed", pb.OperationID, "groupID", pb.GroupID, "flag", saveFlag, "ownerId", pb.OwnerID, "fromUserId", pb.FromUserID, "toUserID", pb.ToUserID)
|
||||
// return nil, err
|
||||
// }
|
||||
//
|
||||
// if saveFlag == 1 {
|
||||
// if groupRequest.ToUserID == "0" {
|
||||
// err = InsertIntoGroupMember(pb.GroupID, pb.FromUserID, groupRequest.FromUserNickname, groupRequest.FromUserFaceUrl, 0)
|
||||
// if err != nil {
|
||||
// log.ErrorByKv("InsertIntoGroupMember failed", pb.OperationID, "groupID", pb.GroupID, "fromUserId", pb.FromUserID)
|
||||
// return nil, err
|
||||
// }
|
||||
// } else {
|
||||
// err = InsertIntoGroupMember(pb.GroupID, pb.ToUserID, groupRequest.ToUserNickname, groupRequest.ToUserFaceUrl, 0)
|
||||
// if err != nil {
|
||||
// log.ErrorByKv("InsertIntoGroupMember failed", pb.OperationID, "groupID", pb.GroupID, "fromUserId", pb.FromUserID)
|
||||
// return nil, err
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return &group.GroupApplicationResponseResp{}, nil
|
||||
//}
|
||||
//
|
||||
//func FindGroupBeInvitedRequestInfoByUidAndGroupID(groupId, uid string) (*GroupRequest, error) {
|
||||
// dbConn, err := DB.DB.MysqlDB.DefaultGormDB()
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// var beInvitedRequestUserInfo GroupRequest
|
||||
// err = dbConn.Table("group_request").Where("to_user_id=? and group_id=?", uid, groupId).Find(&beInvitedRequestUserInfo).Error
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return &beInvitedRequestUserInfo, nil
|
||||
//
|
||||
//}
|
||||
//
|
||||
//func InsertGroupRequest(groupId, fromUser, fromUserNickName, fromUserFaceUrl, toUser, requestMsg, handledMsg string, handleStatus int) error {
|
||||
// return nil
|
||||
|
||||
//func (g *GroupRequestGorm) Find(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...any) (resultGroupRequests []*relation.GroupRequestModel, err error) {
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests, "resultGroupRequests", resultGroupRequests)
|
||||
// }()
|
||||
// var where [][]interface{}
|
||||
// for _, groupMember := range groupRequests {
|
||||
// where = append(where, []interface{}{groupMember.GroupID, groupMember.UserID})
|
||||
// }
|
||||
// return resultGroupRequests, utils.Wrap(getDBConn(g.DB, tx).Where("(group_id, user_id) in ?", where).Find(&resultGroupRequests).Error, utils.GetSelfFuncName())
|
||||
//}
|
||||
|
||||
func (g *GroupRequestGorm) Take(ctx context.Context, groupID string, userID string, tx ...any) (groupRequest *relation.GroupRequestModel, err error) {
|
||||
groupRequest = &relation.GroupRequestModel{}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "groupRequest", *groupRequest)
|
||||
}()
|
||||
return groupRequest, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id = ? ", groupID, userID).Take(groupRequest).Error, utils.GetSelfFuncName())
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) Page(ctx context.Context, userID string, pageNumber, showNumber int32, tx ...any) (total uint32, groups []*relation.GroupRequestModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groups", groups)
|
||||
}()
|
||||
return gormSearch[relation.GroupRequestModel](getDBConn(g.DB, tx).Where("user_id = ?", userID), nil, "", pageNumber, showNumber)
|
||||
}
|
||||
|
@ -1,83 +0,0 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GroupRequestGorm struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func NewGroupRequest(db *gorm.DB) *GroupRequestGorm {
|
||||
return &GroupRequestGorm{
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) Create(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Create(&groupRequests).Error, utils.GetSelfFuncName())
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) Delete(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Delete(&groupRequests).Error, utils.GetSelfFuncName())
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) UpdateMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "args", args)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupRequestModel{}).Where("group_id = ? and user_id = ? ", groupID, userID).Updates(args).Error, utils.GetSelfFuncName())
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) UpdateHandler(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "handledMsg", handledMsg, "handleResult", handleResult)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupRequestModel{}).Where("group_id = ? and user_id = ? ", groupID, userID).Updates(map[string]any{
|
||||
"handle_msg": handledMsg,
|
||||
"handle_result": handleResult,
|
||||
}).Error, utils.GetSelfFuncName())
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) Update(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Updates(&groupRequests).Error, utils.GetSelfFuncName())
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) Find(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...*gorm.DB) (resultGroupRequests []*relation.GroupRequestModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests, "resultGroupRequests", resultGroupRequests)
|
||||
}()
|
||||
var where [][]interface{}
|
||||
for _, groupMember := range groupRequests {
|
||||
where = append(where, []interface{}{groupMember.GroupID, groupMember.UserID})
|
||||
}
|
||||
return resultGroupRequests, utils.Wrap(getDBConn(g.DB, tx).Where("(group_id, user_id) in ?", where).Find(&resultGroupRequests).Error, utils.GetSelfFuncName())
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) Take(ctx context.Context, groupID string, userID string, tx ...*gorm.DB) (groupRequest *relation.GroupRequestModel, err error) {
|
||||
groupRequest = &relation.GroupRequestModel{}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "groupRequest", *groupRequest)
|
||||
}()
|
||||
return groupRequest, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id = ? ", groupID, userID).Take(groupRequest).Error, utils.GetSelfFuncName())
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) Page(ctx context.Context, userID string, pageNumber, showNumber int32, tx ...*gorm.DB) (total int32, groups []*relation.GroupRequestModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groups", groups)
|
||||
}()
|
||||
return gormSearch[relation.GroupRequestModel](getDBConn(g.DB, tx).Where("user_id = ?", userID), nil, "", pageNumber, showNumber)
|
||||
}
|
@ -87,9 +87,11 @@ func (w Writer) Printf(format string, args ...interface{}) {
|
||||
fmt.Printf(format, args...)
|
||||
}
|
||||
|
||||
func getDBConn(db *gorm.DB, tx []*gorm.DB) *gorm.DB {
|
||||
func getDBConn(db *gorm.DB, tx []any) *gorm.DB {
|
||||
if len(tx) > 0 {
|
||||
return tx[0]
|
||||
if txDb, ok := tx[0].(*gorm.DB); ok {
|
||||
return txDb
|
||||
}
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func gormPage[E any](db *gorm.DB, pageNumber, showNumber int32) (int32, []*E, error) {
|
||||
func gormPage[E any](db *gorm.DB, pageNumber, showNumber int32) (uint32, []*E, error) {
|
||||
var count int64
|
||||
if err := db.Model(new(E)).Count(&count).Error; err != nil {
|
||||
return 0, nil, utils.Wrap(err, "")
|
||||
@ -14,10 +14,10 @@ func gormPage[E any](db *gorm.DB, pageNumber, showNumber int32) (int32, []*E, er
|
||||
if err := db.Limit(int(showNumber)).Offset(int(pageNumber * showNumber)).Find(&es).Error; err != nil {
|
||||
return 0, nil, utils.Wrap(err, "")
|
||||
}
|
||||
return int32(count), es, nil
|
||||
return uint32(count), es, nil
|
||||
}
|
||||
|
||||
func gormSearch[E any](db *gorm.DB, fields []string, value string, pageNumber, showNumber int32) (int32, []*E, error) {
|
||||
func gormSearch[E any](db *gorm.DB, fields []string, value string, pageNumber, showNumber int32) (uint32, []*E, error) {
|
||||
if len(fields) > 0 && value != "" {
|
||||
value = "%" + value + "%"
|
||||
if len(fields) == 1 {
|
||||
|
@ -1,6 +1,9 @@
|
||||
package relation
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
GroupModelTableName = "groups"
|
||||
@ -29,4 +32,11 @@ func (GroupModel) TableName() string {
|
||||
}
|
||||
|
||||
type GroupModelInterface interface {
|
||||
Create(ctx context.Context, groups []*GroupModel, tx ...any) (err error)
|
||||
//Delete(ctx context.Context, groupIDs []string, tx ...any) (err error)
|
||||
UpdateMap(ctx context.Context, groupID string, args map[string]interface{}, tx ...any) (err error)
|
||||
UpdateStatus(ctx context.Context, groupID string, status int32, tx ...any) (err error)
|
||||
Find(ctx context.Context, groupIDs []string, tx ...any) (groups []*GroupModel, err error)
|
||||
Take(ctx context.Context, groupID string, tx ...any) (group *GroupModel, err error)
|
||||
Search(ctx context.Context, keyword string, pageNumber, showNumber int32, tx ...any) (total uint32, groups []*GroupModel, err error)
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package relation
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
GroupMemberModelTableName = "group_members"
|
||||
@ -25,4 +28,17 @@ func (GroupMemberModel) TableName() string {
|
||||
}
|
||||
|
||||
type GroupMemberModelInterface interface {
|
||||
Create(ctx context.Context, groupMemberList []*GroupMemberModel, tx ...any) (err error)
|
||||
Delete(ctx context.Context, groupID string, userIDs []string, tx ...any) (err error)
|
||||
DeleteGroup(ctx context.Context, groupIDs []string, tx ...any) (err error)
|
||||
//UpdateByMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...any) (err error)
|
||||
Update(ctx context.Context, groupID string, userID string, data map[string]any, tx ...any) (err error)
|
||||
UpdateRoleLevel(ctx context.Context, groupID string, userID string, roleLevel int32, tx ...any) (rowsAffected int64, err error)
|
||||
Find(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, tx ...any) (groupList []*GroupMemberModel, err error)
|
||||
//FindGroupUser(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, tx ...any) (groupList []*GroupMemberModel, err error)
|
||||
Take(ctx context.Context, groupID string, userID string, tx ...any) (groupMember *GroupMemberModel, err error)
|
||||
TakeOwner(ctx context.Context, groupID string, tx ...any) (groupMember *GroupMemberModel, err error)
|
||||
SearchMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32, tx ...any) (total uint32, groupList []*GroupMemberModel, err error)
|
||||
MapGroupMemberNum(ctx context.Context, groupIDs []string, tx ...any) (count map[string]uint32, err error)
|
||||
FindJoinUserID(ctx context.Context, groupIDs []string, tx ...any) (groupUsers map[string][]string, err error)
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package relation
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
GroupRequestModelTableName = "group_requests"
|
||||
@ -25,4 +28,12 @@ func (GroupRequestModel) TableName() string {
|
||||
}
|
||||
|
||||
type GroupRequestModelInterface interface {
|
||||
Create(ctx context.Context, groupRequests []*GroupRequestModel, tx ...any) (err error)
|
||||
//Delete(ctx context.Context, groupRequests []*GroupRequestModel, tx ...any) (err error)
|
||||
//UpdateMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...any) (err error)
|
||||
UpdateHandler(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, tx ...any) (err error)
|
||||
//Update(ctx context.Context, groupRequests []*GroupRequestModel, tx ...any) (err error)
|
||||
//Find(ctx context.Context, groupRequests []*GroupRequestModel, tx ...any) (resultGroupRequests []*GroupRequestModel, err error)
|
||||
Take(ctx context.Context, groupID string, userID string, tx ...any) (groupRequest *GroupRequestModel, err error)
|
||||
Page(ctx context.Context, userID string, pageNumber, showNumber int32, tx ...any) (total uint32, groups []*GroupRequestModel, err error)
|
||||
}
|
||||
|
@ -28,11 +28,22 @@ func (UserToSuperGroupModel) TableName() string {
|
||||
}
|
||||
|
||||
type SuperGroupModelInterface interface {
|
||||
// tx is your transaction object
|
||||
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...interface{}) error
|
||||
GetSuperGroup(ctx context.Context, groupID string) (SuperGroupModel, error)
|
||||
AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...interface{}) error
|
||||
RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...interface{}) error
|
||||
GetSuperGroupByUserID(ctx context.Context, userID string) (*UserToSuperGroupModel, error)
|
||||
DeleteSuperGroup(ctx context.Context, groupID string, tx ...interface{}) error
|
||||
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...any) error
|
||||
FindSuperGroup(ctx context.Context, groupIDs []string, tx ...any) (groups []*SuperGroupModel, err error)
|
||||
AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error
|
||||
RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error
|
||||
GetSuperGroupByUserID(ctx context.Context, userID string, tx ...any) (*UserToSuperGroupModel, error)
|
||||
DeleteSuperGroup(ctx context.Context, groupID string, tx ...any) error
|
||||
RemoveGroupFromUser(ctx context.Context, groupID string, userIDs []string, tx ...any) error
|
||||
}
|
||||
|
||||
//type SuperGroupModelInterface interface {
|
||||
// // tx is your transaction object
|
||||
// CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...interface{}) error
|
||||
// FindSuperGroup(ctx context.Context, groupIDs []string, tx ...interface{}) ([]*SuperGroupModel, error)
|
||||
// //GetSuperGroup(ctx context.Context, groupID string) (SuperGroupModel, error)
|
||||
// AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...interface{}) error
|
||||
// RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...interface{}) error
|
||||
// GetSuperGroupByUserID(ctx context.Context, userID string, tx ...interface{}) (*UserToSuperGroupModel, error)
|
||||
// DeleteSuperGroup(ctx context.Context, groupID string, tx ...interface{}) error
|
||||
//}
|
||||
|
@ -11,6 +11,13 @@ import (
|
||||
"go.mongodb.org/mongo-driver/mongo/readconcern"
|
||||
)
|
||||
|
||||
var _ unrelation.SuperGroupModelInterface = (*SuperGroupMongoDriver)(nil)
|
||||
|
||||
func NewSuperGroupMongoDriver(mgoClient *mongo.Client) *SuperGroupMongoDriver {
|
||||
mgoDB := mgoClient.Database(config.Config.Mongo.DBDatabase)
|
||||
return &SuperGroupMongoDriver{MgoDB: mgoDB, MgoClient: mgoClient, superGroupCollection: mgoDB.Collection(unrelation.CSuperGroup), userToSuperGroupCollection: mgoDB.Collection(unrelation.CUserToSuperGroup)}
|
||||
}
|
||||
|
||||
type SuperGroupMongoDriver struct {
|
||||
MgoClient *mongo.Client
|
||||
MgoDB *mongo.Database
|
||||
@ -18,44 +25,178 @@ type SuperGroupMongoDriver struct {
|
||||
userToSuperGroupCollection *mongo.Collection
|
||||
}
|
||||
|
||||
func NewSuperGroupMongoDriver(mgoClient *mongo.Client) *SuperGroupMongoDriver {
|
||||
mgoDB := mgoClient.Database(config.Config.Mongo.DBDatabase)
|
||||
return &SuperGroupMongoDriver{MgoDB: mgoDB, MgoClient: mgoClient, superGroupCollection: mgoDB.Collection(unrelation.CSuperGroup), userToSuperGroupCollection: mgoDB.Collection(unrelation.CUserToSuperGroup)}
|
||||
}
|
||||
// func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...interface{}) error {
|
||||
// superGroup := unrelation.SuperGroupModel{
|
||||
// GroupID: groupID,
|
||||
// MemberIDs: initMemberIDs,
|
||||
// }
|
||||
// coll := getTxCtx(s.superGroupCollection, tx)
|
||||
// _, err := coll.InsertOne(ctx, superGroup)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// opts := &options.UpdateOptions{
|
||||
// Upsert: utils.ToPtr(true),
|
||||
// }
|
||||
// for _, userID := range initMemberIDs {
|
||||
// _, err = coll.UpdateOne(ctx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
//
|
||||
// func (s *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []string, tx ...interface{}) (groups []*unrelation.SuperGroupModel, err error) {
|
||||
// cursor, err := s.superGroupCollection.Find(ctx, bson.M{"group_id": bson.M{
|
||||
// "$in": groupIDs,
|
||||
// }})
|
||||
// if err != nil {
|
||||
// return nil, utils.Wrap(err, "")
|
||||
// }
|
||||
// defer cursor.Close(ctx)
|
||||
// if err := cursor.All(ctx, &groups); err != nil {
|
||||
// return nil, utils.Wrap(err, "")
|
||||
// }
|
||||
// return groups, nil
|
||||
// }
|
||||
//
|
||||
// func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...interface{}) error {
|
||||
// opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||
// return s.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||
// _, err := s.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDs}}})
|
||||
// if err != nil {
|
||||
// _ = sCtx.AbortTransaction(ctx)
|
||||
// return err
|
||||
// }
|
||||
// upsert := true
|
||||
// opts := &options.UpdateOptions{
|
||||
// Upsert: &upsert,
|
||||
// }
|
||||
// for _, userID := range userIDs {
|
||||
// _, err = s.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
|
||||
// if err != nil {
|
||||
// _ = sCtx.AbortTransaction(ctx)
|
||||
// return utils.Wrap(err, "transaction failed")
|
||||
// }
|
||||
// }
|
||||
// return sCtx.CommitTransaction(ctx)
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...interface{}) error {
|
||||
// opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||
// return s.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||
// _, err := s.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDs}}})
|
||||
// if err != nil {
|
||||
// _ = sCtx.AbortTransaction(ctx)
|
||||
// return err
|
||||
// }
|
||||
// err = s.RemoveGroupFromUser(sCtx, groupID, userIDs)
|
||||
// if err != nil {
|
||||
// _ = sCtx.AbortTransaction(ctx)
|
||||
// return err
|
||||
// }
|
||||
// return sCtx.CommitTransaction(ctx)
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// func (s *SuperGroupMongoDriver) GetSuperGroupByUserID(ctx context.Context, userID string, tx ...interface{}) (*unrelation.UserToSuperGroupModel, error) {
|
||||
// //TODO implement me
|
||||
// panic("implement me")
|
||||
// }
|
||||
//
|
||||
// func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string, tx ...interface{}) error {
|
||||
// //TODO implement me
|
||||
// panic("implement me")
|
||||
// }
|
||||
|
||||
func (db *SuperGroupMongoDriver) CreateSuperGroup(sCtx mongo.SessionContext, groupID string, initMemberIDs []string) error {
|
||||
superGroup := unrelation.SuperGroupModel{
|
||||
GroupID: groupID,
|
||||
MemberIDs: initMemberIDs,
|
||||
}
|
||||
_, err := db.superGroupCollection.InsertOne(sCtx, superGroup)
|
||||
func (s *SuperGroupMongoDriver) Transaction(ctx context.Context, fn func(s unrelation.SuperGroupModelInterface, tx any) error) error {
|
||||
sess, err := s.MgoClient.StartSession()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
upsert := true
|
||||
opts := &options.UpdateOptions{
|
||||
Upsert: &upsert,
|
||||
txCtx := mongo.NewSessionContext(ctx, sess)
|
||||
defer sess.EndSession(txCtx)
|
||||
if err := fn(s, txCtx); err != nil {
|
||||
_ = sess.AbortTransaction(txCtx)
|
||||
return err
|
||||
}
|
||||
return utils.Wrap(sess.CommitTransaction(txCtx), "")
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) getTxCtx(ctx context.Context, tx []any) context.Context {
|
||||
if len(tx) > 0 {
|
||||
if ctx, ok := tx[0].(mongo.SessionContext); ok {
|
||||
return ctx
|
||||
}
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
//func (s *SuperGroupMongoDriver) Transaction(ctx context.Context, fn func(ctx mongo.SessionContext) error) error {
|
||||
// sess, err := s.MgoClient.StartSession()
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// sCtx := mongo.NewSessionContext(ctx, sess)
|
||||
//
|
||||
// defer sess.EndSession(sCtx)
|
||||
// if err := fn(sCtx); err != nil {
|
||||
// _ = sess.AbortTransaction(sCtx)
|
||||
// return err
|
||||
// }
|
||||
// return utils.Wrap(sess.CommitTransaction(sCtx), "")
|
||||
//}
|
||||
|
||||
func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...any) error {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
_, err := s.superGroupCollection.InsertOne(ctx, &unrelation.SuperGroupModel{
|
||||
GroupID: groupID,
|
||||
MemberIDs: initMemberIDs,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, userID := range initMemberIDs {
|
||||
_, err = db.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
|
||||
_, err = s.userToSuperGroupCollection.UpdateOne(ctx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, &options.UpdateOptions{
|
||||
Upsert: utils.ToPtr(true),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (db *SuperGroupMongoDriver) GetSuperGroup(ctx context.Context, groupID string) (*unrelation.SuperGroupModel, error) {
|
||||
superGroup := unrelation.SuperGroupModel{}
|
||||
err := db.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&superGroup)
|
||||
return &superGroup, err
|
||||
func (s *SuperGroupMongoDriver) TakeSuperGroup(ctx context.Context, groupID string, tx ...any) (group *unrelation.SuperGroupModel, err error) {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
if err := s.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&group); err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
return group, nil
|
||||
}
|
||||
|
||||
func (db *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
|
||||
func (s *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []string, tx ...any) (groups []*unrelation.SuperGroupModel, err error) {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
cursor, err := s.superGroupCollection.Find(ctx, bson.M{"group_id": bson.M{
|
||||
"$in": groupIDs,
|
||||
}})
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
if err := cursor.All(ctx, &groups); err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||
return db.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||
_, err := db.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDs}}})
|
||||
return s.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||
_, err := s.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDs}}})
|
||||
if err != nil {
|
||||
_ = sCtx.AbortTransaction(ctx)
|
||||
return err
|
||||
@ -65,7 +206,7 @@ func (db *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupI
|
||||
Upsert: &upsert,
|
||||
}
|
||||
for _, userID := range userIDs {
|
||||
_, err = db.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
|
||||
_, err = s.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
|
||||
if err != nil {
|
||||
_ = sCtx.AbortTransaction(ctx)
|
||||
return utils.Wrap(err, "transaction failed")
|
||||
@ -75,15 +216,16 @@ func (db *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupI
|
||||
})
|
||||
}
|
||||
|
||||
func (db *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
|
||||
func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||
return db.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||
_, err := db.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDs}}})
|
||||
return s.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||
_, err := s.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDs}}})
|
||||
if err != nil {
|
||||
_ = sCtx.AbortTransaction(ctx)
|
||||
return err
|
||||
}
|
||||
err = db.RemoveGroupFromUser(sCtx, groupID, userIDs)
|
||||
err = s.RemoveGroupFromUser(sCtx, groupID, userIDs)
|
||||
if err != nil {
|
||||
_ = sCtx.AbortTransaction(ctx)
|
||||
return err
|
||||
@ -92,40 +234,45 @@ func (db *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context,
|
||||
})
|
||||
}
|
||||
|
||||
func (db *SuperGroupMongoDriver) GetSuperGroupByUserID(ctx context.Context, userID string) (*unrelation.UserToSuperGroupModel, error) {
|
||||
func (s *SuperGroupMongoDriver) GetSuperGroupByUserID(ctx context.Context, userID string, tx ...any) (*unrelation.UserToSuperGroupModel, error) {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
var user unrelation.UserToSuperGroupModel
|
||||
err := db.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
|
||||
err := s.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
|
||||
return &user, utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
func (db *SuperGroupMongoDriver) GetJoinGroup(ctx context.Context, userID string, pageNumber, showNumber int32) (int32, []string, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (db *SuperGroupMongoDriver) MapGroupMemberCount(ctx context.Context, groupIDs []string) (map[string]uint32, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (db *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string) error {
|
||||
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||
return db.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||
superGroup := &unrelation.SuperGroupModel{}
|
||||
_, err := db.superGroupCollection.DeleteOne(sCtx, bson.M{"group_id": groupID})
|
||||
func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string, tx ...any) error {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
group, err := s.TakeSuperGroup(ctx, groupID, tx...)
|
||||
if err != nil {
|
||||
_ = sCtx.AbortTransaction(ctx)
|
||||
return err
|
||||
}
|
||||
if err = db.RemoveGroupFromUser(sCtx, groupID, superGroup.MemberIDs); err != nil {
|
||||
_ = sCtx.AbortTransaction(ctx)
|
||||
return err
|
||||
if _, err := s.superGroupCollection.DeleteOne(ctx, bson.M{"group_id": groupID}); err != nil {
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
return sCtx.CommitTransaction(ctx)
|
||||
})
|
||||
return s.RemoveGroupFromUser(ctx, groupID, group.MemberIDs)
|
||||
}
|
||||
|
||||
func (db *SuperGroupMongoDriver) RemoveGroupFromUser(sCtx context.Context, groupID string, userIDs []string) error {
|
||||
_, err := db.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": bson.M{"$in": userIDs}}, bson.M{"$pull": bson.M{"group_id_list": groupID}})
|
||||
return err
|
||||
//func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string, tx ...any) error {
|
||||
// ctx = s.getTxCtx(ctx, tx)
|
||||
// opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||
// return s.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||
// superGroup := &unrelation.SuperGroupModel{}
|
||||
// _, err := s.superGroupCollection.DeleteOne(sCtx, bson.M{"group_id": groupID})
|
||||
// if err != nil {
|
||||
// _ = sCtx.AbortTransaction(ctx)
|
||||
// return err
|
||||
// }
|
||||
// if err = s.RemoveGroupFromUser(sCtx, groupID, superGroup.MemberIDs); err != nil {
|
||||
// _ = sCtx.AbortTransaction(ctx)
|
||||
// return err
|
||||
// }
|
||||
// return sCtx.CommitTransaction(ctx)
|
||||
// })
|
||||
//}
|
||||
|
||||
func (s *SuperGroupMongoDriver) RemoveGroupFromUser(ctx context.Context, groupID string, userIDs []string, tx ...any) error {
|
||||
ctx = s.getTxCtx(ctx, tx)
|
||||
_, err := s.userToSuperGroupCollection.UpdateOne(ctx, bson.M{"user_id": bson.M{"$in": userIDs}}, bson.M{"$pull": bson.M{"group_id_list": groupID}})
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -37,17 +37,17 @@ message GetGroupApplicationListReq {
|
||||
string fromUserID = 2; //owner or admin
|
||||
}
|
||||
message GetGroupApplicationListResp {
|
||||
int32 total = 1;
|
||||
uint32 total = 1;
|
||||
repeated server_api_params.GroupRequest groupRequests = 2;
|
||||
}
|
||||
|
||||
message GetUserReqApplicationListReq{
|
||||
string userID = 1;
|
||||
server_api_params.RequestPagination pagination = 2;
|
||||
server_api_params.RequestPagination pagination = 1;
|
||||
string userID = 2;
|
||||
}
|
||||
|
||||
message GetUserReqApplicationListResp{
|
||||
int32 total = 1;
|
||||
uint32 total = 1;
|
||||
repeated server_api_params.GroupRequest groupRequests = 2;
|
||||
}
|
||||
|
||||
@ -90,15 +90,14 @@ message QuitGroupResp{
|
||||
}
|
||||
|
||||
|
||||
|
||||
message GetGroupMemberListReq {
|
||||
string groupID = 1;
|
||||
int32 filter = 2;
|
||||
server_api_params.RequestPagination pagination = 4;
|
||||
server_api_params.RequestPagination pagination = 1;
|
||||
string groupID = 2;
|
||||
int32 filter = 3;
|
||||
}
|
||||
|
||||
message GetGroupMemberListResp {
|
||||
int32 total = 1;
|
||||
uint32 total = 1;
|
||||
repeated server_api_params.GroupMemberFullInfo members = 2;
|
||||
}
|
||||
|
||||
@ -124,11 +123,11 @@ message KickGroupMemberResp {
|
||||
|
||||
|
||||
message GetJoinedGroupListReq {
|
||||
string fromUserID = 1;
|
||||
server_api_params.RequestPagination pagination = 2;
|
||||
server_api_params.RequestPagination pagination = 1;
|
||||
string fromUserID = 2;
|
||||
}
|
||||
message GetJoinedGroupListResp{
|
||||
int32 total = 1;
|
||||
uint32 total = 1;
|
||||
repeated server_api_params.GroupInfo groups = 2;
|
||||
}
|
||||
|
||||
@ -144,8 +143,8 @@ message InviteUserToGroupResp {
|
||||
|
||||
|
||||
message GetGroupAllMemberReq {
|
||||
string groupID = 1;
|
||||
server_api_params.RequestPagination pagination = 2;
|
||||
server_api_params.RequestPagination pagination = 1;
|
||||
string groupID = 2;
|
||||
}
|
||||
message GetGroupAllMemberResp {
|
||||
repeated server_api_params.GroupMemberFullInfo members = 1;
|
||||
@ -165,8 +164,8 @@ message GetGroupsReq {
|
||||
}
|
||||
|
||||
message GetGroupsResp {
|
||||
repeated CMSGroup groups = 1;
|
||||
int32 GroupNum = 2;
|
||||
uint32 total = 1;
|
||||
repeated CMSGroup groups = 2;
|
||||
}
|
||||
|
||||
message GetGroupMemberReq {
|
||||
@ -174,14 +173,14 @@ message GetGroupMemberReq {
|
||||
}
|
||||
|
||||
message GetGroupMembersCMSReq {
|
||||
string groupID = 1;
|
||||
string userName = 2;
|
||||
server_api_params.RequestPagination pagination = 3;
|
||||
server_api_params.RequestPagination pagination = 1;
|
||||
string groupID = 2;
|
||||
string userName = 3;
|
||||
}
|
||||
|
||||
message GetGroupMembersCMSResp {
|
||||
repeated server_api_params.GroupMemberFullInfo members = 1;
|
||||
int32 memberNums = 2;
|
||||
uint32 total = 1;
|
||||
repeated server_api_params.GroupMemberFullInfo members = 2;
|
||||
}
|
||||
|
||||
message DismissGroupReq{
|
||||
@ -240,13 +239,11 @@ message SetGroupMemberNicknameResp{
|
||||
}
|
||||
|
||||
message GetJoinedSuperGroupListReq {
|
||||
server_api_params.RequestPagination pagination = 1;
|
||||
string userID = 2;
|
||||
string userID = 1;
|
||||
}
|
||||
|
||||
message GetJoinedSuperGroupListResp {
|
||||
int32 total = 1;
|
||||
repeated server_api_params.GroupInfo groups = 2;
|
||||
repeated server_api_params.GroupInfo groups = 1;
|
||||
}
|
||||
|
||||
message GetSuperGroupsInfoReq {
|
||||
@ -276,7 +273,7 @@ message GetGroupAbstractInfoReq{
|
||||
|
||||
message GroupAbstractInfo{
|
||||
string groupID = 1;
|
||||
int32 groupMemberNumber = 2;
|
||||
uint32 groupMemberNumber = 2;
|
||||
uint64 groupMemberListHash = 3;
|
||||
}
|
||||
|
||||
|
@ -322,6 +322,10 @@ func If[T any](isa bool, a, b T) T {
|
||||
return b
|
||||
}
|
||||
|
||||
func ToPtr[T any](t T) *T {
|
||||
return &t
|
||||
}
|
||||
|
||||
// Equal 比较切片是否相对(包括元素顺序)
|
||||
func Equal[E comparable](a []E, b []E) bool {
|
||||
if len(a) != len(b) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user