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.
289 lines
11 KiB
289 lines
11 KiB
4 years ago
|
/*
|
||
|
** description("").
|
||
|
** copyright('open-im,www.open-im.io').
|
||
|
** author("fg,Gordon@open-im.io").
|
||
|
** time(2021/3/5 14:31).
|
||
|
*/
|
||
2 years ago
|
package push
|
||
4 years ago
|
|
||
|
import (
|
||
3 years ago
|
"Open_IM/pkg/common/config"
|
||
3 years ago
|
"Open_IM/pkg/common/constant"
|
||
3 years ago
|
"Open_IM/pkg/common/log"
|
||
2 years ago
|
"Open_IM/pkg/common/prome"
|
||
3 years ago
|
pbPush "Open_IM/pkg/proto/push"
|
||
3 years ago
|
pbRelay "Open_IM/pkg/proto/relay"
|
||
2 years ago
|
pbRtc "Open_IM/pkg/proto/rtc"
|
||
3 years ago
|
"Open_IM/pkg/utils"
|
||
4 years ago
|
"context"
|
||
2 years ago
|
"github.com/golang/protobuf/proto"
|
||
2 years ago
|
"strings"
|
||
4 years ago
|
)
|
||
|
|
||
3 years ago
|
type AtContent struct {
|
||
|
Text string `json:"text"`
|
||
|
AtUserList []string `json:"atUserList"`
|
||
|
IsAtSelf bool `json:"isAtSelf"`
|
||
|
}
|
||
4 years ago
|
|
||
3 years ago
|
func MsgToUser(pushMsg *pbPush.PushMsgReq) {
|
||
2 years ago
|
var wsResult []*pbRelay.SingelMsgToUserResultList
|
||
3 years ago
|
isOfflinePush := utils.GetSwitchFromOptions(pushMsg.MsgData.Options, constant.IsOfflinePush)
|
||
3 years ago
|
log.Debug(pushMsg.OperationID, "Get msg from msg_transfer And push msg", pushMsg.String())
|
||
2 years ago
|
|
||
2 years ago
|
grpcCons := rpc.GetDefaultGatewayConn4Unique(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), pushMsg.OperationID)
|
||
2 years ago
|
|
||
|
var UIDList = []string{pushMsg.PushToUserID}
|
||
|
callbackResp := callbackOnlinePush(pushMsg.OperationID, UIDList, pushMsg.MsgData)
|
||
|
log.NewDebug(pushMsg.OperationID, utils.GetSelfFuncName(), "OnlinePush callback Resp")
|
||
|
if callbackResp.ErrCode != 0 {
|
||
|
log.NewError(pushMsg.OperationID, utils.GetSelfFuncName(), "callbackOnlinePush result: ", callbackResp)
|
||
|
}
|
||
|
if callbackResp.ActionCode != constant.ActionAllow {
|
||
|
log.NewDebug(pushMsg.OperationID, utils.GetSelfFuncName(), "OnlinePush stop")
|
||
|
return
|
||
|
}
|
||
|
|
||
4 years ago
|
//Online push message
|
||
2 years ago
|
log.Debug(pushMsg.OperationID, "len grpc", len(grpcCons), "data", pushMsg.String())
|
||
4 years ago
|
for _, v := range grpcCons {
|
||
2 years ago
|
msgClient := pbRelay.NewRelayClient(v)
|
||
2 years ago
|
reply, err := msgClient.SuperGroupOnlineBatchPushOneMsg(context.Background(), &pbRelay.OnlineBatchPushOneMsgReq{OperationID: pushMsg.OperationID, MsgData: pushMsg.MsgData, PushToUserIDList: []string{pushMsg.PushToUserID}})
|
||
3 years ago
|
if err != nil {
|
||
2 years ago
|
log.NewError("SuperGroupOnlineBatchPushOneMsg push data to client rpc err", pushMsg.OperationID, "err", err)
|
||
3 years ago
|
continue
|
||
3 years ago
|
}
|
||
2 years ago
|
if reply != nil && reply.SinglePushResult != nil {
|
||
|
wsResult = append(wsResult, reply.SinglePushResult...)
|
||
4 years ago
|
}
|
||
|
}
|
||
2 years ago
|
log.NewInfo(pushMsg.OperationID, "push_result", wsResult, "sendData", pushMsg.MsgData, "isOfflinePush", isOfflinePush)
|
||
3 years ago
|
successCount++
|
||
3 years ago
|
if isOfflinePush && pushMsg.PushToUserID != pushMsg.MsgData.SendID {
|
||
2 years ago
|
// save invitation info for offline push
|
||
3 years ago
|
for _, v := range wsResult {
|
||
2 years ago
|
if v.OnlinePush {
|
||
|
return
|
||
3 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
if pushMsg.MsgData.ContentType == constant.SignalingNotification {
|
||
2 years ago
|
isSend, err := db.DB.HandleSignalInfo(pushMsg.OperationID, pushMsg.MsgData, pushMsg.PushToUserID)
|
||
|
if err != nil {
|
||
2 years ago
|
log.NewError(pushMsg.OperationID, utils.GetSelfFuncName(), err.Error(), pushMsg.MsgData)
|
||
|
return
|
||
|
}
|
||
2 years ago
|
if !isSend {
|
||
|
return
|
||
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
var title, detailContent string
|
||
|
callbackResp := callbackOfflinePush(pushMsg.OperationID, UIDList, pushMsg.MsgData, &[]string{})
|
||
|
log.NewDebug(pushMsg.OperationID, utils.GetSelfFuncName(), "offline callback Resp")
|
||
|
if callbackResp.ErrCode != 0 {
|
||
|
log.NewError(pushMsg.OperationID, utils.GetSelfFuncName(), "callbackOfflinePush result: ", callbackResp)
|
||
|
}
|
||
|
if callbackResp.ActionCode != constant.ActionAllow {
|
||
|
log.NewDebug(pushMsg.OperationID, utils.GetSelfFuncName(), "offlinePush stop")
|
||
|
return
|
||
2 years ago
|
}
|
||
|
if pushMsg.MsgData.OfflinePushInfo != nil {
|
||
2 years ago
|
title = pushMsg.MsgData.OfflinePushInfo.Title
|
||
|
detailContent = pushMsg.MsgData.OfflinePushInfo.Desc
|
||
|
}
|
||
|
|
||
|
if offlinePusher == nil {
|
||
|
return
|
||
|
}
|
||
|
opts, err := GetOfflinePushOpts(pushMsg)
|
||
|
if err != nil {
|
||
|
log.NewError(pushMsg.OperationID, utils.GetSelfFuncName(), "GetOfflinePushOpts failed", pushMsg, err.Error())
|
||
2 years ago
|
}
|
||
2 years ago
|
log.NewInfo(pushMsg.OperationID, utils.GetSelfFuncName(), UIDList, title, detailContent, "opts:", opts)
|
||
|
if title == "" {
|
||
2 years ago
|
switch pushMsg.MsgData.ContentType {
|
||
|
case constant.Text:
|
||
2 years ago
|
fallthrough
|
||
2 years ago
|
case constant.Picture:
|
||
2 years ago
|
fallthrough
|
||
2 years ago
|
case constant.Voice:
|
||
2 years ago
|
fallthrough
|
||
2 years ago
|
case constant.Video:
|
||
2 years ago
|
fallthrough
|
||
2 years ago
|
case constant.File:
|
||
2 years ago
|
title = constant.ContentType2PushContent[int64(pushMsg.MsgData.ContentType)]
|
||
2 years ago
|
case constant.AtText:
|
||
|
a := AtContent{}
|
||
|
_ = utils.JsonStringToStruct(string(pushMsg.MsgData.Content), &a)
|
||
|
if utils.IsContain(pushMsg.PushToUserID, a.AtUserList) {
|
||
2 years ago
|
title = constant.ContentType2PushContent[constant.AtText] + constant.ContentType2PushContent[constant.Common]
|
||
3 years ago
|
} else {
|
||
2 years ago
|
title = constant.ContentType2PushContent[constant.GroupMsg]
|
||
2 years ago
|
}
|
||
2 years ago
|
case constant.SignalingNotification:
|
||
2 years ago
|
title = constant.ContentType2PushContent[constant.SignalMsg]
|
||
2 years ago
|
default:
|
||
2 years ago
|
title = constant.ContentType2PushContent[constant.Common]
|
||
2 years ago
|
|
||
3 years ago
|
}
|
||
2 years ago
|
// detailContent = title
|
||
|
}
|
||
|
if detailContent == "" {
|
||
2 years ago
|
detailContent = title
|
||
2 years ago
|
}
|
||
2 years ago
|
pushResult, err := offlinePusher.Push(UIDList, title, detailContent, pushMsg.OperationID, opts)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
prome.PromeInc(prome.MsgOfflinePushFailedCounter)
|
||
2 years ago
|
log.NewError(pushMsg.OperationID, "offline push error", pushMsg.String(), err.Error())
|
||
|
} else {
|
||
2 years ago
|
prome.PromeInc(prome.MsgOfflinePushSuccessCounter)
|
||
2 years ago
|
log.NewDebug(pushMsg.OperationID, "offline push return result is ", pushResult, pushMsg.MsgData)
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
3 years ago
|
|
||
2 years ago
|
func MsgToSuperGroupUser(pushMsg *pbPush.PushMsgReq) {
|
||
2 years ago
|
var wsResult []*pbRelay.SingelMsgToUserResultList
|
||
|
isOfflinePush := utils.GetSwitchFromOptions(pushMsg.MsgData.Options, constant.IsOfflinePush)
|
||
2 years ago
|
log.Debug(pushMsg.OperationID, "Get super group msg from msg_transfer And push msg", pushMsg.String(), config.Config.Callback.CallbackBeforeSuperGroupOnlinePush.Enable)
|
||
2 years ago
|
var pushToUserIDList []string
|
||
|
if config.Config.Callback.CallbackBeforeSuperGroupOnlinePush.Enable {
|
||
|
callbackResp := callbackBeforeSuperGroupOnlinePush(pushMsg.OperationID, pushMsg.PushToUserID, pushMsg.MsgData, &pushToUserIDList)
|
||
|
log.NewDebug(pushMsg.OperationID, utils.GetSelfFuncName(), "offline callback Resp")
|
||
|
if callbackResp.ErrCode != 0 {
|
||
|
log.NewError(pushMsg.OperationID, utils.GetSelfFuncName(), "callbackOfflinePush result: ", callbackResp)
|
||
|
}
|
||
|
if callbackResp.ActionCode != constant.ActionAllow {
|
||
|
log.NewDebug(pushMsg.OperationID, utils.GetSelfFuncName(), "onlinePush stop")
|
||
|
return
|
||
|
}
|
||
|
log.NewDebug(pushMsg.OperationID, utils.GetSelfFuncName(), "callback userIDList Resp", pushToUserIDList)
|
||
2 years ago
|
}
|
||
|
if len(pushToUserIDList) == 0 {
|
||
2 years ago
|
userIDList, err := utils.GetGroupMemberUserIDList(context.Background(), pushMsg.MsgData.GroupID, pushMsg.OperationID)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
log.Error(pushMsg.OperationID, "GetGroupMemberUserIDList failed ", err.Error(), pushMsg.MsgData.GroupID)
|
||
2 years ago
|
return
|
||
|
}
|
||
2 years ago
|
pushToUserIDList = userIDList
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
grpcCons := rpc.GetDefaultGatewayConn4Unique(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), pushMsg.OperationID)
|
||
2 years ago
|
|
||
2 years ago
|
//Online push message
|
||
2 years ago
|
log.Debug(pushMsg.OperationID, "len grpc", len(grpcCons), "data", pushMsg.String())
|
||
2 years ago
|
for _, v := range grpcCons {
|
||
2 years ago
|
msgClient := pbRelay.NewRelayClient(v)
|
||
2 years ago
|
reply, err := msgClient.SuperGroupOnlineBatchPushOneMsg(context.Background(), &pbRelay.OnlineBatchPushOneMsgReq{OperationID: pushMsg.OperationID, MsgData: pushMsg.MsgData, PushToUserIDList: pushToUserIDList})
|
||
2 years ago
|
if err != nil {
|
||
|
log.NewError("push data to client rpc err", pushMsg.OperationID, "err", err)
|
||
|
continue
|
||
|
}
|
||
|
if reply != nil && reply.SinglePushResult != nil {
|
||
|
wsResult = append(wsResult, reply.SinglePushResult...)
|
||
|
}
|
||
|
}
|
||
|
log.Debug(pushMsg.OperationID, "push_result", wsResult, "sendData", pushMsg.MsgData)
|
||
|
successCount++
|
||
|
if isOfflinePush {
|
||
|
var onlineSuccessUserIDList []string
|
||
|
onlineSuccessUserIDList = append(onlineSuccessUserIDList, pushMsg.MsgData.SendID)
|
||
|
for _, v := range wsResult {
|
||
|
if v.OnlinePush && v.UserID != pushMsg.MsgData.SendID {
|
||
|
onlineSuccessUserIDList = append(onlineSuccessUserIDList, v.UserID)
|
||
|
}
|
||
|
}
|
||
2 years ago
|
onlineFailedUserIDList := utils.DifferenceString(onlineSuccessUserIDList, pushToUserIDList)
|
||
2 years ago
|
//Use offline push messaging
|
||
2 years ago
|
var title, detailContent string
|
||
2 years ago
|
if len(onlineFailedUserIDList) > 0 {
|
||
2 years ago
|
var offlinePushUserIDList []string
|
||
|
var needOfflinePushUserIDList []string
|
||
2 years ago
|
callbackResp := callbackOfflinePush(pushMsg.OperationID, onlineFailedUserIDList, pushMsg.MsgData, &offlinePushUserIDList)
|
||
2 years ago
|
log.NewDebug(pushMsg.OperationID, utils.GetSelfFuncName(), "offline callback Resp")
|
||
|
if callbackResp.ErrCode != 0 {
|
||
|
log.NewError(pushMsg.OperationID, utils.GetSelfFuncName(), "callbackOfflinePush result: ", callbackResp)
|
||
|
}
|
||
|
if callbackResp.ActionCode != constant.ActionAllow {
|
||
|
log.NewDebug(pushMsg.OperationID, utils.GetSelfFuncName(), "offlinePush stop")
|
||
|
return
|
||
|
}
|
||
2 years ago
|
if pushMsg.MsgData.OfflinePushInfo != nil {
|
||
|
title = pushMsg.MsgData.OfflinePushInfo.Title
|
||
|
detailContent = pushMsg.MsgData.OfflinePushInfo.Desc
|
||
|
}
|
||
2 years ago
|
if len(offlinePushUserIDList) > 0 {
|
||
|
needOfflinePushUserIDList = offlinePushUserIDList
|
||
|
} else {
|
||
|
needOfflinePushUserIDList = onlineFailedUserIDList
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
if offlinePusher == nil {
|
||
|
return
|
||
|
}
|
||
|
opts, err := GetOfflinePushOpts(pushMsg)
|
||
|
if err != nil {
|
||
|
log.NewError(pushMsg.OperationID, utils.GetSelfFuncName(), "GetOfflinePushOpts failed", pushMsg, err.Error())
|
||
|
}
|
||
2 years ago
|
log.NewInfo(pushMsg.OperationID, utils.GetSelfFuncName(), needOfflinePushUserIDList, title, detailContent, "opts:", opts)
|
||
2 years ago
|
if title == "" {
|
||
|
switch pushMsg.MsgData.ContentType {
|
||
|
case constant.Text:
|
||
|
fallthrough
|
||
|
case constant.Picture:
|
||
|
fallthrough
|
||
|
case constant.Voice:
|
||
|
fallthrough
|
||
|
case constant.Video:
|
||
|
fallthrough
|
||
|
case constant.File:
|
||
|
title = constant.ContentType2PushContent[int64(pushMsg.MsgData.ContentType)]
|
||
|
case constant.AtText:
|
||
|
a := AtContent{}
|
||
|
_ = utils.JsonStringToStruct(string(pushMsg.MsgData.Content), &a)
|
||
|
if utils.IsContain(pushMsg.PushToUserID, a.AtUserList) {
|
||
|
title = constant.ContentType2PushContent[constant.AtText] + constant.ContentType2PushContent[constant.Common]
|
||
|
} else {
|
||
|
title = constant.ContentType2PushContent[constant.GroupMsg]
|
||
|
}
|
||
|
case constant.SignalingNotification:
|
||
|
title = constant.ContentType2PushContent[constant.SignalMsg]
|
||
|
default:
|
||
|
title = constant.ContentType2PushContent[constant.Common]
|
||
|
|
||
|
}
|
||
|
detailContent = title
|
||
|
}
|
||
|
pushResult, err := offlinePusher.Push(needOfflinePushUserIDList, title, detailContent, pushMsg.OperationID, opts)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
prome.PromeInc(prome.MsgOfflinePushFailedCounter)
|
||
2 years ago
|
log.NewError(pushMsg.OperationID, "offline push error", pushMsg.String(), err.Error())
|
||
|
} else {
|
||
2 years ago
|
prome.PromeInc(prome.MsgOfflinePushSuccessCounter)
|
||
2 years ago
|
log.NewDebug(pushMsg.OperationID, "offline push return result is ", pushResult, pushMsg.MsgData)
|
||
|
}
|
||
2 years ago
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
2 years ago
|
func GetOfflinePushOpts(pushMsg *pbPush.PushMsgReq) (opts *Opts, err error) {
|
||
|
if pushMsg.MsgData.ContentType > constant.SignalingNotificationBegin && pushMsg.MsgData.ContentType < constant.SignalingNotificationEnd {
|
||
2 years ago
|
req := &pbRtc.SignalReq{}
|
||
2 years ago
|
if err := proto.Unmarshal(pushMsg.MsgData.Content, req); err != nil {
|
||
2 years ago
|
return nil, utils.Wrap(err, "")
|
||
2 years ago
|
}
|
||
2 years ago
|
opts = &Opts{}
|
||
2 years ago
|
switch req.Payload.(type) {
|
||
2 years ago
|
case *pbRtc.SignalReq_Invite, *pbRtc.SignalReq_InviteInGroup:
|
||
|
opts.Signal.ClientMsgID = pushMsg.MsgData.ClientMsgID
|
||
2 years ago
|
log.NewDebug(pushMsg.OperationID, opts)
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
if pushMsg.MsgData.OfflinePushInfo != nil {
|
||
2 years ago
|
opts = &Opts{}
|
||
2 years ago
|
opts.IOSBadgeCount = pushMsg.MsgData.OfflinePushInfo.IOSBadgeCount
|
||
|
opts.IOSPushSound = pushMsg.MsgData.OfflinePushInfo.IOSPushSound
|
||
2 years ago
|
opts.Ex = pushMsg.MsgData.OfflinePushInfo.Ex
|
||
2 years ago
|
}
|
||
2 years ago
|
return opts, nil
|
||
|
}
|