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

180 lines
4.9 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"
//http2 "Open_IM/pkg/common/http"
3 years ago
"Open_IM/pkg/common/log"
"Open_IM/pkg/utils"
"bytes"
2 years ago
"context"
3 years ago
"crypto/sha256"
3 years ago
"encoding/hex"
3 years ago
"encoding/json"
2 years ago
"errors"
3 years ago
"io/ioutil"
"net/http"
"strconv"
"time"
)
var (
3 years ago
TokenExpireError = errors.New("token expire")
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
tokenExpire = 10001
ttl = 0
)
2 years ago
2 years ago
type Client struct {
cache cache.Cache
3 years ago
}
2 years ago
func newClient(cache cache.Cache) *Client {
return &Client{cache: cache}
3 years ago
}
2 years ago
func (g *Client) Push(ctx context.Context, userIDs []string, title, content, operationID string, opts *push.Opts) error {
token, err := g.cache.GetGetuiToken(ctx)
3 years ago
if err != nil {
2 years ago
log.NewError(operationID, utils.GetSelfFuncName(), "GetGetuiToken failed", err.Error())
3 years ago
}
if token == "" || err != nil {
2 years ago
token, err = g.getTokenAndSave2Redis(ctx)
3 years ago
if err != nil {
3 years ago
log.NewError(operationID, utils.GetSelfFuncName(), "getTokenAndSave2Redis failed", err.Error())
2 years ago
return utils.Wrap(err, "")
3 years ago
}
}
2 years ago
pushReq := newPushReq(title, content)
pushReq.setPushChannel(title, content)
pushResp := struct{}{}
if len(userIDs) > 1 {
taskID, err := g.GetTaskID(ctx, token, pushReq)
2 years ago
if err != nil {
2 years ago
return utils.Wrap(err, "GetTaskIDAndSave2Redis failed")
2 years ago
}
2 years ago
pushReq = PushReq{Audience: &Audience{Alias: userIDs}}
2 years ago
var IsAsync = true
2 years ago
pushReq.IsAsync = &IsAsync
2 years ago
pushReq.TaskID = &taskID
err = g.request(ctx, batchPushURL, pushReq, token, &pushResp)
2 years ago
} else {
2 years ago
reqID := utils.OperationIDGenerator()
2 years ago
pushReq.RequestID = &reqID
2 years ago
pushReq.Audience = &Audience{Alias: []string{userIDs[0]}}
err = g.request(ctx, pushURL, pushReq, token, &pushResp)
2 years ago
}
3 years ago
switch err {
case TokenExpireError:
2 years ago
token, err = g.getTokenAndSave2Redis(ctx)
3 years ago
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "getTokenAndSave2Redis failed, ", err.Error())
3 years ago
} else {
log.NewInfo(operationID, utils.GetSelfFuncName(), "getTokenAndSave2Redis: ", token)
3 years ago
}
}
3 years ago
if err != nil {
2 years ago
return utils.Wrap(err, "push failed")
3 years ago
}
2 years ago
return utils.Wrap(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
}
2 years ago
//log.NewInfo(operationID, utils.GetSelfFuncName(), "result: ", respAuth)
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
func (g *Client) request(ctx context.Context, url string, content interface{}, token string, output interface{}) error {
3 years ago
con, err := json.Marshal(content)
if err != nil {
return err
}
client := &http.Client{}
3 years ago
req, err := http.NewRequest("POST", config.Config.Push.Getui.PushUrl+url, bytes.NewBuffer(con))
3 years ago
if err != nil {
return err
}
3 years ago
if token != "" {
3 years ago
req.Header.Set("token", token)
3 years ago
}
3 years ago
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
}
2 years ago
//log.NewDebug(operationID, "getui", utils.GetSelfFuncName(), "resp, ", string(result))
commonResp := CommonResp{}
commonResp.Data = output
3 years ago
if err := json.Unmarshal(result, &commonResp); err != nil {
3 years ago
return err
}
2 years ago
if commonResp.Code == tokenExpire {
3 years ago
return TokenExpireError
}
3 years ago
return nil
}
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 {
return "", utils.Wrap(err, "Auth failed")
}
2 years ago
err = g.cache.SetGetuiTaskID(ctx, token, 60*60*23)
3 years ago
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
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
ttl := int64(1000 * 60 * 60 * 24)
2 years ago
pushReq.Settings = &Settings{TTL: &ttl}
2 years ago
taskID, err = g.GetTaskID(ctx, token, pushReq)
2 years ago
if err != nil {
return "", utils.Wrap(err, "GetTaskIDAndSave2Redis failed")
}
2 years ago
err = g.cache.SetGetuiTaskID(ctx, taskID, 60*60*23)
2 years ago
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
return token, nil
}