mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-02 08:05:58 +08:00
Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode
Conflicts: internal/push/sdk/tpns-server-sdk-go/go/auth/auth.go pkg/proto/group/group.proto
This commit is contained in:
@@ -1,9 +1,34 @@
|
||||
package controller
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"context"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
type AuthInterface interface {
|
||||
GetTokens(ctx context.Context, userID, platform string) (map[string]int, error)
|
||||
DeleteToken(ctx context.Context, userID, platform string) error
|
||||
CreateToken(ctx context.Context, userID string, platformID int, ttl int64) (string, error)
|
||||
//结果为空 不返回错误
|
||||
GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error)
|
||||
|
||||
//创建token
|
||||
CreateToken(ctx context.Context, userID string, platform string) (string, error)
|
||||
}
|
||||
|
||||
type AuthController struct {
|
||||
database *cache.TokenRedis
|
||||
}
|
||||
|
||||
func NewAuthController(rdb redis.UniversalClient, accessSecret string, accessExpire int64) *AuthController {
|
||||
cache.NewRedisClient(rdb)
|
||||
return &AuthController{database: cache.NewTokenRedis(cache.NewRedisClient(rdb), accessSecret, accessExpire)}
|
||||
}
|
||||
|
||||
// 结果为空 不返回错误
|
||||
func (a *AuthController) GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error) {
|
||||
return a.database.GetTokensWithoutError(ctx, userID, platform)
|
||||
}
|
||||
|
||||
// 创建token
|
||||
func (a *AuthController) CreateToken(ctx context.Context, userID string, platform string) (string, error) {
|
||||
return a.database.CreateToken(ctx, userID, platform)
|
||||
}
|
||||
|
||||
@@ -19,6 +19,12 @@ import (
|
||||
|
||||
//type GroupInterface GroupDataBaseInterface
|
||||
|
||||
type BatchUpdateGroupMember struct {
|
||||
GroupID string
|
||||
UserID string
|
||||
Map map[string]any
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -30,6 +36,7 @@ type GroupInterface interface {
|
||||
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)
|
||||
FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error)
|
||||
PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error)
|
||||
SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error)
|
||||
HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error
|
||||
@@ -37,7 +44,8 @@ type GroupInterface interface {
|
||||
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
|
||||
UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error
|
||||
UpdateGroupMembers(ctx context.Context, data []*BatchUpdateGroupMember) error
|
||||
// GroupRequest
|
||||
CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error
|
||||
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error)
|
||||
@@ -61,6 +69,10 @@ type GroupController struct {
|
||||
database GroupDataBaseInterface
|
||||
}
|
||||
|
||||
func (g *GroupController) FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error) {
|
||||
return g.database.FindGroupMemberUserID(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupController) CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error {
|
||||
return g.database.CreateGroup(ctx, groups, groupMembers)
|
||||
}
|
||||
@@ -125,7 +137,11 @@ func (g *GroupController) TransferGroupOwner(ctx context.Context, groupID string
|
||||
return g.database.TransferGroupOwner(ctx, groupID, oldOwnerUserID, newOwnerUserID, roleLevel)
|
||||
}
|
||||
|
||||
func (g *GroupController) UpdateGroupMember(ctx context.Context, groupID, userID string, data map[string]any) error {
|
||||
func (g *GroupController) UpdateGroupMembers(ctx context.Context, data []*BatchUpdateGroupMember) error {
|
||||
return g.database.UpdateGroupMembers(ctx, data)
|
||||
}
|
||||
|
||||
func (g *GroupController) UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error {
|
||||
return g.database.UpdateGroupMember(ctx, groupID, userID, data)
|
||||
}
|
||||
|
||||
@@ -141,9 +157,6 @@ func (g *GroupController) PageGroupRequestUser(ctx context.Context, userID strin
|
||||
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) FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unrelationTb.SuperGroupModel, error) {
|
||||
return g.database.FindSuperGroup(ctx, groupIDs)
|
||||
}
|
||||
@@ -179,6 +192,7 @@ type GroupDataBaseInterface interface {
|
||||
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)
|
||||
FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error)
|
||||
PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error)
|
||||
SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error)
|
||||
HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error
|
||||
@@ -186,7 +200,8 @@ type GroupDataBaseInterface interface {
|
||||
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
|
||||
UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error
|
||||
UpdateGroupMembers(ctx context.Context, data []*BatchUpdateGroupMember) error
|
||||
// GroupRequest
|
||||
CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error
|
||||
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error)
|
||||
@@ -211,7 +226,7 @@ func NewGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, mgoClient *mongo.C
|
||||
groupMemberDB: groupMemberDB,
|
||||
groupRequestDB: groupRequestDB,
|
||||
db: &newDB,
|
||||
cache: cache.NewGroupCache(rdb, groupDB, groupMemberDB, groupRequestDB, SuperGroupMongoDriver, rockscache.Options{
|
||||
cache: cache.NewGroupCacheRedis(rdb, groupDB, groupMemberDB, groupRequestDB, SuperGroupMongoDriver, rockscache.Options{
|
||||
RandomExpireAdjustment: 0.2,
|
||||
DisableCacheRead: false,
|
||||
DisableCacheDelete: false,
|
||||
@@ -230,10 +245,36 @@ type GroupDataBase struct {
|
||||
groupRequestDB relationTb.GroupRequestModelInterface
|
||||
db *gorm.DB
|
||||
|
||||
cache *cache.GroupCache
|
||||
//cache cache.GroupCache
|
||||
cache *cache.GroupCacheRedis
|
||||
mongoDB *unrelation.SuperGroupMongoDriver
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) delGroupMemberCache(ctx context.Context, groupID string, userIDs []string) error {
|
||||
for _, userID := range userIDs {
|
||||
if err := g.cache.DelJoinedGroupIDs(ctx, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.cache.DelJoinedSuperGroupIDs(ctx, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := g.cache.DelGroupMemberIDs(ctx, groupID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.cache.DelGroupMemberNum(ctx, groupID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.cache.DelGroupMembersHash(ctx, groupID); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error) {
|
||||
return g.cache.GetGroupMemberIDs(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error {
|
||||
if len(groups) > 0 && len(groupMembers) > 0 {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
@@ -253,11 +294,11 @@ func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relationTb.Gr
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error) {
|
||||
return g.groupDB.Take(ctx, groupID)
|
||||
return g.cache.GetGroupInfo(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error) {
|
||||
return g.groupDB.Find(ctx, groupIDs)
|
||||
return g.cache.GetGroupsInfo(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupModel, error) {
|
||||
@@ -265,7 +306,15 @@ func (g *GroupDataBase) SearchGroup(ctx context.Context, keyword string, pageNum
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) UpdateGroup(ctx context.Context, groupID string, data map[string]any) error {
|
||||
return g.groupDB.UpdateMap(ctx, groupID, data)
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.groupDB.UpdateMap(ctx, groupID, data, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.cache.DelGroupInfo(ctx, groupID); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) DismissGroup(ctx context.Context, groupID string) error {
|
||||
@@ -273,13 +322,22 @@ func (g *GroupDataBase) DismissGroup(ctx context.Context, groupID string) error
|
||||
if err := g.groupDB.UpdateStatus(ctx, groupID, constant.GroupStatusDismissed, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.groupMemberDB.DeleteGroup(ctx, []string{groupID}, tx)
|
||||
|
||||
if err := g.groupMemberDB.DeleteGroup(ctx, []string{groupID}, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
userIDs, err := g.cache.GetGroupMemberIDs(ctx, groupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.delGroupMemberCache(ctx, groupID, userIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relationTb.GroupMemberModel, err error) {
|
||||
return g.groupMemberDB.Take(ctx, groupID, userID)
|
||||
return g.cache.GetGroupMemberInfo(ctx, groupID, userID)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error) {
|
||||
@@ -287,6 +345,7 @@ func (g *GroupDataBase) TakeGroupOwner(ctx context.Context, groupID string) (*re
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relationTb.GroupMemberModel, error) {
|
||||
//g.cache.GetGroupMembersInfo()
|
||||
return g.groupMemberDB.Find(ctx, groupIDs, userIDs, roleLevels)
|
||||
}
|
||||
|
||||
@@ -299,19 +358,32 @@ func (g *GroupDataBase) SearchGroupMember(ctx context.Context, keyword string, g
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error {
|
||||
if member == nil {
|
||||
return g.groupRequestDB.UpdateHandler(ctx, groupID, userID, handledMsg, handleResult)
|
||||
}
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.groupRequestDB.UpdateHandler(ctx, groupID, userID, handledMsg, handleResult, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.groupMemberDB.Create(ctx, []*relationTb.GroupMemberModel{member}, tx)
|
||||
if member != nil {
|
||||
if err := g.groupMemberDB.Create(ctx, []*relationTb.GroupMemberModel{member}, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.delGroupMemberCache(ctx, groupID, []string{userID}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
||||
return g.groupMemberDB.Delete(ctx, groupID, userIDs)
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.groupMemberDB.Delete(ctx, groupID, userIDs, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.delGroupMemberCache(ctx, groupID, userIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string][]string, error) {
|
||||
@@ -338,12 +410,37 @@ func (g *GroupDataBase) TransferGroupOwner(ctx context.Context, groupID string,
|
||||
if rowsAffected != 1 {
|
||||
return utils.Wrap(fmt.Errorf("newOwnerUserID %s rowsAffected = %d", newOwnerUserID, rowsAffected), "")
|
||||
}
|
||||
if err := g.delGroupMemberCache(ctx, groupID, []string{oldOwnerUserID, newOwnerUserID}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) UpdateGroupMember(ctx context.Context, groupID, userID string, data map[string]any) error {
|
||||
return g.groupMemberDB.Update(ctx, groupID, userID, data)
|
||||
func (g *GroupDataBase) UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.groupMemberDB.Update(ctx, groupID, userID, data, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.cache.DelGroupMemberInfo(ctx, groupID, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) UpdateGroupMembers(ctx context.Context, data []*BatchUpdateGroupMember) error {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
for _, item := range data {
|
||||
if err := g.groupMemberDB.Update(ctx, item.GroupID, item.UserID, item.Map, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.cache.DelGroupMemberInfo(ctx, item.GroupID, item.UserID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error {
|
||||
|
||||
Reference in New Issue
Block a user