更新好友接口
This commit is contained in:
parent
2c2457f2ac
commit
cfd3cba23b
@ -2,23 +2,78 @@ package robot
|
||||
|
||||
import (
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/modules/friend"
|
||||
"go_dreamfactory/pb"
|
||||
"log"
|
||||
)
|
||||
|
||||
func (r *Robot) handleFriendMsg(msg *pb.UserMessage) {
|
||||
switch msg.SubType {
|
||||
case "apply":
|
||||
case friend.Friend_SubType_Apply:
|
||||
r.handleFriendApply(msg)
|
||||
case friend.Friend_SubType_ApplyList:
|
||||
r.handleFriendApplyList(msg)
|
||||
case friend.Friend_SubType_Agree:
|
||||
r.handleFriendAgree(msg)
|
||||
case friend.Friend_SubType_Refuse:
|
||||
r.handleFriendRefuse(msg)
|
||||
case friend.Friend_SubType_Blacklist:
|
||||
r.handleFriendBlacklist(msg)
|
||||
case friend.Friend_SubType_AddBlack:
|
||||
r.handleFriendAddBlack(msg)
|
||||
case friend.Friend_SubType_DelBlack:
|
||||
r.handleFriendDelBlack(msg)
|
||||
case friend.Friend_SubType_Search:
|
||||
r.handleFriendSearch(msg)
|
||||
}
|
||||
}
|
||||
|
||||
//添加好友
|
||||
//好友列表
|
||||
func (r *Robot) FriendList() {
|
||||
req := &pb.FriendListReq{}
|
||||
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_List}
|
||||
defer traceFunc(head.MainType, head.SubType, r.user.UserData.GetUserId(), req)
|
||||
err := r.SendToClient(head, req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Robot) handleFriendList(msg *pb.UserMessage) {
|
||||
rsp := &pb.FriendListRsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
}
|
||||
|
||||
//好友搜索
|
||||
func (r *Robot) FriendSearch(nickName string) {
|
||||
req := &pb.FriendSearchReq{
|
||||
NickName: nickName,
|
||||
}
|
||||
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_Search}
|
||||
defer traceFunc(head.MainType, head.SubType, r.user.UserData.GetUserId(), req)
|
||||
err := r.SendToClient(head, req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Robot) handleFriendSearch(msg *pb.UserMessage) {
|
||||
rsp := &pb.FriendSearchRsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
}
|
||||
|
||||
//好友申请
|
||||
func (r *Robot) FriendApply(friendId string) {
|
||||
req := &pb.FriendApplyReq{
|
||||
FriendId: friendId,
|
||||
}
|
||||
head := &pb.UserMessage{MainType: "friend", SubType: "apply"}
|
||||
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_Apply}
|
||||
defer traceFunc(head.MainType, head.SubType, r.user.UserData.GetUserId(), req)
|
||||
err := r.SendToClient(head, req)
|
||||
if err != nil {
|
||||
@ -33,3 +88,123 @@ func (r *Robot) handleFriendApply(msg *pb.UserMessage) {
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
}
|
||||
|
||||
//申请列表
|
||||
func (r *Robot) FriendApplyList() {
|
||||
req := &pb.FriendApplyListReq{}
|
||||
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_ApplyList}
|
||||
defer traceFunc(head.MainType, head.SubType, r.user.UserData.GetUserId(), req)
|
||||
err := r.SendToClient(head, req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Robot) handleFriendApplyList(msg *pb.UserMessage) {
|
||||
rsp := &pb.FriendApplyListRsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
}
|
||||
|
||||
//同意
|
||||
func (r *Robot) FriendAgree(friendIds []string) {
|
||||
req := &pb.FriendAgreeReq{
|
||||
FriendIds: friendIds,
|
||||
}
|
||||
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_Apply}
|
||||
defer traceFunc(head.MainType, head.SubType, r.user.UserData.GetUserId(), req)
|
||||
err := r.SendToClient(head, req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Robot) handleFriendAgree(msg *pb.UserMessage) {
|
||||
rsp := &pb.FriendAgreeRsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
}
|
||||
|
||||
//拒绝
|
||||
func (r *Robot) FriendRefuse(friendIds []string) {
|
||||
req := &pb.FriendRefuseReq{
|
||||
FriendIds: friendIds,
|
||||
}
|
||||
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_Refuse}
|
||||
defer traceFunc(head.MainType, head.SubType, r.user.UserData.GetUserId(), req)
|
||||
err := r.SendToClient(head, req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Robot) handleFriendRefuse(msg *pb.UserMessage) {
|
||||
rsp := &pb.FriendRefuseRsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
}
|
||||
|
||||
//黑名单列表
|
||||
func (r *Robot) FriendBlacklist() {
|
||||
req := &pb.FriendBlackListReq{}
|
||||
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_Blacklist}
|
||||
defer traceFunc(head.MainType, head.SubType, r.user.UserData.GetUserId(), req)
|
||||
err := r.SendToClient(head, req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Robot) handleFriendBlacklist(msg *pb.UserMessage) {
|
||||
rsp := &pb.FriendBlackListRsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
}
|
||||
|
||||
//添加黑名单
|
||||
func (r *Robot) FriendAddBlack() {
|
||||
req := &pb.FriendBlackAddReq{}
|
||||
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_AddBlack}
|
||||
defer traceFunc(head.MainType, head.SubType, r.user.UserData.GetUserId(), req)
|
||||
err := r.SendToClient(head, req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Robot) handleFriendAddBlack(msg *pb.UserMessage) {
|
||||
rsp := &pb.FriendBlackAddRsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
}
|
||||
|
||||
//删除黑名单
|
||||
func (r *Robot) FriendDelBlack(friendId string) {
|
||||
req := &pb.FriendDelBlackReq{
|
||||
FriendId: friendId,
|
||||
}
|
||||
head := &pb.UserMessage{MainType: string(comm.SM_FriendModule), SubType: friend.Friend_SubType_DelBlack}
|
||||
defer traceFunc(head.MainType, head.SubType, r.user.UserData.GetUserId(), req)
|
||||
err := r.SendToClient(head, req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Robot) handleFriendDelBlack(msg *pb.UserMessage) {
|
||||
rsp := &pb.FriendDelBlackRsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
}
|
||||
|
@ -9,9 +9,9 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/gorilla/websocket"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type Robot struct {
|
||||
@ -79,13 +79,21 @@ func (r *Robot) handleMsg(msg *pb.UserMessage) {
|
||||
//在这里添加玩家成功登录以后的测试方法
|
||||
func (r *Robot) onUserLoaded() {
|
||||
//user
|
||||
r.CreateUser("user671")
|
||||
// r.CreateUser("乐谷4")
|
||||
|
||||
//friend
|
||||
r.FriendApply("629f147e3d276120561bfa18")
|
||||
// r.FriendApply("629f147e3d276120561bfa18")
|
||||
// r.FriendAgree([]string{})
|
||||
// r.FriendRefuse([]string{})
|
||||
// r.FriendApplyList()
|
||||
// r.FriendList()
|
||||
// r.FriendBlacklist()
|
||||
// r.FriendAddBlack()
|
||||
// r.FriendDelBlack("")
|
||||
r.FriendSearch("乐谷5")
|
||||
|
||||
//pack
|
||||
r.QueryUserPack()
|
||||
// r.QueryUserPack()
|
||||
|
||||
}
|
||||
|
||||
@ -137,10 +145,12 @@ func (r *Robot) AccountRegister() {
|
||||
}
|
||||
}
|
||||
|
||||
//打印响应
|
||||
func printReply(msg *pb.UserMessage, rsp interface{}) {
|
||||
log.Printf("rsp [%s.%s] [%d] [%v]", msg.MainType, msg.SubType, msg.Code, rsp)
|
||||
}
|
||||
|
||||
//方法参数跟踪
|
||||
func traceFunc(module string, funcName string, uid string, funcArgs interface{}) {
|
||||
log.Printf("req [%s.%s] [%s] [%v]", module, funcName, uid, funcArgs)
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package robot
|
||||
|
||||
import (
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/modules/user"
|
||||
"go_dreamfactory/pb"
|
||||
"log"
|
||||
)
|
||||
@ -40,8 +41,8 @@ func (r *Robot) CreateUser(NickName string) {
|
||||
}
|
||||
|
||||
head := &pb.UserMessage{
|
||||
MainType: "user",
|
||||
SubType: "create",
|
||||
MainType: string(comm.SM_UserModule),
|
||||
SubType: user.User_SubType_Create,
|
||||
}
|
||||
|
||||
defer traceFunc(head.MainType, head.SubType, r.user.UserData.GetUserId(), req)
|
||||
|
1
go.mod
1
go.mod
@ -7,6 +7,7 @@ require (
|
||||
github.com/go-playground/validator/v10 v10.10.1
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible
|
||||
github.com/golang/protobuf v1.5.2
|
||||
github.com/gorilla/websocket v1.4.2
|
||||
github.com/hashicorp/consul/api v1.12.0
|
||||
github.com/json-iterator/go v1.1.12
|
||||
|
@ -8,12 +8,20 @@ import (
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/sys/db"
|
||||
"go_dreamfactory/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
Friend_SubType_List = "list"
|
||||
Friend_SubType_Apply = "apply"
|
||||
Friend_SubType_ApplyList = "applylist"
|
||||
Friend_SubType_AddBlack = "addblack"
|
||||
Friend_SubType_DelBlack = "delblack"
|
||||
Friend_SubType_Blacklist = "blacklist"
|
||||
Friend_SubType_Agree = "agree"
|
||||
Friend_SubType_Refuse = "refuse"
|
||||
Friend_SubType_Search = "search"
|
||||
)
|
||||
|
||||
type FriendComp struct {
|
||||
@ -29,6 +37,27 @@ func (this *FriendComp) Init(service core.IService, module core.IModule, comp co
|
||||
|
||||
//搜索
|
||||
func (this *FriendComp) Search(ctx context.Context, session comm.IUserSession, req *pb.FriendSearchReq) error {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
rsp *pb.FriendSearchRsp
|
||||
friend *pb.FriendBase
|
||||
)
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendSearchRsp{
|
||||
Friend: friend,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_Search, code, rsp)
|
||||
}()
|
||||
|
||||
user := db.Defsys.Frined_FindCond(req.NickName)
|
||||
if user != nil {
|
||||
friend = &pb.FriendBase{
|
||||
UserId: user.UserId,
|
||||
NickName: user.NiceName,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -115,7 +144,7 @@ func (this *FriendComp) Apply(ctx context.Context, session comm.IUserSession, re
|
||||
|
||||
//将自己加入到目标用户的申请列表中
|
||||
target.ApplyIds = append(target.ApplyIds, self.UserId)
|
||||
err = cache.Defsys.Friend_Apply(&pb.Cache_FriendData{
|
||||
err = cache.Defsys.Friend_Update(&pb.Cache_FriendData{
|
||||
UserId: req.FriendId,
|
||||
ApplyIds: target.ApplyIds,
|
||||
})
|
||||
@ -138,12 +167,6 @@ func (this *FriendComp) ApplyList(ctx context.Context, session comm.IUserSession
|
||||
list []*pb.FriendBase
|
||||
)
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendApplyListRsp{
|
||||
@ -153,6 +176,12 @@ func (this *FriendComp) ApplyList(ctx context.Context, session comm.IUserSession
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_ApplyList, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
for _, userId := range self.ApplyIds {
|
||||
//TODO 组装FriendBase明细数据
|
||||
list = append(list, &pb.FriendBase{
|
||||
@ -169,28 +198,142 @@ func (this *FriendComp) Del(ctx context.Context, session comm.IUserSession, req
|
||||
}
|
||||
|
||||
//好友列表
|
||||
func (this *FriendComp) List(ctx context.Context, session comm.IUserSession, req *pb.FriendListReq) error {
|
||||
func (this *FriendComp) List(ctx context.Context, session comm.IUserSession, req *pb.FriendListReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendListRsp
|
||||
list []*pb.FriendBase
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendListRsp{
|
||||
List: list,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_List, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
for _, userId := range self.FriendIds {
|
||||
list = append(list, &pb.FriendBase{
|
||||
UserId: userId,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//单个/批量同意
|
||||
func (this *FriendComp) Agree(ctx context.Context, session comm.IUserSession, req *pb.FriendAgreeOrRefuseReq) error {
|
||||
func (this *FriendComp) Agree(ctx context.Context, session comm.IUserSession, req *pb.FriendAgreeReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendAgreeRsp
|
||||
optNum int32
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendAgreeRsp{
|
||||
Num: optNum,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_Agree, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
//将申请人加入到自己的好友列表中
|
||||
for _, userId := range req.FriendIds {
|
||||
if _, ok := utils.Find(self.FriendIds, userId); !ok {
|
||||
self.FriendIds = append(self.FriendIds, userId)
|
||||
}
|
||||
}
|
||||
|
||||
//将自己加入到申请人的好友列表中
|
||||
for _, userId := range req.FriendIds {
|
||||
target, err2 := cache.Defsys.Friend_Get(userId)
|
||||
if target == nil || err2 != nil {
|
||||
code = pb.ErrorCode_FriendTargetNoData
|
||||
continue
|
||||
}
|
||||
if _, ok := utils.Find(target.FriendIds, self.UserId); !ok {
|
||||
target.FriendIds = append(target.FriendIds, self.UserId)
|
||||
}
|
||||
err = cache.Defsys.Friend_Update(target)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//将申请人从申请列表中删除
|
||||
for _, userId := range req.FriendIds {
|
||||
self.ApplyIds = utils.DeleteString(self.ApplyIds, userId)
|
||||
optNum++
|
||||
}
|
||||
|
||||
//更新
|
||||
err = cache.Defsys.Friend_Update(self)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//单个/批量拒绝
|
||||
func (this *FriendComp) Refuse(ctx context.Context, session comm.IUserSession, req *pb.FriendAgreeOrRefuseReq) error {
|
||||
func (this *FriendComp) Refuse(ctx context.Context, session comm.IUserSession, req *pb.FriendRefuseReq) (err error) {
|
||||
//将申请人从申请列表中删除
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendAgreeRsp
|
||||
optNum int32
|
||||
)
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendAgreeRsp{
|
||||
Num: optNum,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_Refuse, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
//将申请人从申请列表中删除
|
||||
for _, userId := range req.FriendIds {
|
||||
self.ApplyIds = utils.DeleteString(self.ApplyIds, userId)
|
||||
optNum++
|
||||
}
|
||||
|
||||
//更新
|
||||
err = cache.Defsys.Friend_Update(self)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//赠送或接收
|
||||
func (this *FriendComp) ReceSend(ctx context.Context, session comm.IUserSession, req *pb.FriendReceiveOrSendReq) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//好友数量
|
||||
func (this *FriendComp) Total(ctx context.Context, session comm.IUserSession, req *pb.FriendTotalReq) error {
|
||||
func (this *FriendComp) ReceSend(ctx context.Context, session comm.IUserSession, req *pb.FriendReceiveReq) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -200,11 +343,129 @@ func (this *FriendComp) Detail(ctx context.Context, session comm.IUserSession, r
|
||||
}
|
||||
|
||||
//黑名单
|
||||
func (this *FriendComp) Blacklist(ctx context.Context, session comm.IUserSession, req *pb.FriendBlackListReq) error {
|
||||
func (this *FriendComp) Blacklist(ctx context.Context, session comm.IUserSession, req *pb.FriendBlackListReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendBlackListRsp
|
||||
list []*pb.FriendBase
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendBlackListRsp{
|
||||
Friends: list,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_Blacklist, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
for _, userId := range self.BlackIds {
|
||||
//TODO 完善FriendBase信息
|
||||
list = append(list, &pb.FriendBase{
|
||||
UserId: userId,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//加入黑名单
|
||||
func (this *FriendComp) Addblack(ctx context.Context, session comm.IUserSession, req *pb.FriendBlackAddReq) error {
|
||||
func (this *FriendComp) Addblack(ctx context.Context, session comm.IUserSession, req *pb.FriendBlackAddReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
target *pb.Cache_FriendData
|
||||
rsp *pb.FriendBlackAddRsp
|
||||
blackNumMax = 50 //TODO 从配置中读取
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendBlackAddRsp{
|
||||
FriendId: req.FriendId,
|
||||
UserId: session.GetUserId(),
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_AddBlack, 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 _, ok := utils.Find(self.FriendIds, req.FriendId); ok {
|
||||
code = pb.ErrorCode_FriendSelfBlackYet
|
||||
return
|
||||
}
|
||||
|
||||
// 判断自己是否在对方的黑名单中
|
||||
if _, ok := utils.Find(target.BlackIds, self.UserId); ok {
|
||||
code = pb.ErrorCode_FriendTargetBlackYet
|
||||
return
|
||||
}
|
||||
|
||||
// 判断是否黑名单人数已满
|
||||
if len(self.BlackIds) >= blackNumMax {
|
||||
code = pb.ErrorCode_FriendBlackMax
|
||||
return
|
||||
}
|
||||
|
||||
//将目标加入黑名单
|
||||
self.BlackIds = append(self.BlackIds, req.FriendId)
|
||||
//更新黑名单
|
||||
err = cache.Defsys.Friend_Update(self)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//删除黑名单
|
||||
func (this *FriendComp) delblack(ctx context.Context, session comm.IUserSession, req *pb.FriendDelBlackReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendDelBlackRsp
|
||||
)
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendDelBlackRsp{
|
||||
FriendId: req.FriendId,
|
||||
UserId: session.GetUserId(),
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_AddBlack, code, rsp)
|
||||
}()
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
//从黑名单列表中删除目标
|
||||
self.BlackIds = utils.DeleteString(self.BlackIds, req.FriendId)
|
||||
//更新黑名单
|
||||
err = cache.Defsys.Friend_Update(self)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -13,6 +13,10 @@ import (
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
)
|
||||
|
||||
const (
|
||||
User_SubType_Create = "create"
|
||||
)
|
||||
|
||||
type UserComp struct {
|
||||
modules.MComp_GateComp
|
||||
module *User
|
||||
@ -26,11 +30,11 @@ func (this *UserComp) Init(service core.IService, module core.IModule, comp core
|
||||
|
||||
//创角
|
||||
func (this *UserComp) Create(ctx context.Context, session comm.IUserSession, req *pb.UserCreateReq) (err error) {
|
||||
defer utils.TraceFunc(session.GetUserId(), "user", "create", req, nil)
|
||||
defer utils.TraceFunc(session.GetUserId(), string(this.module.GetType()), User_SubType_Create, req, nil)
|
||||
user := cache.Defsys.Get(session.GetUserId())
|
||||
var code pb.ErrorCode
|
||||
defer func() {
|
||||
session.SendMsg("user", "create", code, &pb.UserCreateRsp{})
|
||||
session.SendMsg(string(this.module.GetType()), User_SubType_Create, code, &pb.UserCreateRsp{})
|
||||
}()
|
||||
if user == nil {
|
||||
log.Errorf("user no exist")
|
||||
@ -42,6 +46,8 @@ func (this *UserComp) Create(ctx context.Context, session comm.IUserSession, req
|
||||
err = cache.Defsys.Update(user)
|
||||
if err != nil {
|
||||
log.Errorf("cache update err")
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
event.RegisterGO(comm.Event_CreateUser, session.GetUserId())
|
||||
return nil
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -32,4 +32,5 @@ enum ErrorCode {
|
||||
FriendSelfBlackYet = 1107;//已在自己黑名单中
|
||||
FriendTargetBlackYet = 1108;//已在对方的黑名单中
|
||||
FriendApplyError = 1109;//申请失败
|
||||
FriendBlackMax = 1110; //黑名单最大数量
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
syntax = "proto3";
|
||||
option go_package = ".;pb";
|
||||
import "friend/friend_db.proto";
|
||||
|
||||
message FriendBase {
|
||||
string userId = 1; //ID
|
||||
@ -18,7 +17,7 @@ message FriendListReq{
|
||||
}
|
||||
|
||||
message FriendListRsp{
|
||||
repeated Cache_FriendData list = 1;
|
||||
repeated FriendBase list = 1;
|
||||
}
|
||||
|
||||
//申请好友
|
||||
@ -41,15 +40,20 @@ message FriendDelRsp{
|
||||
string userId = 2; //用户ID
|
||||
}
|
||||
|
||||
//同意或拒绝
|
||||
message FriendAgreeOrRefuseReq{
|
||||
string friendId = 1; //被同意或拒绝的用户
|
||||
bool isAgree = 2;
|
||||
|
||||
//同意
|
||||
message FriendAgreeReq{
|
||||
repeated string friendIds = 1; //被同意的用户
|
||||
}
|
||||
message FriendAgressOrRefuseRsp{
|
||||
message FriendAgreeRsp{
|
||||
int32 Num = 1;//操作的数量
|
||||
}
|
||||
|
||||
//拒绝
|
||||
message FriendRefuseReq{
|
||||
repeated string friendIds = 1; //被拒绝的用户
|
||||
}
|
||||
message FriendRefuseRsp{
|
||||
int32 Num = 1;//操作的数量
|
||||
bool isAgree = 2;
|
||||
}
|
||||
|
||||
|
||||
@ -63,12 +67,11 @@ message FriendApplyListRsp{
|
||||
|
||||
//好友搜索
|
||||
message FriendSearchReq{
|
||||
string friendId = 1;
|
||||
string nickName = 2; //好友昵称
|
||||
string nickName = 1; //好友昵称
|
||||
}
|
||||
|
||||
message FriendSearchRsp{
|
||||
repeated FriendBase friends = 1;
|
||||
FriendBase friend = 1;
|
||||
}
|
||||
|
||||
//黑名单
|
||||
@ -90,18 +93,37 @@ message FriendBlackAddRsp{
|
||||
string userId = 2;
|
||||
}
|
||||
|
||||
//赠送或接收
|
||||
message FriendReceiveOrSendReq{
|
||||
//删除黑名单
|
||||
message FriendDelBlackReq{
|
||||
string friendId = 1;
|
||||
bool isReceive = 2;
|
||||
}
|
||||
|
||||
message FriendReceiveOrSendRsp{
|
||||
message FriendDelBlackRsp{
|
||||
string friendId = 1;
|
||||
string userId = 2;
|
||||
bool isReceive = 3;
|
||||
}
|
||||
|
||||
//接收
|
||||
message FriendReceiveReq{
|
||||
string friendId = 1;
|
||||
}
|
||||
|
||||
message FriendReceiveRsp{
|
||||
string friendId = 1;
|
||||
string userId = 2;
|
||||
}
|
||||
|
||||
//赠送
|
||||
message FriendGiveReq{
|
||||
string friendId = 1;
|
||||
}
|
||||
|
||||
message FriendGiveRsp{
|
||||
string friendId = 1;
|
||||
string userId = 2;
|
||||
}
|
||||
|
||||
|
||||
//好友数量
|
||||
message FriendTotalReq{
|
||||
string friendId = 1;
|
||||
|
@ -8,7 +8,7 @@ message Cache_UserData {
|
||||
}
|
||||
|
||||
message DB_UserData {
|
||||
string UserId = 1; //tags:{bson:"_id"}动态Id
|
||||
string UserId = 1; // @go_tags(`bson:"_id"`) ID
|
||||
string Account = 2;
|
||||
string NiceName = 3;
|
||||
int32 ServerId = 4;
|
||||
|
11
sys/cache/friend.go
vendored
11
sys/cache/friend.go
vendored
@ -17,14 +17,15 @@ func getRdsFriendKey(userId string) string {
|
||||
}
|
||||
|
||||
type IFriend interface {
|
||||
Friend_Apply(data *pb.Cache_FriendData) (err error)
|
||||
Friend_Update(data *pb.Cache_FriendData) (err error)
|
||||
Friend_Total(userId string) int32
|
||||
Friend_Get(userId string) (*pb.Cache_FriendData, error)
|
||||
}
|
||||
|
||||
//好友申请
|
||||
func (this *Cache) Friend_Apply(data *pb.Cache_FriendData) (err error) {
|
||||
if err = db.Defsys.Friend_Apply(data); err == nil {
|
||||
//更新
|
||||
func (this *Cache) Friend_Update(data *pb.Cache_FriendData) (err error) {
|
||||
if err = db.Defsys.Friend_SaveOrUpdate(data); err == nil {
|
||||
//更新缓存
|
||||
err = this.redis.Set(getRdsFriendKey(data.UserId), data, 0)
|
||||
}
|
||||
return
|
||||
@ -47,7 +48,7 @@ func (this *Cache) Friend_Get(userId string) (*pb.Cache_FriendData, error) {
|
||||
if err != nil {
|
||||
if err.Error() == string(redis.RedisNil) {
|
||||
d = &pb.Cache_FriendData{UserId: userId}
|
||||
err = this.Friend_Apply(d)
|
||||
err = this.Friend_Update(d)
|
||||
if err != nil {
|
||||
return d, nil
|
||||
}
|
||||
|
2
sys/cache/friend_test.go
vendored
2
sys/cache/friend_test.go
vendored
@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func TestFriendApply(t *testing.T) {
|
||||
err := cache.Defsys.Friend_Apply(&pb.Cache_FriendData{
|
||||
err := cache.Defsys.Friend_Update(&pb.Cache_FriendData{
|
||||
UserId: "629f159310d6970846f7ef26",
|
||||
FriendIds: []string{"629f147e3d276120561bfa18"},
|
||||
ApplyIds: []string{"629eb3f4132dc4bb26139659"},
|
||||
|
6
sys/cache/user.go
vendored
6
sys/cache/user.go
vendored
@ -3,6 +3,7 @@ package cache
|
||||
import (
|
||||
"fmt"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/db"
|
||||
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
)
|
||||
@ -17,7 +18,10 @@ type IUser interface {
|
||||
}
|
||||
|
||||
func (this *Cache) Update(data *pb.Cache_UserData) (err error) {
|
||||
err = this.redis.Set(fmt.Sprintf(Redis_UserCache, data.UserData.UserId), data, -1)
|
||||
err = db.Defsys.User_Update(data.UserData)
|
||||
if err == nil {
|
||||
err = this.redis.Set(fmt.Sprintf(Redis_UserCache, data.UserData.UserId), data, -1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package db
|
||||
|
||||
import (
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
"go_dreamfactory/pb"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
@ -14,11 +15,12 @@ const (
|
||||
)
|
||||
|
||||
type IFriend interface {
|
||||
Friend_Apply(data *pb.Cache_FriendData) (err error)
|
||||
Friend_SaveOrUpdate(data *pb.Cache_FriendData) (err error)
|
||||
Frined_FindCond(nickName string) *pb.DB_UserData
|
||||
}
|
||||
|
||||
//好友申请
|
||||
func (this *DB) Friend_Apply(data *pb.Cache_FriendData) (err error) {
|
||||
//好友
|
||||
func (this *DB) Friend_SaveOrUpdate(data *pb.Cache_FriendData) (err error) {
|
||||
err = this.mgo.FindOneAndUpdate(DB_FriendTable,
|
||||
bson.M{"_id": data.UserId},
|
||||
bson.M{"$set": bson.M{
|
||||
@ -32,3 +34,15 @@ func (this *DB) Friend_Apply(data *pb.Cache_FriendData) (err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (this *DB) Frined_FindCond(nickName string) *pb.DB_UserData {
|
||||
var user *pb.DB_UserData
|
||||
err := this.mgo.FindOne(DB_UserTable, bson.M{
|
||||
"nicename": nickName,
|
||||
}).Decode(&user)
|
||||
if err != nil {
|
||||
log.Errorf("findCond err:%v", err)
|
||||
return nil
|
||||
}
|
||||
return user
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_dreamfactory/pb"
|
||||
"testing"
|
||||
|
||||
@ -8,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func TestFriendAdd(t *testing.T) {
|
||||
err := db.Friend_Apply(&pb.Cache_FriendData{
|
||||
err := db.Friend_SaveOrUpdate(&pb.Cache_FriendData{
|
||||
UserId: "629f159310d6970846f7ef26",
|
||||
FriendIds: []string{
|
||||
"629f147e3d276120561bfa18",
|
||||
@ -17,3 +18,10 @@ func TestFriendAdd(t *testing.T) {
|
||||
|
||||
require.Nil(t, err, nil)
|
||||
}
|
||||
|
||||
func TestFriendFindCond(t *testing.T) {
|
||||
user := db.Frined_FindCond("乐谷5")
|
||||
require.NotNil(t, user, nil)
|
||||
|
||||
fmt.Printf("%v", user)
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ func (this *DB) User_FindByAccount(user *pb.DB_UserData) (*pb.DB_UserData, error
|
||||
|
||||
func (this *DB) User_FindById(id string) (*pb.DB_UserData, error) {
|
||||
filter := bson.M{
|
||||
"_id": id,
|
||||
"userid": id,
|
||||
}
|
||||
sr := this.mgo.FindOne(DB_UserTable, filter)
|
||||
user := &pb.DB_UserData{}
|
||||
@ -53,11 +53,11 @@ func (this *DB) User_Create(user *pb.DB_UserData) (err error) {
|
||||
func (this *DB) User_Update(data *pb.DB_UserData) (err error) {
|
||||
err = this.mgo.FindOneAndUpdate(
|
||||
DB_UserTable,
|
||||
bson.M{"_id": data.UserId},
|
||||
bson.M{"userid": data.UserId},
|
||||
bson.M{"$set": bson.M{
|
||||
"niceName": data.NiceName,
|
||||
"nicename": data.NiceName,
|
||||
}},
|
||||
options.FindOneAndUpdate().SetUpsert(false).SetReturnDocument(options.After),
|
||||
).Decode(data)
|
||||
options.FindOneAndUpdate().SetUpsert(true),
|
||||
).Err()
|
||||
return err
|
||||
}
|
||||
|
@ -16,12 +16,21 @@ func ParseP(p string) (string, string, bool) {
|
||||
return s[0], s[1], true
|
||||
}
|
||||
|
||||
|
||||
func Find(slice []string, val string) (int, bool) {
|
||||
for i, item := range slice {
|
||||
if item == val {
|
||||
return i, true
|
||||
}
|
||||
}
|
||||
return -1, false
|
||||
for i, item := range slice {
|
||||
if item == val {
|
||||
return i, true
|
||||
}
|
||||
}
|
||||
return -1, false
|
||||
}
|
||||
|
||||
func DeleteString(list []string, ele string) []string {
|
||||
result := make([]string, 0)
|
||||
for _, v := range list {
|
||||
if v != ele {
|
||||
result = append(result, v)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
Loading…
Reference in New Issue
Block a user