go_dreamfactory/modules/friend/api_apply.go
2022-06-15 14:09:41 +08:00

108 lines
2.5 KiB
Go

package friend
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"go_dreamfactory/sys/cache"
"go_dreamfactory/utils"
)
//好友申请
func (this *ApiComp) Apply(ctx context.Context, session comm.IUserSession, req *pb.FriendApplyReq) (err error) {
var (
code pb.ErrorCode
self *pb.Cache_FriendData
target *pb.Cache_FriendData
rsp *pb.FriendApplyRsp
)
defer func() {
utils.TraceFunc(session.GetUserId(), string(this.module.GetType()), Friend_SubType_Apply, req, rsp)
}()
defer func() {
if code == pb.ErrorCode_Success {
rsp = &pb.FriendApplyRsp{
UserId: session.GetUserId(),
FriendId: req.FriendId,
}
}
session.SendMsg(string(this.module.GetType()), Friend_SubType_Apply, code, rsp)
}()
self, err = cache.Defsys.Friend_Get(session.GetUserId())
if self == nil || err != nil {
code = pb.ErrorCode_FriendSelfNoData
return
}
target, err = cache.Defsys.Friend_Get(req.FriendId)
if target == nil || err != nil {
code = pb.ErrorCode_FriendTargetNoData
return
}
//判断是否是自己
if req.FriendId == session.GetUserId() {
code = pb.ErrorCode_FriendNotSelf
return
}
//判断是否超过最大好友数量
//TODO 最大数从全局配置中获取
var max int32 = 50
total := cache.Defsys.Friend_Total(session.GetUserId())
if total >= max {
code = pb.ErrorCode_FriendSelfMax
return
}
//判断对方是否也超过最大好友数量
ttotal := cache.Defsys.Friend_Total(req.FriendId)
if ttotal >= max {
code = pb.ErrorCode_FriendTargetMax
return
}
//判断是否是好友
if _, ok := utils.Find(self.FriendIds, req.FriendId); ok {
code = pb.ErrorCode_FriendYet
return
}
//判断自己是否在目标用户的申请列表中
if _, ok := utils.Find(target.ApplyIds, self.UserId); ok {
code = pb.ErrorCode_FriendApplyYet
return
}
//判断目标用户是否在黑名单中
if _, ok := utils.Find(self.BlackIds, req.FriendId); ok {
code = pb.ErrorCode_FriendSelfBlackYet
return
}
//判断是否在对方的黑名单中
if _, ok := utils.Find(target.BlackIds, self.UserId); ok {
code = pb.ErrorCode_FriendTargetBlackYet
return
}
//将自己加入到目标用户的申请列表中
target.ApplyIds = append(target.ApplyIds, self.UserId)
err = cache.Defsys.Friend_Update(&pb.Cache_FriendData{
UserId: req.FriendId,
ApplyIds: target.ApplyIds,
})
if err != nil {
log.Errorf("firend Apply err:%v", err)
code = pb.ErrorCode_FriendApplyError
return
}
return
}