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

180 lines
4.9 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 (
2022-06-02 19:14:11 +08:00
"Open_IM/internal/push"
2022-04-08 15:40:07 +08:00
"Open_IM/pkg/common/config"
2023-02-20 10:13:29 +08:00
"Open_IM/pkg/common/db/cache"
//http2 "Open_IM/pkg/common/http"
2022-04-08 15:40:07 +08:00
"Open_IM/pkg/common/log"
"Open_IM/pkg/utils"
"bytes"
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"
2022-04-08 15:40:07 +08:00
"encoding/json"
2023-02-20 10:13:29 +08:00
"errors"
2022-04-08 15:40:07 +08:00
"io/ioutil"
"net/http"
"strconv"
"time"
)
var (
2022-04-12 11:12:36 +08:00
TokenExpireError = errors.New("token expire")
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-20 10:13:29 +08:00
tokenExpire = 10001
ttl = 0
)
2022-08-10 19:31:57 +08:00
2023-02-20 10:13:29 +08:00
type Client struct {
cache cache.Cache
2022-04-08 15:40:07 +08:00
}
2023-02-20 10:13:29 +08:00
func newClient(cache cache.Cache) *Client {
return &Client{cache: cache}
2022-04-08 15:40:07 +08:00
}
2023-02-20 10:13:29 +08:00
func (g *Client) Push(ctx context.Context, userIDs []string, title, content, operationID string, opts *push.Opts) error {
token, err := g.cache.GetGetuiToken(ctx)
2022-04-08 15:40:07 +08:00
if err != nil {
2022-12-21 16:46:16 +08:00
log.NewError(operationID, utils.GetSelfFuncName(), "GetGetuiToken failed", err.Error())
2022-04-08 15:40:07 +08:00
}
if token == "" || err != nil {
2023-02-20 10:13:29 +08:00
token, err = g.getTokenAndSave2Redis(ctx)
2022-04-08 15:40:07 +08:00
if err != nil {
2022-04-11 14:41:09 +08:00
log.NewError(operationID, utils.GetSelfFuncName(), "getTokenAndSave2Redis failed", err.Error())
2023-02-20 10:13:29 +08:00
return utils.Wrap(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)
pushResp := struct{}{}
if len(userIDs) > 1 {
taskID, err := g.GetTaskID(ctx, token, pushReq)
2022-12-21 16:46:16 +08:00
if err != nil {
2023-02-20 10:13:29 +08:00
return utils.Wrap(err, "GetTaskIDAndSave2Redis failed")
2022-12-21 16:46:16 +08:00
}
2023-02-20 10:13:29 +08:00
pushReq = PushReq{Audience: &Audience{Alias: userIDs}}
2022-12-22 17:35:09 +08:00
var IsAsync = true
2022-12-21 18:03:39 +08:00
pushReq.IsAsync = &IsAsync
2023-02-20 10:13:29 +08:00
pushReq.TaskID = &taskID
err = g.request(ctx, batchPushURL, pushReq, token, &pushResp)
2022-12-21 16:46:16 +08:00
} else {
2022-12-21 18:03:39 +08:00
reqID := utils.OperationIDGenerator()
2022-12-21 19:56:25 +08:00
pushReq.RequestID = &reqID
2023-02-20 10:13:29 +08:00
pushReq.Audience = &Audience{Alias: []string{userIDs[0]}}
err = g.request(ctx, pushURL, pushReq, token, &pushResp)
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
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "getTokenAndSave2Redis failed, ", err.Error())
2022-04-13 16:25:33 +08:00
} else {
log.NewInfo(operationID, utils.GetSelfFuncName(), "getTokenAndSave2Redis: ", token)
2022-04-12 11:12:36 +08:00
}
}
2022-04-08 15:40:07 +08:00
if err != nil {
2023-02-20 10:13:29 +08:00
return utils.Wrap(err, "push failed")
2022-04-08 15:40:07 +08:00
}
2023-02-20 10:13:29 +08:00
return utils.Wrap(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
}
2023-02-20 10:13:29 +08:00
//log.NewInfo(operationID, utils.GetSelfFuncName(), "result: ", respAuth)
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-20 10:13:29 +08:00
func (g *Client) request(ctx context.Context, url string, content interface{}, token string, output interface{}) error {
2022-04-08 15:40:07 +08:00
con, err := json.Marshal(content)
if err != nil {
return err
}
client := &http.Client{}
2022-04-08 17:12:11 +08:00
req, err := http.NewRequest("POST", config.Config.Push.Getui.PushUrl+url, bytes.NewBuffer(con))
2022-04-08 15:40:07 +08:00
if err != nil {
return err
}
2022-04-08 16:40:39 +08:00
if token != "" {
2022-04-11 10:35:33 +08:00
req.Header.Set("token", token)
2022-04-08 16:40:39 +08:00
}
2022-04-08 15:40:07 +08:00
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
}
2023-02-20 10:13:29 +08:00
//log.NewDebug(operationID, "getui", utils.GetSelfFuncName(), "resp, ", string(result))
commonResp := CommonResp{}
commonResp.Data = output
2022-04-08 19:31:13 +08:00
if err := json.Unmarshal(result, &commonResp); err != nil {
2022-04-08 15:40:07 +08:00
return err
}
2023-02-20 10:13:29 +08:00
if commonResp.Code == tokenExpire {
2022-04-12 11:12:36 +08:00
return TokenExpireError
}
2022-04-08 15:40:07 +08:00
return nil
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 {
return "", utils.Wrap(err, "Auth failed")
}
2023-02-20 10:13:29 +08:00
err = g.cache.SetGetuiTaskID(ctx, token, 60*60*23)
2022-04-11 14:41:09 +08:00
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
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) {
2022-12-21 18:03:39 +08:00
ttl := int64(1000 * 60 * 60 * 24)
2022-12-22 11:10:19 +08:00
pushReq.Settings = &Settings{TTL: &ttl}
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 {
return "", utils.Wrap(err, "GetTaskIDAndSave2Redis failed")
}
2023-02-20 10:13:29 +08:00
err = g.cache.SetGetuiTaskID(ctx, taskID, 60*60*23)
2022-12-21 16:46:16 +08:00
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
return token, nil
}