From 10e3b623d0eac6560d2cbac7c73437164839b8f5 Mon Sep 17 00:00:00 2001 From: Fin Date: Thu, 23 Dec 2021 16:34:56 +0800 Subject: [PATCH] Optimisation of V2 API parameter passing --- apns/apns.go | 11 ++-- route_push.go | 136 +++++++++++++++++++++++++++----------------------- version | 2 +- 3 files changed, 81 insertions(+), 68 deletions(-) diff --git a/apns/apns.go b/apns/apns.go index 8b633a4d..7eb4c4a1 100644 --- a/apns/apns.go +++ b/apns/apns.go @@ -23,8 +23,7 @@ type PushMessage struct { Title string `form:"title,omitempty" json:"title,omitempty" xml:"title,omitempty" query:"title,omitempty"` Body string `form:"body,omitempty" json:"body,omitempty" xml:"body,omitempty" query:"body,omitempty"` // ios notification sound(system sound please refer to http://iphonedevwiki.net/index.php/AudioServices) - Sound string `form:"sound,omitempty" json:"sound,omitempty" xml:"sound,omitempty" query:"sound,omitempty"` - Group string `form:"group,omitempty" json:"group,omitempty" xml:"group,omitempty" query:"group,omitempty"` + Sound string `form:"sound,omitempty" json:"sound,omitempty" xml:"sound,omitempty" query:"sound,omitempty"` ExtParams map[string]interface{} `form:"ext_params,omitempty" json:"ext_params,omitempty" xml:"ext_params,omitempty" query:"ext_params,omitempty"` } @@ -81,8 +80,12 @@ func Push(msg *PushMessage) error { AlertTitle(msg.Title). AlertBody(msg.Body). Sound(msg.Sound). - Category(msg.Category). - ThreadID(msg.Group) + Category(msg.Category) + + group, exist := msg.ExtParams["group"] + if exist { + pl = pl.ThreadID(group.(string)) + } for k, v := range msg.ExtParams { // Change all parameter names to lowercase to prevent inconsistent capitalization diff --git a/route_push.go b/route_push.go index 523dfe16..99a62fc8 100644 --- a/route_push.go +++ b/route_push.go @@ -10,27 +10,61 @@ import ( ) func init() { + // V2 API registerRoute("push", func(router fiber.Router) { - router.Post("/push", func(c *fiber.Ctx) error { return routeDoPush(c, false) }) + router.Post("/push", func(c *fiber.Ctx) error { return routeDoPushV2(c) }) }) // compatible with old requests registerRouteWithWeight("push_compat", 1, func(router fiber.Router) { - router.Get("/:device_key", func(c *fiber.Ctx) error { return routeDoPush(c, true) }) - router.Post("/:device_key", func(c *fiber.Ctx) error { return routeDoPush(c, true) }) + router.Get("/:device_key", func(c *fiber.Ctx) error { return routeDoPush(c) }) + router.Post("/:device_key", func(c *fiber.Ctx) error { return routeDoPush(c) }) - router.Get("/:device_key/:body", func(c *fiber.Ctx) error { return routeDoPush(c, true) }) - router.Post("/:device_key/:body", func(c *fiber.Ctx) error { return routeDoPush(c, true) }) + router.Get("/:device_key/:body", func(c *fiber.Ctx) error { return routeDoPush(c) }) + router.Post("/:device_key/:body", func(c *fiber.Ctx) error { return routeDoPush(c) }) - router.Get("/:device_key/:title/:body", func(c *fiber.Ctx) error { return routeDoPush(c, true) }) - router.Post("/:device_key/:title/:body", func(c *fiber.Ctx) error { return routeDoPush(c, true) }) + router.Get("/:device_key/:title/:body", func(c *fiber.Ctx) error { return routeDoPush(c) }) + router.Post("/:device_key/:title/:body", func(c *fiber.Ctx) error { return routeDoPush(c) }) - router.Get("/:device_key/:category/:title/:body", func(c *fiber.Ctx) error { return routeDoPush(c, true) }) - router.Post("/:device_key/:category/:title/:body", func(c *fiber.Ctx) error { return routeDoPush(c, true) }) + router.Get("/:device_key/:category/:title/:body", func(c *fiber.Ctx) error { return routeDoPush(c) }) + router.Post("/:device_key/:category/:title/:body", func(c *fiber.Ctx) error { return routeDoPush(c) }) }) } -func routeDoPush(c *fiber.Ctx, compat bool) error { +func routeDoPush(c *fiber.Ctx) error { + params := make(map[string]interface{}) + + visitor := func(key, value []byte) { + params[strings.ToLower(string(key))] = string(value) + } + // parse query args (medium priority) + c.Request().URI().QueryArgs().VisitAll(visitor) + // parse post args + c.Request().PostArgs().VisitAll(visitor) + + // parse multipartForm values + form, err := c.Request().MultipartForm() + if err == nil { + for key, val := range form.Value { + if len(val) > 0 { + params[key] = val[0] + } + } + } + + return push(c, params) +} + +func routeDoPushV2(c *fiber.Ctx) error { + params := make(map[string]interface{}) + // parse body + if err := c.BodyParser(¶ms); err != nil && err != fiber.ErrUnprocessableEntity { + return c.Status(400).JSON(failed(400, "request bind failed: %v", err)) + } + return push(c, params) +} + +func push(c *fiber.Ctx, params map[string]interface{}) error { // default value msg := apns.PushMessage{ Category: "myNotificationCategory", @@ -39,32 +73,9 @@ func routeDoPush(c *fiber.Ctx, compat bool) error { ExtParams: make(map[string]interface{}), } - // always parse body(Lowest priority) - if err := c.BodyParser(&msg); err != nil && err != fiber.ErrUnprocessableEntity { - return c.Status(400).JSON(failed(400, "request bind failed: %v", err)) - } - - if compat { - params := make(map[string]string) - visitor := func(key, value []byte) { - params[strings.ToLower(string(key))] = string(value) - } - // parse query args (medium priority) - c.Request().URI().QueryArgs().VisitAll(visitor) - // parse post args - c.Request().PostArgs().VisitAll(visitor) - - // parse multipartForm values - form, err := c.Request().MultipartForm() - if err == nil { - for key, val := range form.Value { - if len(val) > 0 { - params[key] = val[0] - } - } - } - - for key, val := range params { + for key, val := range params { + switch val := val.(type) { + case string: switch strings.ToLower(string(key)) { case "device_key": msg.DeviceKey = val @@ -76,41 +87,40 @@ func routeDoPush(c *fiber.Ctx, compat bool) error { msg.Body = val case "sound": msg.Sound = val + ".caf" - case "group": - // 服务端使用 msg.Group 设置 ThreadID, 对通知中心的推送进行分组 - msg.Group = val - // 客户端从 Custom payload 中拿到 group 参数进行分组 - msg.ExtParams[strings.ToLower(string(key))] = val default: msg.ExtParams[strings.ToLower(string(key))] = val } + case map[string]interface{}: + for k, v := range val { + msg.ExtParams[k] = v + } } + } - // parse url path (highest priority) - if pathDeviceKey := c.Params("device_key"); pathDeviceKey != "" { - msg.DeviceKey = pathDeviceKey - } - if category := c.Params("category"); category != "" { - str, err := url.QueryUnescape(category) - if err != nil { - return err - } - msg.Category = str + // parse url path (highest priority) + if pathDeviceKey := c.Params("device_key"); pathDeviceKey != "" { + msg.DeviceKey = pathDeviceKey + } + if category := c.Params("category"); category != "" { + str, err := url.QueryUnescape(category) + if err != nil { + return err } - if title := c.Params("title"); title != "" { - str, err := url.QueryUnescape(title) - if err != nil { - return err - } - msg.Title = str + msg.Category = str + } + if title := c.Params("title"); title != "" { + str, err := url.QueryUnescape(title) + if err != nil { + return err } - if body := c.Params("body"); body != "" { - str, err := url.QueryUnescape(body) - if err != nil { - return err - } - msg.Body = str + msg.Title = str + } + if body := c.Params("body"); body != "" { + str, err := url.QueryUnescape(body) + if err != nil { + return err } + msg.Body = str } if msg.DeviceKey == "" { diff --git a/version b/version index b02d37b0..201271f0 100644 --- a/version +++ b/version @@ -1 +1 @@ -v2.0.2 \ No newline at end of file +v2.0.3 \ No newline at end of file