You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
446 lines
19 KiB
446 lines
19 KiB
1 year ago
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||
|
//
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
1 year ago
|
package controller
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
8 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||
6 months ago
|
redis2 "github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/redis"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/common"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||
1 year ago
|
"time"
|
||
1 year ago
|
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
|
||
8 months ago
|
"github.com/openimsdk/protocol/constant"
|
||
|
"github.com/openimsdk/tools/db/pagination"
|
||
|
"github.com/openimsdk/tools/db/tx"
|
||
|
"github.com/openimsdk/tools/utils/datautil"
|
||
9 months ago
|
"github.com/redis/go-redis/v9"
|
||
1 year ago
|
)
|
||
|
|
||
|
type GroupDatabase interface {
|
||
9 months ago
|
// CreateGroup creates new groups along with their members.
|
||
6 months ago
|
CreateGroup(ctx context.Context, groups []*model.Group, groupMembers []*model.GroupMember) error
|
||
9 months ago
|
// TakeGroup retrieves a single group by its ID.
|
||
6 months ago
|
TakeGroup(ctx context.Context, groupID string) (group *model.Group, err error)
|
||
9 months ago
|
// FindGroup retrieves multiple groups by their IDs.
|
||
6 months ago
|
FindGroup(ctx context.Context, groupIDs []string) (groups []*model.Group, err error)
|
||
9 months ago
|
// SearchGroup searches for groups based on a keyword and pagination settings, returns total count and groups.
|
||
6 months ago
|
SearchGroup(ctx context.Context, keyword string, pagination pagination.Pagination) (int64, []*model.Group, error)
|
||
9 months ago
|
// UpdateGroup updates the properties of a group identified by its ID.
|
||
1 year ago
|
UpdateGroup(ctx context.Context, groupID string, data map[string]any) error
|
||
9 months ago
|
// DismissGroup disbands a group and optionally removes its members based on the deleteMember flag.
|
||
|
DismissGroup(ctx context.Context, groupID string, deleteMember bool) error
|
||
1 year ago
|
|
||
9 months ago
|
// TakeGroupMember retrieves a specific group member by group ID and user ID.
|
||
6 months ago
|
TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *model.GroupMember, err error)
|
||
9 months ago
|
// TakeGroupOwner retrieves the owner of a group by group ID.
|
||
6 months ago
|
TakeGroupOwner(ctx context.Context, groupID string) (*model.GroupMember, error)
|
||
9 months ago
|
// FindGroupMembers retrieves members of a group filtered by user IDs.
|
||
6 months ago
|
FindGroupMembers(ctx context.Context, groupID string, userIDs []string) (groupMembers []*model.GroupMember, err error)
|
||
9 months ago
|
// FindGroupMemberUser retrieves groups that a user is a member of, filtered by group IDs.
|
||
6 months ago
|
FindGroupMemberUser(ctx context.Context, groupIDs []string, userID string) (groupMembers []*model.GroupMember, err error)
|
||
9 months ago
|
// FindGroupMemberRoleLevels retrieves group members filtered by their role levels within a group.
|
||
6 months ago
|
FindGroupMemberRoleLevels(ctx context.Context, groupID string, roleLevels []int32) (groupMembers []*model.GroupMember, err error)
|
||
9 months ago
|
// FindGroupMemberAll retrieves all members of a group.
|
||
6 months ago
|
FindGroupMemberAll(ctx context.Context, groupID string) (groupMembers []*model.GroupMember, err error)
|
||
9 months ago
|
// FindGroupsOwner retrieves the owners for multiple groups.
|
||
6 months ago
|
FindGroupsOwner(ctx context.Context, groupIDs []string) ([]*model.GroupMember, error)
|
||
9 months ago
|
// FindGroupMemberUserID retrieves the user IDs of all members in a group.
|
||
1 year ago
|
FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error)
|
||
9 months ago
|
// FindGroupMemberNum retrieves the number of members in a group.
|
||
1 year ago
|
FindGroupMemberNum(ctx context.Context, groupID string) (uint32, error)
|
||
9 months ago
|
// FindUserManagedGroupID retrieves group IDs managed by a user.
|
||
1 year ago
|
FindUserManagedGroupID(ctx context.Context, userID string) (groupIDs []string, err error)
|
||
9 months ago
|
// PageGroupRequest paginates through group requests for specified groups.
|
||
6 months ago
|
PageGroupRequest(ctx context.Context, groupIDs []string, pagination pagination.Pagination) (int64, []*model.GroupRequest, error)
|
||
9 months ago
|
// GetGroupRoleLevelMemberIDs retrieves user IDs of group members with a specific role level.
|
||
1 year ago
|
GetGroupRoleLevelMemberIDs(ctx context.Context, groupID string, roleLevel int32) ([]string, error)
|
||
1 year ago
|
|
||
9 months ago
|
// PageGetJoinGroup paginates through groups that a user has joined.
|
||
6 months ago
|
PageGetJoinGroup(ctx context.Context, userID string, pagination pagination.Pagination) (total int64, totalGroupMembers []*model.GroupMember, err error)
|
||
9 months ago
|
// PageGetGroupMember paginates through members of a group.
|
||
6 months ago
|
PageGetGroupMember(ctx context.Context, groupID string, pagination pagination.Pagination) (total int64, totalGroupMembers []*model.GroupMember, err error)
|
||
9 months ago
|
// SearchGroupMember searches for group members based on a keyword, group ID, and pagination settings.
|
||
6 months ago
|
SearchGroupMember(ctx context.Context, keyword string, groupID string, pagination pagination.Pagination) (int64, []*model.GroupMember, error)
|
||
9 months ago
|
// HandlerGroupRequest processes a group join request with a specified result.
|
||
6 months ago
|
HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *model.GroupMember) error
|
||
9 months ago
|
// DeleteGroupMember removes specified users from a group.
|
||
1 year ago
|
DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||
9 months ago
|
// MapGroupMemberUserID maps group IDs to their members' simplified user IDs.
|
||
6 months ago
|
MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string]*common.GroupSimpleUserID, error)
|
||
9 months ago
|
// MapGroupMemberNum maps group IDs to their member count.
|
||
1 year ago
|
MapGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]uint32, error)
|
||
9 months ago
|
// TransferGroupOwner transfers the ownership of a group to another user.
|
||
|
TransferGroupOwner(ctx context.Context, groupID string, oldOwnerUserID, newOwnerUserID string, roleLevel int32) error
|
||
|
// UpdateGroupMember updates properties of a group member.
|
||
1 year ago
|
UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error
|
||
9 months ago
|
// UpdateGroupMembers batch updates properties of group members.
|
||
6 months ago
|
UpdateGroupMembers(ctx context.Context, data []*common.BatchUpdateGroupMember) error
|
||
9 months ago
|
|
||
|
// CreateGroupRequest creates new group join requests.
|
||
6 months ago
|
CreateGroupRequest(ctx context.Context, requests []*model.GroupRequest) error
|
||
9 months ago
|
// TakeGroupRequest retrieves a specific group join request.
|
||
6 months ago
|
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*model.GroupRequest, error)
|
||
9 months ago
|
// FindGroupRequests retrieves multiple group join requests.
|
||
6 months ago
|
FindGroupRequests(ctx context.Context, groupID string, userIDs []string) ([]*model.GroupRequest, error)
|
||
9 months ago
|
// PageGroupRequestUser paginates through group join requests made by a user.
|
||
6 months ago
|
PageGroupRequestUser(ctx context.Context, userID string, pagination pagination.Pagination) (int64, []*model.GroupRequest, error)
|
||
1 year ago
|
|
||
9 months ago
|
// CountTotal counts the total number of groups as of a certain date.
|
||
1 year ago
|
CountTotal(ctx context.Context, before *time.Time) (count int64, err error)
|
||
9 months ago
|
// CountRangeEverydayTotal counts the daily group creation total within a specified date range.
|
||
1 year ago
|
CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error)
|
||
9 months ago
|
// DeleteGroupMemberHash deletes the hash entries for group members in specified groups.
|
||
1 year ago
|
DeleteGroupMemberHash(ctx context.Context, groupIDs []string) error
|
||
1 year ago
|
}
|
||
|
|
||
|
func NewGroupDatabase(
|
||
1 year ago
|
rdb redis.UniversalClient,
|
||
8 months ago
|
localCache *config.LocalCache,
|
||
6 months ago
|
groupDB database.Group,
|
||
|
groupMemberDB database.GroupMember,
|
||
|
groupRequestDB database.GroupRequest,
|
||
8 months ago
|
ctxTx tx.Tx,
|
||
1 year ago
|
groupHash cache.GroupHash,
|
||
1 year ago
|
) GroupDatabase {
|
||
1 year ago
|
return &groupDatabase{
|
||
|
groupDB: groupDB,
|
||
|
groupMemberDB: groupMemberDB,
|
||
|
groupRequestDB: groupRequestDB,
|
||
|
ctxTx: ctxTx,
|
||
6 months ago
|
cache: redis2.NewGroupCacheRedis(rdb, localCache, groupDB, groupMemberDB, groupRequestDB, groupHash, redis2.GetRocksCacheOptions()),
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
|
|
||
|
type groupDatabase struct {
|
||
6 months ago
|
groupDB database.Group
|
||
|
groupMemberDB database.GroupMember
|
||
|
groupRequestDB database.GroupRequest
|
||
8 months ago
|
ctxTx tx.Tx
|
||
1 year ago
|
cache cache.GroupCache
|
||
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) FindGroupMembers(ctx context.Context, groupID string, userIDs []string) ([]*model.GroupMember, error) {
|
||
1 year ago
|
return g.cache.GetGroupMembersInfo(ctx, groupID, userIDs)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) FindGroupMemberUser(ctx context.Context, groupIDs []string, userID string) ([]*model.GroupMember, error) {
|
||
1 year ago
|
return g.cache.FindGroupMemberUser(ctx, groupIDs, userID)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) FindGroupMemberRoleLevels(ctx context.Context, groupID string, roleLevels []int32) ([]*model.GroupMember, error) {
|
||
1 year ago
|
return g.cache.GetGroupRolesLevelMemberInfo(ctx, groupID, roleLevels)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) FindGroupMemberAll(ctx context.Context, groupID string) ([]*model.GroupMember, error) {
|
||
1 year ago
|
return g.cache.GetAllGroupMembersInfo(ctx, groupID)
|
||
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) FindGroupsOwner(ctx context.Context, groupIDs []string) ([]*model.GroupMember, error) {
|
||
1 year ago
|
return g.cache.GetGroupsOwner(ctx, groupIDs)
|
||
|
}
|
||
|
|
||
|
func (g *groupDatabase) GetGroupRoleLevelMemberIDs(ctx context.Context, groupID string, roleLevel int32) ([]string, error) {
|
||
|
return g.cache.GetGroupRoleLevelMemberIDs(ctx, groupID, roleLevel)
|
||
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) CreateGroup(ctx context.Context, groups []*model.Group, groupMembers []*model.GroupMember) error {
|
||
1 year ago
|
if len(groups)+len(groupMembers) == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
|
||
6 months ago
|
c := g.cache.CloneGroupCache()
|
||
1 year ago
|
if len(groups) > 0 {
|
||
1 year ago
|
if err := g.groupDB.Create(ctx, groups); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
1 year ago
|
for _, group := range groups {
|
||
|
c = c.DelGroupsInfo(group.GroupID).
|
||
|
DelGroupMembersHash(group.GroupID).
|
||
|
DelGroupMembersHash(group.GroupID).
|
||
|
DelGroupsMemberNum(group.GroupID).
|
||
|
DelGroupMemberIDs(group.GroupID).
|
||
|
DelGroupAllRoleLevel(group.GroupID)
|
||
|
}
|
||
1 year ago
|
}
|
||
|
if len(groupMembers) > 0 {
|
||
1 year ago
|
if err := g.groupMemberDB.Create(ctx, groupMembers); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
1 year ago
|
for _, groupMember := range groupMembers {
|
||
|
c = c.DelGroupMembersHash(groupMember.GroupID).
|
||
|
DelGroupsMemberNum(groupMember.GroupID).
|
||
|
DelGroupMemberIDs(groupMember.GroupID).
|
||
|
DelJoinedGroupID(groupMember.UserID).
|
||
|
DelGroupMembersInfo(groupMember.GroupID, groupMember.UserID).
|
||
|
DelGroupAllRoleLevel(groupMember.GroupID)
|
||
1 year ago
|
}
|
||
|
}
|
||
6 months ago
|
return c.ChainExecDel(ctx)
|
||
1 year ago
|
})
|
||
|
}
|
||
|
|
||
|
func (g *groupDatabase) FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error) {
|
||
|
return g.cache.GetGroupMemberIDs(ctx, groupID)
|
||
|
}
|
||
|
|
||
|
func (g *groupDatabase) FindGroupMemberNum(ctx context.Context, groupID string) (uint32, error) {
|
||
|
num, err := g.cache.GetGroupMemberNum(ctx, groupID)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
1 year ago
|
}
|
||
1 year ago
|
return uint32(num), nil
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) TakeGroup(ctx context.Context, groupID string) (*model.Group, error) {
|
||
1 year ago
|
return g.cache.GetGroupInfo(ctx, groupID)
|
||
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) FindGroup(ctx context.Context, groupIDs []string) ([]*model.Group, error) {
|
||
1 year ago
|
return g.cache.GetGroupsInfo(ctx, groupIDs)
|
||
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) SearchGroup(ctx context.Context, keyword string, pagination pagination.Pagination) (int64, []*model.Group, error) {
|
||
1 year ago
|
return g.groupDB.Search(ctx, keyword, pagination)
|
||
1 year ago
|
}
|
||
|
|
||
|
func (g *groupDatabase) UpdateGroup(ctx context.Context, groupID string, data map[string]any) error {
|
||
|
if err := g.groupDB.UpdateMap(ctx, groupID, data); err != nil {
|
||
|
return err
|
||
|
}
|
||
6 months ago
|
return g.cache.DelGroupsInfo(groupID).ChainExecDel(ctx)
|
||
1 year ago
|
}
|
||
|
|
||
|
func (g *groupDatabase) DismissGroup(ctx context.Context, groupID string, deleteMember bool) error {
|
||
1 year ago
|
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
|
||
6 months ago
|
c := g.cache.CloneGroupCache()
|
||
11 months ago
|
if err := g.groupDB.UpdateStatus(ctx, groupID, constant.GroupStatusDismissed); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
|
if deleteMember {
|
||
|
userIDs, err := g.cache.GetGroupMemberIDs(ctx, groupID)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
1 year ago
|
if err := g.groupMemberDB.Delete(ctx, groupID, nil); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
c = c.DelJoinedGroupID(userIDs...).
|
||
|
DelGroupMemberIDs(groupID).
|
||
|
DelGroupsMemberNum(groupID).
|
||
|
DelGroupMembersHash(groupID).
|
||
|
DelGroupAllRoleLevel(groupID).
|
||
|
DelGroupMembersInfo(groupID, userIDs...)
|
||
1 year ago
|
}
|
||
6 months ago
|
return c.DelGroupsInfo(groupID).ChainExecDel(ctx)
|
||
1 year ago
|
})
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) TakeGroupMember(ctx context.Context, groupID string, userID string) (*model.GroupMember, error) {
|
||
1 year ago
|
return g.cache.GetGroupMemberInfo(ctx, groupID, userID)
|
||
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) TakeGroupOwner(ctx context.Context, groupID string) (*model.GroupMember, error) {
|
||
1 year ago
|
return g.cache.GetGroupOwner(ctx, groupID)
|
||
1 year ago
|
}
|
||
|
|
||
|
func (g *groupDatabase) FindUserManagedGroupID(ctx context.Context, userID string) (groupIDs []string, err error) {
|
||
|
return g.groupMemberDB.FindUserManagedGroupID(ctx, userID)
|
||
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) PageGroupRequest(ctx context.Context, groupIDs []string, pagination pagination.Pagination) (int64, []*model.GroupRequest, error) {
|
||
1 year ago
|
return g.groupRequestDB.PageGroup(ctx, groupIDs, pagination)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) PageGetJoinGroup(ctx context.Context, userID string, pagination pagination.Pagination) (total int64, totalGroupMembers []*model.GroupMember, err error) {
|
||
1 year ago
|
groupIDs, err := g.cache.GetJoinedGroupIDs(ctx, userID)
|
||
|
if err != nil {
|
||
|
return 0, nil, err
|
||
|
}
|
||
8 months ago
|
for _, groupID := range datautil.Paginate(groupIDs, int(pagination.GetPageNumber()), int(pagination.GetShowNumber())) {
|
||
1 year ago
|
groupMembers, err := g.cache.GetGroupMembersInfo(ctx, groupID, []string{userID})
|
||
|
if err != nil {
|
||
|
return 0, nil, err
|
||
|
}
|
||
|
totalGroupMembers = append(totalGroupMembers, groupMembers...)
|
||
|
}
|
||
1 year ago
|
return int64(len(groupIDs)), totalGroupMembers, nil
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) PageGetGroupMember(ctx context.Context, groupID string, pagination pagination.Pagination) (total int64, totalGroupMembers []*model.GroupMember, err error) {
|
||
1 year ago
|
groupMemberIDs, err := g.cache.GetGroupMemberIDs(ctx, groupID)
|
||
|
if err != nil {
|
||
|
return 0, nil, err
|
||
|
}
|
||
8 months ago
|
pageIDs := datautil.Paginate(groupMemberIDs, int(pagination.GetPageNumber()), int(pagination.GetShowNumber()))
|
||
1 year ago
|
if len(pageIDs) == 0 {
|
||
1 year ago
|
return int64(len(groupMemberIDs)), nil, nil
|
||
1 year ago
|
}
|
||
|
members, err := g.cache.GetGroupMembersInfo(ctx, groupID, pageIDs)
|
||
|
if err != nil {
|
||
|
return 0, nil, err
|
||
|
}
|
||
1 year ago
|
return int64(len(groupMemberIDs)), members, nil
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) SearchGroupMember(ctx context.Context, keyword string, groupID string, pagination pagination.Pagination) (int64, []*model.GroupMember, error) {
|
||
1 year ago
|
return g.groupMemberDB.SearchMember(ctx, keyword, groupID, pagination)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *model.GroupMember) error {
|
||
1 year ago
|
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
|
||
|
if err := g.groupRequestDB.UpdateHandler(ctx, groupID, userID, handledMsg, handleResult); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
|
if member != nil {
|
||
6 months ago
|
c := g.cache.CloneGroupCache()
|
||
|
if err := g.groupMemberDB.Create(ctx, []*model.GroupMember{member}); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
6 months ago
|
c = c.DelGroupMembersHash(groupID).
|
||
1 year ago
|
DelGroupMembersInfo(groupID, member.UserID).
|
||
|
DelGroupMemberIDs(groupID).
|
||
|
DelGroupsMemberNum(groupID).
|
||
|
DelJoinedGroupID(member.UserID).
|
||
|
DelGroupRoleLevel(groupID, []int32{member.RoleLevel})
|
||
6 months ago
|
if err := c.ChainExecDel(ctx); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
1 year ago
|
}
|
||
|
return nil
|
||
1 year ago
|
})
|
||
1 year ago
|
}
|
||
|
|
||
|
func (g *groupDatabase) DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
||
|
if err := g.groupMemberDB.Delete(ctx, groupID, userIDs); err != nil {
|
||
|
return err
|
||
|
}
|
||
6 months ago
|
c := g.cache.CloneGroupCache()
|
||
|
return c.DelGroupMembersHash(groupID).
|
||
1 year ago
|
DelGroupMemberIDs(groupID).
|
||
|
DelGroupsMemberNum(groupID).
|
||
|
DelJoinedGroupID(userIDs...).
|
||
|
DelGroupMembersInfo(groupID, userIDs...).
|
||
1 year ago
|
DelGroupAllRoleLevel(groupID).
|
||
6 months ago
|
ChainExecDel(ctx)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string]*common.GroupSimpleUserID, error) {
|
||
1 year ago
|
return g.cache.GetGroupMemberHashMap(ctx, groupIDs)
|
||
|
}
|
||
|
|
||
|
func (g *groupDatabase) MapGroupMemberNum(ctx context.Context, groupIDs []string) (m map[string]uint32, err error) {
|
||
|
m = make(map[string]uint32)
|
||
|
for _, groupID := range groupIDs {
|
||
|
num, err := g.cache.GetGroupMemberNum(ctx, groupID)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
m[groupID] = uint32(num)
|
||
|
}
|
||
|
return m, nil
|
||
|
}
|
||
|
|
||
1 year ago
|
func (g *groupDatabase) TransferGroupOwner(ctx context.Context, groupID string, oldOwnerUserID, newOwnerUserID string, roleLevel int32) error {
|
||
1 year ago
|
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
|
||
|
if err := g.groupMemberDB.UpdateRoleLevel(ctx, groupID, oldOwnerUserID, roleLevel); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
1 year ago
|
if err := g.groupMemberDB.UpdateRoleLevel(ctx, groupID, newOwnerUserID, constant.GroupOwner); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
6 months ago
|
c := g.cache.CloneGroupCache()
|
||
|
return c.DelGroupMembersInfo(groupID, oldOwnerUserID, newOwnerUserID).
|
||
1 year ago
|
DelGroupAllRoleLevel(groupID).
|
||
6 months ago
|
DelGroupMembersHash(groupID).ChainExecDel(ctx)
|
||
1 year ago
|
})
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
func (g *groupDatabase) UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error {
|
||
1 year ago
|
if err := g.groupMemberDB.Update(ctx, groupID, userID, data); err != nil {
|
||
|
return err
|
||
|
}
|
||
6 months ago
|
c := g.cache.CloneGroupCache()
|
||
|
c = c.DelGroupMembersInfo(groupID, userID)
|
||
1 year ago
|
if g.groupMemberDB.IsUpdateRoleLevel(data) {
|
||
|
c = c.DelGroupAllRoleLevel(groupID)
|
||
|
}
|
||
6 months ago
|
return c.ChainExecDel(ctx)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) UpdateGroupMembers(ctx context.Context, data []*common.BatchUpdateGroupMember) error {
|
||
1 year ago
|
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
|
||
6 months ago
|
c := g.cache.CloneGroupCache()
|
||
1 year ago
|
for _, item := range data {
|
||
1 year ago
|
if err := g.groupMemberDB.Update(ctx, item.GroupID, item.UserID, item.Map); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
1 year ago
|
if g.groupMemberDB.IsUpdateRoleLevel(item.Map) {
|
||
|
c = c.DelGroupAllRoleLevel(item.GroupID)
|
||
|
}
|
||
|
c = c.DelGroupMembersInfo(item.GroupID, item.UserID).DelGroupMembersHash(item.GroupID)
|
||
1 year ago
|
}
|
||
6 months ago
|
return c.ChainExecDel(ctx)
|
||
1 year ago
|
})
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) CreateGroupRequest(ctx context.Context, requests []*model.GroupRequest) error {
|
||
1 year ago
|
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
|
||
1 year ago
|
for _, request := range requests {
|
||
1 year ago
|
if err := g.groupRequestDB.Delete(ctx, request.GroupID, request.UserID); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
|
}
|
||
1 year ago
|
return g.groupRequestDB.Create(ctx, requests)
|
||
1 year ago
|
})
|
||
|
}
|
||
|
|
||
1 year ago
|
func (g *groupDatabase) TakeGroupRequest(
|
||
|
ctx context.Context,
|
||
|
groupID string,
|
||
|
userID string,
|
||
6 months ago
|
) (*model.GroupRequest, error) {
|
||
1 year ago
|
return g.groupRequestDB.Take(ctx, groupID, userID)
|
||
|
}
|
||
|
|
||
6 months ago
|
func (g *groupDatabase) PageGroupRequestUser(ctx context.Context, userID string, pagination pagination.Pagination) (int64, []*model.GroupRequest, error) {
|
||
1 year ago
|
return g.groupRequestDB.Page(ctx, userID, pagination)
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
|
func (g *groupDatabase) CountTotal(ctx context.Context, before *time.Time) (count int64, err error) {
|
||
|
return g.groupDB.CountTotal(ctx, before)
|
||
|
}
|
||
|
|
||
1 year ago
|
func (g *groupDatabase) CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error) {
|
||
1 year ago
|
return g.groupDB.CountRangeEverydayTotal(ctx, start, end)
|
||
|
}
|
||
1 year ago
|
|
||
6 months ago
|
func (g *groupDatabase) FindGroupRequests(ctx context.Context, groupID string, userIDs []string) ([]*model.GroupRequest, error) {
|
||
1 year ago
|
return g.groupRequestDB.FindGroupRequests(ctx, groupID, userIDs)
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
func (g *groupDatabase) DeleteGroupMemberHash(ctx context.Context, groupIDs []string) error {
|
||
|
if len(groupIDs) == 0 {
|
||
|
return nil
|
||
|
}
|
||
6 months ago
|
c := g.cache.CloneGroupCache()
|
||
1 year ago
|
for _, groupID := range groupIDs {
|
||
|
c = c.DelGroupMembersHash(groupID)
|
||
|
}
|
||
6 months ago
|
return c.ChainExecDel(ctx)
|
||
1 year ago
|
}
|