refactor: remove config from notification struct. (#617)
* refactor: remove config from notification struct. * chore: update Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
@@ -65,8 +65,6 @@ type ResponsePush struct {
|
||||
|
||||
// PushNotification is single notification request
|
||||
type PushNotification struct {
|
||||
Cfg config.ConfYaml
|
||||
|
||||
// Common
|
||||
ID string `json:"notif_id,omitempty"`
|
||||
Tokens []string `json:"tokens" binding:"required"`
|
||||
@@ -235,7 +233,7 @@ func CheckPushConf(cfg config.ConfYaml) error {
|
||||
}
|
||||
|
||||
// SendNotification send notification
|
||||
func SendNotification(req queue.QueuedMessage) (resp *ResponsePush, err error) {
|
||||
func SendNotification(req queue.QueuedMessage, cfg config.ConfYaml) (resp *ResponsePush, err error) {
|
||||
v, ok := req.(*PushNotification)
|
||||
if !ok {
|
||||
if err = json.Unmarshal(req.Bytes(), &v); err != nil {
|
||||
@@ -245,18 +243,20 @@ func SendNotification(req queue.QueuedMessage) (resp *ResponsePush, err error) {
|
||||
|
||||
switch v.Platform {
|
||||
case core.PlatFormIos:
|
||||
resp, err = PushToIOS(*v)
|
||||
resp, err = PushToIOS(*v, cfg)
|
||||
case core.PlatFormAndroid:
|
||||
resp, err = PushToAndroid(*v)
|
||||
resp, err = PushToAndroid(*v, cfg)
|
||||
case core.PlatFormHuawei:
|
||||
resp, err = PushToHuawei(*v)
|
||||
resp, err = PushToHuawei(*v, cfg)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Run send notification
|
||||
var Run = func(ctx context.Context, msg queue.QueuedMessage) error {
|
||||
_, err := SendNotification(msg)
|
||||
return err
|
||||
var Run = func(cfg config.ConfYaml) func(ctx context.Context, msg queue.QueuedMessage) error {
|
||||
return func(ctx context.Context, msg queue.QueuedMessage) error {
|
||||
_, err := SendNotification(msg, cfg)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -388,16 +388,16 @@ func getApnsClient(cfg config.ConfYaml, req PushNotification) (client *apns2.Cli
|
||||
}
|
||||
|
||||
// PushToIOS provide send notification to APNs server.
|
||||
func PushToIOS(req PushNotification) (resp *ResponsePush, err error) {
|
||||
func PushToIOS(req PushNotification, cfg config.ConfYaml) (resp *ResponsePush, err error) {
|
||||
logx.LogAccess.Debug("Start push notification for iOS")
|
||||
|
||||
if req.Cfg.Core.Sync && !core.IsLocalQueue(core.Queue(req.Cfg.Queue.Engine)) {
|
||||
req.Cfg.Core.Sync = false
|
||||
if cfg.Core.Sync && !core.IsLocalQueue(core.Queue(cfg.Queue.Engine)) {
|
||||
cfg.Core.Sync = false
|
||||
}
|
||||
|
||||
var (
|
||||
retryCount = 0
|
||||
maxRetry = req.Cfg.Ios.MaxRetry
|
||||
maxRetry = cfg.Ios.MaxRetry
|
||||
)
|
||||
|
||||
if req.Retry > 0 && req.Retry < maxRetry {
|
||||
@@ -410,7 +410,7 @@ Retry:
|
||||
var newTokens []string
|
||||
|
||||
notification := GetIOSNotification(req)
|
||||
client := getApnsClient(req.Cfg, req)
|
||||
client := getApnsClient(cfg, req)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for _, token := range req.Tokens {
|
||||
@@ -430,16 +430,16 @@ Retry:
|
||||
}
|
||||
|
||||
// apns server error
|
||||
errLog := logPush(req.Cfg, core.FailedPush, token, req, err)
|
||||
if req.Cfg.Core.Sync {
|
||||
errLog := logPush(cfg, core.FailedPush, token, req, err)
|
||||
if cfg.Core.Sync {
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
} else if req.Cfg.Core.FeedbackURL != "" {
|
||||
} else if cfg.Core.FeedbackURL != "" {
|
||||
go func(logger *logrus.Logger, log logx.LogPushEntry, url string, timeout int64) {
|
||||
err := DispatchFeedback(log, url, timeout)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}(logx.LogError, errLog, req.Cfg.Core.FeedbackURL, req.Cfg.Core.FeedbackTimeout)
|
||||
}(logx.LogError, errLog, cfg.Core.FeedbackURL, cfg.Core.FeedbackTimeout)
|
||||
}
|
||||
|
||||
status.StatStorage.AddIosError(1)
|
||||
@@ -451,7 +451,7 @@ Retry:
|
||||
}
|
||||
|
||||
if res != nil && res.Sent() {
|
||||
logPush(req.Cfg, core.SucceededPush, token, req, nil)
|
||||
logPush(cfg, core.SucceededPush, token, req, nil)
|
||||
status.StatStorage.AddIosSuccess(1)
|
||||
}
|
||||
|
||||
|
||||
@@ -726,14 +726,13 @@ func TestPushToIOS(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
req := PushNotification{
|
||||
Cfg: cfg,
|
||||
Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7", "11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef1"},
|
||||
Platform: 1,
|
||||
Message: "Welcome",
|
||||
}
|
||||
|
||||
// send fail
|
||||
PushToIOS(req)
|
||||
PushToIOS(req, cfg)
|
||||
}
|
||||
|
||||
func TestApnsHostFromRequest(t *testing.T) {
|
||||
|
||||
@@ -106,17 +106,17 @@ func GetAndroidNotification(req PushNotification) *fcm.Message {
|
||||
}
|
||||
|
||||
// PushToAndroid provide send notification to Android server.
|
||||
func PushToAndroid(req PushNotification) (resp *ResponsePush, err error) {
|
||||
func PushToAndroid(req PushNotification, cfg config.ConfYaml) (resp *ResponsePush, err error) {
|
||||
logx.LogAccess.Debug("Start push notification for Android")
|
||||
|
||||
if req.Cfg.Core.Sync && !core.IsLocalQueue(core.Queue(req.Cfg.Queue.Engine)) {
|
||||
req.Cfg.Core.Sync = false
|
||||
if cfg.Core.Sync && !core.IsLocalQueue(core.Queue(cfg.Queue.Engine)) {
|
||||
cfg.Core.Sync = false
|
||||
}
|
||||
|
||||
var (
|
||||
client *fcm.Client
|
||||
retryCount = 0
|
||||
maxRetry = req.Cfg.Android.MaxRetry
|
||||
maxRetry = cfg.Android.MaxRetry
|
||||
)
|
||||
|
||||
if req.Retry > 0 && req.Retry < maxRetry {
|
||||
@@ -136,9 +136,9 @@ Retry:
|
||||
notification := GetAndroidNotification(req)
|
||||
|
||||
if req.APIKey != "" {
|
||||
client, err = InitFCMClient(req.Cfg, req.APIKey)
|
||||
client, err = InitFCMClient(cfg, req.APIKey)
|
||||
} else {
|
||||
client, err = InitFCMClient(req.Cfg, req.Cfg.Android.APIKey)
|
||||
client, err = InitFCMClient(cfg, cfg.Android.APIKey)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@@ -153,30 +153,30 @@ Retry:
|
||||
logx.LogError.Error("FCM server send message error: " + err.Error())
|
||||
|
||||
if req.IsTopic() {
|
||||
errLog := logPush(req.Cfg, core.FailedPush, req.To, req, err)
|
||||
if req.Cfg.Core.Sync {
|
||||
errLog := logPush(cfg, core.FailedPush, req.To, req, err)
|
||||
if cfg.Core.Sync {
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
} else if req.Cfg.Core.FeedbackURL != "" {
|
||||
} else if cfg.Core.FeedbackURL != "" {
|
||||
go func(logger *logrus.Logger, log logx.LogPushEntry, url string, timeout int64) {
|
||||
err := DispatchFeedback(log, url, timeout)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}(logx.LogError, errLog, req.Cfg.Core.FeedbackURL, req.Cfg.Core.FeedbackTimeout)
|
||||
}(logx.LogError, errLog, cfg.Core.FeedbackURL, cfg.Core.FeedbackTimeout)
|
||||
}
|
||||
status.StatStorage.AddAndroidError(1)
|
||||
} else {
|
||||
for _, token := range req.Tokens {
|
||||
errLog := logPush(req.Cfg, core.FailedPush, token, req, err)
|
||||
if req.Cfg.Core.Sync {
|
||||
errLog := logPush(cfg, core.FailedPush, token, req, err)
|
||||
if cfg.Core.Sync {
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
} else if req.Cfg.Core.FeedbackURL != "" {
|
||||
} else if cfg.Core.FeedbackURL != "" {
|
||||
go func(logger *logrus.Logger, log logx.LogPushEntry, url string, timeout int64) {
|
||||
err := DispatchFeedback(log, url, timeout)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}(logx.LogError, errLog, req.Cfg.Core.FeedbackURL, req.Cfg.Core.FeedbackTimeout)
|
||||
}(logx.LogError, errLog, cfg.Core.FeedbackURL, cfg.Core.FeedbackTimeout)
|
||||
}
|
||||
}
|
||||
status.StatStorage.AddAndroidError(int64(len(req.Tokens)))
|
||||
@@ -208,21 +208,21 @@ Retry:
|
||||
newTokens = append(newTokens, to)
|
||||
}
|
||||
|
||||
errLog := logPush(req.Cfg, core.FailedPush, to, req, result.Error)
|
||||
if req.Cfg.Core.Sync {
|
||||
errLog := logPush(cfg, core.FailedPush, to, req, result.Error)
|
||||
if cfg.Core.Sync {
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
} else if req.Cfg.Core.FeedbackURL != "" {
|
||||
} else if cfg.Core.FeedbackURL != "" {
|
||||
go func(logger *logrus.Logger, log logx.LogPushEntry, url string, timeout int64) {
|
||||
err := DispatchFeedback(log, url, timeout)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}(logx.LogError, errLog, req.Cfg.Core.FeedbackURL, req.Cfg.Core.FeedbackTimeout)
|
||||
}(logx.LogError, errLog, cfg.Core.FeedbackURL, cfg.Core.FeedbackTimeout)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
logPush(req.Cfg, core.SucceededPush, to, req, nil)
|
||||
logPush(cfg, core.SucceededPush, to, req, nil)
|
||||
}
|
||||
|
||||
// result from Send messages to topics
|
||||
@@ -236,11 +236,11 @@ Retry:
|
||||
logx.LogAccess.Debug("Send Topic Message: ", to)
|
||||
// Success
|
||||
if res.MessageID != 0 {
|
||||
logPush(req.Cfg, core.SucceededPush, to, req, nil)
|
||||
logPush(cfg, core.SucceededPush, to, req, nil)
|
||||
} else {
|
||||
// failure
|
||||
errLog := logPush(req.Cfg, core.FailedPush, to, req, res.Error)
|
||||
if req.Cfg.Core.Sync {
|
||||
errLog := logPush(cfg, core.FailedPush, to, req, res.Error)
|
||||
if cfg.Core.Sync {
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
}
|
||||
}
|
||||
@@ -250,8 +250,8 @@ Retry:
|
||||
if len(res.FailedRegistrationIDs) > 0 {
|
||||
newTokens = append(newTokens, res.FailedRegistrationIDs...)
|
||||
|
||||
errLog := logPush(req.Cfg, core.FailedPush, notification.To, req, errors.New("device group: partial success or all fails"))
|
||||
if req.Cfg.Core.Sync {
|
||||
errLog := logPush(cfg, core.FailedPush, notification.To, req, errors.New("device group: partial success or all fails"))
|
||||
if cfg.Core.Sync {
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,14 +40,13 @@ func TestPushToAndroidWrongToken(t *testing.T) {
|
||||
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
|
||||
|
||||
req := PushNotification{
|
||||
Cfg: cfg,
|
||||
Tokens: []string{"aaaaaa", "bbbbb"},
|
||||
Platform: core.PlatFormAndroid,
|
||||
Message: "Welcome",
|
||||
}
|
||||
|
||||
// Android Success count: 0, Failure count: 2
|
||||
PushToAndroid(req)
|
||||
PushToAndroid(req, cfg)
|
||||
}
|
||||
|
||||
func TestPushToAndroidRightTokenForJSONLog(t *testing.T) {
|
||||
@@ -61,13 +60,12 @@ func TestPushToAndroidRightTokenForJSONLog(t *testing.T) {
|
||||
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
|
||||
|
||||
req := PushNotification{
|
||||
Cfg: cfg,
|
||||
Tokens: []string{androidToken},
|
||||
Platform: core.PlatFormAndroid,
|
||||
Message: "Welcome",
|
||||
}
|
||||
|
||||
PushToAndroid(req)
|
||||
PushToAndroid(req, cfg)
|
||||
}
|
||||
|
||||
func TestPushToAndroidRightTokenForStringLog(t *testing.T) {
|
||||
@@ -79,13 +77,12 @@ func TestPushToAndroidRightTokenForStringLog(t *testing.T) {
|
||||
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
|
||||
|
||||
req := PushNotification{
|
||||
Cfg: cfg,
|
||||
Tokens: []string{androidToken},
|
||||
Platform: core.PlatFormAndroid,
|
||||
Message: "Welcome",
|
||||
}
|
||||
|
||||
PushToAndroid(req)
|
||||
PushToAndroid(req, cfg)
|
||||
}
|
||||
|
||||
func TestOverwriteAndroidAPIKey(t *testing.T) {
|
||||
@@ -98,7 +95,6 @@ func TestOverwriteAndroidAPIKey(t *testing.T) {
|
||||
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
|
||||
|
||||
req := PushNotification{
|
||||
Cfg: cfg,
|
||||
Tokens: []string{androidToken, "bbbbb"},
|
||||
Platform: core.PlatFormAndroid,
|
||||
Message: "Welcome",
|
||||
@@ -107,7 +103,7 @@ func TestOverwriteAndroidAPIKey(t *testing.T) {
|
||||
}
|
||||
|
||||
// FCM server error: 401 error: 401 Unauthorized (Wrong API Key)
|
||||
resp, err := PushToAndroid(req)
|
||||
resp, err := PushToAndroid(req, cfg)
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Len(t, resp.Logs, 2)
|
||||
@@ -199,14 +195,13 @@ func TestCheckAndroidMessage(t *testing.T) {
|
||||
|
||||
timeToLive := uint(2419201)
|
||||
req := PushNotification{
|
||||
Cfg: cfg,
|
||||
Tokens: []string{"aaaaaa", "bbbbb"},
|
||||
Platform: core.PlatFormAndroid,
|
||||
Message: "Welcome",
|
||||
TimeToLive: &timeToLive,
|
||||
}
|
||||
|
||||
PushToAndroid(req)
|
||||
PushToAndroid(req, cfg)
|
||||
}
|
||||
|
||||
func TestAndroidNotificationStructure(t *testing.T) {
|
||||
|
||||
@@ -166,17 +166,17 @@ func GetHuaweiNotification(req PushNotification) (*model.MessageRequest, error)
|
||||
}
|
||||
|
||||
// PushToHuawei provide send notification to Android server.
|
||||
func PushToHuawei(req PushNotification) (resp *ResponsePush, err error) {
|
||||
func PushToHuawei(req PushNotification, cfg config.ConfYaml) (resp *ResponsePush, err error) {
|
||||
logx.LogAccess.Debug("Start push notification for Huawei")
|
||||
|
||||
if req.Cfg.Core.Sync && !core.IsLocalQueue(core.Queue(req.Cfg.Queue.Engine)) {
|
||||
req.Cfg.Core.Sync = false
|
||||
if cfg.Core.Sync && !core.IsLocalQueue(core.Queue(cfg.Queue.Engine)) {
|
||||
cfg.Core.Sync = false
|
||||
}
|
||||
|
||||
var (
|
||||
client *client.HMSClient
|
||||
retryCount = 0
|
||||
maxRetry = req.Cfg.Huawei.MaxRetry
|
||||
maxRetry = cfg.Huawei.MaxRetry
|
||||
)
|
||||
|
||||
if req.Retry > 0 && req.Retry < maxRetry {
|
||||
@@ -190,7 +190,7 @@ func PushToHuawei(req PushNotification) (resp *ResponsePush, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
client, err = InitHMSClient(req.Cfg, req.Cfg.Huawei.AppSecret, req.Cfg.Huawei.AppID)
|
||||
client, err = InitHMSClient(cfg, cfg.Huawei.AppSecret, cfg.Huawei.AppID)
|
||||
|
||||
if err != nil {
|
||||
// HMS server error
|
||||
@@ -208,7 +208,7 @@ Retry:
|
||||
res, err := client.SendMessage(context.Background(), notification)
|
||||
if err != nil {
|
||||
// Send Message error
|
||||
errLog := logPush(req.Cfg, core.FailedPush, req.To, req, err)
|
||||
errLog := logPush(cfg, core.FailedPush, req.To, req, err)
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
logx.LogError.Error("HMS server send message error: " + err.Error())
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user