mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-13 05:25:59 +08:00
groupModel
This commit is contained in:
Vendored
+3
-4
@@ -15,15 +15,14 @@ const GroupExpireTime = time.Second * 60 * 60 * 12
|
||||
const groupInfoCacheKey = "GROUP_INFO_CACHE:"
|
||||
|
||||
type GroupCache struct {
|
||||
db *mysql.Group
|
||||
db mysql.GroupModelInterface
|
||||
expireTime time.Duration
|
||||
redisClient *RedisClient
|
||||
rcClient *rockscache.Client
|
||||
}
|
||||
|
||||
func NewGroupCache(rdb redis.UniversalClient, db *mysql.Group, opts rockscache.Options) *GroupCache {
|
||||
redisClient := NewRedisClient(rdb)
|
||||
return &GroupCache{rcClient: rockscache.NewClient(rdb, opts), expireTime: GroupExpireTime, db: db, redisClient: redisClient}
|
||||
func NewGroupCache(rdb redis.UniversalClient, db mysql.GroupModelInterface, opts rockscache.Options) *GroupCache {
|
||||
return &GroupCache{rcClient: rockscache.NewClient(rdb, opts), expireTime: GroupExpireTime, db: db, redisClient: NewRedisClient(rdb)}
|
||||
}
|
||||
|
||||
func (g *GroupCache) getRedisClient() *RedisClient {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"Open_IM/pkg/common/db/mysql"
|
||||
"context"
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type BlackModel struct {
|
||||
db *mysql.Black
|
||||
cache *cache.GroupCache
|
||||
}
|
||||
|
||||
func (b *BlackModel) Create(ctx context.Context, blacks []*mysql.Black) (err error) {
|
||||
return b.db.Create(ctx, blacks)
|
||||
}
|
||||
|
||||
func (b *BlackModel) Delete(ctx context.Context, blacks []*mysql.Black) (err error) {
|
||||
return b.db.Delete(ctx, blacks)
|
||||
}
|
||||
|
||||
func (b *BlackModel) UpdateByMap(ctx context.Context, ownerUserID, blockUserID string, args map[string]interface{}) (err error) {
|
||||
return b.db.UpdateByMap(ctx, ownerUserID, blockUserID, args)
|
||||
}
|
||||
|
||||
func (b *BlackModel) Update(ctx context.Context, blacks []*mysql.Black) (err error) {
|
||||
return b.db.Update(ctx, blacks)
|
||||
}
|
||||
|
||||
func (b *BlackModel) Find(ctx context.Context, blacks []*mysql.Black) (blackList []*mysql.Black, err error) {
|
||||
return b.db.Find(ctx, blacks)
|
||||
}
|
||||
|
||||
func (b *BlackModel) Take(ctx context.Context, ownerUserID, blockUserID string) (black *mysql.Black, err error) {
|
||||
return b.db.Take(ctx, ownerUserID, blockUserID)
|
||||
}
|
||||
|
||||
func (b *BlackModel) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*mysql.Black, err error) {
|
||||
return b.db.FindByOwnerUserID(ctx, ownerUserID)
|
||||
}
|
||||
|
||||
func (b *BlackModel) IsExist(ctx context.Context, ownerUserID, blockUserID string) (bool, error) {
|
||||
if _, err := b.Take(ctx, ownerUserID, blockUserID); err == nil {
|
||||
return true, nil
|
||||
} else if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return false, nil
|
||||
} else {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"Open_IM/pkg/common/db/mysql"
|
||||
"context"
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type FriendModel struct {
|
||||
db *mysql.Friend
|
||||
cache *cache.GroupCache
|
||||
}
|
||||
|
||||
func (f *FriendModel) Create(ctx context.Context, friends []*mysql.Friend) (err error) {
|
||||
return f.db.Create(ctx, friends)
|
||||
}
|
||||
|
||||
func (f *FriendModel) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) {
|
||||
return f.db.Delete(ctx, ownerUserID, friendUserIDs)
|
||||
}
|
||||
|
||||
func (f *FriendModel) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) {
|
||||
return f.db.UpdateByMap(ctx, ownerUserID, args)
|
||||
}
|
||||
|
||||
func (f *FriendModel) Update(ctx context.Context, friends []*mysql.Friend) (err error) {
|
||||
return f.db.Update(ctx, friends)
|
||||
}
|
||||
|
||||
func (f *FriendModel) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
|
||||
return f.db.UpdateRemark(ctx, ownerUserID, friendUserID, remark)
|
||||
}
|
||||
|
||||
func (f *FriendModel) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*mysql.Friend, err error) {
|
||||
return f.db.FindOwnerUserID(ctx, ownerUserID)
|
||||
}
|
||||
|
||||
func (f *FriendModel) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*mysql.Friend, err error) {
|
||||
return f.db.FindFriendUserID(ctx, friendUserID)
|
||||
}
|
||||
|
||||
func (f *FriendModel) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *mysql.Friend, err error) {
|
||||
return f.db.Take(ctx, ownerUserID, friendUserID)
|
||||
}
|
||||
|
||||
func (f *FriendModel) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*mysql.Friend, err error) {
|
||||
return f.db.FindUserState(ctx, userID1, userID2)
|
||||
}
|
||||
|
||||
func (f *FriendModel) IsExist(ctx context.Context, ownerUserID, friendUserID string) (bool, error) {
|
||||
if _, err := f.Take(ctx, ownerUserID, friendUserID); err == nil {
|
||||
return true, nil
|
||||
} else if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return false, nil
|
||||
} else {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"Open_IM/pkg/common/db/mysql"
|
||||
"context"
|
||||
)
|
||||
|
||||
type FriendRequestModel struct {
|
||||
db *mysql.FriendRequest
|
||||
cache *cache.GroupCache
|
||||
}
|
||||
|
||||
func (f *FriendRequestModel) Create(ctx context.Context, friends []*mysql.FriendRequest) (err error) {
|
||||
return f.db.Create(ctx, friends)
|
||||
}
|
||||
|
||||
func (f *FriendRequestModel) Delete(ctx context.Context, fromUserID, toUserID string) (err error) {
|
||||
return f.db.Delete(ctx, fromUserID, toUserID)
|
||||
}
|
||||
|
||||
func (f *FriendRequestModel) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) {
|
||||
return f.db.UpdateByMap(ctx, ownerUserID, args)
|
||||
}
|
||||
|
||||
func (f *FriendRequestModel) Update(ctx context.Context, friends []*mysql.FriendRequest) (err error) {
|
||||
return f.db.Update(ctx, friends)
|
||||
}
|
||||
|
||||
func (f *FriendRequestModel) Find(ctx context.Context, ownerUserID string) (friends []*mysql.FriendRequest, err error) {
|
||||
return f.db.Find(ctx, ownerUserID)
|
||||
}
|
||||
|
||||
func (f *FriendRequestModel) Take(ctx context.Context, fromUserID, toUserID string) (friend *mysql.FriendRequest, err error) {
|
||||
return f.db.Take(ctx, fromUserID, toUserID)
|
||||
}
|
||||
|
||||
func (f *FriendRequestModel) FindToUserID(ctx context.Context, toUserID string) (friends []*mysql.FriendRequest, err error) {
|
||||
return f.db.FindToUserID(ctx, toUserID)
|
||||
}
|
||||
|
||||
func (f *FriendRequestModel) FindFromUserID(ctx context.Context, fromUserID string) (friends []*mysql.FriendRequest, err error) {
|
||||
return f.db.FindFromUserID(ctx, fromUserID)
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"Open_IM/pkg/common/db/mongoDB"
|
||||
"Open_IM/pkg/common/db/mysql"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
"github.com/go-redis/redis/v8"
|
||||
@@ -13,13 +12,20 @@ import (
|
||||
//"time"
|
||||
)
|
||||
|
||||
type GroupInterface interface {
|
||||
Find(ctx context.Context, groupIDs []string) (groups []*mysql.Group, err error)
|
||||
Create(ctx context.Context, groups []*mysql.Group) error
|
||||
Delete(ctx context.Context, groupIDs []string) error
|
||||
Take(ctx context.Context, groupID string) (group *mysql.Group, err error)
|
||||
}
|
||||
|
||||
type GroupModel struct {
|
||||
db *mysql.Group
|
||||
db mysql.GroupModelInterface
|
||||
cache *cache.GroupCache
|
||||
mongo *mongoDB.Client
|
||||
}
|
||||
|
||||
func NewGroupModel(db *mysql.Group, rdb redis.UniversalClient, mdb *mongo.Client) *GroupModel {
|
||||
func NewGroupModel(db mysql.GroupModelInterface, rdb redis.UniversalClient, mdb *mongo.Client) *GroupModel {
|
||||
var groupModel GroupModel
|
||||
groupModel.db = db
|
||||
groupModel.cache = cache.NewGroupCache(rdb, db, rockscache.Options{
|
||||
|
||||
@@ -52,7 +52,7 @@ func (b *Black) Update(ctx context.Context, blacks []*Black) (err error) {
|
||||
return utils.Wrap(b.DB.Updates(&blacks).Error, "")
|
||||
}
|
||||
|
||||
func (b *Black) Find(ctx context.Context, blacks []Black) (blackList []*Black, err error) {
|
||||
func (b *Black) Find(ctx context.Context, blacks []*Black) (blackList []*Black, err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blacks", blacks, "blackList", blackList)
|
||||
}()
|
||||
@@ -63,12 +63,12 @@ func (b *Black) Find(ctx context.Context, blacks []Black) (blackList []*Black, e
|
||||
return blackList, utils.Wrap(GroupMemberDB.Where("(owner_user_id, block_user_id) in ?", where).Find(&blackList).Error, "")
|
||||
}
|
||||
|
||||
func (b *Black) Take(ctx context.Context, blackID string) (black *Black, err error) {
|
||||
func (b *Black) Take(ctx context.Context, ownerUserID, blockUserID string) (black *Black, err error) {
|
||||
black = &Black{}
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "blackID", blackID, "black", *black)
|
||||
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "blockUserID", blockUserID, "black", *black)
|
||||
}()
|
||||
return black, utils.Wrap(b.DB.Where("black_id = ?", blackID).Take(black).Error, "")
|
||||
return black, utils.Wrap(b.DB.Where("owner_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Take(black).Error, "")
|
||||
}
|
||||
|
||||
func (b *Black) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*Black, err error) {
|
||||
@@ -61,13 +61,20 @@ func (f *Friend) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, re
|
||||
return utils.Wrap(f.DB.Model(f).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserID).Update("remark", remark).Error, "")
|
||||
}
|
||||
|
||||
func (f *Friend) Find(ctx context.Context, ownerUserID string) (friends []*Friend, err error) {
|
||||
func (f *Friend) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*Friend, err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
func (f *Friend) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*Friend, err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friendUserID", friendUserID, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(f.DB.Where("friend_user_id = ?", friendUserID).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
func (f *Friend) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *Friend, err error) {
|
||||
friend = &Friend{}
|
||||
defer trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "friend", friend)
|
||||
|
||||
@@ -33,16 +33,14 @@ func (f *FriendRequest) Create(ctx context.Context, friends []*FriendRequest) (e
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
|
||||
}()
|
||||
err = utils.Wrap(f.DB.Create(&friends).Error, "")
|
||||
return err
|
||||
return utils.Wrap(f.DB.Create(&friends).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendRequest) Delete(ctx context.Context, fromUserID, toUserID string) (err error) {
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "toUserID", toUserID)
|
||||
}()
|
||||
err = utils.Wrap(f.DB.Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Delete(&FriendRequest{}).Error, "")
|
||||
return err
|
||||
return utils.Wrap(f.DB.Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Delete(&FriendRequest{}).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendRequest) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) {
|
||||
@@ -63,15 +61,13 @@ func (f *FriendRequest) Find(ctx context.Context, ownerUserID string) (friends [
|
||||
defer func() {
|
||||
trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friends", friends)
|
||||
}()
|
||||
err = utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "")
|
||||
return friends, err
|
||||
return friends, utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendRequest) Take(ctx context.Context, fromUserID, toUserID string) (friend *FriendRequest, err error) {
|
||||
friend = &FriendRequest{}
|
||||
defer trace_log.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "toUserID", toUserID, "friend", friend)
|
||||
err = utils.Wrap(f.DB.Where("from_user_id = ? and to_user_id", fromUserID, toUserID).Take(friend).Error, "")
|
||||
return friend, err
|
||||
return friend, utils.Wrap(f.DB.Where("from_user_id = ? and to_user_id", fromUserID, toUserID).Take(friend).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendRequest) FindToUserID(ctx context.Context, toUserID string) (friends []*FriendRequest, err error) {
|
||||
|
||||
@@ -8,6 +8,17 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type GroupModelInterface interface {
|
||||
//mysql
|
||||
Create(ctx context.Context, groups []*Group) (err error)
|
||||
Delete(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (err error)
|
||||
UpdateByMap(ctx context.Context, groupID string, args map[string]interface{}) (err error)
|
||||
Update(ctx context.Context, groups []*Group) (err error)
|
||||
Find(ctx context.Context, groupIDs []string) (groups []*Group, err error)
|
||||
Take(ctx context.Context, groupID string) (group *Group, err error)
|
||||
|
||||
//mongo
|
||||
}
|
||||
type Group struct {
|
||||
GroupID string `gorm:"column:group_id;primary_key;size:64" json:"groupID" binding:"required"`
|
||||
GroupName string `gorm:"column:name;size:255" json:"groupName"`
|
||||
|
||||
Reference in New Issue
Block a user