Merge remote-tracking branch 'origin/errcode' into errcode

This commit is contained in:
withchao
2023-02-09 14:33:45 +08:00
27 changed files with 656 additions and 907 deletions
+13 -10
View File
@@ -1,8 +1,6 @@
package cache
import (
"Open_IM/pkg/common/db/relation"
relation2 "Open_IM/pkg/common/db/table/relation"
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils"
"context"
@@ -17,25 +15,30 @@ const (
blackExpireTime = time.Second * 60 * 60 * 12
)
type BlackCache struct {
blackDB *relation2.BlackModel
type BlackCache interface {
//get blackIDs from cache
GetBlackIDs(ctx context.Context, userID string, fn func(ctx context.Context, userID string) ([]string, error)) (blackIDs []string, err error)
//del user's blackIDs cache, exec when a user's black list changed
DelBlackIDs(ctx context.Context, userID string) (err error)
}
type BlackCacheRedis struct {
expireTime time.Duration
rcClient *rockscache.Client
}
func NewBlackCache(rdb redis.UniversalClient, blackDB *relation.BlackGorm, options rockscache.Options) *BlackCache {
return &BlackCache{
blackDB: blackDB,
func NewBlackCacheRedis(rdb redis.UniversalClient, blackDB BlackCache, options rockscache.Options) *BlackCacheRedis {
return &BlackCacheRedis{
expireTime: blackExpireTime,
rcClient: rockscache.NewClient(rdb, options),
}
}
func (b *BlackCache) getBlackIDsKey(ownerUserID string) string {
func (b *BlackCacheRedis) getBlackIDsKey(ownerUserID string) string {
return blackIDsKey + ownerUserID
}
func (b *BlackCache) GetBlackIDs(ctx context.Context, userID string) (blackIDs []string, err error) {
func (b *BlackCacheRedis) GetBlackIDs(ctx context.Context, userID string) (blackIDs []string, err error) {
getBlackIDList := func() (string, error) {
blackIDs, err := b.blackDB.GetBlackIDs(ctx, userID)
if err != nil {
@@ -58,7 +61,7 @@ func (b *BlackCache) GetBlackIDs(ctx context.Context, userID string) (blackIDs [
return blackIDs, utils.Wrap(err, "")
}
func (b *BlackCache) DelBlackIDListFromCache(ctx context.Context, userID string) (err error) {
func (b *BlackCacheRedis) DelBlackIDs(ctx context.Context, userID string) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID)
}()
+52 -31
View File
@@ -2,7 +2,7 @@ package cache
import (
"Open_IM/pkg/common/db/relation"
relation2 "Open_IM/pkg/common/db/table/relation"
relationTb "Open_IM/pkg/common/db/table/relation"
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils"
"context"
@@ -13,18 +13,39 @@ import (
"time"
)
type DBFun func() (string, error)
const (
conversationKey = "CONVERSATION:"
conversationIDsKey = "CONVERSATION_IDS:"
recvMsgOptKey = "RECV_MSG_OPT:"
superGroupRecvMsgNotNotifyUserIDsKey = "SUPER_GROUP_RECV_MSG_NOT_NOTIFY_USER_IDS:"
conversationExpireTime = time.Second * 60 * 60 * 12
)
type ConversationCache interface {
GetUserConversationIDListFromCache(userID string, fn DBFun) ([]string, error)
DelUserConversationIDListFromCache(userID string) error
GetConversationFromCache(ownerUserID, conversationID string, fn DBFun) (*table.ConversationModel, error)
GetConversationsFromCache(ownerUserID string, conversationIDList []string, fn DBFun) ([]*table.ConversationModel, error)
GetUserAllConversationList(ownerUserID string, fn DBFun) ([]*table.ConversationModel, error)
DelConversationFromCache(ownerUserID, conversationID string) error
GetUserConversationIDs(ctx context.Context, ownerUserID string, f func(ctx context.Context, userID string) ([]string, error)) ([]string, error)
// get user's conversationIDs from cache
GetUserConversationIDs(ctx context.Context, userID string, fn func(ctx context.Context, userID string) ([]string, error)) ([]string, error)
// del user's conversationIDs from cache, call when a user add or reduce a conversation
DelUserConversationIDs(ctx context.Context, userID 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)
// 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)
// 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)
// del one conversation from cache, call when one user's conversation Info changed
DelConversation(ctx context.Context, ownerUserID, 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
DelUserRecvMsgOpt(ctx context.Context, ownerUserID, conversationID string) error
// get one super group recv msg but do not notification userID list
GetSuperGroupRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string, fn func(ctx context.Context, groupID string) (userIDs []string, err error)) (userIDs []string, err error)
// del one super group recv msg but do not notification userID list, call it when this list changed
DelSuperGroupRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) error
//GetSuperGroupRecvMsgNotNotifyUserIDsHash(ctx context.Context, groupID string) (hash uint32, err error)
//DelSuperGroupRecvMsgNotNotifyUserIDsHash(ctx context.Context, groupID string)
}
type ConversationRedis struct {
rcClient *rockscache.Client
}
@@ -33,27 +54,27 @@ func NewConversationRedis(rcClient *rockscache.Client) *ConversationRedis {
return &ConversationRedis{rcClient: rcClient}
}
func NewConversationCache(rdb redis.UniversalClient, conversationDB *relation.ConversationGorm, options rockscache.Options) *ConversationCache {
return &ConversationCache{conversationDB: conversationDB, expireTime: conversationExpireTime, rcClient: rockscache.NewClient(rdb, options)}
func NewNewConversationRedis(rdb redis.UniversalClient, conversationDB *relation.ConversationGorm, options rockscache.Options) *ConversationRedis {
return &ConversationRedis{conversationDB: conversationDB, expireTime: conversationExpireTime, rcClient: rockscache.NewClient(rdb, options)}
}
func (c *ConversationCache) getConversationKey(ownerUserID, conversationID string) string {
func (c *ConversationRedis) getConversationKey(ownerUserID, conversationID string) string {
return conversationKey + ownerUserID + ":" + conversationID
}
func (c *ConversationCache) getConversationIDsKey(ownerUserID string) string {
func (c *ConversationRedis) getConversationIDsKey(ownerUserID string) string {
return conversationIDsKey + ownerUserID
}
func (c *ConversationCache) getRecvMsgOptKey(ownerUserID, conversationID string) string {
func (c *ConversationRedis) getRecvMsgOptKey(ownerUserID, conversationID string) string {
return recvMsgOptKey + ownerUserID + ":" + conversationID
}
func (c *ConversationCache) getSuperGroupRecvNotNotifyUserIDsKey(groupID string) string {
func (c *ConversationRedis) getSuperGroupRecvNotNotifyUserIDsKey(groupID string) string {
return superGroupRecvMsgNotNotifyUserIDsKey + groupID
}
func (c *ConversationCache) GetUserConversationIDs(ctx context.Context, ownerUserID string, f func(ctx context.Context, userID string) ([]string, error)) (conversationIDs []string, err error) {
func (c *ConversationRedis) GetUserConversationIDs(ctx context.Context, ownerUserID string, f func(userID string) ([]string, error)) (conversationIDs []string, err error) {
//getConversationIDs := func() (string, error) {
// conversationIDs, err := relation.GetConversationIDsByUserID(ownerUserID)
// if err != nil {
@@ -79,7 +100,7 @@ func (c *ConversationCache) GetUserConversationIDs(ctx context.Context, ownerUse
})
}
func (c *ConversationCache) GetUserConversationIDs1(ctx context.Context, ownerUserID string) (conversationIDs []string, err error) {
func (c *ConversationRedis) GetUserConversationIDs1(ctx context.Context, ownerUserID string) (conversationIDs []string, err error) {
//getConversationIDs := func() (string, error) {
// conversationIDs, err := relation.GetConversationIDsByUserID(ownerUserID)
// if err != nil {
@@ -149,14 +170,14 @@ func GetCache[T any](ctx context.Context, rcClient *rockscache.Client, key strin
return t, nil
}
func (c *ConversationCache) DelUserConversationIDs(ctx context.Context, ownerUserID string) (err error) {
func (c *ConversationRedis) DelUserConversationIDs(ctx context.Context, ownerUserID string) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID)
}()
return utils.Wrap(c.rcClient.TagAsDeleted(c.getConversationIDsKey(ownerUserID)), "DelUserConversationIDs err")
}
func (c *ConversationCache) GetConversation(ctx context.Context, ownerUserID, conversationID string) (conversation *relation2.ConversationModel, err error) {
func (c *ConversationRedis) GetConversation(ctx context.Context, ownerUserID, conversationID string) (conversation *relationTb.Conversation, err error) {
getConversation := func() (string, error) {
conversation, err := relation.GetConversation(ownerUserID, conversationID)
if err != nil {
@@ -175,19 +196,19 @@ func (c *ConversationCache) GetConversation(ctx context.Context, ownerUserID, co
if err != nil {
return nil, err
}
conversation = &relation2.ConversationModel{}
conversation = &relationTb.ConversationModel{}
err = json.Unmarshal([]byte(conversationStr), &conversation)
return conversation, utils.Wrap(err, "Unmarshal failed")
}
func (c *ConversationCache) DelConversation(ctx context.Context, ownerUserID, conversationID string) (err error) {
func (c *ConversationRedis) DelConversation(ctx context.Context, ownerUserID, conversationID string) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "conversationID", conversationID)
}()
return utils.Wrap(c.rcClient.TagAsDeleted(c.getConversationKey(ownerUserID, conversationID)), "DelConversation err")
}
func (c *ConversationCache) GetConversations(ctx context.Context, ownerUserID string, conversationIDs []string) (conversations []relation2.ConversationModel, err error) {
func (c *ConversationRedis) GetConversations(ctx context.Context, ownerUserID string, conversationIDs []string) (conversations []relationTb.ConversationModel, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "conversationIDs", conversationIDs, "conversations", conversations)
}()
@@ -201,7 +222,7 @@ func (c *ConversationCache) GetConversations(ctx context.Context, ownerUserID st
return conversations, nil
}
func (c *ConversationCache) GetUserAllConversations(ctx context.Context, ownerUserID string) (conversations []relation2.ConversationModel, err error) {
func (c *ConversationRedis) GetUserAllConversations(ctx context.Context, ownerUserID string) (conversations []relationTb.ConversationModel, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "conversations", conversations)
}()
@@ -209,7 +230,7 @@ func (c *ConversationCache) GetUserAllConversations(ctx context.Context, ownerUs
if err != nil {
return nil, err
}
var conversationIDs []relation2.ConversationModel
var conversationIDs []relationTb.ConversationModel
for _, conversationID := range IDs {
conversation, err := c.GetConversation(ctx, ownerUserID, conversationID)
if err != nil {
@@ -220,7 +241,7 @@ func (c *ConversationCache) GetUserAllConversations(ctx context.Context, ownerUs
return conversationIDs, nil
}
func (c *ConversationCache) GetUserRecvMsgOpt(ctx context.Context, ownerUserID, conversationID string) (opt int, err error) {
func (c *ConversationRedis) GetUserRecvMsgOpt(ctx context.Context, ownerUserID, conversationID string) (opt int, err error) {
getConversation := func() (string, error) {
conversation, err := relation.GetConversation(ownerUserID, conversationID)
if err != nil {
@@ -238,22 +259,22 @@ func (c *ConversationCache) GetUserRecvMsgOpt(ctx context.Context, ownerUserID,
return strconv.Atoi(optStr)
}
func (c *ConversationCache) DelUserRecvMsgOpt(ctx context.Context, ownerUserID, conversationID string) error {
func (c *ConversationRedis) DelUserRecvMsgOpt(ctx context.Context, ownerUserID, conversationID string) error {
return utils.Wrap(c.rcClient.TagAsDeleted(c.getConversationKey(ownerUserID, conversationID)), "DelUserRecvMsgOpt failed")
}
func (c *ConversationCache) GetSuperGroupRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) (userIDs []string, err error) {
func (c *ConversationRedis) GetSuperGroupRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) (userIDs []string, err error) {
return nil, nil
}
func (c *ConversationCache) DelSuperGroupRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) (err error) {
func (c *ConversationRedis) DelSuperGroupRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) (err error) {
return nil
}
func (c *ConversationCache) GetSuperGroupRecvMsgNotNotifyUserIDsHash(ctx context.Context, groupID string) (hash uint32, err error) {
func (c *ConversationRedis) GetSuperGroupRecvMsgNotNotifyUserIDsHash(ctx context.Context, groupID string) (hash uint32, err error) {
return
}
func (c *ConversationCache) DelSuperGroupRecvMsgNotNotifyUserIDsHash(ctx context.Context, groupID string) {
func (c *ConversationRedis) DelSuperGroupRecvMsgNotNotifyUserIDsHash(ctx context.Context, groupID string) {
return
}
+23 -14
View File
@@ -2,7 +2,7 @@ package cache
import (
"Open_IM/pkg/common/db/relation"
relation2 "Open_IM/pkg/common/db/table/relation"
relationTb "Open_IM/pkg/common/db/table/relation"
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils"
"context"
@@ -19,33 +19,42 @@ const (
friendKey = "FRIEND_INFO:"
)
type FriendCache struct {
type FriendCache interface {
GetFriendIDs(ctx context.Context, ownerUserID string, fn func(ctx context.Context, ownerUserID string) (friendIDs []string, err error)) (friendIDs []string, err error)
// call when friendID List changed
DelFriendIDs(ctx context.Context, ownerUserID string) (err error)
GetFriend(ctx context.Context, ownerUserID, friendUserID string, fn func(ctx context.Context, ownerUserID, friendUserID string) (friend *relationTb.FriendModel, err error)) (friend *relationTb.FriendModel, err error)
// del friend when friend info changed or remove it
DelFriend(ctx context.Context, ownerUserID, friendUserID string) (err error)
}
type FriendCacheRedis struct {
friendDB *relation.FriendGorm
expireTime time.Duration
rcClient *rockscache.Client
}
func NewFriendCache(rdb redis.UniversalClient, friendDB *relation.FriendGorm, options rockscache.Options) *FriendCache {
return &FriendCache{
func NewFriendCacheRedis(rdb redis.UniversalClient, friendDB *relation.FriendGorm, options rockscache.Options) *FriendCacheRedis {
return &FriendCacheRedis{
friendDB: friendDB,
expireTime: friendExpireTime,
rcClient: rockscache.NewClient(rdb, options),
}
}
func (f *FriendCache) getFriendIDsKey(ownerUserID string) string {
func (f *FriendCacheRedis) getFriendIDsKey(ownerUserID string) string {
return friendIDsKey + ownerUserID
}
func (f *FriendCache) getTwoWayFriendsIDsKey(ownerUserID string) string {
func (f *FriendCacheRedis) getTwoWayFriendsIDsKey(ownerUserID string) string {
return TwoWayFriendsIDsKey + ownerUserID
}
func (f *FriendCache) getFriendKey(ownerUserID, friendUserID string) string {
func (f *FriendCacheRedis) getFriendKey(ownerUserID, friendUserID string) string {
return friendKey + ownerUserID + "-" + friendUserID
}
func (f *FriendCache) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) {
func (f *FriendCacheRedis) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) {
getFriendIDs := func() (string, error) {
friendIDs, err := f.friendDB.GetFriendIDs(ctx, ownerUserID)
if err != nil {
@@ -68,14 +77,14 @@ func (f *FriendCache) GetFriendIDs(ctx context.Context, ownerUserID string) (fri
return friendIDs, utils.Wrap(err, "")
}
func (f *FriendCache) DelFriendIDs(ctx context.Context, ownerUserID string) (err error) {
func (f *FriendCacheRedis) DelFriendIDs(ctx context.Context, ownerUserID string) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID)
}()
return f.rcClient.TagAsDeleted(f.getFriendIDsKey(ownerUserID))
}
func (f *FriendCache) GetTwoWayFriendIDs(ctx context.Context, ownerUserID string) (twoWayFriendIDs []string, err error) {
func (f *FriendCacheRedis) GetTwoWayFriendIDs(ctx context.Context, ownerUserID string) (twoWayFriendIDs []string, err error) {
friendIDs, err := f.GetFriendIDs(ctx, ownerUserID)
if err != nil {
return nil, err
@@ -92,14 +101,14 @@ func (f *FriendCache) GetTwoWayFriendIDs(ctx context.Context, ownerUserID string
return twoWayFriendIDs, nil
}
func (f *FriendCache) DelTwoWayFriendIDs(ctx context.Context, ownerUserID string) (err error) {
func (f *FriendCacheRedis) DelTwoWayFriendIDs(ctx context.Context, ownerUserID string) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID)
}()
return f.rcClient.TagAsDeleted(f.getTwoWayFriendsIDsKey(ownerUserID))
}
func (f *FriendCache) GetFriend(ctx context.Context, ownerUserID, friendUserID string) (friend *relation2.FriendModel, err error) {
func (f *FriendCacheRedis) GetFriend(ctx context.Context, ownerUserID, friendUserID string) (friend *relationTb.FriendModel, err error) {
getFriend := func() (string, error) {
friend, err = f.friendDB.Take(ctx, ownerUserID, friendUserID)
if err != nil {
@@ -115,12 +124,12 @@ func (f *FriendCache) GetFriend(ctx context.Context, ownerUserID, friendUserID s
if err != nil {
return nil, err
}
friend = &relation2.FriendModel{}
friend = &relationTb.FriendModel{}
err = json.Unmarshal([]byte(friendStr), friend)
return friend, utils.Wrap(err, "")
}
func (f *FriendCache) DelFriend(ctx context.Context, ownerUserID, friendUserID string) (err error) {
func (f *FriendCacheRedis) DelFriend(ctx context.Context, ownerUserID, friendUserID string) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID)
}()
+14 -7
View File
@@ -3,7 +3,7 @@ package cache
import (
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db/relation"
relation2 "Open_IM/pkg/common/db/table/relation"
relationTb "Open_IM/pkg/common/db/table/relation"
"Open_IM/pkg/common/db/unrelation"
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils"
@@ -28,18 +28,25 @@ const (
groupMemberNumKey = "GROUP_MEMBER_NUM_CACHE:"
)
type GroupCache struct {
group relation2.GroupModelInterface
groupMember relation2.GroupMemberModelInterface
groupRequest relation2.GroupRequestModelInterface
type GroupCache interface {
GetGroupsInfo(ctx context.Context, groupIDs []string, fn func(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error)) (groups []*relationTb.GroupModel, err error)
DelGroupsInfo(ctx context.Context, groupID string) (err error)
GetGroupInfo(ctx context.Context, groupID string, fn func(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error)) (group *relationTb.GroupModel, err error)
DelGroupInfo(ctx context.Context, groupID string) (err error)
}
type GroupCacheRedis struct {
group *relation.GroupGorm
groupMember *relation.GroupMemberGorm
groupRequest *relation.GroupRequestGorm
mongoDB *unrelation.SuperGroupMongoDriver
expireTime time.Duration
redisClient *RedisClient
rcClient *rockscache.Client
}
func NewGroupCache(rdb redis.UniversalClient, groupDB relation2.GroupModelInterface, groupMemberDB relation2.GroupMemberModelInterface, groupRequestDB relation2.GroupRequestModelInterface, mongoClient *unrelation.SuperGroupMongoDriver, opts rockscache.Options) *GroupCache {
return &GroupCache{rcClient: rockscache.NewClient(rdb, opts), expireTime: groupExpireTime,
func NewGroupCacheRedis(rdb redis.UniversalClient, groupDB *relation.GroupGorm, groupMemberDB *relation.GroupMemberGorm, groupRequestDB *relation.GroupRequestGorm, mongoClient *unrelation.SuperGroupMongoDriver, opts rockscache.Options) *GroupCacheRedis {
return &GroupCacheRedis{rcClient: rockscache.NewClient(rdb, opts), expireTime: groupExpireTime,
group: groupDB, groupMember: groupMemberDB, groupRequest: groupRequestDB, redisClient: NewRedisClient(rdb),
mongoDB: mongoClient,
}
+5 -5
View File
@@ -2,7 +2,7 @@ package cache
import (
"Open_IM/pkg/common/db/relation"
relation2 "Open_IM/pkg/common/db/table/relation"
relationTb "Open_IM/pkg/common/db/table/relation"
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils"
"context"
@@ -44,7 +44,7 @@ func (u *UserCache) getUserGlobalRecvMsgOptKey(userID string) string {
return userGlobalRecvMsgOptKey + userID
}
func (u *UserCache) GetUserInfo(ctx context.Context, userID string) (userInfo *relation2.UserModel, err error) {
func (u *UserCache) GetUserInfo(ctx context.Context, userID string) (userInfo *relationTb.UserModel, err error) {
getUserInfo := func() (string, error) {
userInfo, err := u.userDB.Take(ctx, userID)
if err != nil {
@@ -63,13 +63,13 @@ func (u *UserCache) GetUserInfo(ctx context.Context, userID string) (userInfo *r
if err != nil {
return nil, err
}
userInfo = &relation2.UserModel{}
userInfo = &relationTb.UserModel{}
err = json.Unmarshal([]byte(userInfoStr), userInfo)
return userInfo, utils.Wrap(err, "")
}
func (u *UserCache) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relation2.UserModel, error) {
var users []*relation2.UserModel
func (u *UserCache) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relationTb.UserModel, error) {
var users []*relationTb.UserModel
for _, userID := range userIDs {
user, err := GetUserInfoFromCache(ctx, userID)
if err != nil {
+12 -11
View File
@@ -1,6 +1,7 @@
package controller
import (
relation2 "Open_IM/pkg/common/db/relation"
"Open_IM/pkg/common/db/table/relation"
"context"
"errors"
@@ -27,17 +28,17 @@ func NewBlackController(db *gorm.DB) *BlackController {
}
// Create 增加黑名单
func (b *BlackController) Create(ctx context.Context, blacks []*relation.Black) (err error) {
func (b *BlackController) Create(ctx context.Context, blacks []*relation.BlackModel) (err error) {
return b.database.Create(ctx, blacks)
}
// Delete 删除黑名单
func (b *BlackController) Delete(ctx context.Context, blacks []*relation.Black) (err error) {
func (b *BlackController) Delete(ctx context.Context, blacks []*relation.BlackModel) (err error) {
return b.database.Delete(ctx, blacks)
}
// FindOwnerBlacks 获取黑名单列表
func (b *BlackController) FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blackList []*relation.Black, total int64, err error) {
func (b *BlackController) FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blackList []*relation.BlackModel, total int64, err error) {
return b.database.FindOwnerBlacks(ctx, ownerUserID, pageNumber, showNumber)
}
@@ -48,21 +49,21 @@ func (b *BlackController) CheckIn(ctx context.Context, userID1, userID2 string)
type BlackDatabaseInterface interface {
// Create 增加黑名单
Create(ctx context.Context, blacks []*relation.Black) (err error)
Create(ctx context.Context, blacks []*relation.BlackModel) (err error)
// Delete 删除黑名单
Delete(ctx context.Context, blacks []*relation.Black) (err error)
Delete(ctx context.Context, blacks []*relation.BlackModel) (err error)
// FindOwnerBlacks 获取黑名单列表
FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blacks []*relation.Black, total int64, err error)
FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blacks []*relation.BlackModel, total int64, err error)
// CheckIn 检查user2是否在user1的黑名单列表中(inUser1Blacks==true) 检查user1是否在user2的黑名单列表中(inUser2Blacks==true)
CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error)
}
type BlackDatabase struct {
sqlDB *relation.Black
sqlDB *relation2.BlackGorm
}
func NewBlackDatabase(db *gorm.DB) *BlackDatabase {
sqlDB := relation.NewBlack(db)
sqlDB := relation2.NewBlackGorm(db)
database := &BlackDatabase{
sqlDB: sqlDB,
}
@@ -70,17 +71,17 @@ func NewBlackDatabase(db *gorm.DB) *BlackDatabase {
}
// Create 增加黑名单
func (b *BlackDatabase) Create(ctx context.Context, blacks []*relation.Black) (err error) {
func (b *BlackDatabase) Create(ctx context.Context, blacks []*relation.BlackModel) (err error) {
return b.sqlDB.Create(ctx, blacks)
}
// Delete 删除黑名单
func (b *BlackDatabase) Delete(ctx context.Context, blacks []*relation.Black) (err error) {
func (b *BlackDatabase) Delete(ctx context.Context, blacks []*relation.BlackModel) (err error) {
return b.sqlDB.Delete(ctx, blacks)
}
// FindOwnerBlacks 获取黑名单列表
func (b *BlackDatabase) FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blacks []*relation.Black, total int64, err error) {
func (b *BlackDatabase) FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blacks []*relation.BlackModel, total int64, err error) {
return b.sqlDB.FindOwnerBlacks(ctx, ownerUserID, pageNumber, showNumber)
}
+21 -21
View File
@@ -3,7 +3,7 @@ package controller
import (
"Open_IM/pkg/common/db/cache"
"Open_IM/pkg/common/db/relation"
"Open_IM/pkg/common/db/table"
relationTb "Open_IM/pkg/common/db/table/relation"
"context"
)
@@ -13,15 +13,15 @@ type ConversationInterface interface {
//UpdateUserConversationFiled 更新用户该会话的属性信息
UpdateUsersConversationFiled(ctx context.Context, UserIDList []string, conversationID string, args map[string]interface{}) error
//CreateConversation 创建一批新的会话
CreateConversation(ctx context.Context, conversations []*table.ConversationModel) error
CreateConversation(ctx context.Context, conversations []*relationTb.ConversationModel) error
//SyncPeerUserPrivateConversation 同步对端私聊会话内部保证事务操作
SyncPeerUserPrivateConversationTx(ctx context.Context, conversation *table.ConversationModel) error
SyncPeerUserPrivateConversationTx(ctx context.Context, conversation *relationTb.ConversationModel) error
//FindConversations 根据会话ID获取某个用户的多个会话
FindConversations(ctx context.Context, ownerUserID string, conversationID []string) ([]*table.ConversationModel, error)
FindConversations(ctx context.Context, ownerUserID string, conversationID []string) ([]*relationTb.ConversationModel, error)
//GetUserAllConversation 获取一个用户在服务器上所有的会话
GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*table.ConversationModel, error)
GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*relationTb.ConversationModel, error)
//SetUserConversations 设置用户多个会话属性,如果会话不存在则创建,否则更新,内部保证原子性
SetUserConversations(ctx context.Context, ownerUserID string, conversations []*table.ConversationModel) error
SetUserConversations(ctx context.Context, ownerUserID string, conversations []*relationTb.ConversationModel) error
}
type ConversationController struct {
database ConversationDataBaseInterface
@@ -39,22 +39,22 @@ func (c ConversationController) UpdateUsersConversationFiled(ctx context.Context
panic("implement me")
}
func (c ConversationController) CreateConversation(ctx context.Context, conversations []*table.ConversationModel) error {
func (c ConversationController) CreateConversation(ctx context.Context, conversations []*relationTb.ConversationModel) error {
panic("implement me")
}
func (c ConversationController) SyncPeerUserPrivateConversationTx(ctx context.Context, conversation *table.ConversationModel) error {
func (c ConversationController) SyncPeerUserPrivateConversationTx(ctx context.Context, conversation *relationTb.ConversationModel) error {
panic("implement me")
}
func (c ConversationController) FindConversations(ctx context.Context, ownerUserID string, conversationID []string) ([]*table.ConversationModel, error) {
func (c ConversationController) FindConversations(ctx context.Context, ownerUserID string, conversationID []string) ([]*relationTb.ConversationModel, error) {
panic("implement me")
}
func (c ConversationController) GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*table.ConversationModel, error) {
func (c ConversationController) GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*relationTb.ConversationModel, error) {
panic("implement me")
}
func (c ConversationController) SetUserConversations(ctx context.Context, ownerUserID string, conversations []*table.ConversationModel) error {
func (c ConversationController) SetUserConversations(ctx context.Context, ownerUserID string, conversations []*relationTb.ConversationModel) error {
panic("implement me")
}
@@ -66,15 +66,15 @@ type ConversationDataBaseInterface interface {
//UpdateUserConversationFiled 更新用户该会话的属性信息
UpdateUsersConversationFiled(ctx context.Context, UserIDList []string, conversationID string, args map[string]interface{}) error
//CreateConversation 创建一批新的会话
CreateConversation(ctx context.Context, conversations []*table.ConversationModel) error
CreateConversation(ctx context.Context, conversations []*relationTb.ConversationModel) error
//SyncPeerUserPrivateConversation 同步对端私聊会话内部保证事务操作
SyncPeerUserPrivateConversationTx(ctx context.Context, conversation *table.ConversationModel) error
SyncPeerUserPrivateConversationTx(ctx context.Context, conversation *relationTb.ConversationModel) error
//FindConversations 根据会话ID获取某个用户的多个会话
FindConversations(ctx context.Context, ownerUserID string, conversationID []string) ([]*table.ConversationModel, error)
FindConversations(ctx context.Context, ownerUserID string, conversationID []string) ([]*relationTb.ConversationModel, error)
//GetUserAllConversation 获取一个用户在服务器上所有的会话
GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*table.ConversationModel, error)
GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*relationTb.ConversationModel, error)
//SetUserConversations 设置用户多个会话属性,如果会话不存在则创建,否则更新,内部保证原子性
SetUserConversations(ctx context.Context, ownerUserID string, conversations []*table.ConversationModel) error
SetUserConversations(ctx context.Context, ownerUserID string, conversations []*relationTb.ConversationModel) error
}
type ConversationDataBase struct {
db relation.Conversation
@@ -89,23 +89,23 @@ func (c ConversationDataBase) UpdateUsersConversationFiled(ctx context.Context,
panic("implement me")
}
func (c ConversationDataBase) CreateConversation(ctx context.Context, conversations []*table.ConversationModel) error {
func (c ConversationDataBase) CreateConversation(ctx context.Context, conversations []*relationTb.ConversationModel) error {
panic("implement me")
}
func (c ConversationDataBase) SyncPeerUserPrivateConversationTx(ctx context.Context, conversation *table.ConversationModel) error {
func (c ConversationDataBase) SyncPeerUserPrivateConversationTx(ctx context.Context, conversation *relationTb.ConversationModel) error {
panic("implement me")
}
func (c ConversationDataBase) FindConversations(ctx context.Context, ownerUserID string, conversationID []string) ([]*table.ConversationModel, error) {
func (c ConversationDataBase) FindConversations(ctx context.Context, ownerUserID string, conversationID []string) ([]*relationTb.ConversationModel, error) {
panic("implement me")
}
func (c ConversationDataBase) GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*table.ConversationModel, error) {
func (c ConversationDataBase) GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*relationTb.ConversationModel, error) {
panic("implement me")
}
func (c ConversationDataBase) SetUserConversations(ctx context.Context, ownerUserID string, conversations []*table.ConversationModel) error {
func (c ConversationDataBase) SetUserConversations(ctx context.Context, ownerUserID string, conversations []*relationTb.ConversationModel) error {
panic("implement me")
}
+25 -25
View File
@@ -26,15 +26,15 @@ type FriendInterface interface {
// 更新好友备注 零值也支持
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
// 获取ownerUserID的好友列表 无结果不返回错误
FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
PageOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
// friendUserID在哪些人的好友列表中
FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
PageInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
// 获取我发出去的好友申请 无结果不返回错误
FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
PageFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
// 获取我收到的的好友申请 无结果不返回错误
FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
PageFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
// 获取某人指定好友的信息 如果有一个不存在也返回错误
FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error)
FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error)
}
type FriendController struct {
@@ -81,28 +81,28 @@ func (f *FriendController) UpdateRemark(ctx context.Context, ownerUserID, friend
}
// FindOwnerFriends 获取ownerUserID的好友列表
func (f *FriendController) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
return f.database.FindOwnerFriends(ctx, ownerUserID, pageNumber, showNumber)
func (f *FriendController) PageOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
return f.database.PageOwnerFriends(ctx, ownerUserID, pageNumber, showNumber)
}
// FindInWhoseFriends friendUserID在哪些人的好友列表中
func (f *FriendController) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
return f.database.FindInWhoseFriends(ctx, friendUserID, pageNumber, showNumber)
func (f *FriendController) PageInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
return f.database.PageInWhoseFriends(ctx, friendUserID, pageNumber, showNumber)
}
// FindFriendRequestFromMe 获取我发出去的好友申请
func (f *FriendController) FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
return f.database.FindFriendRequestFromMe(ctx, userID, pageNumber, showNumber)
func (f *FriendController) PageFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
return f.database.PageFriendRequestFromMe(ctx, userID, pageNumber, showNumber)
}
// FindFriendRequestToMe 获取我收到的的好友申请
func (f *FriendController) FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
return f.database.FindFriendRequestToMe(ctx, userID, pageNumber, showNumber)
func (f *FriendController) PageFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
return f.database.PageFriendRequestToMe(ctx, userID, pageNumber, showNumber)
}
// FindFriends 获取某人指定好友的信息
func (f *FriendController) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error) {
return f.database.FindFriends(ctx, ownerUserID, friendUserIDs)
func (f *FriendController) FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error) {
return f.database.FindFriendsWithError(ctx, ownerUserID, friendUserIDs)
}
type FriendDatabaseInterface interface {
@@ -121,15 +121,15 @@ type FriendDatabaseInterface interface {
// 更新好友备注
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
// 获取ownerUserID的好友列表
FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
PageOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
// friendUserID在哪些人的好友列表中
FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
PageInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
// 获取我发出去的好友申请
FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
PageFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
// 获取我收到的的好友申请
FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
PageFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
// 获取某人指定好友的信息
FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error)
FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error)
}
type FriendDatabase struct {
@@ -302,27 +302,27 @@ func (f *FriendDatabase) UpdateRemark(ctx context.Context, ownerUserID, friendUs
}
// 获取ownerUserID的好友列表 无结果不返回错误
func (f *FriendDatabase) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
func (f *FriendDatabase) PageOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
return f.friend.FindOwnerFriends(ctx, ownerUserID, pageNumber, showNumber)
}
// friendUserID在哪些人的好友列表中
func (f *FriendDatabase) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
func (f *FriendDatabase) PageInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
return f.friend.FindInWhoseFriends(ctx, friendUserID, pageNumber, showNumber)
}
// 获取我发出去的好友申请 无结果不返回错误
func (f *FriendDatabase) FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
func (f *FriendDatabase) PageFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
return f.friendRequest.FindFromUserID(ctx, userID, pageNumber, showNumber)
}
// 获取我收到的的好友申请 无结果不返回错误
func (f *FriendDatabase) FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
func (f *FriendDatabase) PageFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
return f.friendRequest.FindToUserID(ctx, userID, pageNumber, showNumber)
}
// 获取某人指定好友的信息 如果有好友不存在,也返回错误
func (f *FriendDatabase) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error) {
func (f *FriendDatabase) FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error) {
friends, err = f.friend.FindFriends(ctx, ownerUserID, friendUserIDs)
if err != nil {
return
+31 -10
View File
@@ -10,6 +10,8 @@ import (
type UserInterface interface {
//获取指定用户的信息 如有userID未找到 也返回错误
FindWithError(ctx context.Context, userIDs []string) (users []*relation2.UserModel, err error)
//获取指定用户的信息 如有userID未找到 不返回错误
Find(ctx context.Context, userIDs []string) (users []*relation2.UserModel, err error)
//插入多条 外部保证userID 不重复 且在db中不存在
Create(ctx context.Context, users []*relation2.UserModel) (err error)
@@ -17,9 +19,9 @@ type UserInterface interface {
Update(ctx context.Context, users []*relation2.UserModel) (err error)
//更新(零值) 外部保证userID存在
UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error)
//获取,如果没找到,不返回错误
Get(ctx context.Context, pageNumber, showNumber int32) (users []*relation2.UserModel, count int64, err error)
//userIDs是否存在 只要有一个存在就为true
//如果没找到,不返回错误
Page(ctx context.Context, pageNumber, showNumber int32) (users []*relation2.UserModel, count int64, err error)
//只要有一个存在就为true
IsExist(ctx context.Context, userIDs []string) (exist bool, err error)
}
@@ -27,6 +29,11 @@ type UserController struct {
database UserDatabaseInterface
}
// 获取指定用户的信息 如有userID未找到 也返回错误
func (u *UserController) FindWithError(ctx context.Context, userIDs []string) (users []*relation2.UserModel, err error) {
return u.database.FindWithError(ctx, userIDs)
}
func (u *UserController) Find(ctx context.Context, userIDs []string) (users []*relation2.UserModel, err error) {
return u.database.Find(ctx, userIDs)
}
@@ -41,8 +48,8 @@ func (u *UserController) UpdateByMap(ctx context.Context, userID string, args ma
return u.database.UpdateByMap(ctx, userID, args)
}
func (u *UserController) Get(ctx context.Context, pageNumber, showNumber int32) (users []*relation2.UserModel, count int64, err error) {
return u.database.Get(ctx, pageNumber, showNumber)
func (u *UserController) Page(ctx context.Context, pageNumber, showNumber int32) (users []*relation2.UserModel, count int64, err error) {
return u.database.Page(ctx, pageNumber, showNumber)
}
func (u *UserController) IsExist(ctx context.Context, userIDs []string) (exist bool, err error) {
@@ -54,11 +61,19 @@ func NewUserController(db *gorm.DB) *UserController {
}
type UserDatabaseInterface interface {
//获取指定用户的信息 如有userID未找到 也返回错误
FindWithError(ctx context.Context, userIDs []string) (users []*relation2.UserModel, err error)
//获取指定用户的信息 如有userID未找到 不返回错误
Find(ctx context.Context, userIDs []string) (users []*relation2.UserModel, err error)
Create(ctx context.Context, users []*relation2.UserModel) error
//插入多条 外部保证userID 不重复 且在db中不存在
Create(ctx context.Context, users []*relation2.UserModel) (err error)
//更新(非零值) 外部保证userID存在
Update(ctx context.Context, users []*relation2.UserModel) (err error)
//更新(零值) 外部保证userID存在
UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error)
Get(ctx context.Context, pageNumber, showNumber int32) (users []*relation2.UserModel, count int64, err error)
//如果没找到,不返回错误
Page(ctx context.Context, pageNumber, showNumber int32) (users []*relation2.UserModel, count int64, err error)
//只要有一个存在就为true
IsExist(ctx context.Context, userIDs []string) (exist bool, err error)
}
@@ -75,7 +90,7 @@ func newUserDatabase(db *gorm.DB) *UserDatabase {
}
// 获取指定用户的信息 如有userID未找到 也返回错误
func (u *UserDatabase) Find(ctx context.Context, userIDs []string) (users []*relation2.UserModel, err error) {
func (u *UserDatabase) FindWithError(ctx context.Context, userIDs []string) (users []*relation2.UserModel, err error) {
users, err = u.user.Find(ctx, userIDs)
if err != nil {
return
@@ -86,6 +101,12 @@ func (u *UserDatabase) Find(ctx context.Context, userIDs []string) (users []*rel
return
}
// 获取指定用户的信息 如有userID未找到 不返回错误
func (u *UserDatabase) Find(ctx context.Context, userIDs []string) (users []*relation2.UserModel, err error) {
users, err = u.user.Find(ctx, userIDs)
return
}
// 插入多条 外部保证userID 不重复 且在db中不存在
func (u *UserDatabase) Create(ctx context.Context, users []*relation2.UserModel) (err error) {
return u.user.Create(ctx, users)
@@ -102,8 +123,8 @@ func (u *UserDatabase) UpdateByMap(ctx context.Context, userID string, args map[
}
// 获取,如果没找到,不返回错误
func (u *UserDatabase) Get(ctx context.Context, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error) {
return u.user.Get(ctx, showNumber, pageNumber)
func (u *UserDatabase) Page(ctx context.Context, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error) {
return u.user.Page(ctx, showNumber, pageNumber)
}
// userIDs是否存在 只要有一个存在就为true
+5 -4
View File
@@ -1,8 +1,8 @@
package localcache
import (
discoveryRegistry "Open_IM/pkg/discovery_registry"
"context"
"github.com/OpenIMSDK/openKeeper"
"sync"
)
@@ -13,16 +13,17 @@ type ConversationLocalCacheInterface interface {
type ConversationLocalCache struct {
lock sync.Mutex
SuperGroupRecvMsgNotNotifyUserIDs map[string][]string
zkClient *openKeeper.ZkClient
client discoveryRegistry.SvcDiscoveryRegistry
}
func NewConversationLocalCache(zkClient *openKeeper.ZkClient) ConversationLocalCache {
func NewConversationLocalCache(client discoveryRegistry.SvcDiscoveryRegistry) ConversationLocalCache {
return ConversationLocalCache{
SuperGroupRecvMsgNotNotifyUserIDs: make(map[string][]string, 0),
zkClient: zkClient,
client: client,
}
}
func (g *ConversationLocalCache) GetRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) []string {
g.client.GetConn()
return []string{}
}
+8 -9
View File
@@ -3,10 +3,9 @@ package localcache
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
discoveryRegistry "Open_IM/pkg/discovery_registry"
"Open_IM/pkg/proto/group"
"context"
"github.com/OpenIMSDK/openKeeper"
"google.golang.org/grpc"
"sync"
)
@@ -15,9 +14,9 @@ type GroupLocalCacheInterface interface {
}
type GroupLocalCache struct {
lock sync.Mutex
cache map[string]GroupMemberIDsHash
zkClient *openKeeper.ZkClient
lock sync.Mutex
cache map[string]GroupMemberIDsHash
client discoveryRegistry.SvcDiscoveryRegistry
}
type GroupMemberIDsHash struct {
@@ -25,17 +24,17 @@ type GroupMemberIDsHash struct {
userIDs []string
}
func NewGroupMemberIDsLocalCache(zkClient *openKeeper.ZkClient) GroupLocalCache {
func NewGroupMemberIDsLocalCache(client discoveryRegistry.SvcDiscoveryRegistry) GroupLocalCache {
return GroupLocalCache{
cache: make(map[string]GroupMemberIDsHash, 0),
zkClient: zkClient,
cache: make(map[string]GroupMemberIDsHash, 0),
client: client,
}
}
func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) {
g.lock.Lock()
defer g.lock.Unlock()
conn, err := g.zkClient.GetConn(config.Config.RpcRegisterName.OpenImGroupName, nil)
conn, err := g.client.GetConn(config.Config.RpcRegisterName.OpenImGroupName, nil)
if err != nil {
return nil, err
}
+11 -11
View File
@@ -31,7 +31,7 @@ type FriendUser struct {
}
// 插入多条记录
func (f *FriendGorm) Create(ctx context.Context, friends []*relation.FriendModel, tx ...*gorm.DB) (err error) {
func (f *FriendGorm) Create(ctx context.Context, friends []*relation.FriendModel, tx ...any) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friends", friends)
}()
@@ -39,7 +39,7 @@ func (f *FriendGorm) Create(ctx context.Context, friends []*relation.FriendModel
}
// 删除ownerUserID指定的好友
func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string, tx ...*gorm.DB) (err error) {
func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string, tx ...any) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserIDs", friendUserIDs)
}()
@@ -48,7 +48,7 @@ func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserI
}
// 更新ownerUserID单个好友信息 更新零值
func (f *FriendGorm) UpdateByMap(ctx context.Context, ownerUserID string, friendUserID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
func (f *FriendGorm) UpdateByMap(ctx context.Context, ownerUserID string, friendUserID string, args map[string]interface{}, tx ...any) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "args", args)
}()
@@ -56,7 +56,7 @@ func (f *FriendGorm) UpdateByMap(ctx context.Context, ownerUserID string, friend
}
// 更新好友信息的非零值
func (f *FriendGorm) Update(ctx context.Context, friends []*relation.FriendModel, tx ...*gorm.DB) (err error) {
func (f *FriendGorm) Update(ctx context.Context, friends []*relation.FriendModel, tx ...any) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friends", friends)
}()
@@ -64,7 +64,7 @@ func (f *FriendGorm) Update(ctx context.Context, friends []*relation.FriendModel
}
// 更新好友备注(也支持零值
func (f *FriendGorm) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string, tx ...*gorm.DB) (err error) {
func (f *FriendGorm) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string, tx ...any) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "remark", remark)
}()
@@ -78,14 +78,14 @@ func (f *FriendGorm) UpdateRemark(ctx context.Context, ownerUserID, friendUserID
}
// 获取单个好友信息,如没找到 返回错误
func (f *FriendGorm) Take(ctx context.Context, ownerUserID, friendUserID string, tx ...*gorm.DB) (friend *relation.FriendModel, err error) {
func (f *FriendGorm) Take(ctx context.Context, ownerUserID, friendUserID string, tx ...any) (friend *relation.FriendModel, err error) {
friend = &relation.FriendModel{}
defer tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "friend", *friend)
return friend, utils.Wrap(getDBConn(f.DB, tx).Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error, "")
}
// 查找好友关系,如果是双向关系,则都返回
func (f *FriendGorm) FindUserState(ctx context.Context, userID1, userID2 string, tx ...*gorm.DB) (friends []*relation.FriendModel, err error) {
func (f *FriendGorm) FindUserState(ctx context.Context, userID1, userID2 string, tx ...any) (friends []*relation.FriendModel, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID1", userID1, "userID2", userID2, "friends", friends)
}()
@@ -93,7 +93,7 @@ func (f *FriendGorm) FindUserState(ctx context.Context, userID1, userID2 string,
}
// 获取 owner指定的好友列表 如果有friendUserIDs不存在,也不返回错误
func (f *FriendGorm) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, tx ...*gorm.DB) (friends []*relation.FriendModel, err error) {
func (f *FriendGorm) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, tx ...any) (friends []*relation.FriendModel, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserIDs", friendUserIDs, "friends", friends)
}()
@@ -101,7 +101,7 @@ func (f *FriendGorm) FindFriends(ctx context.Context, ownerUserID string, friend
}
// 获取哪些人添加了friendUserID 如果有ownerUserIDs不存在,也不返回错误
func (f *FriendGorm) FindReversalFriends(ctx context.Context, friendUserID string, ownerUserIDs []string, tx ...*gorm.DB) (friends []*relation.FriendModel, err error) {
func (f *FriendGorm) FindReversalFriends(ctx context.Context, friendUserID string, ownerUserIDs []string, tx ...any) (friends []*relation.FriendModel, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friendUserID", friendUserID, "ownerUserIDs", ownerUserIDs, "friends", friends)
}()
@@ -109,7 +109,7 @@ func (f *FriendGorm) FindReversalFriends(ctx context.Context, friendUserID strin
}
// 获取ownerUserID好友列表 支持翻页
func (f *FriendGorm) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32, tx ...*gorm.DB) (friends []*relation.FriendModel, total int64, err error) {
func (f *FriendGorm) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32, tx ...any) (friends []*relation.FriendModel, total int64, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "pageNumber", pageNumber, "showNumber", showNumber, "friends", friends, "total", total)
}()
@@ -122,7 +122,7 @@ func (f *FriendGorm) FindOwnerFriends(ctx context.Context, ownerUserID string, p
}
// 获取哪些人添加了friendUserID 支持翻页
func (f *FriendGorm) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32, tx ...*gorm.DB) (friends []*relation.FriendModel, total int64, err error) {
func (f *FriendGorm) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32, tx ...any) (friends []*relation.FriendModel, total int64, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "friendUserID", friendUserID, "pageNumber", pageNumber, "showNumber", showNumber, "friends", friends, "total", total)
}()
+5 -2
View File
@@ -63,6 +63,9 @@ func (m *Mysql) InitConn() *Mysql {
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
}
@@ -89,8 +92,8 @@ func (w Writer) Printf(format string, args ...interface{}) {
func getDBConn(db *gorm.DB, tx []any) *gorm.DB {
if len(tx) > 0 {
if txDb, ok := tx[0].(*gorm.DB); ok {
return txDb
if txDB, ok := tx[0].(*gorm.DB); ok {
return txDB
}
}
return db
+8 -8
View File
@@ -20,7 +20,7 @@ func NewUserGorm(db *gorm.DB) *UserGorm {
}
// 插入多条
func (u *UserGorm) Create(ctx context.Context, users []*relation.UserModel, tx ...*gorm.DB) (err error) {
func (u *UserGorm) Create(ctx context.Context, users []*relation.UserModel, tx ...any) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "users", users)
}()
@@ -28,7 +28,7 @@ func (u *UserGorm) Create(ctx context.Context, users []*relation.UserModel, tx .
}
// 更新用户信息 零值
func (u *UserGorm) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
func (u *UserGorm) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}, tx ...any) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "args", args)
}()
@@ -36,7 +36,7 @@ func (u *UserGorm) UpdateByMap(ctx context.Context, userID string, args map[stri
}
// 更新多个用户信息 非零值
func (u *UserGorm) Update(ctx context.Context, users []*relation.UserModel, tx ...*gorm.DB) (err error) {
func (u *UserGorm) Update(ctx context.Context, users []*relation.UserModel, tx ...any) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "users", users)
}()
@@ -44,7 +44,7 @@ func (u *UserGorm) Update(ctx context.Context, users []*relation.UserModel, tx .
}
// 获取指定用户信息 不存在,也不返回错误
func (u *UserGorm) Find(ctx context.Context, userIDs []string, tx ...*gorm.DB) (users []*relation.UserModel, err error) {
func (u *UserGorm) Find(ctx context.Context, userIDs []string, tx ...any) (users []*relation.UserModel, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userIDs", userIDs, "users", users)
}()
@@ -53,7 +53,7 @@ func (u *UserGorm) Find(ctx context.Context, userIDs []string, tx ...*gorm.DB) (
}
// 获取某个用户信息 不存在,则返回错误
func (u *UserGorm) Take(ctx context.Context, userID string, tx ...*gorm.DB) (user *relation.UserModel, err error) {
func (u *UserGorm) Take(ctx context.Context, userID string, tx ...any) (user *relation.UserModel, err error) {
user = &relation.UserModel{}
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "user", *user)
@@ -63,7 +63,7 @@ func (u *UserGorm) Take(ctx context.Context, userID string, tx ...*gorm.DB) (use
}
// 通过名字查找用户 不存在,不返回错误
func (u *UserGorm) GetByName(ctx context.Context, userName string, pageNumber, showNumber int32, tx ...*gorm.DB) (users []*relation.UserModel, count int64, err error) {
func (u *UserGorm) GetByName(ctx context.Context, userName string, pageNumber, showNumber int32, tx ...any) (users []*relation.UserModel, count int64, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userName", userName, "pageNumber", pageNumber, "showNumber", showNumber, "users", users, "count", count)
}()
@@ -76,7 +76,7 @@ func (u *UserGorm) GetByName(ctx context.Context, userName string, pageNumber, s
}
// 通过名字或userID查找用户 不存在,不返回错误
func (u *UserGorm) GetByNameAndID(ctx context.Context, content string, pageNumber, showNumber int32, tx ...*gorm.DB) (users []*relation.UserModel, count int64, err error) {
func (u *UserGorm) GetByNameAndID(ctx context.Context, content string, pageNumber, showNumber int32, tx ...any) (users []*relation.UserModel, count int64, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "content", content, "pageNumber", pageNumber, "showNumber", showNumber, "users", users, "count", count)
}()
@@ -89,7 +89,7 @@ func (u *UserGorm) GetByNameAndID(ctx context.Context, content string, pageNumbe
}
// 获取用户信息 不存在,不返回错误
func (u *UserGorm) Get(ctx context.Context, pageNumber, showNumber int32, tx ...*gorm.DB) (users []*relation.UserModel, count int64, err error) {
func (u *UserGorm) Page(ctx context.Context, pageNumber, showNumber int32, tx ...any) (users []*relation.UserModel, count int64, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "pageNumber", pageNumber, "showNumber", showNumber, "users", users, "count", count)
}()
@@ -136,3 +136,17 @@ func (m *Mongo) createMongoIndex(collection string, isUnique bool, keys ...strin
}
return nil
}
func MongoTransaction(ctx context.Context, mgo *mongo.Client, fn func(ctx mongo.SessionContext) error) error {
sess, err := mgo.StartSession()
if err != nil {
return err
}
sCtx := mongo.NewSessionContext(ctx, sess)
defer sess.EndSession(sCtx)
if err := fn(sCtx); err != nil {
_ = sess.AbortTransaction(sCtx)
return err
}
return utils.Wrap(sess.CommitTransaction(sCtx), "")
}
-308
View File
@@ -57,40 +57,6 @@ type GroupMember_x struct {
var ErrMsgListNotExist = errors.New("user not have msg in mongoDB")
func (d *db.DataBases) GetMinSeqFromMongo(uid string) (MinSeq uint32, err error) {
return 1, nil
//var i, NB uint32
//var seqUid string
//session := d.mgoSession.Clone()
//if session == nil {
// return MinSeq, errors.New("session == nil")
//}
//defer session.Close()
//c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
//MaxSeq, err := d.GetUserMaxSeq(uid)
//if err != nil && err != redis.ErrNil {
// return MinSeq, err
//}
//NB = uint32(MaxSeq / singleGocMsgNum)
//for i = 0; i <= NB; i++ {
// seqUid = indexGen(uid, i)
// n, err := c.Find(bson.M{"uid": seqUid}).Count()
// if err == nil && n != 0 {
// if i == 0 {
// MinSeq = 1
// } else {
// MinSeq = uint32(i * singleGocMsgNum)
// }
// break
// }
//}
//return MinSeq, nil
}
func (d *db.DataBases) GetMinSeqFromMongo2(uid string) (MinSeq uint32, err error) {
return 1, nil
}
// deleteMsgByLogic
func (d *db.DataBases) DelMsgBySeqList(userID string, seqList []uint32, operationID string) (totalUnexistSeqList []uint32, err error) {
log.Debug(operationID, utils.GetSelfFuncName(), "args ", userID, seqList)
@@ -657,24 +623,6 @@ func (d *db.DataBases) SaveUserChat(uid string, sendTime int64, m *pbMsg.MsgData
return nil
}
func (d *db.DataBases) DelUserChat(uid string) error {
return nil
//session := d.mgoSession.Clone()
//if session == nil {
// return errors.New("session == nil")
//}
//defer session.Close()
//
//c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
//
//delTime := time.Now().Unix() - int64(config.Config.Mongo.DBRetainChatRecords)*24*3600
//if err := c.Update(bson.M{"uid": uid}, bson.M{"$pull": bson.M{"msg": bson.M{"sendtime": bson.M{"$lte": delTime}}}}); err != nil {
// return err
//}
//
//return nil
}
func (d *db.DataBases) DelUserChatMongo2(uid string) error {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
@@ -687,19 +635,6 @@ func (d *db.DataBases) DelUserChatMongo2(uid string) error {
return nil
}
func (d *db.DataBases) MgoUserCount() (int, error) {
return 0, nil
//session := d.mgoSession.Clone()
//if session == nil {
// return 0, errors.New("session == nil")
//}
//defer session.Close()
//
//c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
//
//return c.Find(nil).Count()
}
func (d *db.DataBases) MgoSkipUID(count int) (string, error) {
return "", nil
//session := d.mgoSession.Clone()
@@ -715,249 +650,6 @@ func (d *db.DataBases) MgoSkipUID(count int) (string, error) {
//return sChat.UID, nil
}
func (d *db.DataBases) GetGroupMember(groupID string) []string {
return nil
//groupInfo := GroupMember_x{}
//groupInfo.GroupID = groupID
//groupInfo.UIDList = make([]string, 0)
//
//session := d.mgoSession.Clone()
//if session == nil {
// return groupInfo.UIDList
//}
//defer session.Close()
//
//c := session.DB(config.Config.Mongo.DBDatabase).C(cGroup)
//
//if err := c.Find(bson.M{"groupid": groupInfo.GroupID}).One(&groupInfo); err != nil {
// return groupInfo.UIDList
//}
//
//return groupInfo.UIDList
}
func (d *db.DataBases) AddGroupMember(groupID, uid string) error {
return nil
//session := d.mgoSession.Clone()
//if session == nil {
// return errors.New("session == nil")
//}
//defer session.Close()
//
//c := session.DB(config.Config.Mongo.DBDatabase).C(cGroup)
//
//n, err := c.Find(bson.M{"groupid": groupID}).Count()
//if err != nil {
// return err
//}
//
//if n == 0 {
// groupInfo := GroupMember_x{}
// groupInfo.GroupID = groupID
// groupInfo.UIDList = append(groupInfo.UIDList, uid)
// err = c.Insert(&groupInfo)
// if err != nil {
// return err
// }
//} else {
// err = c.Update(bson.M{"groupid": groupID}, bson.M{"$addToSet": bson.M{"uidlist": uid}})
// if err != nil {
// return err
// }
//}
//
//return nil
}
func (d *db.DataBases) DelGroupMember(groupID, uid string) error {
return nil
//session := d.mgoSession.Clone()
//if session == nil {
// return errors.New("session == nil")
//}
//defer session.Close()
//
//c := session.DB(config.Config.Mongo.DBDatabase).C(cGroup)
//
//if err := c.Update(bson.M{"groupid": groupID}, bson.M{"$pull": bson.M{"uidlist": uid}}); err != nil {
// return err
//}
//
//return nil
}
//type SuperGroup struct {
// GroupID string `bson:"group_id" json:"groupID"`
// MemberIDList []string `bson:"member_id_list" json:"memberIDList"`
//}
//
//type UserToSuperGroup struct {
// UserID string `bson:"user_id" json:"userID"`
// GroupIDList []string `bson:"group_id_list" json:"groupIDList"`
//}
func (d *db.DataBases) CreateSuperGroup(groupID string, initMemberIDList []string, memberNumCount int) error {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup)
session, err := d.mongoClient.StartSession()
if err != nil {
return utils.Wrap(err, "start session failed")
}
defer session.EndSession(ctx)
sCtx := mongo.NewSessionContext(ctx, session)
superGroup := SuperGroup{
GroupID: groupID,
MemberIDList: initMemberIDList,
}
_, err = c.InsertOne(sCtx, superGroup)
if err != nil {
_ = session.AbortTransaction(ctx)
return utils.Wrap(err, "transaction failed")
}
var users []UserToSuperGroup
for _, v := range initMemberIDList {
users = append(users, UserToSuperGroup{
UserID: v,
})
}
upsert := true
opts := &options.UpdateOptions{
Upsert: &upsert,
}
c = d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cUserToSuperGroup)
//_, err = c.UpdateMany(sCtx, bson.M{"user_id": bson.M{"$in": initMemberIDList}}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
//if err != nil {
// session.AbortTransaction(ctx)
// return utils.Wrap(err, "transaction failed")
//}
for _, userID := range initMemberIDList {
_, err = c.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
if err != nil {
_ = session.AbortTransaction(ctx)
return utils.Wrap(err, "transaction failed")
}
}
return err
}
func (d *db.DataBases) GetSuperGroup(groupID string) (SuperGroup, error) {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup)
superGroup := SuperGroup{}
err := c.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&superGroup)
return superGroup, err
}
func (d *db.DataBases) AddUserToSuperGroup(groupID string, userIDList []string) error {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup)
session, err := d.mongoClient.StartSession()
if err != nil {
return utils.Wrap(err, "start session failed")
}
defer session.EndSession(ctx)
sCtx := mongo.NewSessionContext(ctx, session)
if err != nil {
return utils.Wrap(err, "start transaction failed")
}
_, err = c.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDList}}})
if err != nil {
_ = session.AbortTransaction(ctx)
return utils.Wrap(err, "transaction failed")
}
c = d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cUserToSuperGroup)
var users []UserToSuperGroup
for _, v := range userIDList {
users = append(users, UserToSuperGroup{
UserID: v,
})
}
upsert := true
opts := &options.UpdateOptions{
Upsert: &upsert,
}
for _, userID := range userIDList {
_, err = c.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
if err != nil {
_ = session.AbortTransaction(ctx)
return utils.Wrap(err, "transaction failed")
}
}
_ = session.CommitTransaction(ctx)
return err
}
func (d *db.DataBases) RemoverUserFromSuperGroup(groupID string, userIDList []string) error {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup)
session, err := d.mongoClient.StartSession()
if err != nil {
return utils.Wrap(err, "start session failed")
}
defer session.EndSession(ctx)
sCtx := mongo.NewSessionContext(ctx, session)
_, err = c.UpdateOne(ctx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDList}}})
if err != nil {
_ = session.AbortTransaction(ctx)
return utils.Wrap(err, "transaction failed")
}
err = d.RemoveGroupFromUser(ctx, sCtx, groupID, userIDList)
if err != nil {
_ = session.AbortTransaction(ctx)
return utils.Wrap(err, "transaction failed")
}
_ = session.CommitTransaction(ctx)
return err
}
func (d *db.DataBases) GetSuperGroupByUserID(userID string) (UserToSuperGroup, error) {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cUserToSuperGroup)
var user UserToSuperGroup
_ = c.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
return user, nil
}
func (d *db.DataBases) DeleteSuperGroup(groupID string) error {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup)
session, err := d.mongoClient.StartSession()
if err != nil {
return utils.Wrap(err, "start session failed")
}
defer session.EndSession(ctx)
sCtx := mongo.NewSessionContext(ctx, session)
superGroup := &SuperGroup{}
result := c.FindOneAndDelete(sCtx, bson.M{"group_id": groupID})
err = result.Decode(superGroup)
if err != nil {
session.AbortTransaction(ctx)
return utils.Wrap(err, "transaction failed")
}
if err = d.RemoveGroupFromUser(ctx, sCtx, groupID, superGroup.MemberIDList); err != nil {
session.AbortTransaction(ctx)
return utils.Wrap(err, "transaction failed")
}
session.CommitTransaction(ctx)
return nil
}
func (d *db.DataBases) RemoveGroupFromUser(ctx, sCtx context.Context, groupID string, userIDList []string) error {
var users []UserToSuperGroup
for _, v := range userIDList {
users = append(users, UserToSuperGroup{
UserID: v,
})
}
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cUserToSuperGroup)
_, err := c.UpdateOne(sCtx, bson.M{"user_id": bson.M{"$in": userIDList}}, bson.M{"$pull": bson.M{"group_id_list": groupID}})
if err != nil {
return utils.Wrap(err, "UpdateOne transaction failed")
}
return err
}
func generateTagID(tagName, userID string) string {
return utils.Md5(tagName + userID + strconv.Itoa(rand.Int()) + time.Now().String())
}
+30 -40
View File
@@ -1,54 +1,44 @@
package discoveryRegistry
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/utils"
"context"
"fmt"
"github.com/OpenIMSDK/getcdv3"
clientv3 "go.etcd.io/etcd/client/v3"
"google.golang.org/grpc"
"time"
"gopkg.in/yaml.v3"
"strings"
)
type SvcDiscoveryRegistry interface {
Register(serviceName, host string, port int, opts ...grpc.DialOption) error
UnRegister() error
GetConns(serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error)
GetConn(serviceName string, strategy func(slice []*grpc.ClientConn) int, opts ...grpc.DialOption) (*grpc.ClientConn, error)
GetConn(serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error)
//RegisterConf(conf []byte) error
//LoadConf() ([]byte, error)
}
func registerConf(key, conf string) {
etcdAddr := strings.Join(config.Config.Etcd.EtcdAddr, ",")
cli, err := clientv3.New(clientv3.Config{
Endpoints: strings.Split(etcdAddr, ","), DialTimeout: 5 * time.Second})
if err != nil {
panic(err.Error())
}
//lease
if _, err := cli.Put(context.Background(), key, conf); err != nil {
fmt.Println("panic, params: ")
panic(err.Error())
}
}
func RegisterConf() {
bytes, err := yaml.Marshal(config.Config)
if err != nil {
panic(err.Error())
}
secretMD5 := utils.Md5(config.Config.Etcd.Secret)
confBytes, err := utils.AesEncrypt(bytes, []byte(secretMD5[0:16]))
if err != nil {
panic(err.Error())
}
fmt.Println("start register", secretMD5, getcdv3.GetPrefix(config.Config.Etcd.EtcdSchema, config.ConfName))
registerConf(getcdv3.GetPrefix(config.Config.Etcd.EtcdSchema, config.ConfName), string(confBytes))
fmt.Println("etcd register conf ok")
}
//func registerConf(key, conf string) {
// etcdAddr := strings.Join(config.Config.Etcd.EtcdAddr, ",")
// cli, err := clientv3.New(clientv3.Config{
// Endpoints: strings.Split(etcdAddr, ","), DialTimeout: 5 * time.Second})
//
// if err != nil {
// panic(err.Error())
// }
// //lease
// if _, err := cli.Put(context.Background(), key, conf); err != nil {
// fmt.Println("panic, params: ")
// panic(err.Error())
// }
//}
//
//func RegisterConf() {
// bytes, err := yaml.Marshal(config.Config)
// if err != nil {
// panic(err.Error())
// }
// secretMD5 := utils.Md5(config.Config.Etcd.Secret)
// confBytes, err := utils.AesEncrypt(bytes, []byte(secretMD5[0:16]))
// if err != nil {
// panic(err.Error())
// }
// fmt.Println("start register", secretMD5, getcdv3.GetPrefix(config.Config.Etcd.EtcdSchema, config.ConfName))
// registerConf(getcdv3.GetPrefix(config.Config.Etcd.EtcdSchema, config.ConfName), string(confBytes))
// fmt.Println("etcd register conf ok")
//}
-13
View File
@@ -1,7 +1,6 @@
package utils
import (
"Open_IM/pkg/common/config"
"errors"
"net"
)
@@ -24,15 +23,3 @@ func GetLocalIP() (string, error) {
return "", errors.New("no ip")
}
func GetRpcIP() (string, error) {
registerIP := config.Config.RpcRegisterIP
if registerIP == "" {
ip, err := GetLocalIP()
if err != nil {
return "", err
}
registerIP = ip
}
return registerIP, nil
}