1- const R = require ( 'ramda' ) ;
21const { Notification : ApnsMessage } = require ( '@parse/node-apn' ) ;
32const { Message : GcmMessage } = require ( 'node-gcm' ) ;
43
54const { DEFAULT_TTL , GCM_MAX_TTL } = require ( '../constants' ) ;
65
7- const ttlFromExpiry = R . compose (
8- R . min ( GCM_MAX_TTL ) ,
9- R . max ( 0 ) ,
10- ( expiry ) => expiry - Math . floor ( Date . now ( ) / 1000 )
11- ) ;
6+ const ttlFromExpiry = ( expiry ) => {
7+ const ttl = expiry - Math . floor ( Date . now ( ) / 1000 ) ;
8+ return Math . min ( Math . max ( ttl , 0 ) , GCM_MAX_TTL ) ;
9+ } ;
1210
13- const extractTimeToLive = R . cond ( [
14- [ R . propIs ( Number , 'expiry' ) , ( { expiry } ) => ttlFromExpiry ( expiry ) ] ,
15- [ R . propIs ( Number , 'timeToLive' ) , R . prop ( 'timeToLive' ) ] ,
16- [ R . T , R . always ( DEFAULT_TTL ) ] ,
17- ] ) ;
11+ const extractTimeToLive = ( data ) => {
12+ if ( typeof data . expiry === 'number' ) {
13+ return ttlFromExpiry ( data . expiry ) ;
14+ }
15+ if ( typeof data . timeToLive === 'number' ) {
16+ return data . timeToLive ;
17+ }
18+ return DEFAULT_TTL ;
19+ } ;
1820
1921const expiryFromTtl = ( ttl ) => ttl + Math . floor ( Date . now ( ) / 1000 ) ;
2022
21- const extractExpiry = R . cond ( [
22- [ R . propIs ( Number , 'expiry' ) , R . prop ( 'expiry' ) ] ,
23- [
24- R . propIs ( Number , 'timeToLive' ) ,
25- ( { timeToLive } ) => expiryFromTtl ( timeToLive ) ,
26- ] ,
27- [ R . T , ( ) => expiryFromTtl ( DEFAULT_TTL ) ] ,
28- ] ) ;
23+ const extractExpiry = ( data ) => {
24+ if ( typeof data . expiry === 'number' ) {
25+ return data . expiry ;
26+ }
27+ if ( typeof data . timeToLive === 'number' ) {
28+ return expiryFromTtl ( data . timeToLive ) ;
29+ }
30+ return expiryFromTtl ( DEFAULT_TTL ) ;
31+ } ;
2932
3033const getPropValueOrUndefinedIfIsSilent = ( propName , data ) => {
3134 if ( data . silent ) {
@@ -34,17 +37,28 @@ const getPropValueOrUndefinedIfIsSilent = (propName, data) => {
3437 return data [ propName ] ;
3538} ;
3639
37- const toJSONorUndefined = R . when (
38- R . is ( String ) ,
39- R . tryCatch ( JSON . parse , R . always ( undefined ) )
40- ) ;
40+ const toJSONorUndefined = ( value ) => {
41+ if ( typeof value !== 'string' ) {
42+ return value ;
43+ }
44+ try {
45+ return JSON . parse ( value ) ;
46+ } catch ( e ) {
47+ return undefined ;
48+ }
49+ } ;
4150
42- const alertLocArgsToJSON = R . evolve ( {
43- alert : {
44- 'title-loc-args' : toJSONorUndefined ,
45- 'loc-args' : toJSONorUndefined ,
46- } ,
47- } ) ;
51+ const alertLocArgsToJSON = ( data ) => {
52+ const alert = data . alert || { } ;
53+ return {
54+ ...data ,
55+ alert : {
56+ ...alert ,
57+ 'title-loc-args' : toJSONorUndefined ( alert [ 'title-loc-args' ] ) ,
58+ 'loc-args' : toJSONorUndefined ( alert [ 'loc-args' ] ) ,
59+ } ,
60+ } ;
61+ } ;
4862
4963const getDefaultAlert = ( data ) => ( {
5064 title : data . title ,
@@ -57,21 +71,33 @@ const getDefaultAlert = (data) => ({
5771 action : data . action ,
5872} ) ;
5973
60- const alertOrDefault = ( data ) =>
61- R . when (
62- R . propSatisfies ( R . isNil , 'alert' ) ,
63- R . assoc ( 'alert' , getDefaultAlert ( data ) )
64- ) ;
74+ const alertOrDefault = ( data ) => {
75+ if ( data . alert !== null && data . alert !== undefined ) {
76+ return data ;
77+ }
78+ return { ...data , alert : getDefaultAlert ( data ) } ;
79+ } ;
6580
66- const getParsedAlertOrDefault = ( data ) =>
67- R . pipe ( alertOrDefault ( data ) , alertLocArgsToJSON ) ( data ) ;
81+ const getParsedAlertOrDefault = ( data ) => {
82+ const withAlert = alertOrDefault ( data ) ;
83+ return alertLocArgsToJSON ( withAlert ) ;
84+ } ;
6885
69- const pathIsString = R . pathSatisfies ( R . is ( String ) ) ;
86+ const pathIsString = ( path ) => ( val ) => {
87+ const current = path . reduce ( ( acc , key ) => {
88+ if ( acc && typeof acc === 'object' ) {
89+ return acc [ key ] ;
90+ }
91+ return null ;
92+ } , val ) ;
93+ return typeof current === 'string' ;
94+ } ;
7095
71- const containsValidRecipients = R . either (
72- pathIsString ( [ 'recipients' , 'to' ] ) ,
73- pathIsString ( [ 'recipients' , 'condition' ] )
74- ) ;
96+ const containsValidRecipients = ( obj ) => {
97+ const checkTo = pathIsString ( [ 'recipients' , 'to' ] ) ( obj ) ;
98+ const checkCondition = pathIsString ( [ 'recipients' , 'condition' ] ) ( obj ) ;
99+ return checkTo || checkCondition ;
100+ } ;
75101
76102const buildGcmNotification = ( data ) => {
77103 const notification = data . fcm_notification || {
0 commit comments