Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/creators/message-creator.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import _pickBy from 'lodash/fp/pickBy.js'; | import _pickBy from 'lodash/fp/pickBy.js'; | ||||
import { permissionLookup } from 'lib/permissions/thread-permissions.js'; | import { permissionLookup } from 'lib/permissions/thread-permissions.js'; | ||||
import { | import { | ||||
rawMessageInfoFromMessageData, | rawMessageInfoFromMessageData, | ||||
shimUnsupportedRawMessageInfos, | shimUnsupportedRawMessageInfos, | ||||
stripLocalIDs, | stripLocalIDs, | ||||
} from 'lib/shared/message-utils.js'; | } from 'lib/shared/message-utils.js'; | ||||
import { pushTypes } from 'lib/shared/messages/message-spec.js'; | import { pushTypes } from 'lib/shared/messages/message-spec.js'; | ||||
import type { PushType } from 'lib/shared/messages/message-spec.js'; | |||||
import { messageSpecs } from 'lib/shared/messages/message-specs.js'; | import { messageSpecs } from 'lib/shared/messages/message-specs.js'; | ||||
import { | import { | ||||
messageTypes, | messageTypes, | ||||
messageDataLocalID, | messageDataLocalID, | ||||
type MessageData, | type MessageData, | ||||
type RawMessageInfo, | type RawMessageInfo, | ||||
} from 'lib/types/message-types.js'; | } from 'lib/types/message-types.js'; | ||||
import { redisMessageTypes } from 'lib/types/redis-types.js'; | import { redisMessageTypes } from 'lib/types/redis-types.js'; | ||||
▲ Show 20 Lines • Show All 398 Lines • ▼ Show 20 Lines | for (const pair of perUserInfo) { | ||||
); | ); | ||||
const { userNotMemberOfSubthreads } = preUserPushInfo; | const { userNotMemberOfSubthreads } = preUserPushInfo; | ||||
const userDevices = [...preUserPushInfo.devices.values()]; | const userDevices = [...preUserPushInfo.devices.values()]; | ||||
if (userDevices.length === 0) { | if (userDevices.length === 0) { | ||||
continue; | continue; | ||||
} | } | ||||
const userPushInfoMessageInfoPromises = []; | const generateNotifUserInfoPromise = async (pushType: PushType) => { | ||||
const promises = []; | |||||
for (const threadID of preUserPushInfo.notFocusedThreadIDs) { | for (const threadID of preUserPushInfo.notFocusedThreadIDs) { | ||||
const messageIndices = threadsToMessageIndices.get(threadID); | const messageIndices = threadsToMessageIndices.get(threadID); | ||||
invariant(messageIndices, `indices should exist for thread ${threadID}`); | invariant( | ||||
userPushInfoMessageInfoPromises.push( | messageIndices, | ||||
`indices should exist for thread ${threadID}`, | |||||
); | |||||
promises.push( | |||||
...messageIndices.map(async messageIndex => { | ...messageIndices.map(async messageIndex => { | ||||
const messageInfo = messageInfos[messageIndex]; | const messageInfo = messageInfos[messageIndex]; | ||||
const { type } = messageInfo; | const { type } = messageInfo; | ||||
if (messageInfo.creatorID === userID) { | if (messageInfo.creatorID === userID) { | ||||
// We never send a user notifs about their own activity | // We never send a user notifs about their own activity | ||||
return undefined; | return undefined; | ||||
} | } | ||||
const { generatesNotifs } = messageSpecs[type]; | const { generatesNotifs } = messageSpecs[type]; | ||||
const messageData = messageDatas[messageIndex]; | const messageData = messageDatas[messageIndex]; | ||||
const doesGenerateNotif = await generatesNotifs( | const doesGenerateNotif = await generatesNotifs( | ||||
messageInfo, | messageInfo, | ||||
messageData, | messageData, | ||||
{ | { | ||||
notifTargetUserID: userID, | notifTargetUserID: userID, | ||||
userNotMemberOfSubthreads, | userNotMemberOfSubthreads, | ||||
fetchMessageInfoByID: (messageID: string) => | fetchMessageInfoByID: (messageID: string) => | ||||
fetchMessageInfoByID(viewer, messageID), | fetchMessageInfoByID(viewer, messageID), | ||||
}, | }, | ||||
); | ); | ||||
return doesGenerateNotif === pushTypes.NOTIF | return doesGenerateNotif === pushType ? messageInfo : undefined; | ||||
? messageInfo | |||||
: undefined; | |||||
}), | }), | ||||
); | ); | ||||
} | } | ||||
const userPushInfoPromise = (async () => { | |||||
const pushMessageInfos = await Promise.all( | const notifMessageInfos = await Promise.all(promises); | ||||
userPushInfoMessageInfoPromises, | const filteredNotifMessageInfos = notifMessageInfos.filter(Boolean); | ||||
); | |||||
const filteredMessageInfos = pushMessageInfos.filter(Boolean); | if (filteredNotifMessageInfos.length === 0) { | ||||
if (filteredMessageInfos.length === 0) { | |||||
return undefined; | return undefined; | ||||
} | } | ||||
return { | return { | ||||
devices: userDevices, | devices: userDevices, | ||||
messageInfos: filteredMessageInfos, | messageInfos: filteredNotifMessageInfos, | ||||
}; | |||||
}; | }; | ||||
})(); | |||||
const userPushInfoPromise = generateNotifUserInfoPromise(pushTypes.NOTIF); | |||||
userPushInfoPromises[userID] = userPushInfoPromise; | userPushInfoPromises[userID] = userPushInfoPromise; | ||||
} | } | ||||
const latestMessages = flattenLatestMessagesPerUser(latestMessagesPerUser); | const latestMessages = flattenLatestMessagesPerUser(latestMessagesPerUser); | ||||
const [pushInfo] = await Promise.all([ | const [pushInfo] = await Promise.all([ | ||||
promiseAll(userPushInfoPromises), | promiseAll(userPushInfoPromises), | ||||
createReadStatusUpdates(latestMessages), | createReadStatusUpdates(latestMessages), | ||||
▲ Show 20 Lines • Show All 181 Lines • Show Last 20 Lines |