go_dreamfactory/modules/friend/api_cross_del.go
2024-01-17 10:07:54 +08:00

87 lines
2.5 KiB
Go

package friend
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
)
// 删除好友
func (this *apiComp) DelCheck(session comm.IUserSession, req *pb.FriendDelReq) (errdata *pb.ErrorData) {
if req.FriendId == "" {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
this.module.Error("删除好友参数错误", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "params", Value: req.String()})
}
return
}
func (this *apiComp) Del(session comm.IUserSession, req *pb.FriendDelReq) (errdata *pb.ErrorData) {
if errdata = this.DelCheck(session, req); errdata != nil {
return
}
uid := session.GetUserId()
self, err := this.module.modelFriend.GetFriend(uid)
if err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_FriendSelfNoData,
Title: pb.ErrorCode_FriendSelfNoData.ToString(),
}
return
}
// 从好友列表中删除
selfFriendIds := utils.Deletex(self.FriendIds, req.FriendId)
if err := this.module.modelFriend.Change(self.Uid, map[string]interface{}{
"friendIds": selfFriendIds,
}); err != nil {
this.module.Error("删除好友",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "params", Value: req.String()},
log.Field{Key: "err", Value: err.Error()},
)
errdata = &pb.ErrorData{
Code: pb.ErrorCode_FriendApplyError,
Title: pb.ErrorCode_FriendApplyError.ToString(),
}
return
}
target, err := this.module.modelFriend.GetFriend(req.FriendId)
if err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_FriendNotSelf,
Title: pb.ErrorCode_FriendNotSelf.ToString(),
}
return
}
// 将自己从对方好友列表中移除
targetFriendIds := utils.DeleteString(target.FriendIds, uid)
if err := this.module.modelFriend.Change(req.FriendId, map[string]interface{}{
"friendIds": targetFriendIds,
}); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_FriendApplyError,
Title: pb.ErrorCode_FriendApplyError.ToString(),
}
this.module.Error("删除好友",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "params", Value: req.FriendId},
log.Field{Key: "err", Value: err.Error()},
)
return
}
session.SendMsg(string(this.module.GetType()), FriendSubTypeDel, &pb.FriendDelResp{FriendId: req.FriendId, UserId: self.Uid})
// go this.module.AsynHandleSession(session.Clone(), func(session comm.IUserSession) {
// this.module.ModuleSys.CheckOpenCond(session.Clone(), comm.OpencondTypeFriend, int32(len(self.FriendIds)))
// })
return
}