FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
parse-server/src/Controllers/PushController.js at master · mabowgithub/parse-server · GitHub
mabowgithub
/
parse-server
Public
forked from
parse-community/parse-server
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
parse-server
/
src
/
Controllers
/
PushController.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
143 lines (131 loc) · 4.61 KB
Breadcrumbs
parse-server
/
src
/
Controllers
/
PushController.js
Copy path
File metadata and controls
143 lines (131 loc) · 4.61 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
{
Parse
}
from
'parse/node'
;
import
PromiseRouter
from
'../PromiseRouter'
;
import
rest
from
'../rest'
;
export
class
PushController
{
constructor
(
pushAdapter
)
{
this
.
_pushAdapter
=
pushAdapter
;
}
handlePOST
(
req
)
{
if
(
!
this
.
_pushAdapter
)
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
PUSH_MISCONFIGURED
,
'Push adapter is not availabe'
)
;
}
validateMasterKey
(
req
)
;
var
where
=
getQueryCondition
(
req
)
;
var
pushAdapter
=
this
.
_pushAdapter
;
validatePushType
(
where
,
pushAdapter
.
getValidPushTypes
(
)
)
;
// Replace the expiration_time with a valid Unix epoch milliseconds time
req
.
body
[
'expiration_time'
]
=
getExpirationTime
(
req
)
;
// TODO: If the req can pass the checking, we return immediately instead of waiting
// pushes to be sent. We probably change this behaviour in the future.
rest
.
find
(
req
.
config
,
req
.
auth
,
'_Installation'
,
where
)
.
then
(
function
(
response
)
{
return
pushAdapter
.
send
(
req
.
body
,
response
.
results
)
;
}
)
;
return
Parse
.
Promise
.
as
(
{
response
:
{
'result'
:
true
}
}
)
;
}
getExpressRouter
(
)
{
var
router
=
new
PromiseRouter
(
)
;
router
.
route
(
'POST'
,
'/push'
,
(
req
)
=>
{
return
this
.
handlePOST
(
req
)
;
}
)
;
return
router
;
}
}
/**
* Check whether the deviceType parameter in qury condition is valid or not.
*
@param
{
Object
} where A query condition
*
@param
{
Array
} validPushTypes An array of valid push types(string)
*/
function
validatePushType
(
where
,
validPushTypes
)
{
var
where
=
where
||
{
}
;
var
deviceTypeField
=
where
.
deviceType
||
{
}
;
var
deviceTypes
=
[
]
;
if
(
typeof
deviceTypeField
===
'string'
)
{
deviceTypes
.
push
(
deviceTypeField
)
;
}
else
if
(
typeof
deviceTypeField
[
'$in'
]
===
'array'
)
{
deviceTypes
.
concat
(
deviceTypeField
[
'$in'
]
)
;
}
for
(
var
i
=
0
;
i
<
deviceTypes
.
length
;
i
++
)
{
var
deviceType
=
deviceTypes
[
i
]
;
if
(
validPushTypes
.
indexOf
(
deviceType
)
<
0
)
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
PUSH_MISCONFIGURED
,
deviceType
+
' is not supported push type.'
)
;
}
}
}
/**
* Get expiration time from the request body.
*
@param
{
Object
} request A request object
*
@returns
{
Number|undefined
} The expiration time if it exists in the request
*/
function
getExpirationTime
(
req
)
{
var
body
=
req
.
body
||
{
}
;
var
hasExpirationTime
=
!
!
body
[
'expiration_time'
]
;
if
(
!
hasExpirationTime
)
{
return
;
}
var
expirationTimeParam
=
body
[
'expiration_time'
]
;
var
expirationTime
;
if
(
typeof
expirationTimeParam
===
'number'
)
{
expirationTime
=
new
Date
(
expirationTimeParam
*
1000
)
;
}
else
if
(
typeof
expirationTimeParam
===
'string'
)
{
expirationTime
=
new
Date
(
expirationTimeParam
)
;
}
else
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
PUSH_MISCONFIGURED
,
body
[
'expiration_time'
]
+
' is not valid time.'
)
;
}
// Check expirationTime is valid or not, if it is not valid, expirationTime is NaN
if
(
!
isFinite
(
expirationTime
)
)
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
PUSH_MISCONFIGURED
,
body
[
'expiration_time'
]
+
' is not valid time.'
)
;
}
return
expirationTime
.
valueOf
(
)
;
}
/**
* Get query condition from the request body.
*
@param
{
Object
} request A request object
*
@returns
{
Object
} The query condition, the where field in a query api call
*/
function
getQueryCondition
(
req
)
{
var
body
=
req
.
body
||
{
}
;
var
hasWhere
=
typeof
body
.
where
!==
'undefined'
;
var
hasChannels
=
typeof
body
.
channels
!==
'undefined'
;
var
where
;
if
(
hasWhere
&&
hasChannels
)
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
PUSH_MISCONFIGURED
,
'Channels and query can not be set at the same time.'
)
;
}
else
if
(
hasWhere
)
{
where
=
body
.
where
;
}
else
if
(
hasChannels
)
{
where
=
{
"channels"
:
{
"$in"
:
body
.
channels
}
}
}
else
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
PUSH_MISCONFIGURED
,
'Channels and query should be set at least one.'
)
;
}
return
where
;
}
/**
* Check whether the api call has master key or not.
*
@param
{
Object
} request A request object
*/
function
validateMasterKey
(
req
)
{
if
(
req
.
info
.
masterKey
!==
req
.
config
.
masterKey
)
{
throw
new
Parse
.
Error
(
Parse
.
Error
.
PUSH_MISCONFIGURED
,
'Master key is invalid, you should only use master key to send push'
)
;
}
}
if
(
typeof
process
!==
'undefined'
&&
process
.
env
.
NODE_ENV
===
'test'
)
{
PushController
.
getQueryCondition
=
getQueryCondition
;
PushController
.
validateMasterKey
=
validateMasterKey
;
PushController
.
getExpirationTime
=
getExpirationTime
;
PushController
.
validatePushType
=
validatePushType
;
}
export
default
PushController
;
Back
|
FazBrowse Home
|
New Git URL