Files
open-im-server/pkg/common/db/controller/friend.go
T

252 lines
11 KiB
Go
Raw Normal View History

2023-01-28 13:19:36 +08:00
package controller
2023-01-17 17:31:22 +08:00
import (
"context"
2023-03-17 13:44:30 +08:00
"time"
2023-03-16 20:21:10 +08:00
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
2023-03-23 19:02:20 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/tx"
2023-03-21 12:28:21 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2023-01-17 17:31:22 +08:00
"gorm.io/gorm"
)
2023-02-22 12:17:59 +08:00
type FriendDatabase interface {
2023-02-07 20:28:34 +08:00
// 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
2023-02-02 16:39:29 +08:00
CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error)
2023-02-07 20:28:34 +08:00
// 增加或者更新好友申请
2023-01-31 13:37:35 +08:00
AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error)
2023-02-07 20:28:34 +08:00
// 先判断是否在好友表,如果在则不插入
2023-03-16 11:03:14 +08:00
BecomeFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, addSource int32) (err error)
2023-02-07 20:28:34 +08:00
// 拒绝好友申请
2023-02-03 16:51:35 +08:00
RefuseFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error)
2023-02-07 20:28:34 +08:00
// 同意好友申请
2023-02-03 16:51:35 +08:00
AgreeFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error)
2023-02-07 20:28:34 +08:00
// 删除好友
Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error)
// 更新好友备注
2023-01-30 11:34:07 +08:00
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
2023-02-07 20:28:34 +08:00
// 获取ownerUserID的好友列表
2023-02-09 13:02:10 +08:00
PageOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
2023-02-07 20:28:34 +08:00
// friendUserID在哪些人的好友列表中
2023-02-09 13:02:10 +08:00
PageInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
2023-02-07 20:28:34 +08:00
// 获取我发出去的好友申请
2023-02-09 13:02:10 +08:00
PageFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
2023-02-07 20:28:34 +08:00
// 获取我收到的的好友申请
2023-02-09 13:02:10 +08:00
PageFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
2023-02-07 20:28:34 +08:00
// 获取某人指定好友的信息
2023-02-09 13:05:12 +08:00
FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error)
2023-03-06 12:07:25 +08:00
FindFriendUserIDs(ctx context.Context, ownerUserID string) (friendUserIDs []string, err error)
2023-01-17 17:31:22 +08:00
}
2023-02-22 12:17:59 +08:00
type friendDatabase struct {
2023-02-21 12:59:44 +08:00
friend relation.FriendModelInterface
friendRequest relation.FriendRequestModelInterface
tx tx.Tx
2023-03-23 19:02:20 +08:00
cache cache.FriendCache
2023-01-17 17:31:22 +08:00
}
2023-03-23 19:02:20 +08:00
func NewFriendDatabase(friend relation.FriendModelInterface, friendRequest relation.FriendRequestModelInterface, cache cache.FriendCache, tx tx.Tx) FriendDatabase {
return &friendDatabase{friend: friend, friendRequest: friendRequest, cache: cache, tx: tx}
2023-01-30 11:34:07 +08:00
}
2023-02-07 20:28:34 +08:00
// ok 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
2023-02-22 12:17:59 +08:00
func (f *friendDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Friends bool, inUser2Friends bool, err error) {
2023-05-16 16:31:35 +08:00
userID1FriendIDs, err := f.cache.GetFriendIDs(ctx, userID1)
2023-02-07 20:28:34 +08:00
if err != nil {
2023-05-16 16:31:35 +08:00
return
2023-02-07 20:28:34 +08:00
}
2023-05-16 16:31:35 +08:00
userID2FriendIDs, err := f.cache.GetFriendIDs(ctx, userID2)
if err != nil {
return
2023-02-02 16:39:29 +08:00
}
2023-05-16 16:31:35 +08:00
return utils.IsContain(userID2, userID1FriendIDs), utils.IsContain(userID1, userID2FriendIDs), nil
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
2023-02-07 20:28:34 +08:00
// 增加或者更新好友申请 如果之前有记录则更新,没有记录则新增
2023-02-22 12:17:59 +08:00
func (f *friendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error) {
2023-02-21 12:59:44 +08:00
return f.tx.Transaction(func(tx any) error {
_, err := f.friendRequest.NewTx(tx).Take(ctx, fromUserID, toUserID)
2023-02-07 20:28:34 +08:00
//有db错误
2023-03-17 13:44:30 +08:00
if err != nil && errs.Unwrap(err) != gorm.ErrRecordNotFound {
2023-02-03 16:51:35 +08:00
return err
}
2023-02-07 20:28:34 +08:00
//无错误 则更新
if err == nil {
2023-02-03 16:51:35 +08:00
m := make(map[string]interface{}, 1)
m["handle_result"] = 0
m["handle_msg"] = ""
m["req_msg"] = reqMsg
m["ex"] = ex
2023-02-21 12:59:44 +08:00
if err := f.friendRequest.NewTx(tx).UpdateByMap(ctx, fromUserID, toUserID, m); err != nil {
2023-02-03 16:51:35 +08:00
return err
}
2023-02-07 20:28:34 +08:00
return nil
}
//gorm.ErrRecordNotFound 错误,则新增
2023-03-17 14:19:53 +08:00
if err := f.friendRequest.NewTx(tx).Create(ctx, []*relation.FriendRequestModel{{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex, CreateTime: time.Now(), HandleTime: time.Unix(0, 0)}}); err != nil {
2023-02-07 20:28:34 +08:00
return err
2023-02-03 16:51:35 +08:00
}
return nil
})
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
2023-02-07 20:28:34 +08:00
// (1)先判断是否在好友表 (在不在都不返回错误) (2)对于不在好友列表的 插入即可
2023-03-16 11:03:14 +08:00
func (f *friendDatabase) BecomeFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, addSource int32) (err error) {
2023-05-16 16:31:35 +08:00
cache := f.cache.NewCache()
if err := f.tx.Transaction(func(tx any) error {
2023-02-02 16:39:29 +08:00
//先find 找出重复的 去掉重复的
2023-02-21 12:59:44 +08:00
fs1, err := f.friend.NewTx(tx).FindFriends(ctx, ownerUserID, friendUserIDs)
2023-02-02 16:39:29 +08:00
if err != nil {
return err
}
2023-03-21 12:28:21 +08:00
opUserID := mcontext.GetOperationID(ctx)
2023-02-07 20:28:34 +08:00
for _, v := range friendUserIDs {
2023-03-16 11:03:14 +08:00
fs1 = append(fs1, &relation.FriendModel{OwnerUserID: ownerUserID, FriendUserID: v, AddSource: addSource, OperatorUserID: opUserID})
2023-02-07 20:28:34 +08:00
}
2023-02-03 16:51:35 +08:00
fs11 := utils.DistinctAny(fs1, func(e *relation.FriendModel) string {
2023-02-07 20:28:34 +08:00
return e.FriendUserID
2023-02-03 16:10:33 +08:00
})
2023-02-07 20:28:34 +08:00
2023-02-21 12:59:44 +08:00
err = f.friend.NewTx(tx).Create(ctx, fs11)
2023-02-03 16:10:33 +08:00
if err != nil {
return err
}
2023-02-21 12:59:44 +08:00
fs2, err := f.friend.NewTx(tx).FindReversalFriends(ctx, ownerUserID, friendUserIDs)
2023-02-02 16:39:29 +08:00
if err != nil {
return err
}
2023-03-23 19:02:20 +08:00
var newFriendIDs []string
2023-02-07 20:28:34 +08:00
for _, v := range friendUserIDs {
2023-03-16 11:03:14 +08:00
fs2 = append(fs2, &relation.FriendModel{OwnerUserID: v, FriendUserID: ownerUserID, AddSource: addSource, OperatorUserID: opUserID})
2023-03-23 19:02:20 +08:00
newFriendIDs = append(newFriendIDs, v)
2023-02-03 16:10:33 +08:00
}
2023-02-03 16:51:35 +08:00
fs22 := utils.DistinctAny(fs2, func(e *relation.FriendModel) string {
2023-02-07 20:28:34 +08:00
return e.OwnerUserID
2023-02-03 16:10:33 +08:00
})
2023-02-21 12:59:44 +08:00
err = f.friend.NewTx(tx).Create(ctx, fs22)
2023-02-03 16:10:33 +08:00
if err != nil {
return err
}
2023-03-23 19:02:20 +08:00
newFriendIDs = append(newFriendIDs, ownerUserID)
2023-05-16 16:31:35 +08:00
cache = cache.DelFriendIDs(newFriendIDs...)
return nil
}); err != nil {
return nil
}
return cache.ExecDel(ctx)
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
2023-02-07 20:28:34 +08:00
// 拒绝好友申请 (1)检查是否有申请记录且为未处理状态 (没有记录返回错误) (2)修改申请记录 已拒绝
2023-02-22 12:17:59 +08:00
func (f *friendDatabase) RefuseFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
2023-04-19 20:06:20 +08:00
fr, err := f.friendRequest.Take(ctx, friendRequest.FromUserID, friendRequest.ToUserID)
2023-02-07 20:28:34 +08:00
if err != nil {
return err
}
2023-04-19 20:06:20 +08:00
if fr.HandleResult != 0 {
return errs.ErrArgs.Wrap("the friend request has been processed")
}
2023-02-03 16:10:33 +08:00
friendRequest.HandleResult = constant.FriendResponseRefuse
2023-03-17 16:01:56 +08:00
friendRequest.HandleTime = time.Now()
2023-03-17 14:59:32 +08:00
err = f.friendRequest.Update(ctx, friendRequest)
2023-02-03 16:10:33 +08:00
if err != nil {
return err
}
return nil
2023-01-31 13:37:35 +08:00
}
2023-04-19 20:06:20 +08:00
// AgreeFriendRequest 同意好友申请 (1)检查是否有申请记录且为未处理状态 (没有记录返回错误) (2)检查是否好友(不返回错误) (3) 建立双向好友关系(存在的忽略)
2023-02-22 12:17:59 +08:00
func (f *friendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
2023-02-21 12:59:44 +08:00
return f.tx.Transaction(func(tx any) error {
2023-04-19 19:13:31 +08:00
fr, err := f.friendRequest.NewTx(tx).Take(ctx, friendRequest.FromUserID, friendRequest.ToUserID)
2023-02-07 20:28:34 +08:00
if err != nil {
return err
}
2023-04-19 20:06:20 +08:00
if fr.HandleResult != 0 {
return errs.ErrArgs.Wrap("the friend request has been processed")
}
2023-04-19 19:13:31 +08:00
friendRequest.HandlerUserID = mcontext.GetOpUserID(ctx)
2023-02-07 20:28:34 +08:00
friendRequest.HandleResult = constant.FriendResponseAgree
2023-03-17 16:01:56 +08:00
friendRequest.HandleTime = time.Now()
2023-03-17 14:59:32 +08:00
err = f.friendRequest.NewTx(tx).Update(ctx, friendRequest)
2023-02-07 20:28:34 +08:00
if err != nil {
return err
}
2023-04-19 19:13:31 +08:00
exists, err := f.friend.NewTx(tx).FindUserState(ctx, friendRequest.FromUserID, friendRequest.ToUserID)
2023-02-07 20:28:34 +08:00
if err != nil {
return err
2023-02-03 16:10:33 +08:00
}
2023-04-19 19:13:31 +08:00
existsMap := utils.SliceSet(utils.Slice(exists, func(friend *relation.FriendModel) [2]string {
return [...]string{friend.OwnerUserID, friend.FriendUserID} // 自己 - 好友
}))
var adds []*relation.FriendModel
if _, ok := existsMap[[...]string{friendRequest.ToUserID, friendRequest.FromUserID}]; !ok { // 自己 - 好友
adds = append(adds, &relation.FriendModel{OwnerUserID: friendRequest.ToUserID, FriendUserID: friendRequest.FromUserID, AddSource: int32(constant.BecomeFriendByApply), OperatorUserID: friendRequest.FromUserID})
2023-02-07 20:28:34 +08:00
}
2023-04-19 19:13:31 +08:00
if _, ok := existsMap[[...]string{friendRequest.FromUserID, friendRequest.ToUserID}]; !ok { // 好友 - 自己
adds = append(adds, &relation.FriendModel{OwnerUserID: friendRequest.FromUserID, FriendUserID: friendRequest.ToUserID, AddSource: int32(constant.BecomeFriendByApply), OperatorUserID: friendRequest.FromUserID})
2023-02-07 20:28:34 +08:00
}
2023-04-19 19:13:31 +08:00
if len(adds) > 0 {
if err := f.friend.NewTx(tx).Create(ctx, adds); err != nil {
return err
}
2023-02-03 16:10:33 +08:00
}
2023-04-19 19:13:31 +08:00
return f.cache.DelFriendIDs(friendRequest.ToUserID, friendRequest.FromUserID).ExecDel(ctx)
2023-02-03 16:10:33 +08:00
})
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
2023-02-07 20:28:34 +08:00
// 删除好友 外部判断是否好友关系
2023-02-22 12:17:59 +08:00
func (f *friendDatabase) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error) {
2023-05-16 16:31:35 +08:00
if err := f.friend.Delete(ctx, ownerUserID, friendUserIDs); err != nil {
return err
}
return f.cache.DelFriendIDs(append(friendUserIDs, ownerUserID)...).ExecDel(ctx)
2023-01-31 13:37:35 +08:00
}
2023-02-07 20:28:34 +08:00
// 更新好友备注 零值也支持
2023-02-22 12:17:59 +08:00
func (f *friendDatabase) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
2023-05-16 16:31:35 +08:00
if err := f.friend.UpdateRemark(ctx, ownerUserID, friendUserID, remark); err != nil {
return err
}
return f.cache.DelFriend(ownerUserID, friendUserID).ExecDel(ctx)
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
2023-02-07 20:28:34 +08:00
// 获取ownerUserID的好友列表 无结果不返回错误
2023-02-22 12:17:59 +08:00
func (f *friendDatabase) PageOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
2023-02-03 16:10:33 +08:00
return f.friend.FindOwnerFriends(ctx, ownerUserID, pageNumber, showNumber)
2023-01-31 13:37:35 +08:00
}
2023-02-07 20:28:34 +08:00
// friendUserID在哪些人的好友列表中
2023-02-22 12:17:59 +08:00
func (f *friendDatabase) PageInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
2023-02-03 16:10:33 +08:00
return f.friend.FindInWhoseFriends(ctx, friendUserID, pageNumber, showNumber)
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
2023-02-07 20:28:34 +08:00
// 获取我发出去的好友申请 无结果不返回错误
2023-02-22 12:17:59 +08:00
func (f *friendDatabase) PageFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
2023-02-03 16:10:33 +08:00
return f.friendRequest.FindFromUserID(ctx, userID, pageNumber, showNumber)
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
2023-02-07 20:28:34 +08:00
// 获取我收到的的好友申请 无结果不返回错误
2023-02-22 12:17:59 +08:00
func (f *friendDatabase) PageFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
2023-02-03 16:10:33 +08:00
return f.friendRequest.FindToUserID(ctx, userID, pageNumber, showNumber)
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
2023-02-07 20:28:34 +08:00
// 获取某人指定好友的信息 如果有好友不存在,也返回错误
2023-02-22 12:17:59 +08:00
func (f *friendDatabase) FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error) {
2023-02-03 16:10:33 +08:00
friends, err = f.friend.FindFriends(ctx, ownerUserID, friendUserIDs)
2023-02-02 16:39:29 +08:00
if err != nil {
return
}
if len(friends) != len(friendUserIDs) {
2023-03-07 12:19:30 +08:00
err = errs.ErrRecordNotFound.Wrap()
2023-02-02 16:39:29 +08:00
}
return
2023-01-17 17:31:22 +08:00
}
2023-03-06 12:04:44 +08:00
2023-03-06 12:07:25 +08:00
func (f *friendDatabase) FindFriendUserIDs(ctx context.Context, ownerUserID string) (friendUserIDs []string, err error) {
2023-03-23 19:02:20 +08:00
return f.cache.GetFriendIDs(ctx, ownerUserID)
2023-03-06 12:04:44 +08:00
}