go_dreamfactory/modules/friend/api_apply.go
2022-06-28 17:50:32 +08:00

130 lines
3.2 KiB
Go

package friend
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
)
func (this *apiComp) ApplyCheck(session comm.IUserSession, req *pb.FriendApplyReq) (chk map[string]interface{}, code comm.ErrorCode) {
chk = make(map[string]interface{})
var err error
self := &pb.DBFriend{UId: session.GetUserId()}
target := &pb.DBFriend{UId: req.FriendId}
//获取玩家自己好友数据
err = this.module.modelFriend.Get(session.GetUserId(), self)
if self == nil || err != nil {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendSelfNoData}
return
}
//获取好友数据
err = this.module.modelFriend.Get(req.FriendId, target)
if target == nil || err != nil {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendTargetNoData}
return
}
//判断是否是自己
if req.FriendId == session.GetUserId() {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendNotSelf}
return
}
//判断是否超过最大好友数量
//TODO 最大数从全局配置中获取
var max int = 50
total := len(self.FriendIds)
if total >= max {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendSelfMax}
return
}
//判断对方是否也超过最大好友数量
ttotal := len(target.FriendIds)
if ttotal >= max {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendTargetMax}
return
}
//判断是否是好友
if _, ok := utils.Find(self.FriendIds, req.FriendId); ok {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendYet}
return
}
//判断自己是否在目标用户的申请列表中
if _, ok := utils.Find(target.ApplyIds, self.UId); ok {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendApplyYet}
return
}
//判断目标用户是否在黑名单中
if _, ok := utils.Find(self.BlackIds, req.FriendId); ok {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendSelfBlackYet}
return
}
//判断是否在对方的黑名单中
if _, ok := utils.Find(target.BlackIds, self.UId); ok {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendTargetBlackYet}
return
}
chk["self"] = self
chk["target"] = target
return
}
//好友申请
func (this *apiComp) Apply(session comm.IUserSession, chk map[string]interface{}, req *pb.FriendApplyReq) (code pb.ErrorCode) {
var (
target *pb.DBFriend
rsp *pb.FriendApplyRsp
)
defer func() {
utils.TraceFunc(session.GetUserId(), string(this.module.GetType()), FriendSubTypeApply, req, rsp)
}()
defer func() {
if code == pb.ErrorCode_Success {
rsp = &pb.FriendApplyRsp{
UserId: session.GetUserId(),
FriendId: req.FriendId,
}
}
err := session.SendMsg(string(this.module.GetType()), FriendSubTypeApply, rsp)
if err != nil {
code = pb.ErrorCode_SystemError
return
}
}()
if v, ok := chk["target"]; !ok {
code = pb.ErrorCode_FriendTargetNoData
return
} else {
target = v.(*pb.DBFriend)
}
//将自己加入到目标用户的申请列表中
if target.ApplyIds == nil {
target.ApplyIds = []string{}
}
target.ApplyIds = append(target.ApplyIds, session.GetUserId())
err := this.module.modelFriend.Change(req.FriendId, map[string]interface{}{
"applyIds": target.ApplyIds,
})
if err != nil {
log.Errorf("firend Apply err:%v", err)
code = pb.ErrorCode_FriendApplyError
return
}
return
}