go_dreamfactory/modules/chat/api_send.go
2023-06-06 13:55:33 +08:00

139 lines
4.2 KiB
Go

package chat
import (
"context"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"go_dreamfactory/sys/wordfilter"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
//参数校验
func (this *apiComp) SendCheck(session comm.IUserSession, req *pb.ChatSendReq) (errdata *pb.ErrorData) {
if (req.Channel == pb.ChatChannel_Union && req.TargetId == "") || (req.Channel == pb.ChatChannel_Private && req.TargetId == "") {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
}
return
}
///消息发送请求
func (this *apiComp) Send(session comm.IUserSession, req *pb.ChatSendReq) (errdata *pb.ErrorData) {
var (
err error
msg *pb.DBChat
userexpand *pb.DBUserExpand
max_chat int32
)
if errdata = this.SendCheck(session, req); errdata != nil {
return
}
msg = &pb.DBChat{
Id: primitive.NewObjectID().Hex(),
Channel: req.Channel,
Suid: session.GetUserId(),
Slv: req.Ulv,
Uname: req.Uname,
Avatar: req.Avatar,
Stag: session.GetServiecTag(),
Ctype: req.Ctype,
Content: req.Content,
Ctime: time.Now().Unix(),
AppendInt: req.AppendInt,
AppendStr: req.AppendStr,
AppendBool: req.AppendBool,
AppendBytes: req.AppendBytes,
}
if max_chat, err = this.module.configure.GetChannelRecordMax(); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigNoFound,
Title: pb.ErrorCode_ConfigNoFound.ToString(),
Message: err.Error(),
}
return
}
switch msg.Channel {
case pb.ChatChannel_World:
if this.module.options.GM { //判断gm命令
if IsCmd(req.Content) { //是否是GM命令
if _, err = this.service.AcrossClusterRpcGo(
context.Background(),
session.GetServiecTag(),
comm.Service_Worker,
string(comm.Rpc_ModuleGMCreateCmd),
pb.RPCGeneralReqA2{Param1: session.GetUserId(), Param2: req.Content},
nil); err != nil {
this.module.Errorln(err)
}
session.SendMsg(string(this.module.GetType()), "send", &pb.ChatSendResp{Issucc: true})
return
}
}
if msg.Ctype == pb.ChatType_Text { //过滤敏感词
msg.Content = wordfilter.Replace(msg.Content, '*')
}
go this.module.modelChat.sendChatToWorld(msg, max_chat)
// this.module.ModuleRtask.SendToRtask(session, comm.Rtype62, 1)
go this.module.ModuleBuried.TriggerBuried(session.GetUserId(), comm.GetBuriedParam(comm.Rtype62, 1))
break
case pb.ChatChannel_Union:
if msg.Ctype == pb.ChatType_Text { //过滤敏感词
msg.Content = wordfilter.Replace(msg.Content, '*')
}
msg.UnionId = req.TargetId
go this.module.modelChat.sendChatToUnion(msg, max_chat)
break
case pb.ChatChannel_Private:
if msg.Ctype == pb.ChatType_Text { //过滤敏感词
msg.Content = wordfilter.Replace(msg.Content, '*')
}
msg.Ruid = req.TargetId
if err = this.module.modelChat.sendChatToPrivate(msg); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
return
}
session.SendMsg(string(this.module.GetType()), "message", &pb.ChatMessagePush{Chat: msg})
break
case pb.ChatChannel_CrossServer:
if msg.Ctype == pb.ChatType_Text { //过滤敏感词
msg.Content = wordfilter.Replace(msg.Content, '*')
}
if userexpand, err = this.module.ModuleUser.GetUserExpand(session.GetUserId()); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
return
}
msg.ChannelId = userexpand.Chatchannel //指定频道
go this.module.modelChat.sendChatToCrossServer(msg, max_chat)
// this.module.ModuleRtask.SendToRtask(session, comm.Rtype106, 1)
go this.module.ModuleBuried.TriggerBuried(session.GetUserId(), comm.GetBuriedParam(comm.Rtype106, 1))
break
default:
this.module.Errorf("不存在的聊天频道类型channel:%d ", req.Channel)
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
Message: fmt.Sprintf("不存在的聊天频道类型 channel:%d ", req.Channel),
}
return
}
session.SendMsg(string(this.module.GetType()), "send", &pb.ChatSendResp{Issucc: true})
return
}