Files
open-im-server/internal/push/offlinepush/getui/push.go
T

180 lines
5.0 KiB
Go
Raw Normal View History

2022-04-05 10:27:34 +08:00
package getui
2022-04-08 15:40:07 +08:00
import (
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
http2 "github.com/OpenIMSDK/Open-IM-Server/pkg/common/http"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2023-03-21 12:28:21 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils/splitter"
2023-02-22 19:51:14 +08:00
"github.com/go-redis/redis/v8"
2023-02-23 17:28:57 +08:00
"sync"
2023-02-22 19:51:14 +08:00
2023-02-20 10:13:29 +08:00
"context"
2022-04-08 17:48:16 +08:00
"crypto/sha256"
2022-04-08 16:15:22 +08:00
"encoding/hex"
2023-02-20 10:13:29 +08:00
"errors"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2022-04-08 15:40:07 +08:00
"strconv"
"time"
)
var (
2022-04-12 11:12:36 +08:00
TokenExpireError = errors.New("token expire")
2023-02-22 19:51:14 +08:00
UserIDEmptyError = errors.New("userIDs is empty")
2022-04-08 15:40:07 +08:00
)
2022-04-08 17:00:28 +08:00
const (
2023-02-20 10:13:29 +08:00
pushURL = "/push/single/alias"
authURL = "/auth"
taskURL = "/push/list/message"
batchPushURL = "/push/list/alias"
2022-04-08 15:40:07 +08:00
2023-02-22 19:51:14 +08:00
// codes
tokenExpireCode = 10001
tokenExpireTime = 60 * 60 * 23
taskIDTTL = 1000 * 60 * 60 * 24
2023-02-20 10:13:29 +08:00
)
2022-08-10 19:31:57 +08:00
2023-02-20 10:13:29 +08:00
type Client struct {
2023-05-04 12:11:29 +08:00
cache cache.MsgModel
2023-02-22 19:51:14 +08:00
tokenExpireTime int64
taskIDTTL int64
2022-04-08 15:40:07 +08:00
}
2023-05-04 12:11:29 +08:00
func NewClient(cache cache.MsgModel) *Client {
2023-02-22 19:51:14 +08:00
return &Client{cache: cache, tokenExpireTime: tokenExpireTime, taskIDTTL: taskIDTTL}
2022-04-08 15:40:07 +08:00
}
2023-03-03 19:59:10 +08:00
func (g *Client) Push(ctx context.Context, userIDs []string, title, content string, opts *offlinepush.Opts) error {
2023-02-20 10:13:29 +08:00
token, err := g.cache.GetGetuiToken(ctx)
2022-04-08 15:40:07 +08:00
if err != nil {
2023-02-22 19:51:14 +08:00
if err == redis.Nil {
token, err = g.getTokenAndSave2Redis(ctx)
if err != nil {
return err
}
} else {
return err
2022-04-08 15:40:07 +08:00
}
}
2023-02-20 10:13:29 +08:00
pushReq := newPushReq(title, content)
pushReq.setPushChannel(title, content)
if len(userIDs) > 1 {
2023-02-22 19:51:14 +08:00
maxNum := 999
if len(userIDs) > maxNum {
s := splitter.NewSplitter(maxNum, userIDs)
2023-02-23 17:28:57 +08:00
wg := sync.WaitGroup{}
wg.Add(len(s.GetSplitResult()))
2023-02-23 10:47:50 +08:00
for i, v := range s.GetSplitResult() {
go func(index int, userIDs []string) {
2023-02-23 17:28:57 +08:00
defer wg.Done()
2023-02-24 10:41:34 +08:00
if err2 := g.batchPush(ctx, token, userIDs, pushReq); err2 != nil {
2023-03-21 12:28:21 +08:00
log.NewError(mcontext.GetOperationID(ctx), "batchPush failed", i, token, pushReq)
2023-02-24 10:41:34 +08:00
err = err2
2023-02-23 10:47:50 +08:00
}
}(i, v.Item)
2023-02-22 19:51:14 +08:00
}
2023-02-23 17:28:57 +08:00
wg.Wait()
2023-02-22 19:51:14 +08:00
} else {
err = g.batchPush(ctx, token, userIDs, pushReq)
2022-12-21 16:46:16 +08:00
}
2023-02-22 19:51:14 +08:00
} else if len(userIDs) == 1 {
err = g.singlePush(ctx, token, userIDs[0], pushReq)
2022-12-21 16:46:16 +08:00
} else {
2023-02-22 19:51:14 +08:00
return UserIDEmptyError
2022-08-10 19:31:57 +08:00
}
2022-04-12 11:12:36 +08:00
switch err {
case TokenExpireError:
2023-02-20 10:13:29 +08:00
token, err = g.getTokenAndSave2Redis(ctx)
2022-04-12 11:12:36 +08:00
}
2023-02-22 19:51:14 +08:00
return err
2022-04-05 10:27:34 +08:00
}
2023-02-20 10:13:29 +08:00
func (g *Client) Auth(ctx context.Context, timeStamp int64) (token string, expireTime int64, err error) {
2022-04-08 17:48:16 +08:00
h := sha256.New()
2022-04-08 16:15:22 +08:00
h.Write([]byte(config.Config.Push.Getui.AppKey + strconv.Itoa(int(timeStamp)) + config.Config.Push.Getui.MasterSecret))
2023-02-20 10:13:29 +08:00
sign := hex.EncodeToString(h.Sum(nil))
2022-04-08 15:40:07 +08:00
reqAuth := AuthReq{
Sign: sign,
Timestamp: strconv.Itoa(int(timeStamp)),
2023-02-20 10:13:29 +08:00
AppKey: config.Config.Push.Getui.AppKey,
2022-04-08 15:40:07 +08:00
}
respAuth := AuthResp{}
2023-02-20 10:13:29 +08:00
err = g.request(ctx, authURL, reqAuth, "", &respAuth)
2022-04-08 16:40:39 +08:00
if err != nil {
return "", 0, err
}
2022-04-08 15:40:07 +08:00
expire, err := strconv.Atoi(respAuth.ExpireTime)
return respAuth.Token, int64(expire), err
2022-04-05 10:27:34 +08:00
}
2023-02-20 10:13:29 +08:00
func (g *Client) GetTaskID(ctx context.Context, token string, pushReq PushReq) (string, error) {
2022-12-21 16:46:16 +08:00
respTask := TaskResp{}
2022-12-22 11:10:19 +08:00
ttl := int64(1000 * 60 * 5)
pushReq.Settings = &Settings{TTL: &ttl}
2023-02-20 10:13:29 +08:00
err := g.request(ctx, taskURL, pushReq, token, &respTask)
2022-12-21 16:46:16 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return respTask.TaskID, nil
}
2023-02-22 19:51:14 +08:00
// 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)
2022-04-08 15:40:07 +08:00
if err != nil {
return err
}
2023-02-22 19:51:14 +08:00
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 {
2023-03-21 12:28:21 +08:00
operationID := mcontext.GetOperationID(ctx)
2023-02-22 19:51:14 +08:00
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)
2022-04-08 15:40:07 +08:00
if err != nil {
return err
}
2023-03-06 16:28:42 +08:00
return output.parseError()
2022-04-05 10:27:34 +08:00
}
2022-04-11 14:41:09 +08:00
2023-02-20 10:13:29 +08:00
func (g *Client) getTokenAndSave2Redis(ctx context.Context) (token string, err error) {
token, _, err = g.Auth(ctx, time.Now().UnixNano()/1e6)
2022-04-11 14:41:09 +08:00
if err != nil {
2023-02-22 19:51:14 +08:00
return
2022-04-11 14:41:09 +08:00
}
2023-02-22 19:51:14 +08:00
err = g.cache.SetGetuiToken(ctx, token, 60*60*23)
2022-04-11 14:41:09 +08:00
if err != nil {
2023-02-22 19:51:14 +08:00
return
2022-04-11 14:41:09 +08:00
}
return token, nil
}
2022-12-21 16:46:16 +08:00
2023-02-20 10:13:29 +08:00
func (g *Client) GetTaskIDAndSave2Redis(ctx context.Context, token string, pushReq PushReq) (taskID string, err error) {
2023-02-22 19:51:14 +08:00
pushReq.Settings = &Settings{TTL: &g.taskIDTTL}
2023-02-20 10:13:29 +08:00
taskID, err = g.GetTaskID(ctx, token, pushReq)
2022-12-21 16:46:16 +08:00
if err != nil {
2023-02-22 19:51:14 +08:00
return
2022-12-21 16:46:16 +08:00
}
2023-02-22 19:51:14 +08:00
err = g.cache.SetGetuiTaskID(ctx, taskID, g.tokenExpireTime)
2022-12-21 16:46:16 +08:00
if err != nil {
2023-02-22 19:51:14 +08:00
return
2022-12-21 16:46:16 +08:00
}
return token, nil
}