Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/push/utils.js
Show All 40 Lines | |||||
type APNPushResult = | type APNPushResult = | ||||
| { +success: true } | | { +success: true } | ||||
| { | | { | ||||
+errors: $ReadOnlyArray<ResponseFailure>, | +errors: $ReadOnlyArray<ResponseFailure>, | ||||
+invalidTokens?: $ReadOnlyArray<string>, | +invalidTokens?: $ReadOnlyArray<string>, | ||||
}; | }; | ||||
async function apnPush({ | async function apnPush({ | ||||
notification, | notifications, | ||||
deviceTokens, | deviceTokens, | ||||
platformDetails, | platformDetails, | ||||
}: { | }: { | ||||
+notification: apn.Notification, | +notifications: $ReadOnlyArray<apn.Notification>, | ||||
+deviceTokens: $ReadOnlyArray<string>, | +deviceTokens: $ReadOnlyArray<string>, | ||||
+platformDetails: PlatformDetails, | +platformDetails: PlatformDetails, | ||||
}): Promise<APNPushResult> { | }): Promise<APNPushResult> { | ||||
const pushProfile = getAPNPushProfileForCodeVersion(platformDetails); | const pushProfile = getAPNPushProfileForCodeVersion(platformDetails); | ||||
const apnProvider = await getAPNProvider(pushProfile); | const apnProvider = await getAPNProvider(pushProfile); | ||||
if (!apnProvider && process.env.NODE_ENV === 'development') { | if (!apnProvider && process.env.NODE_ENV === 'development') { | ||||
console.log(`no keyserver/secrets/${pushProfile}.json so ignoring notifs`); | console.log(`no keyserver/secrets/${pushProfile}.json so ignoring notifs`); | ||||
return { success: true }; | return { success: true }; | ||||
} | } | ||||
invariant(apnProvider, `keyserver/secrets/${pushProfile}.json should exist`); | invariant(apnProvider, `keyserver/secrets/${pushProfile}.json should exist`); | ||||
const result = await apnProvider.send(notification, deviceTokens); | |||||
const results = await Promise.all( | |||||
notifications.map((notification, idx) => { | |||||
return apnProvider.send(notification, deviceTokens[idx]); | |||||
}), | |||||
); | |||||
const mergedResults = { sent: [], failed: [] }; | |||||
for (const result of results) { | |||||
mergedResults.sent.push(...result.sent); | |||||
mergedResults.failed.push(...result.failed); | |||||
} | |||||
const errors = []; | const errors = []; | ||||
const invalidTokens = []; | const invalidTokens = []; | ||||
for (const error of result.failed) { | for (const error of mergedResults.failed) { | ||||
errors.push(error); | errors.push(error); | ||||
/* eslint-disable eqeqeq */ | /* eslint-disable eqeqeq */ | ||||
if ( | if ( | ||||
error.status == apnTokenInvalidationErrorCode || | error.status == apnTokenInvalidationErrorCode || | ||||
(error.status == apnBadRequestErrorCode && | (error.status == apnBadRequestErrorCode && | ||||
error.response.reason === apnBadTokenErrorString) | error.response.reason === apnBadTokenErrorString) | ||||
) { | ) { | ||||
invalidTokens.push(error.device); | invalidTokens.push(error.device); | ||||
▲ Show 20 Lines • Show All 300 Lines • Show Last 20 Lines |