You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/rpc/message_cms/message_cms.go

168 lines
5.4 KiB

3 years ago
package messageCMS
3 years ago
import (
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/constant"
3 years ago
"Open_IM/pkg/common/db"
3 years ago
errors "Open_IM/pkg/common/http"
3 years ago
"context"
"Open_IM/pkg/common/log"
3 years ago
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
3 years ago
"Open_IM/pkg/grpc-etcdv3/getcdv3"
pbMessageCMS "Open_IM/pkg/proto/message_cms"
3 years ago
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
3 years ago
"Open_IM/pkg/utils"
3 years ago
3 years ago
"net"
"strconv"
"strings"
"google.golang.org/grpc"
)
type messageCMSServer struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
}
func NewMessageCMSServer(port int) *messageCMSServer {
3 years ago
log.NewPrivateLog(constant.LogFileName)
3 years ago
return &messageCMSServer{
rpcPort: port,
3 years ago
rpcRegisterName: config.Config.RpcRegisterName.OpenImMessageCMSName,
3 years ago
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
}
}
func (s *messageCMSServer) Run() {
3 years ago
log.NewInfo("0", "messageCMS rpc start ")
3 years ago
ip := utils.ServerIP
registerAddress := ip + ":" + strconv.Itoa(s.rpcPort)
//listener network
listener, err := net.Listen("tcp", registerAddress)
if err != nil {
log.NewError("0", "Listen failed ", err.Error(), registerAddress)
return
}
log.NewInfo("0", "listen network success, ", registerAddress, listener)
defer listener.Close()
//grpc server
srv := grpc.NewServer()
defer srv.GracefulStop()
//Service registers with etcd
3 years ago
pbMessageCMS.RegisterMessageCMSServer(srv, s)
3 years ago
err = getcdv3.RegisterEtcd(s.etcdSchema, strings.Join(s.etcdAddr, ","), ip, s.rpcPort, s.rpcRegisterName, 10)
if err != nil {
log.NewError("0", "RegisterEtcd failed ", err.Error())
return
}
err = srv.Serve(listener)
if err != nil {
log.NewError("0", "Serve failed ", err.Error())
return
}
3 years ago
log.NewInfo("0", "message cms rpc success")
3 years ago
}
func (s *messageCMSServer) BoradcastMessage(_ context.Context, req *pbMessageCMS.BoradcastMessageReq) (*pbMessageCMS.BoradcastMessageResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "BoradcastMessage", req.String())
resp := &pbMessageCMS.BoradcastMessageResp{}
3 years ago
return resp, errors.WrapError(constant.ErrDB)
3 years ago
}
func (s *messageCMSServer) GetChatLogs(_ context.Context, req *pbMessageCMS.GetChatLogsReq) (*pbMessageCMS.GetChatLogsResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "GetChatLogs", req.String())
resp := &pbMessageCMS.GetChatLogsResp{}
3 years ago
time, err := utils.TimeStringToTime(req.Date)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "time string parse error", err.Error())
}
3 years ago
chatLog := db.ChatLog{
3 years ago
Content: req.Content,
SendTime: time,
3 years ago
ContentType: req.ContentType,
SessionType: req.SessionType,
}
switch chatLog.SessionType {
case constant.SingleChatType:
chatLog.SendID = req.UserId
case constant.GroupChatType:
chatLog.RecvID = req.GroupId
chatLog.SendID = req.UserId
}
nums, err := imdb.GetChatLogCount(chatLog)
resp.ChatLogsNum = int32(nums)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetChatLogCount", err.Error())
3 years ago
}
3 years ago
chatLogs, err := imdb.GetChatLog(chatLog, req.Pagination.PageNumber, req.Pagination.ShowNumber)
3 years ago
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetChatLog", err.Error())
3 years ago
return resp, errors.WrapError(constant.ErrDB)
3 years ago
}
for _, chatLog := range chatLogs {
pbChatLog := &pbMessageCMS.ChatLogs{
3 years ago
SessionType: chatLog.SessionType,
ContentType: chatLog.ContentType,
SearchContent: req.Content,
WholeContent: chatLog.Content,
Date: chatLog.CreateTime.String(),
3 years ago
SenderNickName: chatLog.SenderNickname,
3 years ago
SenderId: chatLog.SendID,
3 years ago
}
if chatLog.SenderNickname == "" {
sendUser, err := imdb.GetUserByUserID(chatLog.SendID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUserByUserID failed", err.Error())
continue
}
pbChatLog.SenderNickName = sendUser.Nickname
3 years ago
}
switch chatLog.SessionType {
case constant.SingleChatType:
recvUser, err := imdb.GetUserByUserID(chatLog.RecvID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUserByUserID failed", err.Error())
continue
}
pbChatLog.ReciverId = recvUser.UserID
pbChatLog.ReciverNickName = recvUser.Nickname
3 years ago
3 years ago
case constant.GroupChatType:
group, err := imdb.GetGroupById(chatLog.RecvID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupById failed")
continue
}
pbChatLog.GroupId = group.GroupID
pbChatLog.GroupName = group.GroupName
}
resp.ChatLogs = append(resp.ChatLogs, pbChatLog)
}
resp.Pagination = &open_im_sdk.ResponsePagination{
CurrentPage: req.Pagination.PageNumber,
ShowNumber: req.Pagination.ShowNumber,
}
3 years ago
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp output: ", resp.String())
3 years ago
return resp, nil
}
func (s *messageCMSServer) MassSendMessage(_ context.Context, req *pbMessageCMS.MassSendMessageReq) (*pbMessageCMS.MassSendMessageResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "MassSendMessage", req.String())
resp := &pbMessageCMS.MassSendMessageResp{}
return resp, nil
}
func (s *messageCMSServer) WithdrawMessage(_ context.Context, req *pbMessageCMS.WithdrawMessageReq) (*pbMessageCMS.WithdrawMessageResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "WithdrawMessage", req.String())
resp := &pbMessageCMS.WithdrawMessageResp{}
return resp, nil
}