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

214 lines
10 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 (
2023-02-02 16:39:29 +08:00
"Open_IM/pkg/common/constant"
2023-01-28 13:19:36 +08:00
"Open_IM/pkg/common/db/relation"
2023-02-01 16:19:44 +08:00
"Open_IM/pkg/common/db/table"
2023-01-17 17:31:22 +08:00
"context"
"gorm.io/gorm"
)
2023-01-30 11:34:07 +08:00
type FriendInterface interface {
2023-01-31 11:34:30 +08:00
// CheckIn 检查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-01-30 21:47:29 +08:00
// AddFriendRequest 增加或者更新好友申请
AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error)
2023-01-31 13:37:35 +08:00
// BecomeFriend 先判断是否在好友表,如果在则不插入
2023-02-02 16:39:29 +08:00
BecomeFriend(ctx context.Context, friends []*table.FriendModel, revFriends []*table.FriendModel) (err error)
2023-01-31 13:37:35 +08:00
// RefuseFriendRequest 拒绝好友申请
2023-02-01 16:44:50 +08:00
RefuseFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error)
2023-01-31 13:37:35 +08:00
// AgreeFriendRequest 同意好友申请
2023-02-01 16:44:50 +08:00
AgreeFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error)
2023-01-31 13:37:35 +08:00
// Delete 删除好友
2023-01-30 11:34:07 +08:00
Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error)
2023-01-31 13:37:35 +08:00
// UpdateRemark 更新好友备注
2023-01-30 11:34:07 +08:00
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
2023-01-31 13:37:35 +08:00
// FindOwnerFriends 获取ownerUserID的好友列表
2023-02-02 16:39:29 +08:00
FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, total int64, err error)
2023-01-31 13:37:35 +08:00
// FindInWhoseFriends friendUserID在哪些人的好友列表中
2023-02-02 16:39:29 +08:00
FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, total int64, err error)
2023-01-31 13:37:35 +08:00
// FindFriendRequestFromMe 获取我发出去的好友申请
2023-02-02 16:39:29 +08:00
FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, total int64, err error)
2023-01-31 13:37:35 +08:00
// FindFriendRequestToMe 获取我收到的的好友申请
2023-02-02 16:39:29 +08:00
FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, total int64, err error)
// FindFriends 获取某人指定好友的信息 如果有一个不存在也返回错误
2023-02-01 16:44:50 +08:00
FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*table.FriendModel, err error)
2023-01-17 17:31:22 +08:00
}
2023-01-30 11:34:07 +08:00
type FriendController struct {
database FriendDatabaseInterface
2023-01-17 17:31:22 +08:00
}
2023-01-30 11:34:07 +08:00
func NewFriendController(db *gorm.DB) *FriendController {
return &FriendController{database: NewFriendDatabase(db)}
2023-01-17 17:31:22 +08:00
}
2023-01-31 13:37:35 +08:00
// 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) {
2023-01-17 17:31:22 +08:00
}
2023-01-31 13:37:35 +08:00
// AddFriendRequest 增加或者更新好友申请
func (f *FriendController) AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error) {
}
// BecomeFriend 先判断是否在好友表,如果在则不插入
2023-02-01 16:44:50 +08:00
func (f *FriendController) BecomeFriend(ctx context.Context, friends []*table.FriendModel) (err error) {
2023-01-17 17:31:22 +08:00
}
2023-01-31 13:37:35 +08:00
// RefuseFriendRequest 拒绝好友申请
2023-02-01 16:44:50 +08:00
func (f *FriendController) RefuseFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error) {
2023-01-17 17:31:22 +08:00
}
2023-01-31 13:37:35 +08:00
// AgreeFriendRequest 同意好友申请
2023-02-01 16:44:50 +08:00
func (f *FriendController) AgreeFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error) {
2023-01-17 17:31:22 +08:00
}
2023-01-31 13:37:35 +08:00
// Delete 删除好友
func (f *FriendController) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) {
}
// UpdateRemark 更新好友备注
2023-01-30 11:34:07 +08:00
func (f *FriendController) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
}
2023-01-31 13:37:35 +08:00
// FindOwnerFriends 获取ownerUserID的好友列表
2023-02-01 16:44:50 +08:00
func (f *FriendController) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, err error) {
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
// FindInWhoseFriends friendUserID在哪些人的好友列表中
2023-02-01 16:44:50 +08:00
func (f *FriendController) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, err error) {
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
// FindFriendRequestFromMe 获取我发出去的好友申请
2023-02-01 16:44:50 +08:00
func (f *FriendController) FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, err error) {
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
// FindFriendRequestToMe 获取我收到的的好友申请
2023-02-01 16:44:50 +08:00
func (f *FriendController) FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, err error) {
2023-01-31 13:37:35 +08:00
}
// FindFriends 获取某人指定好友的信息
2023-02-01 16:44:50 +08:00
func (f *FriendController) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*table.FriendModel, err error) {
2023-01-17 17:31:22 +08:00
}
2023-01-30 11:34:07 +08:00
type FriendDatabaseInterface interface {
2023-01-31 13:37:35 +08:00
// CheckIn 检查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-01-31 13:37:35 +08:00
// AddFriendRequest 增加或者更新好友申请
AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error)
// BecomeFriend 先判断是否在好友表,如果在则不插入
2023-02-01 16:19:44 +08:00
BecomeFriend(ctx context.Context, friends []*table.FriendModel) (err error)
2023-01-31 13:37:35 +08:00
// RefuseFriendRequest 拒绝好友申请
2023-02-01 16:44:50 +08:00
RefuseFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error)
2023-01-31 13:37:35 +08:00
// AgreeFriendRequest 同意好友申请
2023-02-01 16:44:50 +08:00
AgreeFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error)
2023-01-31 13:37:35 +08:00
// Delete 删除好友
2023-01-30 11:34:07 +08:00
Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error)
2023-01-31 13:37:35 +08:00
// UpdateRemark 更新好友备注
2023-01-30 11:34:07 +08:00
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
2023-01-31 13:37:35 +08:00
// FindOwnerFriends 获取ownerUserID的好友列表
2023-02-01 16:44:50 +08:00
FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, err error)
2023-01-31 13:37:35 +08:00
// FindInWhoseFriends friendUserID在哪些人的好友列表中
2023-02-01 16:44:50 +08:00
FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, err error)
2023-01-31 13:37:35 +08:00
// FindFriendRequestFromMe 获取我发出去的好友申请
2023-02-01 16:44:50 +08:00
FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, err error)
2023-01-31 13:37:35 +08:00
// FindFriendRequestToMe 获取我收到的的好友申请
2023-02-01 16:44:50 +08:00
FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, err error)
2023-01-31 13:37:35 +08:00
// FindFriends 获取某人指定好友的信息
2023-02-01 16:44:50 +08:00
FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*table.FriendModel, err error)
2023-01-17 17:31:22 +08:00
}
2023-01-30 11:34:07 +08:00
type FriendDatabase struct {
2023-02-01 16:44:50 +08:00
friend *relation.FriendGorm
friendRequest *relation.FriendRequestGorm
2023-01-17 17:31:22 +08:00
}
2023-01-30 11:34:07 +08:00
func NewFriendDatabase(db *gorm.DB) *FriendDatabase {
2023-02-01 16:44:50 +08:00
return &FriendDatabase{friend: relation.NewFriendGorm(db), friendRequest: relation.NewFriendRequestGorm(db)}
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
// CheckIn 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
2023-02-02 16:39:29 +08:00
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
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
// AddFriendRequest 增加或者更新好友申请
func (f *FriendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error) {
2023-02-02 16:39:29 +08:00
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
// BecomeFriend 先判断是否在好友表,如果在则不插入
2023-02-02 16:39:29 +08:00
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
})
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
// RefuseFriendRequest 拒绝好友申请
2023-02-01 16:44:50 +08:00
func (f *FriendDatabase) RefuseFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error) {
2023-01-31 13:37:35 +08:00
}
// AgreeFriendRequest 同意好友申请
2023-02-01 16:44:50 +08:00
func (f *FriendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error) {
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
// Delete 删除好友
func (f *FriendDatabase) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) {
}
// UpdateRemark 更新好友备注
2023-01-30 11:34:07 +08:00
func (f *FriendDatabase) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
}
2023-01-31 13:37:35 +08:00
// FindOwnerFriends 获取ownerUserID的好友列表
2023-02-01 16:44:50 +08:00
func (f *FriendDatabase) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, err error) {
2023-01-31 13:37:35 +08:00
}
// FindInWhoseFriends friendUserID在哪些人的好友列表中
2023-02-01 16:44:50 +08:00
func (f *FriendDatabase) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, err error) {
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
// FindFriendRequestFromMe 获取我发出去的好友申请
2023-02-01 16:44:50 +08:00
func (f *FriendDatabase) FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, err error) {
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
// FindFriendRequestToMe 获取我收到的的好友申请
2023-02-01 16:44:50 +08:00
func (f *FriendDatabase) FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, err error) {
2023-01-30 11:34:07 +08:00
}
2023-01-31 13:37:35 +08:00
2023-02-02 16:39:29 +08:00
// FindFriends 获取某人指定好友的信息 如果有一个不存在也返回错误
2023-02-01 16:44:50 +08:00
func (f *FriendDatabase) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*table.FriendModel, err error) {
2023-02-02 16:39:29 +08:00
friends, err = f.friend.Find(ctx, ownerUserID, friendUserIDs)
if err != nil {
return
}
if len(friends) != len(friendUserIDs) {
err = constant.ErrRecordNotFound.Wrap()
}
return
2023-01-17 17:31:22 +08:00
}