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) (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 *pb.ErrorData) { var ( err error 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(), 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 { code = pb.ErrorCode_ConfigNoFound data = &pb.ErrorData{ Title: code.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 } code = pb.ErrorCode_Success } 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 { code = pb.ErrorCode_DBError 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 { code = pb.ErrorCode_DBError 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) code = pb.ErrorCode_ReqParameterError data = &pb.ErrorData{ Title: code.ToString(), Message: fmt.Sprintf("不存在的聊天频道类型 channel:%d ", req.Channel), } return } session.SendMsg(string(this.module.GetType()), "send", &pb.ChatSendResp{Issucc: true}) return }