This commit is contained in:
wangchuxiao 2022-12-21 16:46:16 +08:00
parent 26b3f742ce
commit e80afa610d
3 changed files with 112 additions and 50 deletions

View File

@ -7,11 +7,12 @@ import (
"Open_IM/pkg/common/log" "Open_IM/pkg/common/log"
server_api_params "Open_IM/pkg/proto/sdk_ws" server_api_params "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils" "Open_IM/pkg/utils"
goRedis "github.com/go-redis/redis/v8"
"github.com/golang/protobuf/proto"
"math" "math"
"strconv" "strconv"
"strings" "strings"
goRedis "github.com/go-redis/redis/v8"
"github.com/golang/protobuf/proto"
) )
const oldestList = 0 const oldestList = 0
@ -105,6 +106,8 @@ func deleteMongoMsg(operationID string, ID string, index int64, delStruct *delMs
if len(msgs.Msg) > db.GetSingleGocMsgNum() { if len(msgs.Msg) > db.GetSingleGocMsgNum() {
log.NewWarn(operationID, utils.GetSelfFuncName(), "msgs too large", len(msgs.Msg), msgs.UID) log.NewWarn(operationID, utils.GetSelfFuncName(), "msgs too large", len(msgs.Msg), msgs.UID)
} }
lastMsgSendTime := msgs.Msg[len(msgs.Msg)-1].SendTime
var hasMsgDoNotNeedDel bool var hasMsgDoNotNeedDel bool
for i, msg := range msgs.Msg { for i, msg := range msgs.Msg {
// 找到列表中不需要删除的消息了, 表示为递归到最后一个块 // 找到列表中不需要删除的消息了, 表示为递归到最后一个块
@ -130,20 +133,16 @@ func deleteMongoMsg(operationID string, ID string, index int64, delStruct *delMs
} }
// 递归结束 // 递归结束
return msgPb.Seq, nil return msgPb.Seq, nil
} else {
if !msgListIsFull(msgs) {
}
} }
} }
// 该列表中消息全部为老消息并且列表满了, 加入删除列表继续递归 // 该列表中消息全部为老消息并且列表满了, 加入删除列表继续递归
lastMsgPb := &server_api_params.MsgData{} // lastMsgPb := &server_api_params.MsgData{}
err = proto.Unmarshal(msgs.Msg[len(msgs.Msg)-1].Msg, lastMsgPb) // err = proto.Unmarshal(msgs.Msg[len(msgs.Msg)-1].Msg, lastMsgPb)
if err != nil { // if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), len(msgs.Msg)-1, msgs.UID) // log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), len(msgs.Msg)-1, msgs.UID)
return 0, utils.Wrap(err, "proto.Unmarshal failed") // return 0, utils.Wrap(err, "proto.Unmarshal failed")
} // }
delStruct.minSeq = lastMsgPb.Seq // delStruct.minSeq = lastMsgPb.Seq
if msgListIsFull(msgs) { if msgListIsFull(msgs) {
log.NewDebug(operationID, "msg list is full", msgs.UID) log.NewDebug(operationID, "msg list is full", msgs.UID)
delStruct.delUidList = append(delStruct.delUidList, msgs.UID) delStruct.delUidList = append(delStruct.delUidList, msgs.UID)

View File

@ -28,6 +28,8 @@ var (
const ( const (
PushURL = "/push/single/alias" PushURL = "/push/single/alias"
AuthURL = "/auth" AuthURL = "/auth"
TaskURL = "/push/list/message"
BatchPushURL = "/push/list/alias"
) )
func init() { func init() {
@ -53,8 +55,15 @@ type AuthResp struct {
Token string `json:"token"` Token string `json:"token"`
} }
type TaskResp struct {
TaskID string `json:"taskID"`
}
type PushReq struct { type PushReq struct {
RequestID string `json:"request_id"` RequestID string `json:"request_id"`
Settings struct {
TTL int32 `json:"ttl"`
} `json:"settings"`
Audience struct { Audience struct {
Alias []string `json:"alias"` Alias []string `json:"alias"`
} `json:"audience"` } `json:"audience"`
@ -66,6 +75,8 @@ type PushReq struct {
Ios Ios `json:"ios"` Ios Ios `json:"ios"`
Android Android `json:"android"` Android Android `json:"android"`
} `json:"push_channel"` } `json:"push_channel"`
IsAsync bool `json:"is_async"`
Taskid string `json:"taskid"`
} }
type Ios struct { type Ios struct {
@ -121,7 +132,7 @@ func (g *Getui) Push(userIDList []string, title, detailContent, operationID stri
token, err := db.DB.GetGetuiToken() token, err := db.DB.GetGetuiToken()
log.NewDebug(operationID, utils.GetSelfFuncName(), "token", token) log.NewDebug(operationID, utils.GetSelfFuncName(), "token", token)
if err != nil { if err != nil {
log.NewError(operationID, utils.OperationIDGenerator(), "GetGetuiToken failed", err.Error()) log.NewError(operationID, utils.GetSelfFuncName(), "GetGetuiToken failed", err.Error())
} }
if token == "" || err != nil { if token == "" || err != nil {
token, err = g.getTokenAndSave2Redis(operationID) token, err = g.getTokenAndSave2Redis(operationID)
@ -130,12 +141,9 @@ func (g *Getui) Push(userIDList []string, title, detailContent, operationID stri
return "", utils.Wrap(err, "") return "", utils.Wrap(err, "")
} }
} }
pushReq := PushReq{
RequestID: utils.OperationIDGenerator(), var pushReq PushReq
Audience: struct { pushResp := PushResp{}
Alias []string `json:"alias"`
}{Alias: []string{userIDList[0]}},
}
pushReq.PushMessage.Notification = Notification{ pushReq.PushMessage.Notification = Notification{
Title: title, Title: title,
Body: detailContent, Body: detailContent,
@ -143,6 +151,29 @@ func (g *Getui) Push(userIDList []string, title, detailContent, operationID stri
ChannelID: config.Config.Push.Getui.ChannelID, ChannelID: config.Config.Push.Getui.ChannelID,
ChannelName: config.Config.Push.Getui.ChannelName, ChannelName: config.Config.Push.Getui.ChannelName,
} }
if len(userIDList) > 1 {
taskID, err := db.DB.GetGetuiTaskID()
log.NewDebug(operationID, utils.GetSelfFuncName(), "token", token)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "GetGetuiTaskID failed", err.Error())
}
if taskID == "" || err != nil {
taskID, err = g.GetTaskIDAndSave2Redis(operationID, token, pushReq)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "GetTaskIDAndSave2Redis failed", err.Error())
return "", utils.Wrap(err, "")
}
}
pushReq.IsAsync = true
pushReq.Taskid = taskID
err = g.request(BatchPushURL, pushReq, token, &pushResp, operationID)
} else {
pushReq := PushReq{
RequestID: utils.OperationIDGenerator(),
Audience: struct {
Alias []string `json:"alias"`
}{Alias: []string{userIDList[0]}},
}
pushReq.PushChannel.Ios.Aps.Sound = "default" pushReq.PushChannel.Ios.Aps.Sound = "default"
pushReq.PushChannel.Ios.Aps.Alert = Alert{ pushReq.PushChannel.Ios.Aps.Alert = Alert{
Title: title, Title: title,
@ -169,8 +200,8 @@ func (g *Getui) Push(userIDList []string, title, detailContent, operationID stri
Classification: 1, Classification: 1,
}, },
} }
pushResp := PushResp{}
err = g.request(PushURL, pushReq, token, &pushResp, operationID) err = g.request(PushURL, pushReq, token, &pushResp, operationID)
}
switch err { switch err {
case TokenExpireError: case TokenExpireError:
token, err = g.getTokenAndSave2Redis(operationID) token, err = g.getTokenAndSave2Redis(operationID)
@ -209,6 +240,15 @@ func (g *Getui) Auth(operationID string, timeStamp int64) (token string, expireT
return respAuth.Token, int64(expire), err return respAuth.Token, int64(expire), err
} }
func (g *Getui) GetTaskID(operationID, token string, pushReq PushReq) (string, error) {
respTask := TaskResp{}
err := g.request(TaskURL, pushReq, token, &respTask, operationID)
if err != nil {
return "", utils.Wrap(err, "")
}
return respTask.TaskID, nil
}
func (g *Getui) request(url string, content interface{}, token string, returnStruct interface{}, operationID string) error { func (g *Getui) request(url string, content interface{}, token string, returnStruct interface{}, operationID string) error {
con, err := json.Marshal(content) con, err := json.Marshal(content)
if err != nil { if err != nil {
@ -257,3 +297,16 @@ func (g *Getui) getTokenAndSave2Redis(operationID string) (token string, err err
} }
return token, nil return token, nil
} }
func (g *Getui) GetTaskIDAndSave2Redis(operationID, token string, pushReq PushReq) (taskID string, err error) {
pushReq.Settings.TTL = 1000 * 60 * 60 * 24
taskID, err = g.GetTaskID(operationID, token, pushReq)
if err != nil {
return "", utils.Wrap(err, "GetTaskIDAndSave2Redis failed")
}
err = db.DB.SetGetuiTaskID(taskID, 60*60*23)
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
return token, nil
}

View File

@ -28,6 +28,7 @@ const (
uidPidToken = "UID_PID_TOKEN_STATUS:" uidPidToken = "UID_PID_TOKEN_STATUS:"
conversationReceiveMessageOpt = "CON_RECV_MSG_OPT:" conversationReceiveMessageOpt = "CON_RECV_MSG_OPT:"
getuiToken = "GETUI_TOKEN" getuiToken = "GETUI_TOKEN"
getuiTaskID = "GETUI_TASK_ID"
messageCache = "MESSAGE_CACHE:" messageCache = "MESSAGE_CACHE:"
SignalCache = "SIGNAL_CACHE:" SignalCache = "SIGNAL_CACHE:"
SignalListCache = "SIGNAL_LIST_CACHE:" SignalListCache = "SIGNAL_LIST_CACHE:"
@ -397,6 +398,15 @@ func (d *DataBases) GetGetuiToken() (string, error) {
return result, err return result, err
} }
func (d *DataBases) SetGetuiTaskID(taskID string, expireTime int64) error {
return d.RDB.Set(context.Background(), getuiTaskID, taskID, time.Duration(expireTime)*time.Second).Err()
}
func (d *DataBases) GetGetuiTaskID() (string, error) {
result, err := d.RDB.Get(context.Background(), getuiTaskID).Result()
return result, err
}
func (d *DataBases) SetSendMsgStatus(status int32, operationID string) error { func (d *DataBases) SetSendMsgStatus(status int32, operationID string) error {
return d.RDB.Set(context.Background(), sendMsgFailedFlag+operationID, status, time.Hour*24).Err() return d.RDB.Set(context.Background(), sendMsgFailedFlag+operationID, status, time.Hour*24).Err()
} }