diff --git a/keyserver/src/creators/message-creator.js b/keyserver/src/creators/message-creator.js --- a/keyserver/src/creators/message-creator.js +++ b/keyserver/src/creators/message-creator.js @@ -1,6 +1,7 @@ // @flow import invariant from 'invariant'; +import _pickBy from 'lodash/fp/pickBy'; import { permissionLookup } from 'lib/permissions/thread-permissions'; import { @@ -18,6 +19,7 @@ import { redisMessageTypes } from 'lib/types/redis-types'; import { threadPermissions } from 'lib/types/thread-types'; import { updateTypes } from 'lib/types/update-types'; +import { promiseAll } from 'lib/utils/promises'; import { dbQuery, @@ -361,39 +363,51 @@ } } - const pushInfo = {}; const messageInfosPerUser = {}; const latestMessagesPerUser: LatestMessagesPerUser = new Map(); + const userPushInfoPromises = {}; for (const pair of perUserInfo) { const [userID, preUserPushInfo] = pair; const { userNotMemberOfSubthreads } = preUserPushInfo; - const userPushInfo = { - devices: [...preUserPushInfo.devices.values()], - messageInfos: [], - }; + const userDevices = [...preUserPushInfo.devices.values()]; + if (userDevices.length === 0) { + continue; + } + const userPushInfoMessageInfoPromises = []; for (const threadID of preUserPushInfo.notFocusedThreadIDs) { const messageIndices = threadsToMessageIndices.get(threadID); invariant(messageIndices, `indices should exist for thread ${threadID}`); - for (const messageIndex of messageIndices) { - const messageInfo = messageInfos[messageIndex]; - const { type } = messageInfo; - if (messageInfo.creatorID === userID) { - // We never send a user notifs about their own activity - continue; - } - const { generatesNotifs } = messageSpecs[type]; - if (generatesNotifs(messageInfo, { userNotMemberOfSubthreads })) { - userPushInfo.messageInfos.push(messageInfo); - } - } - } - if ( - userPushInfo.devices.length > 0 && - userPushInfo.messageInfos.length > 0 - ) { - pushInfo[userID] = userPushInfo; + userPushInfoMessageInfoPromises.push( + ...messageIndices.map(async messageIndex => { + const messageInfo = messageInfos[messageIndex]; + const { type } = messageInfo; + if (messageInfo.creatorID === userID) { + // We never send a user notifs about their own activity + return undefined; + } + const { generatesNotifs } = messageSpecs[type]; + const doesGenerateNotif = await generatesNotifs(messageInfo, { + userNotMemberOfSubthreads, + }); + return doesGenerateNotif ? messageInfo : undefined; + }), + ); } + const userPushInfoPromise = (async () => { + const userMessageInfos = await Promise.all( + userPushInfoMessageInfoPromises, + ); + const filteredMessageInfos = userMessageInfos.filter(Boolean); + if (filteredMessageInfos.length === 0) { + return undefined; + } + return { + devices: userDevices, + messageInfos: filteredMessageInfos, + }; + })(); + userPushInfoPromises[userID] = userPushInfoPromise; const userMessageInfos = []; for (const threadID of preUserPushInfo.threadIDs) { const messageIndices = threadsToMessageIndices.get(threadID); @@ -420,13 +434,14 @@ const latestMessages = flattenLatestMessagesPerUser(latestMessagesPerUser); - await Promise.all([ + const [pushInfo] = await Promise.all([ + promiseAll(userPushInfoPromises), createReadStatusUpdates(latestMessages), redisPublish(viewer, messageInfosPerUser, updatesForCurrentSession), updateLatestMessages(latestMessages), ]); - await sendPushNotifs(pushInfo); + await sendPushNotifs(_pickBy(Boolean)(pushInfo)); } async function redisPublish( 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 @@ -177,7 +177,7 @@ ); }, - generatesNotifs: () => false, + generatesNotifs: async () => false, userIDs(rawMessageInfo: RawAddMembersMessageInfo): $ReadOnlyArray { return rawMessageInfo.addedUserIDs; 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 @@ -192,5 +192,5 @@ ); }, - generatesNotifs: () => true, + generatesNotifs: async () => true, }); 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 @@ -190,5 +190,5 @@ ); }, - generatesNotifs: () => true, + generatesNotifs: async () => true, }); 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 @@ -179,5 +179,5 @@ return joinResult(rawMessageInfo.creatorID, rawMessageInfo.entryID); }, - generatesNotifs: () => true, + generatesNotifs: async () => true, }); 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 @@ -236,7 +236,7 @@ }; }, - generatesNotifs: () => true, + generatesNotifs: async () => true, userIDs(rawMessageInfo: RawCreateSidebarMessageInfo): $ReadOnlyArray { return rawMessageInfo.initialThreadState.memberIDs; 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 @@ -160,7 +160,7 @@ ); }, - generatesNotifs: ( + generatesNotifs: async ( rawMessageInfo: RawCreateSubthreadMessageInfo, params: GeneratesNotifsParams, ) => { 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 @@ -202,7 +202,7 @@ }; }, - generatesNotifs: () => true, + generatesNotifs: async () => true, userIDs(rawMessageInfo: RawCreateThreadMessageInfo): $ReadOnlyArray { return rawMessageInfo.initialThreadState.memberIDs; 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 @@ -153,5 +153,5 @@ }; }, - generatesNotifs: () => true, + generatesNotifs: async () => true, }); 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 @@ -179,5 +179,5 @@ return joinResult(rawMessageInfo.creatorID, rawMessageInfo.entryID); }, - generatesNotifs: () => true, + generatesNotifs: async () => true, }); 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 @@ -134,5 +134,5 @@ return joinResult(rawMessageInfo.type, rawMessageInfo.threadID); }, - generatesNotifs: () => false, + generatesNotifs: async () => false, }); 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 @@ -134,5 +134,5 @@ return joinResult(rawMessageInfo.type, rawMessageInfo.threadID); }, - generatesNotifs: () => false, + generatesNotifs: async () => false, }); 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 @@ -110,7 +110,7 @@ +generatesNotifs: ( rawMessageInfo: RawInfo, params: GeneratesNotifsParams, - ) => boolean, + ) => Promise, +userIDs?: (rawMessageInfo: RawInfo) => $ReadOnlyArray, +startsThread?: boolean, +threadIDs?: (rawMessageInfo: RawInfo) => $ReadOnlyArray, 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 @@ -318,7 +318,7 @@ ); }, - generatesNotifs: () => true, + generatesNotifs: async () => true, includedInRepliesCount: true, }); 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 @@ -208,5 +208,5 @@ ); }, - generatesNotifs: () => true, + generatesNotifs: async () => true, }); 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 @@ -177,7 +177,7 @@ ); }, - generatesNotifs: () => false, + generatesNotifs: async () => false, userIDs(rawMessageInfo: RawRemoveMembersMessageInfo): $ReadOnlyArray { return rawMessageInfo.removedUserIDs; 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 @@ -153,5 +153,5 @@ }; }, - generatesNotifs: () => true, + generatesNotifs: async () => true, }); 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 @@ -177,7 +177,7 @@ return params.notificationTexts([sourceMessageInfo], threadInfo); }, - generatesNotifs: () => false, + generatesNotifs: async () => false, startsThread: true, }); 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 @@ -195,6 +195,6 @@ } }, - generatesNotifs: () => true, + generatesNotifs: async () => true, includedInRepliesCount: true, }); diff --git a/lib/shared/messages/unsupported-message-spec.js b/lib/shared/messages/unsupported-message-spec.js --- a/lib/shared/messages/unsupported-message-spec.js +++ b/lib/shared/messages/unsupported-message-spec.js @@ -90,5 +90,5 @@ return `${creator} ${messageInfo.robotext}`; }, - generatesNotifs: () => true, + generatesNotifs: async () => true, }); 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 @@ -197,5 +197,5 @@ }; }, - generatesNotifs: () => true, + generatesNotifs: async () => true, });