fix add friend

This commit is contained in:
wangchuxiao 2023-03-16 20:21:10 +08:00
parent 0ffce32f62
commit ba6dddf986
7 changed files with 27 additions and 18 deletions

View File

@ -2,6 +2,7 @@ package api
import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/internal/api/a2r"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
@ -28,7 +29,6 @@ func (o *Conversation) client() (conversation.ConversationClient, error) {
}
func (o *Conversation) GetAllConversations(c *gin.Context) {
return
a2r.Call(conversation.ConversationClient.GetAllConversations, o.client, c)
}

View File

@ -2,6 +2,7 @@ package api
import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/internal/api/a2r"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
@ -32,6 +33,10 @@ func (o *Friend) ApplyToAddFriend(c *gin.Context) {
a2r.Call(friend.FriendClient.ApplyToAddFriend, o.client, c)
}
func (o *Friend) RespondFriendApply(c *gin.Context) {
a2r.Call(friend.FriendClient.RespondFriendApply, o.client, c)
}
func (o *Friend) DeleteFriend(c *gin.Context) {
a2r.Call(friend.FriendClient.DeleteFriend, o.client, c)
}
@ -48,10 +53,6 @@ func (o *Friend) GetFriendList(c *gin.Context) {
a2r.Call(friend.FriendClient.GetDesignatedFriends, o.client, c)
}
func (o *Friend) RespondFriendApply(c *gin.Context) {
a2r.Call(friend.FriendClient.RespondFriendApply, o.client, c)
}
func (o *Friend) SetFriendRemark(c *gin.Context) {
a2r.Call(friend.FriendClient.SetFriendRemark, o.client, c)
}

View File

@ -2,6 +2,7 @@ package api
import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/internal/api/a2r"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"

View File

@ -46,11 +46,11 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, rdb redis.Universal
{
f := NewFriend(zk)
friendRouterGroup.Use(mw.GinParseToken(rdb))
friendRouterGroup.POST("/add_friend", f.ApplyToAddFriend) //1
friendRouterGroup.POST("/delete_friend", f.DeleteFriend) //1
friendRouterGroup.POST("/get_friend_apply_list", f.GetFriendApplyList) //1
friendRouterGroup.POST("/get_self_friend_apply_list", f.GetSelfApplyList) //1
friendRouterGroup.POST("/get_friend_list", f.GetFriendList) //1
friendRouterGroup.POST("/add_friend", f.ApplyToAddFriend) //1
friendRouterGroup.POST("/add_friend_response", f.RespondFriendApply) //1
friendRouterGroup.POST("/set_friend_remark", f.SetFriendRemark) //1
friendRouterGroup.POST("/add_black", f.AddBlack) //1

View File

@ -2,12 +2,13 @@ package friend
import (
"context"
"time"
"github.com/OpenIMSDK/Open-IM-Server/internal/common/convert"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tokenverify"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tracelog"
pbFriend "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend"
"time"
)
func (s *friendServer) GetPaginationBlacks(ctx context.Context, req *pbFriend.GetPaginationBlacksReq) (resp *pbFriend.GetPaginationBlacksResp, err error) {
@ -58,6 +59,10 @@ func (s *friendServer) AddBlack(ctx context.Context, req *pbFriend.AddBlackReq)
if err := tokenverify.CheckAccessV3(ctx, req.OwnerUserID); err != nil {
return nil, err
}
_, err := s.userCheck.GetUsersInfos(ctx, []string{req.OwnerUserID, req.BlackUserID}, true)
if err != nil {
return nil, err
}
black := relation.BlackModel{OwnerUserID: req.OwnerUserID, BlockUserID: req.BlackUserID, OperatorUserID: tracelog.GetOpUserID(ctx), CreateTime: time.Now()}
if err := s.BlackDatabase.Create(ctx, []*relation.BlackModel{&black}); err != nil {
return nil, err

View File

@ -3,6 +3,7 @@ package controller
import (
"context"
"errors"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/tx"
@ -88,7 +89,7 @@ func (f *friendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUse
return nil
}
//gorm.ErrRecordNotFound 错误,则新增
if err := f.friendRequest.NewTx(tx).Create(ctx, []*relation.FriendRequestModel{&relation.FriendRequestModel{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex}}); err != nil {
if err := f.friendRequest.NewTx(tx).Create(ctx, []*relation.FriendRequestModel{{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex}}); err != nil {
return err
}
return nil

View File

@ -2,6 +2,7 @@ package relation
import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"gorm.io/gorm"
@ -21,53 +22,53 @@ func (f *FriendRequestGorm) NewTx(tx any) relation.FriendRequestModelInterface {
// 插入多条记录
func (f *FriendRequestGorm) Create(ctx context.Context, friendRequests []*relation.FriendRequestModel) (err error) {
return utils.Wrap(f.DB.Create(&friendRequests).Error, "")
return utils.Wrap(f.db(ctx).Create(&friendRequests).Error, "")
}
// 删除记录
func (f *FriendRequestGorm) Delete(ctx context.Context, fromUserID, toUserID string) (err error) {
return utils.Wrap(f.DB.Where("from_user_id = ? AND to_user_id = ?", fromUserID, toUserID).Delete(&relation.FriendRequestModel{}).Error, "")
return utils.Wrap(f.db(ctx).Where("from_user_id = ? AND to_user_id = ?", fromUserID, toUserID).Delete(&relation.FriendRequestModel{}).Error, "")
}
// 更新零值
func (f *FriendRequestGorm) UpdateByMap(ctx context.Context, formUserID string, toUserID string, args map[string]interface{}) (err error) {
return utils.Wrap(f.DB.Model(&relation.FriendRequestModel{}).Where("from_user_id = ? AND to_user_id ", formUserID, toUserID).Updates(args).Error, "")
return utils.Wrap(f.db(ctx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? AND to_user_id ", formUserID, toUserID).Updates(args).Error, "")
}
// 更新多条记录 (非零值)
func (f *FriendRequestGorm) Update(ctx context.Context, friendRequests []*relation.FriendRequestModel) (err error) {
return utils.Wrap(f.DB.Updates(&friendRequests).Error, "")
return utils.Wrap(f.db(ctx).Updates(&friendRequests).Error, "")
}
// 获取来指定用户的好友申请 未找到 不返回错误
func (f *FriendRequestGorm) Find(ctx context.Context, fromUserID, toUserID string) (friendRequest *relation.FriendRequestModel, err error) {
friendRequest = &relation.FriendRequestModel{}
utils.Wrap(f.DB.Where("from_user_id = ? and to_user_id", fromUserID, toUserID).Find(friendRequest).Error, "")
utils.Wrap(f.db(ctx).Where("from_user_id = ? and to_user_id", fromUserID, toUserID).Find(friendRequest).Error, "")
return
}
func (f *FriendRequestGorm) Take(ctx context.Context, fromUserID, toUserID string) (friendRequest *relation.FriendRequestModel, err error) {
friendRequest = &relation.FriendRequestModel{}
utils.Wrap(f.DB.Where("from_user_id = ? and to_user_id", fromUserID, toUserID).Take(friendRequest).Error, "")
utils.Wrap(f.db(ctx).Where("from_user_id = ? and to_user_id", fromUserID, toUserID).Take(friendRequest).Error, "")
return
}
// 获取toUserID收到的好友申请列表
func (f *FriendRequestGorm) FindToUserID(ctx context.Context, toUserID string, pageNumber, showNumber int32) (friendRequests []*relation.FriendRequestModel, total int64, err error) {
err = f.DB.Model(&relation.FriendRequestModel{}).Where("to_user_id = ? ", toUserID).Count(&total).Error
err = f.db(ctx).Model(&relation.FriendRequestModel{}).Where("to_user_id = ? ", toUserID).Count(&total).Error
if err != nil {
return nil, 0, utils.Wrap(err, "")
}
err = utils.Wrap(f.DB.Where("to_user_id = ? ", toUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&friendRequests).Error, "")
err = utils.Wrap(f.db(ctx).Where("to_user_id = ? ", toUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber-1)).Find(&friendRequests).Error, "")
return
}
// 获取fromUserID发出去的好友申请列表
func (f *FriendRequestGorm) FindFromUserID(ctx context.Context, fromUserID string, pageNumber, showNumber int32) (friendRequests []*relation.FriendRequestModel, total int64, err error) {
err = f.DB.Model(&relation.FriendRequestModel{}).Where("from_user_id = ? ", fromUserID).Count(&total).Error
err = f.db(ctx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? ", fromUserID).Count(&total).Error
if err != nil {
return nil, 0, utils.Wrap(err, "")
}
err = utils.Wrap(f.DB.Where("from_user_id = ? ", fromUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&friendRequests).Error, "")
err = utils.Wrap(f.db(ctx).Where("from_user_id = ? ", fromUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber-1)).Find(&friendRequests).Error, "")
return
}