diff --git a/keyserver/src/push/send.js b/keyserver/src/push/send.js --- a/keyserver/src/push/send.js +++ b/keyserver/src/push/send.js @@ -59,6 +59,7 @@ TargetedAPNsNotification, TargetedAndroidNotification, TargetedWebNotification, + TargetedWNSNotification, } from './types.js'; import { apnPush, @@ -435,8 +436,11 @@ unreadCount, platformDetails, }); - const deviceTokens = devices.map(({ deviceToken }) => deviceToken); - return await sendWNSNotification(notification, deviceTokens, { + const targetedNotifications = devices.map(({ deviceToken }) => ({ + notification, + deviceToken, + })); + return await sendWNSNotification(targetedNotifications, { ...notificationInfo, codeVersion, stateVersion, @@ -1338,17 +1342,15 @@ +invalidTokens?: $ReadOnlyArray, }; async function sendWNSNotification( - notification: WNSNotification, - deviceTokens: $ReadOnlyArray, + targetedNotifications: $ReadOnlyArray, notificationInfo: NotificationInfo, ): Promise { const { source, codeVersion, stateVersion } = notificationInfo; - const response = await wnsPush({ - notification, - deviceTokens, - }); - + const response = await wnsPush(targetedNotifications); + const deviceTokens = targetedNotifications.map( + ({ deviceToken }) => deviceToken, + ); const wnsIDs = response.wnsIDs ?? []; const delivery: WNSDelivery = { source, diff --git a/keyserver/src/push/types.js b/keyserver/src/push/types.js --- a/keyserver/src/push/types.js +++ b/keyserver/src/push/types.js @@ -2,7 +2,10 @@ import apn from '@parse/node-apn'; -import type { WebNotification } from 'lib/types/notif-types.js'; +import type { + WebNotification, + WNSNotification, +} from 'lib/types/notif-types.js'; export type TargetedAPNsNotification = { +notification: apn.Notification, @@ -65,6 +68,12 @@ +encryptionOrder?: number, }; +export type TargetedWNSNotification = { + +notification: WNSNotification, + +deviceToken: string, + +encryptionOrder?: number, +}; + export type NotificationTargetDevice = { +cookieID: string, +deviceToken: string, diff --git a/keyserver/src/push/utils.js b/keyserver/src/push/utils.js --- a/keyserver/src/push/utils.js +++ b/keyserver/src/push/utils.js @@ -11,7 +11,6 @@ import blobService from 'lib/facts/blob-service.js'; import type { PlatformDetails } from 'lib/types/device-types.js'; -import type { WNSNotification } from 'lib/types/notif-types.js'; import { threadSubscriptions } from 'lib/types/subscription-types.js'; import { threadPermissions } from 'lib/types/thread-permission-types.js'; import { toBase64URL } from 'lib/utils/base64.js'; @@ -30,6 +29,7 @@ TargetedAPNsNotification, TargetedAndroidNotification, TargetedWebNotification, + TargetedWNSNotification, } from './types.js'; import { dbQuery, SQL } from '../database/database.js'; import { generateKey, encrypt } from '../utils/aes-crypto-utils.js'; @@ -291,13 +291,9 @@ +errors?: $ReadOnlyArray, +invalidTokens?: $ReadOnlyArray, }; -async function wnsPush({ - notification, - deviceTokens, -}: { - +notification: WNSNotification, - +deviceTokens: $ReadOnlyArray, -}): Promise { +async function wnsPush( + targetedNotifications: $ReadOnlyArray, +): Promise { const token = await getWNSToken(); if (!token && process.env.NODE_ENV === 'development') { console.log(`no keyserver/secrets/wns_config.json so ignoring notifs`); @@ -305,11 +301,17 @@ } invariant(token, `keyserver/secrets/wns_config.json should exist`); - const notificationString = JSON.stringify(notification); + const pushResults = targetedNotifications.map(async targetedNotification => { + const notificationString = JSON.stringify( + targetedNotification.notification, + ); - const pushResults = deviceTokens.map(async devicePushURL => { try { - return await wnsSinglePush(token, notificationString, devicePushURL); + return await wnsSinglePush( + token, + notificationString, + targetedNotification.deviceToken, + ); } catch (error) { return { error }; } @@ -318,6 +320,9 @@ const errors = []; const notifIDs = []; const invalidTokens = []; + const deviceTokens = targetedNotifications.map( + ({ deviceToken }) => deviceToken, + ); for (let i = 0; i < pushResults.length; i++) { const pushResult = await pushResults[i]; if (pushResult.error) {