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

169 lines
4.4 KiB

package getui
3 years ago
import (
"Open_IM/internal/push"
3 years ago
"Open_IM/pkg/common/config"
2 years ago
"Open_IM/pkg/common/db/cache"
2 years ago
http2 "Open_IM/pkg/common/http"
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils/splitter"
"github.com/go-redis/redis/v8"
3 years ago
"Open_IM/pkg/utils"
2 years ago
"context"
3 years ago
"crypto/sha256"
3 years ago
"encoding/hex"
2 years ago
"errors"
3 years ago
"strconv"
"time"
)
var (
3 years ago
TokenExpireError = errors.New("token expire")
2 years ago
UserIDEmptyError = errors.New("userIDs is empty")
3 years ago
)
3 years ago
const (
2 years ago
pushURL = "/push/single/alias"
authURL = "/auth"
taskURL = "/push/list/message"
batchPushURL = "/push/list/alias"
3 years ago
2 years ago
// codes
tokenExpireCode = 10001
tokenExpireTime = 60 * 60 * 23
taskIDTTL = 1000 * 60 * 60 * 24
2 years ago
)
2 years ago
2 years ago
type Client struct {
2 years ago
cache cache.Cache
tokenExpireTime int64
taskIDTTL int64
3 years ago
}
2 years ago
func NewClient(cache cache.Cache) *Client {
return &Client{cache: cache, tokenExpireTime: tokenExpireTime, taskIDTTL: taskIDTTL}
3 years ago
}
2 years ago
func (g *Client) Push(ctx context.Context, userIDs []string, title, content string, opts *push.Opts) error {
2 years ago
token, err := g.cache.GetGetuiToken(ctx)
3 years ago
if err != nil {
2 years ago
if err == redis.Nil {
token, err = g.getTokenAndSave2Redis(ctx)
if err != nil {
return err
}
} else {
return err
3 years ago
}
}
2 years ago
pushReq := newPushReq(title, content)
pushReq.setPushChannel(title, content)
if len(userIDs) > 1 {
2 years ago
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)
2 years ago
}
2 years ago
} else if len(userIDs) == 1 {
err = g.singlePush(ctx, token, userIDs[0], pushReq)
2 years ago
} else {
2 years ago
return UserIDEmptyError
2 years ago
}
3 years ago
switch err {
case TokenExpireError:
2 years ago
token, err = g.getTokenAndSave2Redis(ctx)
3 years ago
}
2 years ago
return err
}
2 years ago
func (g *Client) Auth(ctx context.Context, timeStamp int64) (token string, expireTime int64, err error) {
3 years ago
h := sha256.New()
3 years ago
h.Write([]byte(config.Config.Push.Getui.AppKey + strconv.Itoa(int(timeStamp)) + config.Config.Push.Getui.MasterSecret))
2 years ago
sign := hex.EncodeToString(h.Sum(nil))
3 years ago
reqAuth := AuthReq{
Sign: sign,
Timestamp: strconv.Itoa(int(timeStamp)),
2 years ago
AppKey: config.Config.Push.Getui.AppKey,
3 years ago
}
respAuth := AuthResp{}
2 years ago
err = g.request(ctx, authURL, reqAuth, "", &respAuth)
3 years ago
if err != nil {
return "", 0, err
}
3 years ago
expire, err := strconv.Atoi(respAuth.ExpireTime)
return respAuth.Token, int64(expire), err
}
2 years ago
func (g *Client) GetTaskID(ctx context.Context, token string, pushReq PushReq) (string, error) {
2 years ago
respTask := TaskResp{}
2 years ago
ttl := int64(1000 * 60 * 5)
pushReq.Settings = &Settings{TTL: &ttl}
2 years ago
err := g.request(ctx, taskURL, pushReq, token, &respTask)
2 years ago
if err != nil {
return "", utils.Wrap(err, "")
}
return respTask.TaskID, nil
}
2 years ago
// 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)
3 years ago
if err != nil {
return err
}
2 years ago
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)
3 years ago
if err != nil {
return err
}
2 years ago
return output.parseError()
}
3 years ago
2 years ago
func (g *Client) getTokenAndSave2Redis(ctx context.Context) (token string, err error) {
token, _, err = g.Auth(ctx, time.Now().UnixNano()/1e6)
3 years ago
if err != nil {
2 years ago
return
3 years ago
}
2 years ago
err = g.cache.SetGetuiToken(ctx, token, 60*60*23)
3 years ago
if err != nil {
2 years ago
return
3 years ago
}
return token, nil
}
2 years ago
2 years ago
func (g *Client) GetTaskIDAndSave2Redis(ctx context.Context, token string, pushReq PushReq) (taskID string, err error) {
2 years ago
pushReq.Settings = &Settings{TTL: &g.taskIDTTL}
2 years ago
taskID, err = g.GetTaskID(ctx, token, pushReq)
2 years ago
if err != nil {
2 years ago
return
2 years ago
}
2 years ago
err = g.cache.SetGetuiTaskID(ctx, taskID, g.tokenExpireTime)
2 years ago
if err != nil {
2 years ago
return
2 years ago
}
return token, nil
}