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 @@ -164,21 +164,22 @@ newRawMessageInfos, { platform: 'ios', codeVersion }, ); - const notification = prepareIOSNotification( - allMessageInfos, - shimmedNewRawMessageInfos, - threadInfo, - notifInfo.collapseKey, - badgeOnly, - unreadCounts[userID], - codeVersion, - ); - deliveryPromises.push( - sendIOSNotification(notification, [...deviceTokens], { + const deliveryPromise = (async () => { + const notification = await prepareIOSNotification( + allMessageInfos, + shimmedNewRawMessageInfos, + threadInfo, + notifInfo.collapseKey, + badgeOnly, + unreadCounts[userID], + codeVersion, + ); + await sendIOSNotification(notification, [...deviceTokens], { ...notificationInfo, codeVersion, - }), - ); + }); + })(); + deliveryPromises.push(deliveryPromise); } } const androidVersionsToTokens = byDeviceType.get('android'); @@ -189,22 +190,23 @@ newRawMessageInfos, { platform: 'android', codeVersion }, ); - const notification = prepareAndroidNotification( - allMessageInfos, - shimmedNewRawMessageInfos, - threadInfo, - notifInfo.collapseKey, - badgeOnly, - unreadCounts[userID], - dbID, - codeVersion, - ); - deliveryPromises.push( - sendAndroidNotification(notification, [...deviceTokens], { + const deliveryPromise = (async () => { + const notification = await prepareAndroidNotification( + allMessageInfos, + shimmedNewRawMessageInfos, + threadInfo, + notifInfo.collapseKey, + badgeOnly, + unreadCounts[userID], + dbID, + codeVersion, + ); + await sendAndroidNotification(notification, [...deviceTokens], { ...notificationInfo, codeVersion, - }), - ); + }); + })(); + deliveryPromises.push(deliveryPromise); } } @@ -466,7 +468,7 @@ return byDeviceType; } -function prepareIOSNotification( +async function prepareIOSNotification( allMessageInfos: MessageInfo[], newRawMessageInfos: RawMessageInfo[], threadInfo: ThreadInfo, @@ -474,12 +476,12 @@ badgeOnly: boolean, unreadCount: number, codeVersion: number, -): apn.Notification { +): Promise { const uniqueID = uuidv4(); const notification = new apn.Notification(); notification.topic = getAPNsNotificationTopic(codeVersion); - const { merged, ...rest } = notifTextsForMessageInfo( + const { merged, ...rest } = await notifTextsForMessageInfo( allMessageInfos, threadInfo, ); @@ -527,7 +529,7 @@ return notification; } -function prepareAndroidNotification( +async function prepareAndroidNotification( allMessageInfos: MessageInfo[], newRawMessageInfos: RawMessageInfo[], threadInfo: ThreadInfo, @@ -536,9 +538,9 @@ unreadCount: number, dbID: string, codeVersion: number, -): Object { +): Promise { const notifID = collapseKey ? collapseKey : dbID; - const { merged, ...rest } = notifTextsForMessageInfo( + const { merged, ...rest } = await notifTextsForMessageInfo( allMessageInfos, threadInfo, ); diff --git a/lib/shared/messages/add-members-message-spec.js b/lib/shared/messages/add-members-message-spec.js --- a/lib/shared/messages/add-members-message-spec.js +++ b/lib/shared/messages/add-members-message-spec.js @@ -111,11 +111,11 @@ return ET`${creator} added ${addedUsers}`; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const addedMembersObject = {}; for (const messageInfo of messageInfos) { invariant( diff --git a/lib/shared/messages/change-role-message-spec.js b/lib/shared/messages/change-role-message-spec.js --- a/lib/shared/messages/change-role-message-spec.js +++ b/lib/shared/messages/change-role-message-spec.js @@ -129,11 +129,11 @@ return ET`${creator} ${verb} ${affectedUsers} as ${noun}`; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const membersObject = {}; for (const messageInfo of messageInfos) { invariant( diff --git a/lib/shared/messages/change-settings-message-spec.js b/lib/shared/messages/change-settings-message-spec.js --- a/lib/shared/messages/change-settings-message-spec.js +++ b/lib/shared/messages/change-settings-message-spec.js @@ -137,11 +137,11 @@ })} ${messageInfo.field} to "${value}"`; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const mostRecentMessageInfo = messageInfos[0]; invariant( mostRecentMessageInfo.type === messageTypes.CHANGE_SETTINGS, diff --git a/lib/shared/messages/create-entry-message-spec.js b/lib/shared/messages/create-entry-message-spec.js --- a/lib/shared/messages/create-entry-message-spec.js +++ b/lib/shared/messages/create-entry-message-spec.js @@ -111,11 +111,11 @@ return ET`${creator} created an event scheduled for ${date}: "${text}"`; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const hasCreateEntry = messageInfos.some( messageInfo => messageInfo.type === messageTypes.CREATE_ENTRY, ); diff --git a/lib/shared/messages/create-sidebar-message-spec.js b/lib/shared/messages/create-sidebar-message-spec.js --- a/lib/shared/messages/create-sidebar-message-spec.js +++ b/lib/shared/messages/create-sidebar-message-spec.js @@ -177,10 +177,10 @@ return unwrapped; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, - ): NotifTexts { + ): Promise { const messageInfo = assertSingleMessageInfo(messageInfos); invariant( messageInfo.type === messageTypes.CREATE_SIDEBAR, diff --git a/lib/shared/messages/create-sub-thread-message-spec.js b/lib/shared/messages/create-sub-thread-message-spec.js --- a/lib/shared/messages/create-sub-thread-message-spec.js +++ b/lib/shared/messages/create-sub-thread-message-spec.js @@ -126,11 +126,11 @@ return ET`${creator} ${text}`; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const messageInfo = assertSingleMessageInfo(messageInfos); invariant( messageInfo.type === messageTypes.CREATE_SUB_THREAD, diff --git a/lib/shared/messages/create-thread-message-spec.js b/lib/shared/messages/create-thread-message-spec.js --- a/lib/shared/messages/create-thread-message-spec.js +++ b/lib/shared/messages/create-thread-message-spec.js @@ -139,11 +139,11 @@ return ET`${creator} ${text}`; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const messageInfo = assertSingleMessageInfo(messageInfos); invariant( messageInfo.type === messageTypes.CREATE_THREAD, diff --git a/lib/shared/messages/delete-entry-message-spec.js b/lib/shared/messages/delete-entry-message-spec.js --- a/lib/shared/messages/delete-entry-message-spec.js +++ b/lib/shared/messages/delete-entry-message-spec.js @@ -111,11 +111,11 @@ return ET`${creator} deleted an event scheduled for ${date}: "${text}"`; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const messageInfo = assertSingleMessageInfo(messageInfos); invariant( messageInfo.type === messageTypes.DELETE_ENTRY, diff --git a/lib/shared/messages/edit-entry-message-spec.js b/lib/shared/messages/edit-entry-message-spec.js --- a/lib/shared/messages/edit-entry-message-spec.js +++ b/lib/shared/messages/edit-entry-message-spec.js @@ -111,11 +111,11 @@ return ET`${creator} updated the text of an event scheduled for ${date}: "${text}"`; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const hasCreateEntry = messageInfos.some( messageInfo => messageInfo.type === messageTypes.CREATE_ENTRY, ); diff --git a/lib/shared/messages/join-thread-message-spec.js b/lib/shared/messages/join-thread-message-spec.js --- a/lib/shared/messages/join-thread-message-spec.js +++ b/lib/shared/messages/join-thread-message-spec.js @@ -79,11 +79,11 @@ })}`; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const joinerArray = {}; for (const messageInfo of messageInfos) { invariant( diff --git a/lib/shared/messages/leave-thread-message-spec.js b/lib/shared/messages/leave-thread-message-spec.js --- a/lib/shared/messages/leave-thread-message-spec.js +++ b/lib/shared/messages/leave-thread-message-spec.js @@ -79,11 +79,11 @@ })}`; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const leaverBeavers = {}; for (const messageInfo of messageInfos) { invariant( diff --git a/lib/shared/messages/message-spec.js b/lib/shared/messages/message-spec.js --- a/lib/shared/messages/message-spec.js +++ b/lib/shared/messages/message-spec.js @@ -60,7 +60,7 @@ +notificationTexts: ( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, - ) => NotifTexts, + ) => Promise, }; export type GeneratesNotifsParams = { @@ -105,7 +105,7 @@ messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ) => NotifTexts, + ) => Promise, +notificationCollapseKey?: (rawMessageInfo: RawInfo) => string, +generatesNotifs: ( rawMessageInfo: RawInfo, diff --git a/lib/shared/messages/multimedia-message-spec.js b/lib/shared/messages/multimedia-message-spec.js --- a/lib/shared/messages/multimedia-message-spec.js +++ b/lib/shared/messages/multimedia-message-spec.js @@ -262,11 +262,11 @@ return undefined; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const media = []; for (const messageInfo of messageInfos) { invariant( diff --git a/lib/shared/messages/reaction-message-spec.js b/lib/shared/messages/reaction-message-spec.js --- a/lib/shared/messages/reaction-message-spec.js +++ b/lib/shared/messages/reaction-message-spec.js @@ -160,11 +160,11 @@ return unwrapped; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const messageInfo = assertSingleMessageInfo(messageInfos); invariant( messageInfo.type === messageTypes.REACTION, diff --git a/lib/shared/messages/remove-members-message-spec.js b/lib/shared/messages/remove-members-message-spec.js --- a/lib/shared/messages/remove-members-message-spec.js +++ b/lib/shared/messages/remove-members-message-spec.js @@ -111,11 +111,11 @@ return ET`${creator} removed ${removedUsers}`; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const removedMembersObject = {}; for (const messageInfo of messageInfos) { invariant( diff --git a/lib/shared/messages/restore-entry-message-spec.js b/lib/shared/messages/restore-entry-message-spec.js --- a/lib/shared/messages/restore-entry-message-spec.js +++ b/lib/shared/messages/restore-entry-message-spec.js @@ -111,11 +111,11 @@ return ET`${creator} restored an event scheduled for ${date}: "${text}"`; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const messageInfo = assertSingleMessageInfo(messageInfos); invariant( messageInfo.type === messageTypes.RESTORE_ENTRY, diff --git a/lib/shared/messages/sidebar-source-message-spec.js b/lib/shared/messages/sidebar-source-message-spec.js --- a/lib/shared/messages/sidebar-source-message-spec.js +++ b/lib/shared/messages/sidebar-source-message-spec.js @@ -163,18 +163,18 @@ return unwrapped; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const messageInfo = assertSingleMessageInfo(messageInfos); invariant( messageInfo.type === messageTypes.SIDEBAR_SOURCE, 'messageInfo should be messageTypes.SIDEBAR_SOURCE!', ); const sourceMessageInfo = messageInfo.sourceMessage; - return params.notificationTexts([sourceMessageInfo], threadInfo); + return await params.notificationTexts([sourceMessageInfo], threadInfo); }, generatesNotifs: async () => undefined, diff --git a/lib/shared/messages/text-message-spec.js b/lib/shared/messages/text-message-spec.js --- a/lib/shared/messages/text-message-spec.js +++ b/lib/shared/messages/text-message-spec.js @@ -166,11 +166,11 @@ } }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, params: NotificationTextsParams, - ): NotifTexts { + ): Promise { const messageInfo = assertSingleMessageInfo(messageInfos); invariant( messageInfo.type === messageTypes.TEXT, diff --git a/lib/shared/messages/update-relationship-message-spec.js b/lib/shared/messages/update-relationship-message-spec.js --- a/lib/shared/messages/update-relationship-message-spec.js +++ b/lib/shared/messages/update-relationship-message-spec.js @@ -152,10 +152,10 @@ return unwrapped; }, - notificationTexts( + async notificationTexts( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, - ): NotifTexts { + ): Promise { const messageInfo = assertSingleMessageInfo(messageInfos); const prefix = stringForUser(messageInfo.creator); const title = threadInfo.uiName; diff --git a/lib/shared/notif-utils.js b/lib/shared/notif-utils.js --- a/lib/shared/notif-utils.js +++ b/lib/shared/notif-utils.js @@ -18,11 +18,14 @@ import { threadNoun } from './thread-utils'; import { stringForUser } from './user-utils'; -function notifTextsForMessageInfo( +async function notifTextsForMessageInfo( messageInfos: MessageInfo[], threadInfo: ThreadInfo, -): NotifTexts { - const fullNotifTexts = fullNotifTextsForMessageInfo(messageInfos, threadInfo); +): Promise { + const fullNotifTexts = await fullNotifTextsForMessageInfo( + messageInfos, + threadInfo, + ); const merged = trimText(fullNotifTexts.merged, 300); const body = trimText(fullNotifTexts.body, 300); const title = trimText(fullNotifTexts.title, 100); @@ -74,17 +77,17 @@ return messageInfos[0].type; } -function fullNotifTextsForMessageInfo( +async function fullNotifTextsForMessageInfo( messageInfos: $ReadOnlyArray, threadInfo: ThreadInfo, -): NotifTexts { +): Promise { const mostRecentType = mostRecentMessageInfoType(messageInfos); const messageSpec = messageSpecs[mostRecentType]; invariant( messageSpec.notificationTexts, `we're not aware of messageType ${mostRecentType}`, ); - return messageSpec.notificationTexts(messageInfos, threadInfo, { + return await messageSpec.notificationTexts(messageInfos, threadInfo, { notifThreadName, notifTextForSubthreadCreation, strippedRobotextForMessageInfo,