-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
route_push.go
160 lines (141 loc) · 4.22 KB
/
route_push.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"github.com/gofiber/fiber/v2/utils"
"net/url"
"strings"
"github.com/finb/bark-server/v2/apns"
"github.com/gofiber/fiber/v2"
)
func init() {
// V2 API
registerRoute("push", func(router fiber.Router) {
router.Post("/push", func(c *fiber.Ctx) error { return routeDoPush(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) })
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) })
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) })
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) })
router.Post("/:device_key/:category/:title/:body", func(c *fiber.Ctx) error { return routeDoPush(c) })
})
}
func routeDoPush(c *fiber.Ctx) error {
// Get content-type
contentType := utils.ToLower(utils.UnsafeString(c.Request().Header.ContentType()))
contentType = utils.ParseVendorSpecificContentType(contentType)
// Json request uses the API V2
if strings.HasPrefix(contentType, "application/json") {
return routeDoPushV2(c)
}
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))
}
// parse query args (medium priority)
c.Request().URI().QueryArgs().VisitAll(func(key, value []byte){
params[strings.ToLower(string(key))] = string(value)
})
return push(c, params)
}
func push(c *fiber.Ctx, params map[string]interface{}) error {
// default value
msg := apns.PushMessage{
Category: "myNotificationCategory",
Body: "NoContent",
Sound: "1107",
ExtParams: make(map[string]interface{}),
}
for key, val := range params {
switch val := val.(type) {
case string:
switch strings.ToLower(string(key)) {
case "device_key":
msg.DeviceKey = val
case "category":
msg.Category = val
case "title":
msg.Title = val
case "body":
msg.Body = val
case "sound":
// Compatible with old parameters
if strings.HasSuffix(val, ".caf") {
msg.Sound = val
} else {
msg.Sound = val + ".caf"
}
default:
msg.ExtParams[strings.ToLower(string(key))] = val
}
case map[string]interface{}:
for k, v := range val {
msg.ExtParams[k] = v
}
default:
msg.ExtParams[key] = val
}
}
// 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
}
if title := c.Params("title"); title != "" {
str, err := url.QueryUnescape(title)
if err != nil {
return err
}
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 == "" {
return c.Status(400).JSON(failed(400, "device key is empty"))
}
deviceToken, err := db.DeviceTokenByKey(msg.DeviceKey)
if err != nil {
return c.Status(400).JSON(failed(400, "failed to get device token: %v", err))
}
msg.DeviceToken = deviceToken
err = apns.Push(&msg)
if err != nil {
return c.Status(500).JSON(failed(500, "push failed: %v", err))
}
return c.JSON(success())
}