Created
April 20, 2017 14:26
-
-
Save giladno/36904f8cc320cd446f1b9109532b9165 to your computer and use it in GitHub Desktop.
push aws
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.push = coroutine(function*(data, users){ | |
let params = { | |
MessageStructure: 'json', | |
Message: JSON.stringify({ | |
default: data.subject, | |
APNS: JSON.stringify(assign({ | |
aps: assign({alert: data.alert, sound: data.sound}, data.apns||{}), | |
},_.omit(data, 'alert', 'collapseKey', 'subject', 'apns', 'sound'))), | |
GCM: JSON.stringify({ | |
collapseKey: data.collapseKey, | |
data: assign({timestamp: Date.now()}, | |
_.omit(data, 'alert', 'collapseKey', 'apns'), {sound: (data.sound||'').replace('.caf', '')}), | |
}), | |
}), | |
}; | |
if (!users) | |
{ | |
return assign(yield publish(assign({TopicArn: topic}, params)), | |
{Message: JSON.parse(params.Message)}); | |
} | |
return yield Promise.all(users.map(coroutine(function*(arn){ | |
try { | |
return assign(yield publish(assign({TargetArn: arn}, params)), | |
{Message: JSON.parse(params.Message)}); | |
} catch(err) { | |
logger.error('failed to push', {arn, error: err}); | |
if (err.code!='EndpointDisabled') | |
return; | |
} | |
try { | |
let account = yield db.account.findOne({'sns.arn': arn}); | |
if (!account) | |
return; | |
for (let sns of account.sns) | |
{ | |
if (sns.arn!=arn) | |
continue; | |
yield unsubscribe({SubscriptionArn: sns.subscription}); | |
yield deleteEndpoint({EndpointArn: arn}); | |
yield db.account.update({_id: account._id}, | |
{$pull: {sns: sns}}); | |
break; | |
} | |
} catch(err) { | |
logger.error('failed to remove device', {arn, error: err}); | |
} | |
}))); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment