mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-15 22:39:03 +08:00
errCode
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"Open_IM/pkg/common/db/mysql"
|
||||
"context"
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type BlackModel struct {
|
||||
db *relation.Black
|
||||
cache *cache.GroupCache
|
||||
}
|
||||
|
||||
func (b *BlackModel) Create(ctx context.Context, blacks []*relation.Black) (err error) {
|
||||
return b.db.Create(ctx, blacks)
|
||||
}
|
||||
|
||||
func (b *BlackModel) Delete(ctx context.Context, blacks []*relation.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 []*relation.Black) (err error) {
|
||||
return b.db.Update(ctx, blacks)
|
||||
}
|
||||
|
||||
func (b *BlackModel) Find(ctx context.Context, blacks []*relation.Black) (blackList []*relation.Black, err error) {
|
||||
return b.db.Find(ctx, blacks)
|
||||
}
|
||||
|
||||
func (b *BlackModel) Take(ctx context.Context, ownerUserID, blockUserID string) (black *relation.Black, err error) {
|
||||
return b.db.Take(ctx, ownerUserID, blockUserID)
|
||||
}
|
||||
|
||||
func (b *BlackModel) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*relation.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,61 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"Open_IM/pkg/common/db/mysql"
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
"context"
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type FriendModel struct {
|
||||
db *relation.Friend
|
||||
cache *cache.GroupCache
|
||||
}
|
||||
|
||||
func (f *FriendModel) Create(ctx context.Context, friends []*relation.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 []*relation.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 []*relation.Friend, err error) {
|
||||
return f.db.FindOwnerUserID(ctx, ownerUserID)
|
||||
}
|
||||
|
||||
func (f *FriendModel) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*relation.Friend, err error) {
|
||||
return f.db.FindFriendUserID(ctx, friendUserID)
|
||||
}
|
||||
|
||||
func (f *FriendModel) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *relation.Friend, err error) {
|
||||
return f.db.Take(ctx, ownerUserID, friendUserID)
|
||||
}
|
||||
|
||||
func (f *FriendModel) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*relation.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 controller
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"Open_IM/pkg/common/db/mysql"
|
||||
"context"
|
||||
)
|
||||
|
||||
type FriendRequestModel struct {
|
||||
db *relation.FriendRequest
|
||||
cache *cache.GroupCache
|
||||
}
|
||||
|
||||
func (f *FriendRequestModel) Create(ctx context.Context, friends []*relation.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 []*relation.FriendRequest) (err error) {
|
||||
return f.db.Update(ctx, friends)
|
||||
}
|
||||
|
||||
func (f *FriendRequestModel) Find(ctx context.Context, ownerUserID string) (friends []*relation.FriendRequest, err error) {
|
||||
return f.db.Find(ctx, ownerUserID)
|
||||
}
|
||||
|
||||
func (f *FriendRequestModel) Take(ctx context.Context, fromUserID, toUserID string) (friend *relation.FriendRequest, err error) {
|
||||
return f.db.Take(ctx, fromUserID, toUserID)
|
||||
}
|
||||
|
||||
func (f *FriendRequestModel) FindToUserID(ctx context.Context, toUserID string) (friends []*relation.FriendRequest, err error) {
|
||||
return f.db.FindToUserID(ctx, toUserID)
|
||||
}
|
||||
|
||||
func (f *FriendRequestModel) FindFromUserID(ctx context.Context, fromUserID string) (friends []*relation.FriendRequest, err error) {
|
||||
return f.db.FindFromUserID(ctx, fromUserID)
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
"Open_IM/pkg/common/db/unrelation"
|
||||
"context"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
_ "github.com/dtm-labs/rockscache"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GroupInterface interface {
|
||||
FindGroupsByID(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error)
|
||||
CreateGroup(ctx context.Context, groups []*relation.Group) error
|
||||
DeleteGroupByIDs(ctx context.Context, groupIDs []string) error
|
||||
TakeGroupByID(ctx context.Context, groupID string) (group *relation.Group, err error)
|
||||
|
||||
//mongo
|
||||
GetSuperGroupByID(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error)
|
||||
}
|
||||
|
||||
type GroupController struct {
|
||||
database DataBase
|
||||
}
|
||||
|
||||
func NewGroupController(db *gorm.DB, rdb redis.UniversalClient, mgoDB *mongo.Database) GroupInterface {
|
||||
groupController := &GroupController{database: newGroupDatabase(db, rdb, mgoDB)}
|
||||
return groupController
|
||||
}
|
||||
|
||||
func (g *GroupController) FindGroupsByID(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error) {
|
||||
return g.database.Find(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupController) CreateGroup(ctx context.Context, groups []*relation.Group) error {
|
||||
return g.database.Create(ctx, groups)
|
||||
}
|
||||
|
||||
func (g *GroupController) DeleteGroupByIDs(ctx context.Context, groupIDs []string) error {
|
||||
return g.database.Delete(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupController) TakeGroupByID(ctx context.Context, groupID string) (group *relation.Group, err error) {
|
||||
return g.database.Take(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupController) GetSuperGroupByID(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error) {
|
||||
return g.database.GetSuperGroup(ctx, groupID)
|
||||
}
|
||||
|
||||
type DataBase interface {
|
||||
Find(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error)
|
||||
Create(ctx context.Context, groups []*relation.Group) error
|
||||
Delete(ctx context.Context, groupIDs []string) error
|
||||
Take(ctx context.Context, groupID string) (group *relation.Group, err error)
|
||||
GetSuperGroup(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error)
|
||||
}
|
||||
|
||||
type GroupDataBase struct {
|
||||
sqlDB *relation.Group
|
||||
cache *cache.GroupCache
|
||||
mongoDB *unrelation.SuperGroupMgo
|
||||
}
|
||||
|
||||
func newGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, mgoDB *mongo.Database) DataBase {
|
||||
sqlDB := relation.NewGroupDB(db)
|
||||
database := &GroupDataBase{
|
||||
sqlDB: sqlDB,
|
||||
cache: cache.NewGroupCache(rdb, sqlDB, rockscache.Options{
|
||||
RandomExpireAdjustment: 0.2,
|
||||
DisableCacheRead: false,
|
||||
DisableCacheDelete: false,
|
||||
StrongConsistency: true,
|
||||
}),
|
||||
mongoDB: unrelation.NewSuperGroupMgoDB(mgoDB),
|
||||
}
|
||||
return database
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) Find(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error) {
|
||||
return g.cache.GetGroupsInfoFromCache(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) Create(ctx context.Context, groups []*relation.Group) error {
|
||||
return g.sqlDB.Create(ctx, groups)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) Delete(ctx context.Context, groupIDs []string) error {
|
||||
err := g.sqlDB.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.sqlDB.Delete(ctx, groupIDs, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.cache.DelGroupsInfoFromCache(ctx, groupIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) Take(ctx context.Context, groupID string) (group *relation.Group, err error) {
|
||||
return g.cache.GetGroupInfoFromCache(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) Update(ctx context.Context, groups []*relation.Group) error {
|
||||
err := g.sqlDB.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.sqlDB.Update(ctx, groups, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
var groupIDs []string
|
||||
for _, group := range groups {
|
||||
groupIDs = append(groupIDs, group.GroupID)
|
||||
}
|
||||
if err := g.cache.DelGroupsInfoFromCache(ctx, groupIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) GetSuperGroup(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error) {
|
||||
return g.mongoDB.GetSuperGroup(ctx, groupID)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/mysql"
|
||||
"context"
|
||||
)
|
||||
|
||||
type UserModel struct {
|
||||
db *relation.User
|
||||
}
|
||||
|
||||
func NewGroupUser(ctx context.Context) *UserModel {
|
||||
var userModel UserModel
|
||||
userModel.db = relation.NewUserDB()
|
||||
return &userModel
|
||||
}
|
||||
|
||||
func (u *UserModel) Find(ctx context.Context, userIDs []string) (users []*relation.User, err error) {
|
||||
return u.db.Find(ctx, userIDs)
|
||||
}
|
||||
|
||||
func (u *UserModel) Create(ctx context.Context, users []*relation.User) error {
|
||||
return u.db.Create(ctx, users)
|
||||
}
|
||||
Reference in New Issue
Block a user