Skip to content

Commit

Permalink
Optimisation of V2 API parameter passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Finb committed Dec 23, 2021
1 parent 828adc6 commit 10e3b62
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 68 deletions.
11 changes: 7 additions & 4 deletions apns/apns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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
Expand Down
136 changes: 73 additions & 63 deletions route_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params); 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",
Expand All @@ -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
Expand All @@ -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 == "" {
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0.2
v2.0.3

0 comments on commit 10e3b62

Please sign in to comment.