mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-05 01:25:58 +08:00
Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode
Conflicts: cmd/rpc/auth/main.go cmd/rpc/friend/main.go cmd/rpc/group/main.go cmd/rpc/msg/main.go cmd/rpc/user/main.go internal/rpc/auth/auth.go internal/rpc/conversation/conversaion.go internal/rpc/friend/friend.go internal/rpc/group/callback.go internal/rpc/group/group.go internal/rpc/msg/pull_message.go internal/rpc/msg/query_msg.go internal/rpc/msg/rpc_chat.go internal/rpc/msg/send_msg.go internal/rpc/user/user.go pkg/common/db/cache/redis.go pkg/common/db/controller/msg.go pkg/common/http/http_client.go
This commit is contained in:
Vendored
+40
-4
@@ -20,21 +20,25 @@ const (
|
||||
superGroupRecvMsgNotNotifyUserIDsKey = "SUPER_GROUP_RECV_MSG_NOT_NOTIFY_USER_IDS:"
|
||||
conversationExpireTime = time.Second * 60 * 60 * 12
|
||||
)
|
||||
type FuncDB func() (string, error)
|
||||
|
||||
// arg fn will exec when no data in cache
|
||||
type ConversationCache interface {
|
||||
// get user's conversationIDs from cache
|
||||
GetUserConversationIDs(ctx context.Context, userID string, fn func(ctx context.Context, userID string) ([]string, error)) ([]string, error)
|
||||
GetUserConversationIDs(ctx context.Context, userID string, fn FuncDB) ([]string, error)
|
||||
// del user's conversationIDs from cache, call when a user add or reduce a conversation
|
||||
DelUserConversationIDs(ctx context.Context, userID string) error
|
||||
DelUsersConversationIDs(ctx context.Context,userIDList []string)error
|
||||
// get one conversation from cache
|
||||
GetConversation(ctx context.Context, ownerUserID, conversationID string, fn func(ctx context.Context, ownerUserID, conversationID string) (*relationTb.ConversationModel, error)) (*relationTb.ConversationModel, error)
|
||||
GetConversation(ctx context.Context, ownerUserID, conversationID string, fn FuncDB) (*relationTb.ConversationModel, error)
|
||||
// get one conversation from cache
|
||||
GetConversations(ctx context.Context, ownerUserID string, conversationIDs []string, fn func(ctx context.Context, ownerUserID, conversationIDs []string) ([]*relationTb.ConversationModel, error)) ([]*relationTb.ConversationModel, error)
|
||||
GetConversations(ctx context.Context, ownerUserID string, conversationIDs []string, fn FuncDB)([]*relationTb.ConversationModel, error)
|
||||
// get one user's all conversations from cache
|
||||
GetUserAllConversations(ctx context.Context, ownerUserID string, fn func(ctx context.Context, ownerUserIDs string) ([]*relationTb.ConversationModel, error)) ([]*relationTb.ConversationModel, error)
|
||||
GetUserAllConversations(ctx context.Context, ownerUserID string, fn FuncDB ) ([]*relationTb.ConversationModel, error)
|
||||
// del one conversation from cache, call when one user's conversation Info changed
|
||||
DelConversation(ctx context.Context, ownerUserID, conversationID string) error
|
||||
DelUserConversations(ctx context.Context, ownerUserID string, conversationIDList []string) error
|
||||
DelUsersConversation(ctx context.Context, ownerUserIDList []string, conversationID string) error
|
||||
// get user conversation recv msg from cache
|
||||
GetUserRecvMsgOpt(ctx context.Context, ownerUserID, conversationID string, fn func(ctx context.Context, ownerUserID, conversationID string) (opt int, err error)) (opt int, err error)
|
||||
// del user recv msg opt from cache, call when user's conversation recv msg opt changed
|
||||
@@ -51,6 +55,38 @@ type ConversationRedis struct {
|
||||
rcClient *rockscache.Client
|
||||
}
|
||||
|
||||
func (c *ConversationRedis) GetUserConversationIDs(ctx context.Context, userID string, fn func(ctx context.Context, userID string) ([]string, error)) ([]string, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c *ConversationRedis) DelUsersConversationIDs(ctx context.Context, userIDList []string) error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c *ConversationRedis) GetConversation(ctx context.Context, ownerUserID, conversationID string, fn func(ctx context.Context, ownerUserID string, conversationID string) (*relationTb.ConversationModel, error)) (*relationTb.ConversationModel, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c *ConversationRedis) GetConversations(ctx context.Context, ownerUserID string, conversationIDs []string, fn FuncDB) ([]*relationTb.ConversationModel, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c *ConversationRedis) GetUserAllConversations(ctx context.Context, ownerUserID string, fn FuncDB) ([]*relationTb.ConversationModel, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c *ConversationRedis) DelUsersConversation(ctx context.Context, ownerUserIDList []string, conversationID string) error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c *ConversationRedis) GetUserRecvMsgOpt(ctx context.Context, ownerUserID, conversationID string, fn func(ctx context.Context, ownerUserID string, conversationID string) (opt int, err error)) (opt int, err error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c *ConversationRedis) GetSuperGroupRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string, fn func(ctx context.Context, groupID string) (userIDs []string, err error)) (userIDs []string, err error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func NewConversationRedis(rcClient *rockscache.Client) *ConversationRedis {
|
||||
return &ConversationRedis{rcClient: rcClient}
|
||||
}
|
||||
|
||||
Vendored
+126
-115
@@ -1,17 +1,14 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
relationTb "Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/db/unrelation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -157,6 +154,27 @@ func (g *GroupCacheRedis) GetGroupMembersHash(ctx context.Context, groupID strin
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupCacheRedis) GetGroupMemberHash1(ctx context.Context, groupIDs []string) (map[string]*relationTb.GroupSimpleUserID, error) {
|
||||
// todo
|
||||
mapGroupUserIDs, err := g.groupMember.FindJoinUserID(ctx, groupIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res := make(map[string]*relationTb.GroupSimpleUserID)
|
||||
for _, groupID := range groupIDs {
|
||||
userIDs := mapGroupUserIDs[groupID]
|
||||
users := &relationTb.GroupSimpleUserID{}
|
||||
if len(userIDs) > 0 {
|
||||
utils.Sort(userIDs, true)
|
||||
bi := big.NewInt(0)
|
||||
bi.SetString(utils.Md5(strings.Join(userIDs, ";"))[0:8], 16)
|
||||
users.Hash = bi.Uint64()
|
||||
}
|
||||
res[groupID] = users
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (g *GroupCacheRedis) DelGroupMembersHash(ctx context.Context, groupID string) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID)
|
||||
@@ -178,111 +196,104 @@ func (g *GroupCacheRedis) DelGroupMemberIDs(ctx context.Context, groupID string)
|
||||
return g.rcClient.TagAsDeleted(g.getGroupMemberIDsKey(groupID))
|
||||
}
|
||||
|
||||
// JoinedGroups
|
||||
func (g *GroupCacheRedis) GetJoinedGroupIDs(ctx context.Context, userID string) (joinedGroupIDs []string, err error) {
|
||||
getJoinedGroupIDList := func() (string, error) {
|
||||
joinedGroupList, err := relation.GetJoinedGroupIDListByUserID(userID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
bytes, err := json.Marshal(joinedGroupList)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "joinedGroupIDs", joinedGroupIDs)
|
||||
}()
|
||||
joinedGroupIDListStr, err := g.rcClient.Fetch(g.getJoinedGroupsKey(userID), time.Second*30*60, getJoinedGroupIDList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal([]byte(joinedGroupIDListStr), &joinedGroupIDs)
|
||||
return joinedGroupIDs, utils.Wrap(err, "")
|
||||
}
|
||||
//// JoinedGroups
|
||||
//func (g *GroupCacheRedis) GetJoinedGroupIDs(ctx context.Context, userID string) (joinedGroupIDs []string, err error) {
|
||||
// getJoinedGroupIDList := func() (string, error) {
|
||||
// joinedGroupList, err := relation.GetJoinedGroupIDListByUserID(userID)
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
// bytes, err := json.Marshal(joinedGroupList)
|
||||
// if err != nil {
|
||||
// return "", utils.Wrap(err, "")
|
||||
// }
|
||||
// return string(bytes), nil
|
||||
// }
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "joinedGroupIDs", joinedGroupIDs)
|
||||
// }()
|
||||
// joinedGroupIDListStr, err := g.rcClient.Fetch(g.getJoinedGroupsKey(userID), time.Second*30*60, getJoinedGroupIDList)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// err = json.Unmarshal([]byte(joinedGroupIDListStr), &joinedGroupIDs)
|
||||
// return joinedGroupIDs, utils.Wrap(err, "")
|
||||
//}
|
||||
|
||||
func (g *GroupCacheRedis) DelJoinedGroupIDs(ctx context.Context, userID string) (err error) {
|
||||
func (g *GroupCacheRedis) DelJoinedGroupID(ctx context.Context, userID string) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID)
|
||||
}()
|
||||
return g.rcClient.TagAsDeleted(g.getJoinedGroupsKey(userID))
|
||||
}
|
||||
|
||||
// GetGroupMemberInfo
|
||||
func (g *GroupCacheRedis) GetGroupMemberInfo(ctx context.Context, groupID, userID string) (groupMember *relation.GroupMember, err error) {
|
||||
getGroupMemberInfo := func() (string, error) {
|
||||
groupMemberInfo, err := relation.GetGroupMemberInfoByGroupIDAndUserID(groupID, userID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
bytes, err := json.Marshal(groupMemberInfo)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "groupMember", *groupMember)
|
||||
}()
|
||||
groupMemberInfoStr, err := g.rcClient.Fetch(g.getGroupMemberInfoKey(groupID, userID), time.Second*30*60, getGroupMemberInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groupMember = &relation.GroupMember{}
|
||||
err = json.Unmarshal([]byte(groupMemberInfoStr), groupMember)
|
||||
return groupMember, utils.Wrap(err, "")
|
||||
//func (g *GroupCacheRedis) DelJoinedGroupIDs(ctx context.Context, userIDs []string) (err error) {
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID)
|
||||
// }()
|
||||
// for _, userID := range userIDs {
|
||||
// if err := g.DelJoinedGroupID(ctx, userID); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
|
||||
func (g *GroupCacheRedis) GetGroupMemberInfo(ctx context.Context, groupID, userID string) (groupMember *relationTb.GroupMemberModel, err error) {
|
||||
return GetCache(ctx, g.rcClient, g.getGroupMemberInfoKey(groupID, userID), g.expireTime, func(ctx context.Context) (*relationTb.GroupMemberModel, error) {
|
||||
return g.groupMember.Take(ctx, groupID, userID)
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupCacheRedis) GetGroupMembersInfo(ctx context.Context, groupID, userIDs []string) (groupMember *relationTb.GroupMemberModel, err error) {
|
||||
//func (g *GroupCacheRedis) GetGroupMembersInfo(ctx context.Context, groupID, userIDs []string) (groupMember *relationTb.GroupMemberModel, err error) {
|
||||
//
|
||||
// return nil, err
|
||||
//}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (g *GroupCacheRedis) GetGroupMembersInfo(ctx context.Context, count, offset int32, groupID string) (groupMembers []*relation.GroupMember, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "count", count, "offset", offset, "groupID", groupID, "groupMember", groupMembers)
|
||||
}()
|
||||
groupMemberIDList, err := g.GetGroupMemberIDs(ctx, groupID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count < 0 || offset < 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var groupMemberList []*relation.GroupMember
|
||||
var start, stop int32
|
||||
start = offset
|
||||
stop = offset + count
|
||||
l := int32(len(groupMemberIDList))
|
||||
if start > stop {
|
||||
return nil, nil
|
||||
}
|
||||
if start >= l {
|
||||
return nil, nil
|
||||
}
|
||||
if count != 0 {
|
||||
if stop >= l {
|
||||
stop = l
|
||||
}
|
||||
groupMemberIDList = groupMemberIDList[start:stop]
|
||||
} else {
|
||||
if l < 1000 {
|
||||
stop = l
|
||||
} else {
|
||||
stop = 1000
|
||||
}
|
||||
groupMemberIDList = groupMemberIDList[start:stop]
|
||||
}
|
||||
for _, userID := range groupMemberIDList {
|
||||
groupMember, err := g.GetGroupMemberInfo(ctx, groupID, userID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
groupMembers = append(groupMembers, groupMember)
|
||||
}
|
||||
return groupMemberList, nil
|
||||
}
|
||||
//func (g *GroupCacheRedis) GetGroupMembersInfo(ctx context.Context, count, offset int32, groupID string) (groupMembers []*relation.GroupMember, err error) {
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "count", count, "offset", offset, "groupID", groupID, "groupMember", groupMembers)
|
||||
// }()
|
||||
// groupMemberIDList, err := g.GetGroupMemberIDs(ctx, groupID)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// if count < 0 || offset < 0 {
|
||||
// return nil, nil
|
||||
// }
|
||||
// var groupMemberList []*relation.GroupMember
|
||||
// var start, stop int32
|
||||
// start = offset
|
||||
// stop = offset + count
|
||||
// l := int32(len(groupMemberIDList))
|
||||
// if start > stop {
|
||||
// return nil, nil
|
||||
// }
|
||||
// if start >= l {
|
||||
// return nil, nil
|
||||
// }
|
||||
// if count != 0 {
|
||||
// if stop >= l {
|
||||
// stop = l
|
||||
// }
|
||||
// groupMemberIDList = groupMemberIDList[start:stop]
|
||||
// } else {
|
||||
// if l < 1000 {
|
||||
// stop = l
|
||||
// } else {
|
||||
// stop = 1000
|
||||
// }
|
||||
// groupMemberIDList = groupMemberIDList[start:stop]
|
||||
// }
|
||||
// for _, userID := range groupMemberIDList {
|
||||
// groupMember, err := g.GetGroupMemberInfo(ctx, groupID, userID)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// groupMembers = append(groupMembers, groupMember)
|
||||
// }
|
||||
// return groupMemberList, nil
|
||||
//}
|
||||
|
||||
func (g *GroupCacheRedis) DelGroupMemberInfo(ctx context.Context, groupID, userID string) (err error) {
|
||||
defer func() {
|
||||
@@ -292,23 +303,23 @@ func (g *GroupCacheRedis) DelGroupMemberInfo(ctx context.Context, groupID, userI
|
||||
}
|
||||
|
||||
// groupMemberNum
|
||||
func (g *GroupCacheRedis) GetGroupMemberNum(ctx context.Context, groupID string) (num int, err error) {
|
||||
getGroupMemberNum := func() (string, error) {
|
||||
num, err := relation.GetGroupMemberNumByGroupID(groupID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strconv.Itoa(int(num)), nil
|
||||
}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "num", num)
|
||||
}()
|
||||
groupMember, err := g.rcClient.Fetch(g.getGroupMemberNumKey(groupID), time.Second*30*60, getGroupMemberNum)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return strconv.Atoi(groupMember)
|
||||
}
|
||||
//func (g *GroupCacheRedis) GetGroupMemberNum(ctx context.Context, groupID string) (num int, err error) {
|
||||
// getGroupMemberNum := func() (string, error) {
|
||||
// num, err := relation.GetGroupMemberNumByGroupID(groupID)
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
// return strconv.Itoa(int(num)), nil
|
||||
// }
|
||||
// defer func() {
|
||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "num", num)
|
||||
// }()
|
||||
// groupMember, err := g.rcClient.Fetch(g.getGroupMemberNumKey(groupID), time.Second*30*60, getGroupMemberNum)
|
||||
// if err != nil {
|
||||
// return 0, err
|
||||
// }
|
||||
// return strconv.Atoi(groupMember)
|
||||
//}
|
||||
|
||||
func (g *GroupCacheRedis) DelGroupMemberNum(ctx context.Context, groupID string) (err error) {
|
||||
defer func() {
|
||||
|
||||
Vendored
+23
-20
@@ -88,14 +88,14 @@ type Cache interface {
|
||||
|
||||
// native redis operate
|
||||
|
||||
type RedisClient struct {
|
||||
rdb redis.UniversalClient
|
||||
}
|
||||
//func NewRedis() *RedisClient {
|
||||
// o := &RedisClient{}
|
||||
// o.InitRedis()
|
||||
// return o
|
||||
//}
|
||||
|
||||
func (r *RedisClient) InitRedis() error {
|
||||
func NewRedis() (*RedisClient, error) {
|
||||
var rdb redis.UniversalClient
|
||||
var err error
|
||||
ctx := context.Background()
|
||||
if config.Config.Redis.EnableCluster {
|
||||
rdb = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: config.Config.Redis.DBAddress,
|
||||
@@ -103,11 +103,10 @@ func (r *RedisClient) InitRedis() error {
|
||||
Password: config.Config.Redis.DBPassWord, // no password set
|
||||
PoolSize: 50,
|
||||
})
|
||||
_, err = rdb.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
fmt.Println("redis cluster failed address ", config.Config.Redis.DBAddress, config.Config.Redis.DBUserName, config.Config.Redis.DBPassWord)
|
||||
return err
|
||||
}
|
||||
//if err := rdb.Ping(ctx).Err();err != nil {
|
||||
// return nil, fmt.Errorf("redis ping %w", err)
|
||||
//}
|
||||
//return &RedisClient{rdb: rdb}, nil
|
||||
} else {
|
||||
rdb = redis.NewClient(&redis.Options{
|
||||
Addr: config.Config.Redis.DBAddress[0],
|
||||
@@ -116,18 +115,22 @@ func (r *RedisClient) InitRedis() error {
|
||||
DB: 0, // use default DB
|
||||
PoolSize: 100, // 连接池大小
|
||||
})
|
||||
_, err = rdb.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
fmt.Println(" redis " + config.Config.Redis.DBAddress[0] + config.Config.Redis.DBUserName + config.Config.Redis.DBPassWord)
|
||||
return err
|
||||
}
|
||||
//err := rdb.Ping(ctx).Err()
|
||||
//if err != nil {
|
||||
// panic(err.Error() + " redis " + config.Config.Redis.DBAddress[0] + config.Config.Redis.DBUserName + config.Config.Redis.DBPassWord)
|
||||
//}
|
||||
}
|
||||
r.rdb = rdb
|
||||
return nil
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
err := rdb.Ping(ctx).Err()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("redis ping %w", err)
|
||||
}
|
||||
return &RedisClient{rdb: rdb}, nil
|
||||
}
|
||||
|
||||
func (r *RedisClient) GetClient() redis.UniversalClient {
|
||||
return r.rdb
|
||||
type RedisClient struct {
|
||||
rdb redis.UniversalClient
|
||||
}
|
||||
|
||||
func NewRedisClient(rdb redis.UniversalClient) *RedisClient {
|
||||
|
||||
@@ -19,7 +19,6 @@ type AuthController struct {
|
||||
}
|
||||
|
||||
func NewAuthController(rdb redis.UniversalClient, accessSecret string, accessExpire int64) *AuthController {
|
||||
cache.NewRedisClient(rdb)
|
||||
return &AuthController{database: cache.NewTokenRedis(cache.NewRedisClient(rdb), accessSecret, accessExpire)}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,32 +1,42 @@
|
||||
package controller
|
||||
|
||||
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"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ConversationInterface interface {
|
||||
//GetUserIDExistConversation 获取拥有该会话的的用户ID列表
|
||||
GetUserIDExistConversation(ctx context.Context, userIDList []string, conversationID string) ([]string, error)
|
||||
//UpdateUserConversationFiled 更新用户该会话的属性信息
|
||||
UpdateUsersConversationFiled(ctx context.Context, UserIDList []string, conversationID string, args map[string]interface{}) error
|
||||
UpdateUsersConversationFiled(ctx context.Context, userIDList []string, conversationID string, args map[string]interface{}) error
|
||||
//CreateConversation 创建一批新的会话
|
||||
CreateConversation(ctx context.Context, conversations []*relationTb.ConversationModel) error
|
||||
//SyncPeerUserPrivateConversation 同步对端私聊会话内部保证事务操作
|
||||
SyncPeerUserPrivateConversationTx(ctx context.Context, conversation *relationTb.ConversationModel) error
|
||||
//FindConversations 根据会话ID获取某个用户的多个会话
|
||||
FindConversations(ctx context.Context, ownerUserID string, conversationID []string) ([]*relationTb.ConversationModel, error)
|
||||
FindConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*relationTb.ConversationModel, error)
|
||||
//GetUserAllConversation 获取一个用户在服务器上所有的会话
|
||||
GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*relationTb.ConversationModel, error)
|
||||
//SetUserConversations 设置用户多个会话属性,如果会话不存在则创建,否则更新,内部保证原子性
|
||||
SetUserConversations(ctx context.Context, ownerUserID string, conversations []*relationTb.ConversationModel) error
|
||||
//SetUsersConversationFiledTx 设置多个用户会话关于某个字段的更新操作,如果会话不存在则创建,否则更新,内部保证事务操作
|
||||
SetUsersConversationFiledTx(ctx context.Context, userIDList []string, conversation *relationTb.ConversationModel, filedMap map[string]interface{}) error
|
||||
}
|
||||
type ConversationController struct {
|
||||
database ConversationDataBaseInterface
|
||||
}
|
||||
|
||||
func (c *ConversationController) SetUsersConversationFiledTx(ctx context.Context, userIDList []string, conversation *relationTb.ConversationModel, filedMap map[string]interface{}) error {
|
||||
return c.database.SetUsersConversationFiledTx(ctx, userIDList, conversation, filedMap)
|
||||
}
|
||||
|
||||
func NewConversationController(database ConversationDataBaseInterface) *ConversationController {
|
||||
return &ConversationController{database: database}
|
||||
}
|
||||
@@ -36,26 +46,26 @@ func (c *ConversationController) GetUserIDExistConversation(ctx context.Context,
|
||||
}
|
||||
|
||||
func (c ConversationController) UpdateUsersConversationFiled(ctx context.Context, UserIDList []string, conversationID string, args map[string]interface{}) error {
|
||||
panic("implement me")
|
||||
return c.database.UpdateUsersConversationFiled(ctx, UserIDList, conversationID, args)
|
||||
}
|
||||
|
||||
func (c ConversationController) CreateConversation(ctx context.Context, conversations []*relationTb.ConversationModel) error {
|
||||
panic("implement me")
|
||||
return c.database.CreateConversation(ctx, conversations)
|
||||
}
|
||||
|
||||
func (c ConversationController) SyncPeerUserPrivateConversationTx(ctx context.Context, conversation *relationTb.ConversationModel) error {
|
||||
panic("implement me")
|
||||
return c.database.SyncPeerUserPrivateConversationTx(ctx, conversation)
|
||||
}
|
||||
|
||||
func (c ConversationController) FindConversations(ctx context.Context, ownerUserID string, conversationID []string) ([]*relationTb.ConversationModel, error) {
|
||||
panic("implement me")
|
||||
func (c ConversationController) FindConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*relationTb.ConversationModel, error) {
|
||||
return c.database.FindConversations(ctx, ownerUserID, conversationIDs)
|
||||
}
|
||||
|
||||
func (c ConversationController) GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*relationTb.ConversationModel, error) {
|
||||
panic("implement me")
|
||||
return c.database.GetUserAllConversation(ctx, ownerUserID)
|
||||
}
|
||||
func (c ConversationController) SetUserConversations(ctx context.Context, ownerUserID string, conversations []*relationTb.ConversationModel) error {
|
||||
panic("implement me")
|
||||
return c.database.SetUserConversations(ctx, ownerUserID, conversations)
|
||||
}
|
||||
|
||||
var _ ConversationInterface = (*ConversationController)(nil)
|
||||
@@ -70,15 +80,65 @@ type ConversationDataBaseInterface interface {
|
||||
//SyncPeerUserPrivateConversation 同步对端私聊会话内部保证事务操作
|
||||
SyncPeerUserPrivateConversationTx(ctx context.Context, conversation *relationTb.ConversationModel) error
|
||||
//FindConversations 根据会话ID获取某个用户的多个会话
|
||||
FindConversations(ctx context.Context, ownerUserID string, conversationID []string) ([]*relationTb.ConversationModel, error)
|
||||
FindConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*relationTb.ConversationModel, error)
|
||||
//GetUserAllConversation 获取一个用户在服务器上所有的会话
|
||||
GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*relationTb.ConversationModel, error)
|
||||
//SetUserConversations 设置用户多个会话属性,如果会话不存在则创建,否则更新,内部保证原子性
|
||||
SetUserConversations(ctx context.Context, ownerUserID string, conversations []*relationTb.ConversationModel) error
|
||||
//SetUsersConversationFiledTx 设置多个用户会话关于某个字段的更新操作,如果会话不存在则创建,否则更新,内部保证事务操作
|
||||
SetUsersConversationFiledTx(ctx context.Context, userIDList []string, conversation *relationTb.ConversationModel, filedMap map[string]interface{}) error
|
||||
}
|
||||
|
||||
var _ ConversationDataBaseInterface = (*ConversationDataBase)(nil)
|
||||
|
||||
type ConversationDataBase struct {
|
||||
db relation.Conversation
|
||||
cache cache.ConversationCache
|
||||
conversationDB relation.Conversation
|
||||
cache cache.ConversationCache
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func (c ConversationDataBase) SetUsersConversationFiledTx(ctx context.Context, userIDList []string, conversation *relationTb.ConversationModel, filedMap map[string]interface{}) error {
|
||||
return c.db.Transaction(func(tx *gorm.DB) error {
|
||||
haveUserID, err := c.conversationDB.FindUserID(ctx, userIDList, conversation.ConversationID, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(haveUserID) > 0 {
|
||||
err = c.conversationDB.UpdateByMap(ctx, haveUserID, conversation.ConversationID, filedMap, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
NotUserID := utils.DifferenceString(haveUserID, userIDList)
|
||||
var cList []*relationTb.ConversationModel
|
||||
for _, v := range NotUserID {
|
||||
temp := new(relationTb.ConversationModel)
|
||||
if err := utils.CopyStructFields(temp, conversation); err != nil {
|
||||
return err
|
||||
}
|
||||
temp.OwnerUserID = v
|
||||
cList = append(cList, temp)
|
||||
}
|
||||
err = c.conversationDB.Create(ctx, cList)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(NotUserID) > 0 {
|
||||
err = c.cache.DelUsersConversationIDs(ctx, NotUserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = c.cache.DelUsersConversation(ctx, haveUserID, conversation.ConversationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func NewConversationDataBase(db relation.Conversation, cache cache.ConversationCache) *ConversationDataBase {
|
||||
return &ConversationDataBase{conversationDB: db, cache: cache}
|
||||
}
|
||||
|
||||
func (c ConversationDataBase) GetUserIDExistConversation(ctx context.Context, userIDList []string, conversationID string) ([]string, error) {
|
||||
@@ -94,26 +154,155 @@ func (c ConversationDataBase) CreateConversation(ctx context.Context, conversati
|
||||
}
|
||||
|
||||
func (c ConversationDataBase) SyncPeerUserPrivateConversationTx(ctx context.Context, conversation *relationTb.ConversationModel) error {
|
||||
panic("implement me")
|
||||
return c.db.Transaction(func(tx *gorm.DB) error {
|
||||
userIDList := []string{conversation.OwnerUserID, conversation.UserID}
|
||||
haveUserID, err := c.conversationDB.FindUserID(ctx, userIDList, conversation.ConversationID, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
filedMap := map[string]interface{}{"is_private_chat": conversation.IsPrivateChat}
|
||||
if len(haveUserID) > 0 {
|
||||
err = c.conversationDB.UpdateByMap(ctx, haveUserID, conversation.ConversationID, filedMap, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
NotUserID := utils.DifferenceString(haveUserID, userIDList)
|
||||
var cList []*relationTb.ConversationModel
|
||||
for _, v := range NotUserID {
|
||||
temp := new(relationTb.ConversationModel)
|
||||
if v == conversation.UserID {
|
||||
temp.OwnerUserID = conversation.UserID
|
||||
temp.ConversationID = utils.GetConversationIDBySessionType(conversation.OwnerUserID, constant.SingleChatType)
|
||||
temp.ConversationType = constant.SingleChatType
|
||||
temp.UserID = conversation.OwnerUserID
|
||||
temp.IsPrivateChat = conversation.IsPrivateChat
|
||||
} else {
|
||||
if err := utils.CopyStructFields(temp, conversation); err != nil {
|
||||
return err
|
||||
}
|
||||
temp.OwnerUserID = v
|
||||
}
|
||||
cList = append(cList, temp)
|
||||
}
|
||||
if len(NotUserID) > 0 {
|
||||
err = c.conversationDB.Create(ctx, cList)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = c.cache.DelUsersConversationIDs(ctx, NotUserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.cache.DelUsersConversation(ctx, haveUserID, conversation.ConversationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (c ConversationDataBase) FindConversations(ctx context.Context, ownerUserID string, conversationID []string) ([]*relationTb.ConversationModel, error) {
|
||||
panic("implement me")
|
||||
func (c ConversationDataBase) FindConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*relationTb.ConversationModel, error) {
|
||||
getConversation := func() (string, error) {
|
||||
conversationList, err := c.conversationDB.Find(ctx, ownerUserID, conversationIDs)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "get failed")
|
||||
}
|
||||
bytes, err := json.Marshal(conversationList)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "Marshal failed")
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
return c.cache.GetConversations(ctx, ownerUserID, conversationIDs, getConversation)
|
||||
}
|
||||
|
||||
func (c ConversationDataBase) GetConversation(ctx context.Context, ownerUserID string, conversationID string) (*relationTb.ConversationModel, error) {
|
||||
getConversation := func() (string, error) {
|
||||
conversationList, err := c.conversationDB.Take(ctx, ownerUserID, conversationID)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "get failed")
|
||||
}
|
||||
bytes, err := json.Marshal(conversationList)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "Marshal failed")
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
return c.cache.GetConversation(ctx, ownerUserID, conversationID, getConversation)
|
||||
}
|
||||
|
||||
func (c ConversationDataBase) GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*relationTb.ConversationModel, error) {
|
||||
panic("implement me")
|
||||
getConversationIDList := func() (string, error) {
|
||||
conversationIDList, err := c.conversationDB.FindUserIDAllConversationID(ctx, ownerUserID)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "getConversationIDList failed")
|
||||
}
|
||||
bytes, err := json.Marshal(conversationIDList)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
conversationIDList, err := c.cache.GetUserConversationIDs(ctx, ownerUserID, getConversationIDList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var conversations []*relationTb.ConversationModel
|
||||
for _, conversationID := range conversationIDList {
|
||||
conversation, tErr := c.GetConversation(ctx, ownerUserID, conversationID)
|
||||
if tErr != nil {
|
||||
return nil, utils.Wrap(tErr, "GetConversation failed")
|
||||
}
|
||||
conversations = append(conversations, conversation)
|
||||
}
|
||||
return conversations, nil
|
||||
}
|
||||
|
||||
func (c ConversationDataBase) SetUserConversations(ctx context.Context, ownerUserID string, conversations []*relationTb.ConversationModel) error {
|
||||
panic("implement me")
|
||||
}
|
||||
return c.db.Transaction(func(tx *gorm.DB) error {
|
||||
var conversationIDList []string
|
||||
for _, conversation := range conversations {
|
||||
conversationIDList = append(conversationIDList, conversation.ConversationID)
|
||||
}
|
||||
haveConversations, err := c.conversationDB.Find(ctx, ownerUserID, conversationIDList, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(haveConversations) > 0 {
|
||||
err = c.conversationDB.Update(ctx, conversations, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var haveConversationID []string
|
||||
for _, conversation := range haveConversations {
|
||||
haveConversationID = append(haveConversationID, conversation.ConversationID)
|
||||
}
|
||||
|
||||
func NewConversationDataBase(db relation.Conversation, cache cache.ConversationCache) *ConversationDataBase {
|
||||
return &ConversationDataBase{db: db, cache: cache}
|
||||
NotConversationID := utils.DifferenceString(haveConversationID, conversationIDList)
|
||||
var NotConversations []*relationTb.ConversationModel
|
||||
for _, conversation := range conversations {
|
||||
if !utils.IsContain(conversation.ConversationID, haveConversationID) {
|
||||
NotConversations = append(NotConversations, conversation)
|
||||
}
|
||||
}
|
||||
if len(NotConversations) > 0 {
|
||||
err = c.conversationDB.Create(ctx, NotConversations)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = c.cache.DelUsersConversationIDs(ctx, NotConversationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.cache.DelUserConversations(ctx, ownerUserID, haveConversationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
//func NewConversationController(db *gorm.DB, rdb redis.UniversalClient) ConversationInterface {
|
||||
// groupController := &ConversationController{database: newGroupDatabase(db, rdb, mgoClient)}
|
||||
// return groupController
|
||||
//}
|
||||
|
||||
@@ -15,8 +15,6 @@ import (
|
||||
"github.com/go-redis/redis/v8"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"gorm.io/gorm"
|
||||
"math/big"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//type GroupInterface GroupDataBaseInterface
|
||||
@@ -57,8 +55,8 @@ type GroupInterface interface {
|
||||
|
||||
var _ GroupInterface = (*GroupController)(nil)
|
||||
|
||||
func NewGroupInterface(db *gorm.DB, rdb redis.UniversalClient, mgoClient *mongo.Client) GroupInterface {
|
||||
return &GroupController{database: NewGroupDatabase(db, rdb, mgoClient)}
|
||||
func NewGroupInterface(database GroupDataBaseInterface) GroupInterface {
|
||||
return &GroupController{database: database}
|
||||
}
|
||||
|
||||
type GroupController struct {
|
||||
@@ -74,7 +72,7 @@ func (g *GroupController) CreateGroup(ctx context.Context, groups []*relationTb.
|
||||
}
|
||||
|
||||
func (g *GroupController) TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error) {
|
||||
return g.TakeGroup(ctx, groupID)
|
||||
return g.database.TakeGroup(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupController) FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error) {
|
||||
@@ -177,6 +175,53 @@ func (g *GroupController) CreateSuperGroupMember(ctx context.Context, groupID st
|
||||
return g.database.CreateSuperGroupMember(ctx, groupID, userIDs)
|
||||
}
|
||||
|
||||
type Group 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) (uint32, []*relationTb.GroupModel, error)
|
||||
UpdateGroup(ctx context.Context, groupID string, data map[string]any) error
|
||||
DismissGroup(ctx context.Context, groupID string) error // 解散群,并删除群成员
|
||||
}
|
||||
|
||||
type GroupMember 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
|
||||
DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string]*relationTb.GroupSimpleUserID, 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 string, userID string, data map[string]any) error
|
||||
UpdateGroupMembers(ctx context.Context, data []*relationTb.BatchUpdateGroupMember) error
|
||||
}
|
||||
|
||||
type GroupRequest interface {
|
||||
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) (uint32, []*relationTb.GroupRequestModel, error)
|
||||
}
|
||||
|
||||
type SuperGroup interface {
|
||||
FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unrelationTb.SuperGroupModel, error)
|
||||
FindJoinSuperGroup(ctx context.Context, userID string) (*unrelationTb.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
|
||||
}
|
||||
|
||||
type GroupDataBase1 interface {
|
||||
Group
|
||||
GroupMember
|
||||
GroupRequest
|
||||
SuperGroup
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -248,7 +293,7 @@ type GroupDataBase struct {
|
||||
|
||||
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 {
|
||||
if err := g.cache.DelJoinedGroupID(ctx, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.cache.DelJoinedSuperGroupIDs(ctx, userID); err != nil {
|
||||
@@ -272,25 +317,31 @@ func (g *GroupDataBase) FindGroupMemberUserID(ctx context.Context, groupID strin
|
||||
}
|
||||
|
||||
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 {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if len(groups) > 0 {
|
||||
if err := g.groupDB.Create(ctx, groups, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.groupMemberDB.Create(ctx, groupMembers, tx)
|
||||
})
|
||||
}
|
||||
if len(groups) > 0 {
|
||||
return g.groupDB.Create(ctx, groups)
|
||||
}
|
||||
if len(groupMembers) > 0 {
|
||||
return g.groupMemberDB.Create(ctx, groupMembers)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if len(groupMembers) > 0 {
|
||||
if err := g.groupMemberDB.Create(ctx, groupMembers, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
//if err := g.cache.DelJoinedGroupIDs(ctx, utils.Slice(groupMembers, func(e *relationTb.GroupMemberModel) string {
|
||||
// return e.UserID
|
||||
//})); err != nil {
|
||||
// return err
|
||||
//}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error) {
|
||||
return g.cache.GetGroupInfo(ctx, groupID)
|
||||
//return g.cache.GetGroupInfo(ctx, groupID)
|
||||
return cache.GetCache(ctx, g.rcClient, g.getGroupInfoKey(groupID), g.expireTime, func(ctx context.Context) (*relationTb.GroupModel, error) {
|
||||
return g.group.Take(ctx, groupID)
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error) {
|
||||
@@ -337,12 +388,11 @@ func (g *GroupDataBase) TakeGroupMember(ctx context.Context, groupID string, use
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error) {
|
||||
return g.groupMemberDB.TakeOwner(ctx, groupID)
|
||||
return g.groupMemberDB.TakeOwner(ctx, groupID) // todo cache group owner
|
||||
}
|
||||
|
||||
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)
|
||||
return g.groupMemberDB.Find(ctx, groupIDs, userIDs, roleLevels) // todo cache group find
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error) {
|
||||
@@ -383,23 +433,7 @@ func (g *GroupDataBase) DeleteGroupMember(ctx context.Context, groupID string, u
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string]*relationTb.GroupSimpleUserID, error) {
|
||||
mapGroupUserIDs, err := g.groupMemberDB.FindJoinUserID(ctx, groupIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res := make(map[string]*relationTb.GroupSimpleUserID)
|
||||
for _, groupID := range groupIDs {
|
||||
userIDs := mapGroupUserIDs[groupID]
|
||||
users := &relationTb.GroupSimpleUserID{}
|
||||
if len(userIDs) > 0 {
|
||||
utils.Sort(userIDs, true)
|
||||
bi := big.NewInt(0)
|
||||
bi.SetString(utils.Md5(strings.Join(userIDs, ";"))[0:8], 16)
|
||||
users.Hash = bi.Uint64()
|
||||
}
|
||||
res[groupID] = users
|
||||
}
|
||||
return res, nil
|
||||
return g.cache.GetGroupMemberHash1(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) MapGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]uint32, error) {
|
||||
|
||||
@@ -24,8 +24,8 @@ type GroupMemberIDsHash struct {
|
||||
userIDs []string
|
||||
}
|
||||
|
||||
func NewGroupMemberIDsLocalCache(client discoveryRegistry.SvcDiscoveryRegistry) GroupLocalCache {
|
||||
return GroupLocalCache{
|
||||
func NewGroupMemberIDsLocalCache(client discoveryRegistry.SvcDiscoveryRegistry) *GroupLocalCache {
|
||||
return &GroupLocalCache{
|
||||
cache: make(map[string]GroupMemberIDsHash, 0),
|
||||
client: client,
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/table"
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
@@ -10,12 +10,15 @@ import (
|
||||
|
||||
type Conversation interface {
|
||||
TableName() string
|
||||
Create(ctx context.Context, conversations []*table.ConversationModel) (err error)
|
||||
Create(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error)
|
||||
Delete(ctx context.Context, groupIDs []string) (err error)
|
||||
UpdateByMap(ctx context.Context, groupID string, args map[string]interface{}) (err error)
|
||||
Update(ctx context.Context, groups []*table.ConversationModel) (err error)
|
||||
Find(ctx context.Context, groupIDs []string) (groups []*table.ConversationModel, err error)
|
||||
Take(ctx context.Context, groupID string) (group *table.ConversationModel, err error)
|
||||
UpdateByMap(ctx context.Context, userIDList []string, conversationID string, args map[string]interface{}, tx ...any) (err error)
|
||||
Update(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error)
|
||||
Find(ctx context.Context, ownerUserID string, conversationIDs []string, tx ...any) (conversations []*relation.ConversationModel, err error)
|
||||
FindUserID(ctx context.Context, userIDList []string, conversationID string, tx ...any) ([]string, error)
|
||||
FindUserIDAllConversationID(ctx context.Context, userID string, tx ...any) ([]string, error)
|
||||
Take(ctx context.Context, userID, conversationID string, tx ...any) (conversation *relation.ConversationModel, err error)
|
||||
FindConversationID(ctx context.Context, userID string, conversationIDList []string, tx ...any) (existConversationID []string, err error)
|
||||
}
|
||||
type ConversationGorm struct {
|
||||
DB *gorm.DB
|
||||
@@ -29,45 +32,69 @@ func NewConversationGorm(DB *gorm.DB) Conversation {
|
||||
return &ConversationGorm{DB: DB}
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) Create(ctx context.Context, conversations []*table.ConversationModel) (err error) {
|
||||
func (c *ConversationGorm) Create(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "conversations", conversations)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Create(&conversations).Error, "")
|
||||
return utils.Wrap(getDBConn(c.DB, tx).Create(&conversations).Error, "")
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) Delete(ctx context.Context, groupIDs []string) (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(&table.ConversationModel{}).Error, "")
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.ConversationModel{}).Error, "")
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) UpdateByMap(ctx context.Context, groupID string, args map[string]interface{}) (err error) {
|
||||
func (c *ConversationGorm) UpdateByMap(ctx context.Context, userIDList []string, conversationID string, args map[string]interface{}, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "args", args)
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userIDList", userIDList, "conversationID", conversationID)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Model(g).Updates(args).Error, "")
|
||||
return utils.Wrap(getDBConn(c.DB, tx).Model(&relation.ConversationModel{}).Where("owner_user_id IN (?) and conversation_id=?", userIDList, conversationID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) Update(ctx context.Context, groups []*table.ConversationModel) (err error) {
|
||||
func (c *ConversationGorm) Update(ctx context.Context, conversations []*relation.ConversationModel, tx ...any) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "conversations", conversations)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Updates(&groups).Error, "")
|
||||
return utils.Wrap(getDBConn(c.DB, tx).Updates(&conversations).Error, "")
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) Find(ctx context.Context, groupIDs []string) (groups []*table.ConversationModel, err error) {
|
||||
func (c *ConversationGorm) Find(ctx context.Context, ownerUserID string, conversationIDs []string, tx ...any) (conversations []*relation.ConversationModel, err error) {
|
||||
var newConversations []relation.ConversationModel
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groups", groups)
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "groups", conversations)
|
||||
}()
|
||||
return groups, utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Find(&groups).Error, "")
|
||||
err = utils.Wrap(getDBConn(c.DB, tx).Where("owner_user_id=? and conversation_id IN (?)", ownerUserID, conversationIDs).Find(&newConversations).Error, "")
|
||||
for _, v := range newConversations {
|
||||
v1 := v
|
||||
conversations = append(conversations, &v1)
|
||||
}
|
||||
return conversations, err
|
||||
}
|
||||
|
||||
func (c *ConversationGorm) Take(ctx context.Context, groupID string) (group *table.ConversationModel, err error) {
|
||||
group = &Group{}
|
||||
func (c *ConversationGorm) Take(ctx context.Context, userID, conversationID string, tx ...any) (conversation *relation.ConversationModel, err error) {
|
||||
cc := &relation.ConversationModel{}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", *group)
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "conversation", *conversation)
|
||||
}()
|
||||
return group, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Take(group).Error, "")
|
||||
return cc, utils.Wrap(getDBConn(c.DB, tx).Where("conversation_id = ? And owner_user_id = ?", conversationID, userID).Take(cc).Error, "")
|
||||
}
|
||||
func (c *ConversationGorm) FindUserID(ctx context.Context, userIDList []string, conversationID string, tx ...any) (existUserID []string, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userIDList, "existUserID", existUserID)
|
||||
}()
|
||||
return existUserID, utils.Wrap(getDBConn(c.DB, tx).Where(" owner_user_id IN (?) and conversation_id=?", userIDList, conversationID).Pluck("owner_user_id", &existUserID).Error, "")
|
||||
}
|
||||
func (c *ConversationGorm) FindConversationID(ctx context.Context, userID string, conversationIDList []string, tx ...any) (existConversationID []string, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "existConversationIDList", existConversationID)
|
||||
}()
|
||||
return existConversationID, utils.Wrap(getDBConn(c.DB, tx).Where(" conversation_id IN (?) and owner_user_id=?", conversationIDList, userID).Pluck("conversation_id", &existConversationID).Error, "")
|
||||
}
|
||||
func (c *ConversationGorm) FindUserIDAllConversationID(ctx context.Context, userID string, tx ...any) (conversationIDList []string, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "conversationIDList", conversationIDList)
|
||||
}()
|
||||
return conversationIDList, utils.Wrap(getDBConn(c.DB, tx).Model(&relation.ConversationModel{}).Where("owner_user_id=?", userID).Pluck("conversation_id", &conversationIDList).Error, "")
|
||||
}
|
||||
|
||||
@@ -130,5 +130,5 @@ func (g *GroupMemberGorm) FindMemberUserID(ctx context.Context, groupID string,
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userIDs", userIDs)
|
||||
}()
|
||||
return userIDs, utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ?", groupID).Pluck("user_id", &userIDs), "")
|
||||
return userIDs, utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ?", groupID).Pluck("user_id", &userIDs).Error, "")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func NewGormDB() (*gorm.DB, error) {
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
|
||||
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], "mysql")
|
||||
db, err := gorm.Open(mysql.Open(dsn), nil)
|
||||
if err != nil {
|
||||
time.Sleep(time.Duration(30) * time.Second)
|
||||
db, err = gorm.Open(mysql.Open(dsn), nil)
|
||||
if err != nil {
|
||||
panic(err.Error() + " open failed " + dsn)
|
||||
}
|
||||
}
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer sqlDB.Close()
|
||||
sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s default charset utf8 COLLATE utf8_general_ci;", config.Config.Mysql.DBDatabaseName)
|
||||
err = db.Exec(sql).Error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("init db %w", err)
|
||||
}
|
||||
dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
|
||||
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName)
|
||||
newLogger := logger.New(
|
||||
Writer{},
|
||||
logger.Config{
|
||||
SlowThreshold: time.Duration(config.Config.Mysql.SlowThreshold) * time.Millisecond, // Slow SQL threshold
|
||||
LogLevel: logger.LogLevel(config.Config.Mysql.LogLevel), // Log level
|
||||
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
|
||||
Colorful: true, // Disable color
|
||||
},
|
||||
)
|
||||
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
Logger: newLogger,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sqlDB, err = db.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.Config.Mysql.DBMaxLifeTime))
|
||||
sqlDB.SetMaxOpenConns(config.Config.Mysql.DBMaxOpenConns)
|
||||
sqlDB.SetMaxIdleConns(config.Config.Mysql.DBMaxIdleConns)
|
||||
return db, nil
|
||||
}
|
||||
|
||||
type Mysql struct {
|
||||
gormConn *gorm.DB
|
||||
}
|
||||
|
||||
func (m *Mysql) GormConn() *gorm.DB {
|
||||
return m.gormConn
|
||||
}
|
||||
|
||||
//func (m *Mysql) SetGormConn(gormConn *gorm.DB) {
|
||||
// m.gormConn = gormConn
|
||||
//}
|
||||
//
|
||||
//func (m *Mysql) InitConn() *Mysql {
|
||||
// dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
|
||||
// config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], "mysql")
|
||||
// var db *gorm.DB
|
||||
// db, err := gorm.Open(mysql.Open(dsn), nil)
|
||||
// if err != nil {
|
||||
// time.Sleep(time.Duration(30) * time.Second)
|
||||
// db, err = gorm.Open(mysql.Open(dsn), nil)
|
||||
// if err != nil {
|
||||
// panic(err.Error() + " open failed " + dsn)
|
||||
// }
|
||||
// }
|
||||
// sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s default charset utf8 COLLATE utf8_general_ci;", config.Config.Mysql.DBDatabaseName)
|
||||
// err = db.Exec(sql).Error
|
||||
// if err != nil {
|
||||
// panic(err.Error() + " Exec failed:" + sql)
|
||||
// }
|
||||
// dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
|
||||
// config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName)
|
||||
// newLogger := logger.New(
|
||||
// Writer{},
|
||||
// logger.Config{
|
||||
// SlowThreshold: time.Duration(config.Config.Mysql.SlowThreshold) * time.Millisecond, // Slow SQL threshold
|
||||
// LogLevel: logger.LogLevel(config.Config.Mysql.LogLevel), // Log level
|
||||
// IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
|
||||
// Colorful: true, // Disable color
|
||||
// },
|
||||
// )
|
||||
// db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
// Logger: newLogger,
|
||||
// })
|
||||
// if err != nil {
|
||||
// panic(err.Error() + " Open failed " + dsn)
|
||||
// }
|
||||
// sqlDB, err := db.DB()
|
||||
// if err != nil {
|
||||
// panic(err.Error() + " DB.DB() failed ")
|
||||
// }
|
||||
// sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.Config.Mysql.DBMaxLifeTime))
|
||||
// sqlDB.SetMaxOpenConns(config.Config.Mysql.DBMaxOpenConns)
|
||||
// sqlDB.SetMaxIdleConns(config.Config.Mysql.DBMaxIdleConns)
|
||||
// if db == nil {
|
||||
// panic("db is nil")
|
||||
// }
|
||||
// m.SetGormConn(db)
|
||||
// return m
|
||||
//}
|
||||
|
||||
//models := []interface{}{&Friend{}, &FriendRequest{}, &Group{}, &GroupMember{}, &GroupRequest{},
|
||||
// &User{}, &Black{}, &ChatLog{}, &Conversation{}, &AppVersion{}}
|
||||
|
||||
//func (m *Mysql) AutoMigrateModel(model interface{}) error {
|
||||
// err := m.gormConn.AutoMigrate(model)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// m.gormConn.Set("gorm:table_options", "CHARSET=utf8")
|
||||
// m.gormConn.Set("gorm:table_options", "collation=utf8_unicode_ci")
|
||||
// _ = m.gormConn.Migrator().CreateTable(model)
|
||||
// return nil
|
||||
//}
|
||||
|
||||
type Writer struct{}
|
||||
|
||||
func (w Writer) Printf(format string, args ...interface{}) {
|
||||
fmt.Printf(format, args...)
|
||||
}
|
||||
|
||||
func getDBConn(db *gorm.DB, tx []any) *gorm.DB {
|
||||
if len(tx) > 0 {
|
||||
if txDB, ok := tx[0].(*gorm.DB); ok {
|
||||
return txDB
|
||||
}
|
||||
}
|
||||
return db
|
||||
}
|
||||
@@ -13,6 +13,47 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
//func NewMongo() *Mongo {
|
||||
// mgo := &Mongo{}
|
||||
// mgo.InitMongo()
|
||||
// return mgo
|
||||
//}
|
||||
|
||||
func NewMongo() (*Mongo, error) {
|
||||
uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority"
|
||||
if config.Config.Mongo.DBUri != "" {
|
||||
// example: mongodb://$user:$password@mongo1.mongo:27017,mongo2.mongo:27017,mongo3.mongo:27017/$DBDatabase/?replicaSet=rs0&readPreference=secondary&authSource=admin&maxPoolSize=$DBMaxPoolSize
|
||||
uri = config.Config.Mongo.DBUri
|
||||
} else {
|
||||
//mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=mySet&authSource=authDB
|
||||
mongodbHosts := ""
|
||||
for i, v := range config.Config.Mongo.DBAddress {
|
||||
if i == len(config.Config.Mongo.DBAddress)-1 {
|
||||
mongodbHosts += v
|
||||
} else {
|
||||
mongodbHosts += v + ","
|
||||
}
|
||||
}
|
||||
if config.Config.Mongo.DBPassword != "" && config.Config.Mongo.DBUserName != "" {
|
||||
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
|
||||
config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword, mongodbHosts,
|
||||
config.Config.Mongo.DBDatabase, config.Config.Mongo.DBMaxPoolSize)
|
||||
} else {
|
||||
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin",
|
||||
mongodbHosts, config.Config.Mongo.DBDatabase,
|
||||
config.Config.Mongo.DBMaxPoolSize)
|
||||
}
|
||||
}
|
||||
fmt.Println("mongo:", uri)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
|
||||
defer cancel()
|
||||
mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Mongo{db: mongoClient}, nil
|
||||
}
|
||||
|
||||
type Mongo struct {
|
||||
db *mongo.Client
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user