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/push/logic/push_to_client.go

150 lines
5.1 KiB

4 years ago
/*
** description("").
** copyright('open-im,www.open-im.io').
** author("fg,Gordon@open-im.io").
** time(2021/3/5 14:31).
*/
package logic
import (
3 years ago
rpcChat "Open_IM/internal/rpc/chat"
"Open_IM/internal/rpc/user/internal_service"
4 years ago
"Open_IM/src/common/config"
"Open_IM/src/common/constant"
"Open_IM/src/common/log"
3 years ago
"Open_IM/src/grpc-etcdv3/getcdv3"
4 years ago
pbChat "Open_IM/src/proto/chat"
3 years ago
pbGroup "Open_IM/src/proto/group"
4 years ago
pbRelay "Open_IM/src/proto/relay"
pbGetInfo "Open_IM/src/proto/user"
"Open_IM/src/utils"
"context"
"encoding/json"
"fmt"
"strings"
)
type EChatContent struct {
SessionType int `json:"chatType"`
From string `json:"from"`
To string `json:"to"`
Seq int64 `json:"seq"`
}
func MsgToUser(sendPbData *pbRelay.MsgToUserReq, OfflineInfo, Options string) {
var wsResult []*pbRelay.SingleMsgToUser
isShouldOfflinePush := true
3 years ago
MOptions := utils.JsonStringToMap(Options) //Control whether to push message to sender's other terminal
//isSenderSync := utils.GetSwitchFromOptions(MOptions, "senderSync")
4 years ago
isOfflinePush := utils.GetSwitchFromOptions(MOptions, "offlinePush")
log.InfoByKv("Get chat from msg_transfer And push chat", sendPbData.OperationID, "PushData", sendPbData)
grpcCons := getcdv3.GetConn4Unique(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImOnlineMessageRelayName)
//Online push message
3 years ago
log.InfoByKv("test", sendPbData.OperationID, "len grpc", len(grpcCons), "data", sendPbData)
4 years ago
for _, v := range grpcCons {
msgClient := pbRelay.NewOnlineMessageRelayServiceClient(v)
reply, err := msgClient.MsgToUser(context.Background(), sendPbData)
3 years ago
if err != nil {
log.InfoByKv("push data to client rpc err", sendPbData.OperationID, "err", err)
}
4 years ago
if reply != nil && reply.Resp != nil && err == nil {
wsResult = append(wsResult, reply.Resp...)
}
}
3 years ago
log.InfoByKv("push_result", sendPbData.OperationID, "result", wsResult)
if isOfflinePush {
4 years ago
for _, t := range pushTerminal {
for _, v := range wsResult {
if v.RecvPlatFormID == t && v.ResultCode == 0 {
isShouldOfflinePush = false
break
}
}
if isShouldOfflinePush {
//Use offline push messaging
var UIDList []string
UIDList = append(UIDList, sendPbData.RecvID)
var sendUIDList []string
sendUIDList = append(sendUIDList, sendPbData.SendID)
userInfo, err := internal_service.GetUserInfoClient(&pbGetInfo.GetUserInfoReq{UserIDList: sendUIDList, OperationID: sendPbData.OperationID})
if err != nil {
log.ErrorByArgs(fmt.Sprintf("err=%v,call GetUserInfoClient rpc server failed", err))
return
}
customContent := EChatContent{
SessionType: int(sendPbData.SessionType),
From: sendPbData.SendID,
To: sendPbData.RecvID,
Seq: sendPbData.RecvSeq,
}
bCustomContent, _ := json.Marshal(customContent)
jsonCustomContent := string(bCustomContent)
switch sendPbData.ContentType {
case constant.Text:
IOSAccountListPush(UIDList, userInfo.Data[0].Name, sendPbData.Content, jsonCustomContent)
case constant.Picture:
IOSAccountListPush(UIDList, userInfo.Data[0].Name, constant.ContentType2PushContent[constant.Picture], jsonCustomContent)
case constant.Voice:
IOSAccountListPush(UIDList, userInfo.Data[0].Name, constant.ContentType2PushContent[constant.Voice], jsonCustomContent)
case constant.Video:
IOSAccountListPush(UIDList, userInfo.Data[0].Name, constant.ContentType2PushContent[constant.Video], jsonCustomContent)
case constant.File:
IOSAccountListPush(UIDList, userInfo.Data[0].Name, constant.ContentType2PushContent[constant.File], jsonCustomContent)
default:
}
} else {
isShouldOfflinePush = true
}
}
}
}
func SendMsgByWS(m *pbChat.WSToMsgSvrChatMsg) {
m.MsgID = rpcChat.GetMsgID(m.SendID)
m.ClientMsgID = m.MsgID
3 years ago
switch m.SessionType {
case constant.SingleChatType:
sendMsgToKafka(m, m.SendID, "msgKey--sendID")
sendMsgToKafka(m, m.RecvID, "msgKey--recvID")
case constant.GroupChatType:
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName)
client := pbGroup.NewGroupClient(etcdConn)
req := &pbGroup.GetGroupAllMemberReq{
GroupID: m.RecvID,
Token: config.Config.Secret,
OperationID: m.OperationID,
}
reply, err := client.GetGroupAllMember(context.Background(), req)
if err != nil {
log.Error(m.Token, m.OperationID, "rpc getGroupInfo failed, err = %s", err.Error())
return
}
if reply.ErrorCode != 0 {
log.Error(m.Token, m.OperationID, "rpc getGroupInfo failed, err = %s", reply.ErrorMsg)
return
}
groupID := m.RecvID
for i, v := range reply.MemberList {
3 years ago
m.RecvID = v.UserId + " " + groupID
sendMsgToKafka(m, utils.IntToString(i), "msgKey--recvID+\" \"+groupID")
3 years ago
}
default:
4 years ago
}
3 years ago
}
func sendMsgToKafka(m *pbChat.WSToMsgSvrChatMsg, key string, flag string) {
pid, offset, err := producer.SendMessage(m, key)
4 years ago
if err != nil {
3 years ago
log.ErrorByKv("kafka send failed", m.OperationID, "send data", m.String(), "pid", pid, "offset", offset, "err", err.Error(), flag, key)
4 years ago
}
3 years ago
4 years ago
}