go_dreamfactory/modules/chat/api_send.go

96 lines
2.6 KiB
Go

package chat
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/protobuf/proto"
)
//参数校验
func (this *apiComp) SendCheck(session comm.IUserSession, req *pb.ChatSendReq) (code pb.ErrorCode) {
if (req.Channel == pb.ChatChannel_Union && req.TargetId == "") || (req.Channel == pb.ChatChannel_Private && req.TargetId == "") {
code = pb.ErrorCode_ReqParameterError
}
return
}
///消息发送请求
func (this *apiComp) Send(session comm.IUserSession, req *pb.ChatSendReq) (code pb.ErrorCode, data proto.Message) {
var (
err error
max int32
msg *pb.DBChat
userexpand *pb.DBUserExpand
max_chat int32
)
if code = this.SendCheck(session, req); code != pb.ErrorCode_Success {
return
}
msg = &pb.DBChat{
Id: primitive.NewObjectID().Hex(),
Channel: req.Channel,
Suid: session.GetUserId(),
Stag: session.GetServiecTag(),
Avatar: req.Avatar,
Content: req.Content,
Ctime: time.Now().Unix(),
}
if max, err = this.module.configure.GetChannelRecordMax(); err != nil {
code = pb.ErrorCode_ConfigNoFound
return
}
if max_chat, err = this.module.configure.GetChannelRecordMax(); err != nil {
code = pb.ErrorCode_ConfigNoFound
return
}
switch msg.Channel {
case pb.ChatChannel_World:
if err = this.module.modelChat.addChatMsg(fmt.Sprintf("%s-%s", worldchatkey, session.GetServiecTag()), int64(max), msg); err != nil {
code = pb.ErrorCode_DBError
return
}
if err = this.module.PushWorld(msg); err != nil {
code = pb.ErrorCode_DBError
return
}
break
case pb.ChatChannel_Union:
msg.UnionId = req.TargetId
if err = this.module.modelChat.addChatMsg(fmt.Sprintf("%s:%s", unionchatkey, req.TargetId), int64(max_chat), msg); err != nil {
code = pb.ErrorCode_DBError
return
}
if err = this.module.PushUnion(req.TargetId, msg); err != nil {
code = pb.ErrorCode_DBError
return
}
break
case pb.ChatChannel_Private:
msg.Ruid = req.TargetId
if err = this.module.PushUser(msg); err != nil {
code = pb.ErrorCode_DBError
return
}
break
case pb.ChatChannel_CrossServer:
if userexpand, err = this.module.ModuleUser.GetUserExpand(session.GetUserId()); err != nil {
code = pb.ErrorCode_DBError
return
}
msg.ChannelId = userexpand.Chatchannel //指定频道
if err = this.module.modelChat.addChatMsg(fmt.Sprintf("%s:%d", crosschatkey, userexpand.Chatchannel), int64(max_chat), msg); err != nil {
code = pb.ErrorCode_DBError
return
}
this.module.PushToUsers(userexpand.Chatchannel, msg)
break
}
session.SendMsg(string(this.module.GetType()), "send", &pb.ChatSendResp{})
return
}