Skip to content

Commit bcef3e0

Browse files
committed
refactor: refactor notification delivery for modularity and testability
- Refactor configuration checks for iOS, Android, and Huawei platforms into separate validation functions - Modularize APNS client initialization and payload construction with new helper functions for certificate/key loading and notification attribute setup - Break notification payload generation into smaller, composable functions for improved clarity and testability - Split FCM notification construction and payload formatting into dedicated helper functions - Separate Huawei notification building into specific helper functions for message targets, Android config, and notification content - Add extensive unit tests for new helper functions and refactored logic across iOS, Android/FCM, and Huawei notification handling - Implement filtering and counting utilities for notification targets in the queue server logic - Enhance code readability, maintainability, and test coverage throughout the notification module Signed-off-by: appleboy <[email protected]>
1 parent 0cc4e5e commit bcef3e0

File tree

10 files changed

+1607
-478
lines changed

10 files changed

+1607
-478
lines changed

notify/notification.go

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -197,45 +197,61 @@ func SetProxy(proxy string) error {
197197
return nil
198198
}
199199

200-
// CheckPushConf provide check your yml config.
201-
func CheckPushConf(cfg *config.ConfYaml) error {
202-
if !cfg.Ios.Enabled && !cfg.Android.Enabled && !cfg.Huawei.Enabled {
203-
return errors.New("please enable iOS, Android or Huawei config in yml config")
200+
// checkIOSConf validates iOS configuration.
201+
func checkIOSConf(cfg *config.ConfYaml) error {
202+
if !cfg.Ios.Enabled {
203+
return nil
204204
}
205-
206-
if cfg.Ios.Enabled {
207-
if cfg.Ios.KeyPath == "" && cfg.Ios.KeyBase64 == "" {
208-
return errors.New("missing iOS certificate key")
209-
}
210-
211-
// check certificate file exist
212-
if cfg.Ios.KeyPath != "" {
213-
if _, err := os.Stat(cfg.Ios.KeyPath); os.IsNotExist(err) {
214-
return errors.New("certificate file does not exist")
215-
}
205+
if cfg.Ios.KeyPath == "" && cfg.Ios.KeyBase64 == "" {
206+
return errors.New("missing iOS certificate key")
207+
}
208+
if cfg.Ios.KeyPath != "" {
209+
if _, err := os.Stat(cfg.Ios.KeyPath); os.IsNotExist(err) {
210+
return errors.New("certificate file does not exist")
216211
}
217212
}
213+
return nil
214+
}
218215

219-
if cfg.Android.Enabled {
220-
credential := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
221-
if cfg.Android.Credential == "" &&
222-
cfg.Android.KeyPath == "" &&
223-
credential == "" {
224-
return errors.New("missing fcm credential data")
225-
}
216+
// checkAndroidConf validates Android/FCM configuration.
217+
func checkAndroidConf(cfg *config.ConfYaml) error {
218+
if !cfg.Android.Enabled {
219+
return nil
226220
}
221+
credential := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
222+
if cfg.Android.Credential == "" && cfg.Android.KeyPath == "" && credential == "" {
223+
return errors.New("missing fcm credential data")
224+
}
225+
return nil
226+
}
227227

228-
if cfg.Huawei.Enabled {
229-
if cfg.Huawei.AppSecret == "" {
230-
return errors.New("missing huawei app secret")
231-
}
228+
// checkHuaweiConf validates Huawei/HMS configuration.
229+
func checkHuaweiConf(cfg *config.ConfYaml) error {
230+
if !cfg.Huawei.Enabled {
231+
return nil
232+
}
233+
if cfg.Huawei.AppSecret == "" {
234+
return errors.New("missing huawei app secret")
235+
}
236+
if cfg.Huawei.AppID == "" {
237+
return errors.New("missing huawei app id")
238+
}
239+
return nil
240+
}
232241

233-
if cfg.Huawei.AppID == "" {
234-
return errors.New("missing huawei app id")
235-
}
242+
// CheckPushConf provide check your yml config.
243+
func CheckPushConf(cfg *config.ConfYaml) error {
244+
if !cfg.Ios.Enabled && !cfg.Android.Enabled && !cfg.Huawei.Enabled {
245+
return errors.New("please enable iOS, Android or Huawei config in yml config")
236246
}
237247

238-
return nil
248+
if err := checkIOSConf(cfg); err != nil {
249+
return err
250+
}
251+
if err := checkAndroidConf(cfg); err != nil {
252+
return err
253+
}
254+
return checkHuaweiConf(cfg)
239255
}
240256

241257
// SendNotification provide send notification.

0 commit comments

Comments
 (0)