This commit is contained in:
wangchuxiao
2023-02-22 19:51:14 +08:00
parent 2b38381296
commit 94d50a6c71
31 changed files with 500 additions and 602 deletions
+30 -2
View File
@@ -1,13 +1,32 @@
package getui
import "Open_IM/pkg/common/config"
import (
"Open_IM/pkg/common/config"
"fmt"
)
type CommonResp struct {
type Resp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func (r *Resp) parseError() (err error) {
switch r.Code {
case tokenExpireCode:
err = TokenExpireError
case 0:
err = nil
default:
err = fmt.Errorf("code %d, msg %s", r.Code, r.Msg)
}
return err
}
type RespI interface {
parseError() error
}
type AuthReq struct {
Sign string `json:"sign"`
Timestamp string `json:"timestamp"`
@@ -95,6 +114,10 @@ type Options struct {
} `json:"VV"`
}
type Payload struct {
IsSignal bool `json:"isSignal"`
}
func newPushReq(title, content string) PushReq {
pushReq := PushReq{PushMessage: &PushMessage{Notification: &Notification{
Title: title,
@@ -106,6 +129,11 @@ func newPushReq(title, content string) PushReq {
return pushReq
}
func newBatchPushReq(userIDs []string, taskID string) PushReq {
var IsAsync = true
return PushReq{Audience: &Audience{Alias: userIDs}, IsAsync: &IsAsync, TaskID: &taskID}
}
func (pushReq *PushReq) setPushChannel(title string, body string) {
pushReq.PushChannel = &PushChannel{}
// autoBadge := "+1"
+66 -77
View File
@@ -4,23 +4,23 @@ import (
"Open_IM/internal/push"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/db/cache"
//http2 "Open_IM/pkg/common/http"
"Open_IM/pkg/common/log"
http2 "Open_IM/pkg/common/http"
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils/splitter"
"github.com/go-redis/redis/v8"
"Open_IM/pkg/utils"
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strconv"
"time"
)
var (
TokenExpireError = errors.New("token expire")
UserIDEmptyError = errors.New("userIDs is empty")
)
const (
@@ -29,62 +29,56 @@ const (
taskURL = "/push/list/message"
batchPushURL = "/push/list/alias"
tokenExpire = 10001
ttl = 0
// codes
tokenExpireCode = 10001
tokenExpireTime = 60 * 60 * 23
taskIDTTL = 1000 * 60 * 60 * 24
)
type Client struct {
cache cache.Cache
cache cache.Cache
tokenExpireTime int64
taskIDTTL int64
}
func newClient(cache cache.Cache) *Client {
return &Client{cache: cache}
func NewClient(cache cache.Cache) *Client {
return &Client{cache: cache, tokenExpireTime: tokenExpireTime, taskIDTTL: taskIDTTL}
}
func (g *Client) Push(ctx context.Context, userIDs []string, title, content, operationID string, opts *push.Opts) error {
func (g *Client) Push(ctx context.Context, userIDs []string, title, content string, opts *push.Opts) error {
token, err := g.cache.GetGetuiToken(ctx)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "GetGetuiToken failed", err.Error())
}
if token == "" || err != nil {
token, err = g.getTokenAndSave2Redis(ctx)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "getTokenAndSave2Redis failed", err.Error())
return utils.Wrap(err, "")
if err == redis.Nil {
token, err = g.getTokenAndSave2Redis(ctx)
if err != nil {
return err
}
} else {
return err
}
}
pushReq := newPushReq(title, content)
pushReq.setPushChannel(title, content)
pushResp := struct{}{}
if len(userIDs) > 1 {
taskID, err := g.GetTaskID(ctx, token, pushReq)
if err != nil {
return utils.Wrap(err, "GetTaskIDAndSave2Redis failed")
maxNum := 999
if len(userIDs) > maxNum {
s := splitter.NewSplitter(maxNum, userIDs)
for _, v := range s.GetSplitResult() {
err = g.batchPush(ctx, token, v.Item, pushReq)
}
} else {
err = g.batchPush(ctx, token, userIDs, pushReq)
}
pushReq = PushReq{Audience: &Audience{Alias: userIDs}}
var IsAsync = true
pushReq.IsAsync = &IsAsync
pushReq.TaskID = &taskID
err = g.request(ctx, batchPushURL, pushReq, token, &pushResp)
} else if len(userIDs) == 1 {
err = g.singlePush(ctx, token, userIDs[0], pushReq)
} else {
reqID := utils.OperationIDGenerator()
pushReq.RequestID = &reqID
pushReq.Audience = &Audience{Alias: []string{userIDs[0]}}
err = g.request(ctx, pushURL, pushReq, token, &pushResp)
return UserIDEmptyError
}
switch err {
case TokenExpireError:
token, err = g.getTokenAndSave2Redis(ctx)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "getTokenAndSave2Redis failed, ", err.Error())
} else {
log.NewInfo(operationID, utils.GetSelfFuncName(), "getTokenAndSave2Redis: ", token)
}
}
if err != nil {
return utils.Wrap(err, "push failed")
}
return utils.Wrap(err, "")
return err
}
func (g *Client) Auth(ctx context.Context, timeStamp int64) (token string, expireTime int64, err error) {
@@ -101,7 +95,6 @@ func (g *Client) Auth(ctx context.Context, timeStamp int64) (token string, expir
if err != nil {
return "", 0, err
}
//log.NewInfo(operationID, utils.GetSelfFuncName(), "result: ", respAuth)
expire, err := strconv.Atoi(respAuth.ExpireTime)
return respAuth.Token, int64(expire), err
}
@@ -117,63 +110,59 @@ func (g *Client) GetTaskID(ctx context.Context, token string, pushReq PushReq) (
return respTask.TaskID, nil
}
func (g *Client) request(ctx context.Context, url string, content interface{}, token string, output interface{}) error {
con, err := json.Marshal(content)
// max num is 999
func (g *Client) batchPush(ctx context.Context, token string, userIDs []string, pushReq PushReq) error {
taskID, err := g.GetTaskID(ctx, token, pushReq)
if err != nil {
return err
}
client := &http.Client{}
req, err := http.NewRequest("POST", config.Config.Push.Getui.PushUrl+url, bytes.NewBuffer(con))
pushReq = newBatchPushReq(userIDs, taskID)
return g.request(ctx, batchPushURL, pushReq, token, nil)
}
func (g *Client) singlePush(ctx context.Context, token, userID string, pushReq PushReq) error {
operationID := tracelog.GetOperationID(ctx)
pushReq.RequestID = &operationID
pushReq.Audience = &Audience{Alias: []string{userID}}
return g.request(ctx, pushURL, pushReq, token, nil)
}
func (g *Client) request(ctx context.Context, url string, input interface{}, token string, output interface{}) error {
header := map[string]string{"token": token}
resp := &Resp{}
resp.Data = output
return g.postReturn(config.Config.Push.Getui.PushUrl+url, header, input, resp, 3)
}
func (g *Client) postReturn(url string, header map[string]string, input interface{}, output RespI, timeout int) error {
err := http2.PostReturn(url, header, input, output, timeout)
if err != nil {
return err
}
if token != "" {
req.Header.Set("token", token)
}
req.Header.Set("content-type", "application/json")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
//log.NewDebug(operationID, "getui", utils.GetSelfFuncName(), "resp, ", string(result))
commonResp := CommonResp{}
commonResp.Data = output
if err := json.Unmarshal(result, &commonResp); err != nil {
return err
}
if commonResp.Code == tokenExpire {
return TokenExpireError
}
return nil
return output.parseError()
}
func (g *Client) getTokenAndSave2Redis(ctx context.Context) (token string, err error) {
token, _, err = g.Auth(ctx, time.Now().UnixNano()/1e6)
if err != nil {
return "", utils.Wrap(err, "Auth failed")
return
}
err = g.cache.SetGetuiTaskID(ctx, token, 60*60*23)
err = g.cache.SetGetuiToken(ctx, token, 60*60*23)
if err != nil {
return "", utils.Wrap(err, "Auth failed")
return
}
return token, nil
}
func (g *Client) GetTaskIDAndSave2Redis(ctx context.Context, token string, pushReq PushReq) (taskID string, err error) {
ttl := int64(1000 * 60 * 60 * 24)
pushReq.Settings = &Settings{TTL: &ttl}
pushReq.Settings = &Settings{TTL: &g.taskIDTTL}
taskID, err = g.GetTaskID(ctx, token, pushReq)
if err != nil {
return "", utils.Wrap(err, "GetTaskIDAndSave2Redis failed")
return
}
err = g.cache.SetGetuiTaskID(ctx, taskID, 60*60*23)
err = g.cache.SetGetuiTaskID(ctx, taskID, g.tokenExpireTime)
if err != nil {
return "", utils.Wrap(err, "Auth failed")
return
}
return token, nil
}