Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/push/providers.js
// @flow | // @flow | ||||
import apn from '@parse/node-apn'; | import apn from '@parse/node-apn'; | ||||
import type { Provider as APNProvider } from '@parse/node-apn'; | import type { Provider as APNProvider } from '@parse/node-apn'; | ||||
import fcmAdmin from 'firebase-admin'; | import fcmAdmin from 'firebase-admin'; | ||||
import type { FirebaseApp } from 'firebase-admin'; | import type { FirebaseApp } from 'firebase-admin'; | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import webpush from 'web-push'; | |||||
import { importJSON } from '../utils/import-json.js'; | import { importJSON } from '../utils/import-json.js'; | ||||
type APNPushProfile = 'apn_config' | 'comm_apn_config'; | type APNPushProfile = 'apn_config' | 'comm_apn_config'; | ||||
function getAPNPushProfileForCodeVersion(codeVersion: ?number): APNPushProfile { | function getAPNPushProfileForCodeVersion(codeVersion: ?number): APNPushProfile { | ||||
return codeVersion && codeVersion >= 87 ? 'comm_apn_config' : 'apn_config'; | return codeVersion && codeVersion >= 87 ? 'comm_apn_config' : 'apn_config'; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | for (const provider of cachedAPNProviders.values()) { | ||||
provider?.shutdown(); | provider?.shutdown(); | ||||
} | } | ||||
} | } | ||||
function getAPNsNotificationTopic(codeVersion: ?number): string { | function getAPNsNotificationTopic(codeVersion: ?number): string { | ||||
return codeVersion && codeVersion >= 87 ? 'app.comm' : 'org.squadcal.app'; | return codeVersion && codeVersion >= 87 ? 'app.comm' : 'org.squadcal.app'; | ||||
} | } | ||||
type WebPushConfig = { +publicKey: string, +privateKey: string }; | |||||
let cachedWebPushConfig: ?WebPushConfig = null; | |||||
async function getWebPushConfig(): Promise<?WebPushConfig> { | |||||
if (cachedWebPushConfig) { | |||||
return cachedWebPushConfig; | |||||
} | |||||
cachedWebPushConfig = await importJSON<WebPushConfig>({ | |||||
folder: 'secrets', | |||||
name: 'web_push_config', | |||||
}); | |||||
if (cachedWebPushConfig) { | |||||
webpush.setVapidDetails( | |||||
'mailto:support@comm.app', | |||||
cachedWebPushConfig.publicKey, | |||||
cachedWebPushConfig.privateKey, | |||||
); | |||||
} | |||||
return cachedWebPushConfig; | |||||
} | |||||
export { | export { | ||||
getAPNPushProfileForCodeVersion, | getAPNPushProfileForCodeVersion, | ||||
getFCMPushProfileForCodeVersion, | getFCMPushProfileForCodeVersion, | ||||
getAPNProvider, | getAPNProvider, | ||||
getFCMProvider, | getFCMProvider, | ||||
endFirebase, | endFirebase, | ||||
endAPNs, | endAPNs, | ||||
getAPNsNotificationTopic, | getAPNsNotificationTopic, | ||||
getWebPushConfig, | |||||
}; | }; |