mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-03 00:25:59 +08:00
Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
"Open_IM/pkg/common/db/table"
|
||||
"context"
|
||||
@@ -9,11 +10,11 @@ import (
|
||||
|
||||
type FriendInterface interface {
|
||||
// CheckIn 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
CheckIn(ctx context.Context, user1, user2 string) (err error, inUser1Friends bool, inUser2Friends bool)
|
||||
CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error)
|
||||
// AddFriendRequest 增加或者更新好友申请
|
||||
AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error)
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
BecomeFriend(ctx context.Context, friends []*table.FriendModel) (err error)
|
||||
BecomeFriend(ctx context.Context, friends []*table.FriendModel, revFriends []*table.FriendModel) (err error)
|
||||
// RefuseFriendRequest 拒绝好友申请
|
||||
RefuseFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error)
|
||||
// AgreeFriendRequest 同意好友申请
|
||||
@@ -23,14 +24,14 @@ type FriendInterface interface {
|
||||
// UpdateRemark 更新好友备注
|
||||
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
|
||||
// FindOwnerFriends 获取ownerUserID的好友列表
|
||||
FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, err error)
|
||||
FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, total int64, err error)
|
||||
// FindInWhoseFriends friendUserID在哪些人的好友列表中
|
||||
FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, err error)
|
||||
FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, total int64, err error)
|
||||
// FindFriendRequestFromMe 获取我发出去的好友申请
|
||||
FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, err error)
|
||||
FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, total int64, err error)
|
||||
// FindFriendRequestToMe 获取我收到的的好友申请
|
||||
FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, err error)
|
||||
// FindFriends 获取某人指定好友的信息
|
||||
FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, total int64, err error)
|
||||
// FindFriends 获取某人指定好友的信息 如果有一个不存在也返回错误
|
||||
FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*table.FriendModel, err error)
|
||||
}
|
||||
|
||||
@@ -92,7 +93,7 @@ func (f *FriendController) FindFriends(ctx context.Context, ownerUserID string,
|
||||
|
||||
type FriendDatabaseInterface interface {
|
||||
// CheckIn 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
CheckIn(ctx context.Context, user1, user2 string) (err error, inUser1Friends bool, inUser2Friends bool)
|
||||
CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error)
|
||||
// AddFriendRequest 增加或者更新好友申请
|
||||
AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error)
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
@@ -127,15 +128,44 @@ func NewFriendDatabase(db *gorm.DB) *FriendDatabase {
|
||||
}
|
||||
|
||||
// CheckIn 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
func (f *FriendDatabase) CheckIn(ctx context.Context, user1, user2 string) (err error, inUser1Friends bool, inUser2Friends bool) {
|
||||
func (f *FriendDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Friends bool, inUser2Friends bool, err error) {
|
||||
friends, err := f.friend.FindUserState(ctx, userID1, userID2)
|
||||
for _, v := range friends {
|
||||
if v.OwnerUserID == userID1 && v.FriendUserID == userID2 {
|
||||
inUser1Friends = true
|
||||
}
|
||||
if v.OwnerUserID == userID2 && v.FriendUserID == userID1 {
|
||||
inUser2Friends = true
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// AddFriendRequest 增加或者更新好友申请
|
||||
func (f *FriendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error) {
|
||||
|
||||
}
|
||||
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
func (f *FriendDatabase) BecomeFriend(ctx context.Context, friends []*table.FriendModel) (err error) {
|
||||
func (f *FriendDatabase) BecomeFriend(ctx context.Context, ownerUserID string, friends []*table.FriendModel) (err error) {
|
||||
return f.friend.DB.Transaction(func(tx *gorm.DB) error {
|
||||
//先find 找出重复的 去掉重复的
|
||||
friendUserIDs := make([]string, 0, len(friends))
|
||||
for _, v := range friends {
|
||||
friendUserIDs = append(friendUserIDs, v.FriendUserID)
|
||||
}
|
||||
fs1, err := f.friend.FindFriends(ctx, ownerUserID, friendUserIDs, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fs2, err := f.friend.FindReversalFriends(ctx, ownerUserID, friendUserIDs, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// RefuseFriendRequest 拒绝好友申请
|
||||
@@ -170,6 +200,14 @@ func (f *FriendDatabase) FindFriendRequestFromMe(ctx context.Context, userID str
|
||||
func (f *FriendDatabase) FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, err error) {
|
||||
}
|
||||
|
||||
// FindFriends 获取某人指定好友的信息
|
||||
// FindFriends 获取某人指定好友的信息 如果有一个不存在也返回错误
|
||||
func (f *FriendDatabase) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*table.FriendModel, err error) {
|
||||
friends, err = f.friend.Find(ctx, ownerUserID, friendUserIDs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(friends) != len(friendUserIDs) {
|
||||
err = constant.ErrRecordNotFound.Wrap()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
"Open_IM/pkg/common/db/table"
|
||||
"context"
|
||||
@@ -10,11 +11,17 @@ import (
|
||||
type UserInterface interface {
|
||||
//获取指定用户的信息 如果有记录未找到 也返回错误
|
||||
Find(ctx context.Context, userIDs []string) (users []*table.UserModel, err error)
|
||||
//插入
|
||||
Create(ctx context.Context, users []*table.UserModel) error
|
||||
//更新
|
||||
Update(ctx context.Context, users []*table.UserModel) (err error)
|
||||
//更新带零值的
|
||||
UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error)
|
||||
//通过名字搜索
|
||||
GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error)
|
||||
//通过名字和id搜索
|
||||
GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error)
|
||||
//获取,如果没找到,不不返回错误
|
||||
Get(ctx context.Context, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error)
|
||||
//userIDs是否存在 只要有一个存在就为true
|
||||
IsExist(ctx context.Context, userIDs []string) (exist bool, err error)
|
||||
@@ -30,24 +37,29 @@ func (u *UserController) Find(ctx context.Context, userIDs []string) (users []*t
|
||||
func (u *UserController) Create(ctx context.Context, users []*table.UserModel) error {
|
||||
return u.database.Create(ctx, users)
|
||||
}
|
||||
func (u *UserController) Take(ctx context.Context, userID string) (user *table.UserModel, err error) {
|
||||
return u.database.Take(ctx, userID)
|
||||
}
|
||||
|
||||
func (u *UserController) Update(ctx context.Context, users []*table.UserModel) (err error) {
|
||||
return u.database.Update(ctx, users)
|
||||
}
|
||||
func (u *UserController) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
|
||||
return u.database.UpdateByMap(ctx, userID, args)
|
||||
}
|
||||
|
||||
func (u *UserController) GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error) {
|
||||
return u.database.GetByName(ctx, userName, showNumber, pageNumber)
|
||||
}
|
||||
|
||||
func (u *UserController) GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error) {
|
||||
return u.database.GetByNameAndID(ctx, content, showNumber, pageNumber)
|
||||
}
|
||||
|
||||
func (u *UserController) Get(ctx context.Context, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error) {
|
||||
return u.database.Get(ctx, showNumber, pageNumber)
|
||||
}
|
||||
|
||||
func (u *UserController) IsExist(ctx context.Context, userIDs []string) (exist bool, err error) {
|
||||
return u.IsExist(ctx, userIDs)
|
||||
}
|
||||
func NewUserController(db *gorm.DB) *UserController {
|
||||
controller := &UserController{database: newUserDatabase(db)}
|
||||
return controller
|
||||
@@ -56,12 +68,12 @@ func NewUserController(db *gorm.DB) *UserController {
|
||||
type UserDatabaseInterface interface {
|
||||
Find(ctx context.Context, userIDs []string) (users []*table.UserModel, err error)
|
||||
Create(ctx context.Context, users []*table.UserModel) error
|
||||
Take(ctx context.Context, userID string) (user *table.UserModel, err error)
|
||||
Update(ctx context.Context, users []*table.UserModel) (err error)
|
||||
UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error)
|
||||
GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error)
|
||||
GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error)
|
||||
Get(ctx context.Context, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error)
|
||||
IsExist(ctx context.Context, userIDs []string) (exist bool, err error)
|
||||
}
|
||||
|
||||
type UserDatabase struct {
|
||||
@@ -76,16 +88,22 @@ func newUserDatabase(db *gorm.DB) *UserDatabase {
|
||||
return database
|
||||
}
|
||||
|
||||
// 获取指定用户的信息 如果有记录未找到 也返回错误
|
||||
func (u *UserDatabase) Find(ctx context.Context, userIDs []string) (users []*table.UserModel, err error) {
|
||||
return u.sqlDB.Find(ctx, userIDs)
|
||||
users, err = u.sqlDB.Find(ctx, userIDs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(users) != len(userIDs) {
|
||||
err = constant.ErrRecordNotFound.Wrap()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (u *UserDatabase) Create(ctx context.Context, users []*table.UserModel) error {
|
||||
func (u *UserDatabase) Create(ctx context.Context, users []*table.UserModel) (err error) {
|
||||
return u.sqlDB.Create(ctx, users)
|
||||
}
|
||||
func (u *UserDatabase) Take(ctx context.Context, userID string) (user *table.UserModel, err error) {
|
||||
return u.sqlDB.Take(ctx, userID)
|
||||
}
|
||||
|
||||
func (u *UserDatabase) Update(ctx context.Context, users []*table.UserModel) (err error) {
|
||||
return u.sqlDB.Update(ctx, users)
|
||||
}
|
||||
@@ -98,6 +116,20 @@ func (u *UserDatabase) GetByName(ctx context.Context, userName string, showNumbe
|
||||
func (u *UserDatabase) GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error) {
|
||||
return u.sqlDB.GetByNameAndID(ctx, content, showNumber, pageNumber)
|
||||
}
|
||||
|
||||
// 获取,如果没找到,不返回错误
|
||||
func (u *UserDatabase) Get(ctx context.Context, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error) {
|
||||
return u.sqlDB.Get(ctx, showNumber, pageNumber)
|
||||
}
|
||||
|
||||
// userIDs是否存在 只要有一个存在就为true
|
||||
func (u *UserDatabase) IsExist(ctx context.Context, userIDs []string) (exist bool, err error) {
|
||||
users, err := u.sqlDB.Find(ctx, userIDs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(users) > 0 {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
@@ -30,11 +30,11 @@ type FriendUser struct {
|
||||
Nickname string `gorm:"column:name;size:255"`
|
||||
}
|
||||
|
||||
func (f *FriendGorm) Create(ctx context.Context, friends []*table.FriendModel) (err error) {
|
||||
func (f *FriendGorm) Create(ctx context.Context, friends []*table.FriendModel, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
|
||||
}()
|
||||
return utils.Wrap(f.DB.Model(&table.FriendModel{}).Create(&friends).Error, "")
|
||||
return utils.Wrap(getDBConn(f.DB, tx).Model(&table.FriendModel{}).Create(&friends).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) {
|
||||
@@ -52,7 +52,7 @@ func (f *FriendGorm) UpdateByMap(ctx context.Context, ownerUserID string, args m
|
||||
return utils.Wrap(f.DB.Model(&table.FriendModel{}).Where("owner_user_id = ?", ownerUserID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendGorm) Update(ctx context.Context, friends []*table.FriendModel) (err error) {
|
||||
func (f *FriendGorm) Update(ctx context.Context, friends []*table.FriendModel, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
|
||||
}()
|
||||
@@ -92,3 +92,19 @@ func (f *FriendGorm) FindUserState(ctx context.Context, userID1, userID2 string)
|
||||
}()
|
||||
return friends, utils.Wrap(f.DB.Model(&table.FriendModel{}).Where("(owner_user_id = ? and friend_user_id = ?) or (owner_user_id = ? and friend_user_id = ?)", userID1, userID2, userID2, userID1).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 获取 owner的好友列表
|
||||
func (f *FriendGorm) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, tx ...*gorm.DB) (friends []*table.FriendModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friendUserIDs", friendUserIDs, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(getDBConn(f.DB, tx).Where("owner_user_id = ? AND friend_user_id in (?)", ownerUserID, friendUserIDs).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 获取哪些人添加了friendUserID
|
||||
func (f *FriendGorm) FindReversalFriends(ctx context.Context, friendUserID string, ownerUserIDs []string, tx ...*gorm.DB) (friends []*table.FriendModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friendUserID", friendUserID, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(getDBConn(f.DB, tx).Where("friend_user_id = ? AND owner_user_id in (?)", friendUserID, ownerUserIDs).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
@@ -19,12 +19,11 @@ func NewUserGorm(db *gorm.DB) *UserGorm {
|
||||
return &user
|
||||
}
|
||||
|
||||
func (u *UserGorm) Create(ctx context.Context, users []*table.UserModel) (err error) {
|
||||
func (u *UserGorm) Create(ctx context.Context, users []*table.UserModel, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "users", users)
|
||||
}()
|
||||
err = utils.Wrap(u.DB.Model(&table.UserModel{}).Create(&users).Error, "")
|
||||
return err
|
||||
return utils.Wrap(getDBConn(u.DB, tx).Model(&table.UserModel{}).Create(&users).Error, "")
|
||||
}
|
||||
|
||||
func (u *UserGorm) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
|
||||
|
||||
Reference in New Issue
Block a user