mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-05-05 17:56:50 +08:00
Merge remote-tracking branch 'origin/errcode' into errcode
# Conflicts: # pkg/common/db/cache/conversation.go
This commit is contained in:
commit
99504eec2e
@ -303,7 +303,7 @@ const (
|
||||
BecomeFriendByImport = 1 //管理员导入
|
||||
BecomeFriendByMyApply = 2 //自己主动申请添加
|
||||
BecomeFriendByPeerApply = 3 //对方主动申请添加
|
||||
BecomeFriendByApply = 4 //自己主动申请添加
|
||||
BecomeFriendByApply = 4 //申请添加
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -3,7 +3,8 @@ package controller
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
relation2 "Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/db/table"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@ -14,25 +15,25 @@ type FriendInterface interface {
|
||||
// AddFriendRequest 增加或者更新好友申请
|
||||
AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error)
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
BecomeFriend(ctx context.Context, friends []*relation2.FriendModel, revFriends []*relation2.FriendModel) (err error)
|
||||
BecomeFriend(ctx context.Context, friends []*table.FriendModel, revFriends []*table.FriendModel) (err error)
|
||||
// RefuseFriendRequest 拒绝好友申请
|
||||
RefuseFriendRequest(ctx context.Context, friendRequest *relation2.FriendRequestModel) (err error)
|
||||
RefuseFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error)
|
||||
// AgreeFriendRequest 同意好友申请
|
||||
AgreeFriendRequest(ctx context.Context, friendRequest *relation2.FriendRequestModel) (err error)
|
||||
AgreeFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error)
|
||||
// Delete 删除好友
|
||||
Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error)
|
||||
// UpdateRemark 更新好友备注
|
||||
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
|
||||
// FindOwnerFriends 获取ownerUserID的好友列表
|
||||
FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation2.FriendModel, total int64, 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 []*relation2.FriendModel, total int64, 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 []*relation2.FriendRequestModel, total int64, 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 []*relation2.FriendRequestModel, total int64, err error)
|
||||
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 []*relation2.FriendModel, err error)
|
||||
FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*table.FriendModel, err error)
|
||||
}
|
||||
|
||||
type FriendController struct {
|
||||
@ -44,51 +45,64 @@ func NewFriendController(db *gorm.DB) *FriendController {
|
||||
}
|
||||
|
||||
// CheckIn 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
func (f *FriendController) CheckIn(ctx context.Context, user1, user2 string) (err error, inUser1Friends bool, inUser2Friends bool) {
|
||||
func (f *FriendController) CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error) {
|
||||
return f.database.CheckIn(ctx, user1, user2)
|
||||
}
|
||||
|
||||
// AddFriendRequest 增加或者更新好友申请
|
||||
func (f *FriendController) AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error) {
|
||||
return f.database.AddFriendRequest(ctx, fromUserID, toUserID, reqMsg, ex)
|
||||
}
|
||||
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
func (f *FriendController) BecomeFriend(ctx context.Context, friends []*relation2.FriendModel) (err error) {
|
||||
func (f *FriendController) BecomeFriend(ctx context.Context, ownerUserID string, friends []*table.FriendModel) (err error) {
|
||||
return f.database.BecomeFriend(ctx, ownerUserID, friends)
|
||||
}
|
||||
|
||||
// RefuseFriendRequest 拒绝好友申请
|
||||
func (f *FriendController) RefuseFriendRequest(ctx context.Context, friendRequest *relation2.FriendRequestModel) (err error) {
|
||||
func (f *FriendController) RefuseFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error) {
|
||||
return f.database.RefuseFriendRequest(ctx, friendRequest)
|
||||
}
|
||||
|
||||
// AgreeFriendRequest 同意好友申请
|
||||
func (f *FriendController) AgreeFriendRequest(ctx context.Context, friendRequest *relation2.FriendRequestModel) (err error) {
|
||||
func (f *FriendController) AgreeFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error) {
|
||||
return f.database.AgreeFriendRequest(ctx, friendRequest)
|
||||
}
|
||||
|
||||
// Delete 删除好友
|
||||
func (f *FriendController) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) {
|
||||
return f.database.Delete(ctx, ownerUserID, friendUserIDs)
|
||||
}
|
||||
|
||||
// UpdateRemark 更新好友备注
|
||||
func (f *FriendController) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
|
||||
return f.database.UpdateRemark(ctx, ownerUserID, friendUserID, remark)
|
||||
}
|
||||
|
||||
// FindOwnerFriends 获取ownerUserID的好友列表
|
||||
func (f *FriendController) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation2.FriendModel, err error) {
|
||||
func (f *FriendController) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, total int64, err error) {
|
||||
return f.database.FindOwnerFriends(ctx, ownerUserID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
// FindInWhoseFriends friendUserID在哪些人的好友列表中
|
||||
func (f *FriendController) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation2.FriendModel, err error) {
|
||||
func (f *FriendController) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, total int64, err error) {
|
||||
return f.database.FindInWhoseFriends(ctx, friendUserID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
// FindFriendRequestFromMe 获取我发出去的好友申请
|
||||
func (f *FriendController) FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation2.FriendRequestModel, err error) {
|
||||
func (f *FriendController) FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, total int64, err error) {
|
||||
return f.database.FindFriendRequestFromMe(ctx, userID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
// FindFriendRequestToMe 获取我收到的的好友申请
|
||||
func (f *FriendController) FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation2.FriendRequestModel, err error) {
|
||||
func (f *FriendController) FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, total int64, err error) {
|
||||
return f.database.FindFriendRequestToMe(ctx, userID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
// FindFriends 获取某人指定好友的信息
|
||||
func (f *FriendController) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation2.FriendModel, err error) {
|
||||
func (f *FriendController) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*table.FriendModel, err error) {
|
||||
return f.database.FindFriends(ctx, ownerUserID, friendUserIDs)
|
||||
|
||||
}
|
||||
|
||||
type FriendDatabaseInterface interface {
|
||||
@ -97,25 +111,25 @@ type FriendDatabaseInterface interface {
|
||||
// AddFriendRequest 增加或者更新好友申请
|
||||
AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error)
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
BecomeFriend(ctx context.Context, friends []*relation2.FriendModel) (err error)
|
||||
BecomeFriend(ctx context.Context, ownerUserID string, friends []*table.FriendModel) (err error)
|
||||
// RefuseFriendRequest 拒绝好友申请
|
||||
RefuseFriendRequest(ctx context.Context, friendRequest *relation2.FriendRequestModel) (err error)
|
||||
RefuseFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error)
|
||||
// AgreeFriendRequest 同意好友申请
|
||||
AgreeFriendRequest(ctx context.Context, friendRequest *relation2.FriendRequestModel) (err error)
|
||||
AgreeFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error)
|
||||
// Delete 删除好友
|
||||
Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error)
|
||||
// UpdateRemark 更新好友备注
|
||||
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
|
||||
// FindOwnerFriends 获取ownerUserID的好友列表
|
||||
FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation2.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 []*relation2.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 []*relation2.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 []*relation2.FriendRequestModel, err error)
|
||||
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 []*relation2.FriendModel, err error)
|
||||
FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*table.FriendModel, err error)
|
||||
}
|
||||
|
||||
type FriendDatabase struct {
|
||||
@ -147,7 +161,7 @@ func (f *FriendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUse
|
||||
}
|
||||
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
func (f *FriendDatabase) BecomeFriend(ctx context.Context, ownerUserID string, friends []*relation2.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))
|
||||
@ -158,51 +172,117 @@ func (f *FriendDatabase) BecomeFriend(ctx context.Context, ownerUserID string, f
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fs1 = append(fs1, friends...)
|
||||
fs11 := utils.DistinctAny(fs1, func(e *table.FriendModel) string {
|
||||
return utils.UniqueJoin(e.OwnerUserID, e.FriendUserID)
|
||||
})
|
||||
err = f.friend.Create(ctx, fs11, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fs2, err := f.friend.FindReversalFriends(ctx, ownerUserID, friendUserIDs, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, v := range friends {
|
||||
fs2 = append(fs2, &table.FriendModel{OwnerUserID: v.FriendUserID, FriendUserID: ownerUserID})
|
||||
}
|
||||
fs22 := utils.DistinctAny(fs2, func(e *table.FriendModel) string {
|
||||
return utils.UniqueJoin(e.OwnerUserID, e.FriendUserID)
|
||||
})
|
||||
err = f.friend.Create(ctx, fs22, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// RefuseFriendRequest 拒绝好友申请
|
||||
func (f *FriendDatabase) RefuseFriendRequest(ctx context.Context, friendRequest *relation2.FriendRequestModel) (err error) {
|
||||
func (f *FriendDatabase) RefuseFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error) {
|
||||
friendRequest.HandleResult = constant.FriendResponseRefuse
|
||||
err = f.friendRequest.Update(ctx, []*table.FriendRequestModel{friendRequest})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AgreeFriendRequest 同意好友申请
|
||||
func (f *FriendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest *relation2.FriendRequestModel) (err error) {
|
||||
func (f *FriendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error) {
|
||||
return f.friend.DB.Transaction(func(tx *gorm.DB) error {
|
||||
//先find 找出重复的 去掉重复的
|
||||
fs1, err := f.friend.FindFriends(ctx, friendRequest.FromUserID, []string{friendRequest.ToUserID}, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(fs1) == 0 {
|
||||
err = f.friend.Create(ctx, []*table.FriendModel{
|
||||
&table.FriendModel{
|
||||
OwnerUserID: friendRequest.FromUserID,
|
||||
FriendUserID: friendRequest.ToUserID,
|
||||
OperatorUserID: friendRequest.ToUserID,
|
||||
AddSource: constant.BecomeFriendByApply,
|
||||
}}, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fs2, err := f.friend.FindReversalFriends(ctx, friendRequest.ToUserID, []string{friendRequest.FromUserID}, tx)
|
||||
if len(fs2) == 0 {
|
||||
err = f.friend.Create(ctx, []*table.FriendModel{
|
||||
&table.FriendModel{
|
||||
OwnerUserID: friendRequest.ToUserID,
|
||||
FriendUserID: friendRequest.FromUserID,
|
||||
OperatorUserID: friendRequest.ToUserID,
|
||||
AddSource: constant.BecomeFriendByApply,
|
||||
}}, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
friendRequest.HandleResult = constant.FriendResponseAgree
|
||||
err = f.friendRequest.Update(ctx, []*table.FriendRequestModel{friendRequest}, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Delete 删除好友
|
||||
func (f *FriendDatabase) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) {
|
||||
func (f *FriendDatabase) Delete(ctx context.Context, ownerUserID string, friendUserID string) (err error) {
|
||||
return f.friend.Delete(ctx, ownerUserID, friendUserID)
|
||||
}
|
||||
|
||||
// UpdateRemark 更新好友备注
|
||||
func (f *FriendDatabase) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
|
||||
return f.friend.UpdateRemark(ctx, ownerUserID, friendUserID, remark)
|
||||
}
|
||||
|
||||
// FindOwnerFriends 获取ownerUserID的好友列表
|
||||
func (f *FriendDatabase) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation2.FriendModel, err error) {
|
||||
func (f *FriendDatabase) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, total int64, err error) {
|
||||
return f.friend.FindOwnerFriends(ctx, ownerUserID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
// FindInWhoseFriends friendUserID在哪些人的好友列表中
|
||||
func (f *FriendDatabase) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation2.FriendModel, err error) {
|
||||
func (f *FriendDatabase) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, total int64, err error) {
|
||||
return f.friend.FindInWhoseFriends(ctx, friendUserID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
// FindFriendRequestFromMe 获取我发出去的好友申请
|
||||
func (f *FriendDatabase) FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation2.FriendRequestModel, err error) {
|
||||
func (f *FriendDatabase) FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, total int64, err error) {
|
||||
return f.friendRequest.FindFromUserID(ctx, userID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
// FindFriendRequestToMe 获取我收到的的好友申请
|
||||
func (f *FriendDatabase) FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation2.FriendRequestModel, err error) {
|
||||
func (f *FriendDatabase) FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, total int64, err error) {
|
||||
return f.friendRequest.FindToUserID(ctx, userID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
// FindFriends 获取某人指定好友的信息 如果有一个不存在也返回错误
|
||||
func (f *FriendDatabase) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation2.FriendModel, err error) {
|
||||
friends, err = f.friend.Find(ctx, ownerUserID, friendUserIDs)
|
||||
// FindFriends 获取某人指定好友的信息 如果有一个不存在,也返回错误
|
||||
func (f *FriendDatabase) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*table.FriendModel, err error) {
|
||||
friends, err = f.friend.FindFriends(ctx, ownerUserID, friendUserIDs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/db/table"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
@ -9,12 +9,12 @@ import (
|
||||
)
|
||||
|
||||
type FriendDB interface {
|
||||
Create(ctx context.Context, friends []*relation.FriendModel) (err error)
|
||||
Create(ctx context.Context, friends []*table.FriendModel) (err error)
|
||||
Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error)
|
||||
UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error)
|
||||
Update(ctx context.Context, friends []*relation.FriendModel) (err error)
|
||||
Update(ctx context.Context, friends []*table.FriendModel) (err error)
|
||||
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
|
||||
FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*relation.FriendModel, err error)
|
||||
FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*table.FriendModel, err error)
|
||||
}
|
||||
|
||||
type FriendGorm struct {
|
||||
@ -30,18 +30,18 @@ type FriendUser struct {
|
||||
Nickname string `gorm:"column:name;size:255"`
|
||||
}
|
||||
|
||||
func (f *FriendGorm) Create(ctx context.Context, friends []*relation.FriendModel, tx ...*gorm.DB) (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(getDBConn(f.DB, tx).Model(&relation.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) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserIDs", friendUserIDs)
|
||||
}()
|
||||
err = utils.Wrap(f.DB.Model(&relation.FriendModel{}).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserIDs).Delete(&relation.FriendModel{}).Error, "")
|
||||
err = utils.Wrap(f.DB.Model(&table.FriendModel{}).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserIDs).Delete(&table.FriendModel{}).Error, "")
|
||||
return err
|
||||
}
|
||||
|
||||
@ -49,62 +49,86 @@ func (f *FriendGorm) UpdateByMap(ctx context.Context, ownerUserID string, args m
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "args", args)
|
||||
}()
|
||||
return utils.Wrap(f.DB.Model(&relation.FriendModel{}).Where("owner_user_id = ?", ownerUserID).Updates(args).Error, "")
|
||||
return utils.Wrap(f.DB.Model(&table.FriendModel{}).Where("owner_user_id = ?", ownerUserID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendGorm) Update(ctx context.Context, friends []*relation.FriendModel, tx ...*gorm.DB) (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)
|
||||
}()
|
||||
return utils.Wrap(f.DB.Model(&relation.FriendModel{}).Updates(&friends).Error, "")
|
||||
return utils.Wrap(f.DB.Model(&table.FriendModel{}).Updates(&friends).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendGorm) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "remark", remark)
|
||||
}()
|
||||
return utils.Wrap(f.DB.Model(&relation.FriendModel{}).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserID).Update("remark", remark).Error, "")
|
||||
return utils.Wrap(f.DB.Model(&table.FriendModel{}).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserID).Update("remark", remark).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendGorm) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*relation.FriendModel, err error) {
|
||||
func (f *FriendGorm) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*table.FriendModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(f.DB.Model(&relation.FriendModel{}).Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "")
|
||||
return friends, utils.Wrap(f.DB.Model(&table.FriendModel{}).Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendGorm) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*relation.FriendModel, err error) {
|
||||
func (f *FriendGorm) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*table.FriendModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friendUserID", friendUserID, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(f.DB.Model(&relation.FriendModel{}).Where("friend_user_id = ?", friendUserID).Find(&friends).Error, "")
|
||||
return friends, utils.Wrap(f.DB.Model(&table.FriendModel{}).Where("friend_user_id = ?", friendUserID).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendGorm) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *relation.FriendModel, err error) {
|
||||
friend = &relation.FriendModel{}
|
||||
func (f *FriendGorm) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *table.FriendModel, err error) {
|
||||
friend = &table.FriendModel{}
|
||||
defer tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "friend", friend)
|
||||
return friend, utils.Wrap(f.DB.Model(&relation.FriendModel{}).Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error, "")
|
||||
return friend, utils.Wrap(f.DB.Model(&table.FriendModel{}).Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendGorm) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*relation.FriendModel, err error) {
|
||||
func (f *FriendGorm) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*table.FriendModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "userID1", userID1, "userID2", userID2)
|
||||
}()
|
||||
return friends, utils.Wrap(f.DB.Model(&relation.FriendModel{}).Where("(owner_user_id = ? and friend_user_id = ?) or (owner_user_id = ? and friend_user_id = ?)", userID1, userID2, userID2, userID1).Find(&friends).Error, "")
|
||||
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 []*relation.FriendModel, err 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 []*relation.FriendModel, err 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, "")
|
||||
}
|
||||
|
||||
func (f *FriendGorm) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32, tx ...*gorm.DB) (friends []*table.FriendModel, total int64, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "pageNumber", pageNumber, "showNumber", showNumber, "friends", friends, "total", total)
|
||||
}()
|
||||
err = getDBConn(f.DB, tx).Model(f).Where("owner_user_id = ? ", ownerUserID).Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, utils.Wrap(err, "")
|
||||
}
|
||||
err = utils.Wrap(getDBConn(f.DB, tx).Model(f).Where("owner_user_id = ? ", ownerUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&friends).Error, "")
|
||||
return
|
||||
}
|
||||
|
||||
func (f *FriendGorm) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32, tx ...*gorm.DB) (friends []*table.FriendModel, total int64, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friendUserID", friendUserID, "pageNumber", pageNumber, "showNumber", showNumber, "friends", friends, "total", total)
|
||||
}()
|
||||
err = getDBConn(f.DB, tx).Model(f).Where("friend_user_id = ? ", friendUserID).Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, utils.Wrap(err, "")
|
||||
}
|
||||
err = utils.Wrap(getDBConn(f.DB, tx).Model(f).Where("friend_user_id = ? ", friendUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&friends).Error, "")
|
||||
return
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package relation
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/db/table"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
@ -20,57 +20,69 @@ type FriendRequestGorm struct {
|
||||
DB *gorm.DB `gorm:"-"`
|
||||
}
|
||||
|
||||
func (f *FriendRequestGorm) Create(ctx context.Context, friends []*relation.FriendRequestModel) (err error) {
|
||||
func (f *FriendRequestGorm) Create(ctx context.Context, friends []*table.FriendRequestModel) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
|
||||
}()
|
||||
return utils.Wrap(f.DB.Model(&relation.FriendRequestModel{}).Create(&friends).Error, "")
|
||||
return utils.Wrap(f.DB.Model(&table.FriendRequestModel{}).Create(&friends).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendRequestGorm) Delete(ctx context.Context, fromUserID, toUserID string) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "toUserID", toUserID)
|
||||
}()
|
||||
return utils.Wrap(f.DB.Model(&relation.FriendRequestModel{}).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Delete(&relation.FriendRequestModel{}).Error, "")
|
||||
return utils.Wrap(f.DB.Model(&table.FriendRequestModel{}).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Delete(&table.FriendRequestModel{}).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendRequestGorm) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "args", args)
|
||||
}()
|
||||
return utils.Wrap(f.DB.Model(&relation.FriendRequestModel{}).Where("owner_user_id = ?", ownerUserID).Updates(args).Error, "")
|
||||
return utils.Wrap(f.DB.Model(&table.FriendRequestModel{}).Where("owner_user_id = ?", ownerUserID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendRequestGorm) Update(ctx context.Context, friends []*relation.FriendRequestModel) (err error) {
|
||||
func (f *FriendRequestGorm) Update(ctx context.Context, friendRequests []*table.FriendRequestModel, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friendRequests", friendRequests)
|
||||
}()
|
||||
return utils.Wrap(f.DB.Model(&relation.FriendRequestModel{}).Updates(&friends).Error, "")
|
||||
return utils.Wrap(getDBConn(f.DB, tx).Model(&table.FriendRequestModel{}).Updates(&friendRequests).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendRequestGorm) Find(ctx context.Context, ownerUserID string) (friends []*relation.FriendRequestModel, err error) {
|
||||
func (f *FriendRequestGorm) Find(ctx context.Context, ownerUserID string) (friends []*table.FriendRequestModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(f.DB.Model(&relation.FriendRequestModel{}).Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "")
|
||||
return friends, utils.Wrap(f.DB.Model(&table.FriendRequestModel{}).Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendRequestGorm) Take(ctx context.Context, fromUserID, toUserID string) (friend *relation.FriendRequestModel, err error) {
|
||||
friend = &relation.FriendRequestModel{}
|
||||
func (f *FriendRequestGorm) Take(ctx context.Context, fromUserID, toUserID string) (friend *table.FriendRequestModel, err error) {
|
||||
friend = &table.FriendRequestModel{}
|
||||
defer tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "toUserID", toUserID, "friend", friend)
|
||||
return friend, utils.Wrap(f.DB.Model(&relation.FriendRequestModel{}).Where("from_user_id = ? and to_user_id", fromUserID, toUserID).Take(friend).Error, "")
|
||||
return friend, utils.Wrap(f.DB.Model(&table.FriendRequestModel{}).Where("from_user_id = ? and to_user_id", fromUserID, toUserID).Take(friend).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendRequestGorm) FindToUserID(ctx context.Context, toUserID string) (friends []*relation.FriendRequestModel, err error) {
|
||||
func (f *FriendRequestGorm) FindToUserID(ctx context.Context, toUserID string, pageNumber, showNumber int32, tx ...*gorm.DB) (friends []*table.FriendRequestModel, total int64, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "toUserID", toUserID, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(f.DB.Model(&relation.FriendRequestModel{}).Where("to_user_id = ?", toUserID).Find(&friends).Error, "")
|
||||
|
||||
err = getDBConn(f.DB, tx).Model(&table.FriendRequestModel{}).Where("to_user_id = ? ", toUserID).Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, utils.Wrap(err, "")
|
||||
}
|
||||
err = utils.Wrap(getDBConn(f.DB, tx).Model(&table.FriendRequestModel{}).Where("to_user_id = ? ", toUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&friends).Error, "")
|
||||
return
|
||||
}
|
||||
|
||||
func (f *FriendRequestGorm) FindFromUserID(ctx context.Context, fromUserID string) (friends []*relation.FriendRequestModel, err error) {
|
||||
func (f *FriendRequestGorm) FindFromUserID(ctx context.Context, fromUserID string, pageNumber, showNumber int32, tx ...*gorm.DB) (friends []*table.FriendRequestModel, total int64, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(f.DB.Model(&relation.FriendRequestModel{}).Where("from_user_id = ?", fromUserID).Find(&friends).Error, "")
|
||||
|
||||
err = getDBConn(f.DB, tx).Model(&table.FriendRequestModel{}).Where("from_user_id = ? ", fromUserID).Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, utils.Wrap(err, "")
|
||||
}
|
||||
err = utils.Wrap(getDBConn(f.DB, tx).Model(&table.FriendRequestModel{}).Where("from_user_id = ? ", fromUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&friends).Error, "")
|
||||
return
|
||||
}
|
||||
|
@ -89,6 +89,26 @@ func Contain[E comparable](es []E, e E) bool {
|
||||
return IndexOf(es, e) >= 0
|
||||
}
|
||||
|
||||
// DuplicateAny judge whether it is repeated
|
||||
func DuplicateAny[E any, K comparable](es []E, fn func(e E) K) bool {
|
||||
t := make(map[K]struct{})
|
||||
for _, e := range es {
|
||||
k := fn(e)
|
||||
if _, ok := t[k]; ok {
|
||||
return true
|
||||
}
|
||||
t[k] = struct{}{}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Duplicate judge whether it is repeated
|
||||
func Duplicate[E comparable](es []E) bool {
|
||||
return DuplicateAny(es, func(e E) E {
|
||||
return e
|
||||
})
|
||||
}
|
||||
|
||||
// SliceToMapOkAny slice to map
|
||||
func SliceToMapOkAny[E any, K comparable, V any](es []E, fn func(e E) (K, V, bool)) map[K]V {
|
||||
kv := make(map[K]V)
|
||||
@ -224,6 +244,22 @@ func CompleteAny[K comparable, E any](ks []K, es []E, fn func(e E) K) bool {
|
||||
return len(a) == 0
|
||||
}
|
||||
|
||||
func Complete[E comparable](a []E, b []E) bool {
|
||||
if len(a) == 0 && len(b) == 0 {
|
||||
return true
|
||||
}
|
||||
if (len(a) == 0 && len(b) != 0) || (len(a) != 0 && len(b) == 0) {
|
||||
return false
|
||||
}
|
||||
t := SliceSet(a)
|
||||
for _, e := range b {
|
||||
if _, ok := t[e]; !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// MapKey get map keys
|
||||
func MapKey[K comparable, V any](kv map[K]V) []K {
|
||||
ks := make([]K, 0, len(kv))
|
||||
@ -270,6 +306,25 @@ func If[T any](isa bool, a, b T) T {
|
||||
return b
|
||||
}
|
||||
|
||||
func Equal[E comparable](a []E, b []E) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(a); i++ {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Single
|
||||
|
||||
func Single[E comparable](a, b []E) []E {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func UniqueJoin(s ...string) string {
|
||||
data, _ := json.Marshal(s)
|
||||
return string(data)
|
||||
|
Loading…
x
Reference in New Issue
Block a user