Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/push/send.js
Show All 27 Lines | import { | ||||
type MessageInfo, | type MessageInfo, | ||||
messageTypes, | messageTypes, | ||||
} from 'lib/types/message-types.js'; | } from 'lib/types/message-types.js'; | ||||
import type { ServerThreadInfo, ThreadInfo } from 'lib/types/thread-types.js'; | import type { ServerThreadInfo, ThreadInfo } from 'lib/types/thread-types.js'; | ||||
import { updateTypes } from 'lib/types/update-types.js'; | import { updateTypes } from 'lib/types/update-types.js'; | ||||
import { promiseAll } from 'lib/utils/promises.js'; | import { promiseAll } from 'lib/utils/promises.js'; | ||||
import { getAPNsNotificationTopic } from './providers.js'; | import { getAPNsNotificationTopic } from './providers.js'; | ||||
import { rescindPushNotifs } from './rescind.js'; | |||||
import { | import { | ||||
apnPush, | apnPush, | ||||
fcmPush, | fcmPush, | ||||
getUnreadCounts, | getUnreadCounts, | ||||
apnMaxNotificationPayloadByteSize, | apnMaxNotificationPayloadByteSize, | ||||
fcmMaxNotificationPayloadByteSize, | fcmMaxNotificationPayloadByteSize, | ||||
} from './utils.js'; | } from './utils.js'; | ||||
import createIDs from '../creators/id-creator.js'; | import createIDs from '../creators/id-creator.js'; | ||||
▲ Show 20 Lines • Show All 195 Lines • ▼ Show 20 Lines | async function sendPushNotifs(pushInfo: PushInfo) { | ||||
const [deliveryResults] = await Promise.all([ | const [deliveryResults] = await Promise.all([ | ||||
Promise.all(deliveryPromises), | Promise.all(deliveryPromises), | ||||
Promise.all(cleanUpPromises), | Promise.all(cleanUpPromises), | ||||
]); | ]); | ||||
await saveNotifResults(deliveryResults, notifications, true); | await saveNotifResults(deliveryResults, notifications, true); | ||||
} | } | ||||
async function sendRescindNotifs(rescindInfo: PushInfo) { | |||||
if (Object.keys(rescindInfo).length === 0) { | |||||
return; | |||||
} | |||||
const [{ usersToCollapsableNotifInfo }] = await Promise.all([ | |||||
fetchInfos(rescindInfo), | |||||
]); | |||||
const promises = []; | |||||
for (const userID in usersToCollapsableNotifInfo) { | |||||
for (const notifInfo of usersToCollapsableNotifInfo[userID]) { | |||||
if (notifInfo.existingMessageInfos.length === 0) { | |||||
continue; | |||||
} | |||||
for (const existingMessageInfo of notifInfo.existingMessageInfos) { | |||||
const rescindCondition = SQL` | |||||
n.user = ${userID} AND | |||||
n.thread = ${existingMessageInfo.threadID} AND | |||||
n.message = ${existingMessageInfo.id} | |||||
`; | |||||
const inputCountCondition = SQL` | |||||
IF(m.thread = ${existingMessageInfo.threadID}, NULL, m.thread) | |||||
`; | |||||
promises.push(rescindPushNotifs(rescindCondition, inputCountCondition)); | |||||
} | |||||
} | |||||
} | |||||
await Promise.all(promises); | |||||
} | |||||
// The results in deliveryResults will be combined with the rows | // The results in deliveryResults will be combined with the rows | ||||
// in rowsToSave and then written to the notifications table | // in rowsToSave and then written to the notifications table | ||||
async function saveNotifResults( | async function saveNotifResults( | ||||
deliveryResults: $ReadOnlyArray<IOSResult | AndroidResult>, | deliveryResults: $ReadOnlyArray<IOSResult | AndroidResult>, | ||||
inputRowsToSave: Map<string, NotificationRow>, | inputRowsToSave: Map<string, NotificationRow>, | ||||
rescindable: boolean, | rescindable: boolean, | ||||
) { | ) { | ||||
const rowsToSave = new Map(inputRowsToSave); | const rowsToSave = new Map(inputRowsToSave); | ||||
▲ Show 20 Lines • Show All 580 Lines • ▼ Show 20 Lines | for (const [codeVer, deviceTokens] of androidVersionsToTokens) { | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
const deliveryResults = await Promise.all(deliveryPromises); | const deliveryResults = await Promise.all(deliveryPromises); | ||||
await saveNotifResults(deliveryResults, new Map(), false); | await saveNotifResults(deliveryResults, new Map(), false); | ||||
} | } | ||||
export { sendPushNotifs, updateBadgeCount }; | export { sendPushNotifs, sendRescindNotifs, updateBadgeCount }; |