go_dreamfactory/modules/friend/api_cross_apply.go
2022-10-19 14:23:20 +08:00

124 lines
2.8 KiB
Go

package friend
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
"google.golang.org/protobuf/proto"
)
func (this *apiComp) ApplyCheck(session comm.IUserSession, req *pb.FriendApplyReq) (code pb.ErrorCode) {
if req.FriendId == "" {
code = pb.ErrorCode_ReqParameterError
}
return
}
//好友申请
func (this *apiComp) Apply(session comm.IUserSession, req *pb.FriendApplyReq) (code pb.ErrorCode, data proto.Message) {
if code = this.ApplyCheck(session, req); code != pb.ErrorCode_Success {
return
}
globalCnf := this.moduleFriend.configure.GetGlobalConf()
if globalCnf == nil {
code = pb.ErrorCode_ConfigNoFound
return
}
var (
err error
self *pb.DBFriend
target *pb.DBFriend
Resp *pb.FriendApplyResp
)
defer func() {
if code == pb.ErrorCode_Success {
Resp = &pb.FriendApplyResp{
UserId: session.GetUserId(),
FriendId: req.FriendId,
}
}
if err := session.SendMsg(string(this.moduleFriend.GetType()), FriendSubTypeApply, Resp); err != nil {
code = pb.ErrorCode_SystemError
return
}
}()
//获取玩家自己好友数据
self = this.moduleFriend.modelFriend.GetFriend(session.GetUserId())
if self == nil {
code = pb.ErrorCode_FriendSelfNoData
return
}
//获取好友数据
target = this.moduleFriend.modelFriend.GetFriend(req.FriendId)
if target == nil {
code = pb.ErrorCode_FriendTargetNoData
return
}
//判断是否是自己
if req.FriendId == session.GetUserId() {
code = pb.ErrorCode_FriendNotSelf
return
}
//判断是否超过最大好友数量
if len(self.FriendIds) >= int(globalCnf.FriendMaxnum) {
code = pb.ErrorCode_FriendSelfMax
return
}
//判断对方是否也超过最大好友数量
if len(target.FriendIds) >= int(globalCnf.FriendMaxnum) {
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.Uid); 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.Uid); ok {
code = pb.ErrorCode_FriendTargetBlackYet
return
}
//将自己加入到目标用户的申请列表中
if target.ApplyIds == nil {
target.ApplyIds = []string{}
}
target.ApplyIds = append(target.ApplyIds, session.GetUserId())
err = this.moduleFriend.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
}