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

314 lines
8.6 KiB

package getui
3 years ago
import (
"Open_IM/internal/push"
3 years ago
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/db"
"Open_IM/pkg/common/log"
"Open_IM/pkg/utils"
"bytes"
3 years ago
"crypto/sha256"
3 years ago
"errors"
3 years ago
//"crypto/sha512"
3 years ago
"encoding/hex"
3 years ago
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"time"
)
var (
GetuiClient *Getui
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
)
3 years ago
func init() {
GetuiClient = newGetuiClient()
}
type Getui struct{}
type GetuiCommonResp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
type AuthReq struct {
Sign string `json:"sign"`
Timestamp string `json:"timestamp"`
Appkey string `json:"appkey"`
}
type AuthResp struct {
ExpireTime string `json:"expire_time"`
Token string `json:"token"`
}
2 years ago
type TaskResp struct {
TaskID string `json:"taskID"`
}
3 years ago
type PushReq struct {
2 years ago
RequestID *string `json:"request_id"`
2 years ago
Settings struct {
2 years ago
TTL *int64 `json:"ttl"`
2 years ago
} `json:"settings"`
Audience struct {
3 years ago
Alias []string `json:"alias"`
3 years ago
} `json:"audience"`
3 years ago
PushMessage struct {
2 years ago
Notification *Notification `json:"notification,omitempty"`
Transmission *string `json:"transmission,omitempty"`
3 years ago
} `json:"push_message"`
3 years ago
PushChannel struct {
2 years ago
Ios *Ios `json:"ios"`
Android *Android `json:"android"`
3 years ago
} `json:"push_channel"`
2 years ago
IsAsync *bool `json:"is_async"`
Taskid *string `json:"taskid"`
3 years ago
}
type Ios struct {
Aps struct {
Sound string `json:"sound"`
Alert Alert `json:"alert"`
} `json:"aps"`
}
type Alert struct {
Title string `json:"title"`
Body string `json:"body"`
}
type Android struct {
Ups struct {
Notification Notification `json:"notification"`
2 years ago
Options Options `json:"options"`
3 years ago
} `json:"ups"`
3 years ago
}
type Notification struct {
2 years ago
Title string `json:"title"`
Body string `json:"body"`
ChannelID string `json:"channelID"`
ChannelName string `json:"ChannelName"`
ClickType string `json:"click_type"`
3 years ago
}
2 years ago
type Options struct {
HW struct {
DefaultSound bool `json:"/message/android/notification/default_sound"`
ChannelID string `json:"/message/android/notification/channel_id"`
Sound string `json:"/message/android/notification/sound"`
Importance string `json:"/message/android/notification/importance"`
} `json:"HW"`
XM struct {
ChannelID string `json:"/extra.channel_id"`
2 years ago
} `json:"XM"`
VV struct {
Classification int `json:"/classification"`
} `json:"VV"`
2 years ago
}
3 years ago
type PushResp struct {
}
func newGetuiClient() *Getui {
return &Getui{}
}
func (g *Getui) Push(userIDList []string, title, detailContent, operationID string, opts push.PushOpts) (resp string, err error) {
3 years ago
token, err := db.DB.GetGetuiToken()
2 years ago
log.NewDebug(operationID, utils.GetSelfFuncName(), "token", token, userIDList)
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 {
3 years ago
token, err = g.getTokenAndSave2Redis(operationID)
3 years ago
if err != nil {
3 years ago
log.NewError(operationID, utils.GetSelfFuncName(), "getTokenAndSave2Redis failed", err.Error())
return "", utils.Wrap(err, "")
3 years ago
}
}
2 years ago
var pushReq PushReq
pushResp := PushResp{}
2 years ago
pushReq.PushMessage.Notification = &Notification{
2 years ago
Title: title,
Body: detailContent,
2 years ago
ClickType: "startapp",
ChannelID: config.Config.Push.Getui.ChannelID,
ChannelName: config.Config.Push.Getui.ChannelName,
3 years ago
}
2 years ago
if len(userIDList) > 1 {
2 years ago
taskID, err := g.GetTaskID(operationID, token, pushReq)
2 years ago
if err != nil {
2 years ago
return "", utils.Wrap(err, "GetTaskIDAndSave2Redis failed")
2 years ago
}
2 years ago
var IsAsync = true
pushReq.IsAsync = &IsAsync
pushReq.Taskid = &taskID
2 years ago
pushReq.PushMessage.Notification = nil
pushReq.PushMessage.Transmission = nil
2 years ago
pushReq.Audience = struct {
Alias []string `json:"alias"`
}{Alias: userIDList}
2 years ago
err = g.request(BatchPushURL, pushReq, token, &pushResp, operationID)
} else {
2 years ago
reqID := utils.OperationIDGenerator()
2 years ago
pushReq.RequestID = &reqID
pushReq.Audience = struct {
Alias []string `json:"alias"`
}{Alias: []string{userIDList[0]}}
2 years ago
pushReq.PushChannel.Ios = &Ios{}
2 years ago
pushReq.PushChannel.Ios.Aps.Sound = "default"
pushReq.PushChannel.Ios.Aps.Alert = Alert{
Title: title,
Body: title,
}
2 years ago
pushReq.PushChannel.Android = &Android{}
2 years ago
pushReq.PushChannel.Android.Ups.Notification = Notification{
Title: title,
Body: title,
ClickType: "startapp",
}
pushReq.PushChannel.Android.Ups.Options = Options{
HW: struct {
DefaultSound bool `json:"/message/android/notification/default_sound"`
ChannelID string `json:"/message/android/notification/channel_id"`
Sound string `json:"/message/android/notification/sound"`
Importance string `json:"/message/android/notification/importance"`
}{ChannelID: "RingRing4", Sound: "/raw/ring001", Importance: "NORMAL"},
XM: struct {
ChannelID string `json:"/extra.channel_id"`
}{ChannelID: "high_system"},
VV: struct {
Classification int "json:\"/classification\""
}{
Classification: 1,
},
}
err = g.request(PushURL, pushReq, token, &pushResp, operationID)
2 years ago
}
3 years ago
switch err {
case TokenExpireError:
3 years ago
token, err = g.getTokenAndSave2Redis(operationID)
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 {
3 years ago
return "", utils.Wrap(err, "push failed")
3 years ago
}
respBytes, err := json.Marshal(pushResp)
3 years ago
return string(respBytes), utils.Wrap(err, "")
}
3 years ago
func (g *Getui) Auth(operationID string, timeStamp int64) (token string, expireTime int64, err error) {
3 years ago
log.NewInfo(operationID, utils.GetSelfFuncName(), config.Config.Push.Getui.AppKey, timeStamp, config.Config.Push.Getui.MasterSecret)
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))
sum := h.Sum(nil)
sign := hex.EncodeToString(sum)
log.NewInfo(operationID, utils.GetSelfFuncName(), "sha256 result", sign)
3 years ago
reqAuth := AuthReq{
Sign: sign,
Timestamp: strconv.Itoa(int(timeStamp)),
3 years ago
Appkey: config.Config.Push.Getui.AppKey,
3 years ago
}
respAuth := AuthResp{}
3 years ago
err = g.request(AuthURL, reqAuth, "", &respAuth, operationID)
3 years ago
if err != nil {
return "", 0, err
}
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 *Getui) GetTaskID(operationID, token string, pushReq PushReq) (string, error) {
respTask := TaskResp{}
err := g.request(TaskURL, pushReq, token, &respTask, operationID)
if err != nil {
return "", utils.Wrap(err, "")
}
return respTask.TaskID, nil
}
3 years ago
func (g *Getui) request(url string, content interface{}, token string, returnStruct interface{}, operationID string) error {
3 years ago
con, err := json.Marshal(content)
if err != nil {
return err
}
client := &http.Client{}
2 years ago
log.Debug(operationID, utils.GetSelfFuncName(), "json:", string(con), "token:", token)
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))
3 years ago
commonResp := GetuiCommonResp{}
commonResp.Data = returnStruct
3 years ago
if err := json.Unmarshal(result, &commonResp); err != nil {
3 years ago
return err
}
3 years ago
if commonResp.Code == 10001 {
return TokenExpireError
}
3 years ago
return nil
}
3 years ago
func (g *Getui) getTokenAndSave2Redis(operationID string) (token string, err error) {
token, expireTime, err := g.Auth(operationID, time.Now().UnixNano()/1e6)
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
log.NewDebug(operationID, "getui", utils.GetSelfFuncName(), token, expireTime, err)
err = db.DB.SetGetuiToken(token, 60*60*23)
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
return token, nil
}
2 years ago
func (g *Getui) GetTaskIDAndSave2Redis(operationID, token string, pushReq PushReq) (taskID string, err error) {
2 years ago
ttl := int64(1000 * 60 * 60 * 24)
pushReq.Settings.TTL = &ttl
2 years ago
taskID, err = g.GetTaskID(operationID, token, pushReq)
if err != nil {
return "", utils.Wrap(err, "GetTaskIDAndSave2Redis failed")
}
err = db.DB.SetGetuiTaskID(taskID, 60*60*23)
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
return token, nil
}