Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/push/rescind.js
// @flow | // @flow | ||||
import apn from '@parse/node-apn'; | import apn from '@parse/node-apn'; | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import { threadSubscriptions } from 'lib/types/subscription-types'; | |||||
import { threadPermissions } from 'lib/types/thread-types'; | |||||
import { promiseAll } from 'lib/utils/promises'; | import { promiseAll } from 'lib/utils/promises'; | ||||
import createIDs from '../creators/id-creator'; | import createIDs from '../creators/id-creator'; | ||||
import { dbQuery, SQL } from '../database/database'; | import { dbQuery, SQL } from '../database/database'; | ||||
import type { SQLStatementType } from '../database/types'; | import type { SQLStatementType } from '../database/types'; | ||||
import { fetchUnreadNotifs } from '../fetchers/notification-fetchers'; | |||||
import { getAPNsNotificationTopic } from './providers'; | import { getAPNsNotificationTopic } from './providers'; | ||||
import { apnPush, fcmPush } from './utils'; | import { apnPush, fcmPush } from './utils'; | ||||
async function rescindPushNotifs( | async function rescindPushNotifs( | ||||
notifCondition: SQLStatementType, | notifCondition: SQLStatementType, | ||||
inputCountCondition?: SQLStatementType, | inputCountCondition?: SQLStatementType, | ||||
) { | ) { | ||||
const notificationExtractString = `$.${threadSubscriptions.home}`; | const unreadNotifs = await fetchUnreadNotifs( | ||||
const visPermissionExtractString = `$.${threadPermissions.VISIBLE}.value`; | notifCondition, | ||||
const fetchQuery = SQL` | inputCountCondition, | ||||
SELECT n.id, n.user, n.thread, n.message, n.delivery, n.collapse_key, COUNT( | ); | ||||
`; | |||||
fetchQuery.append(inputCountCondition ? inputCountCondition : SQL`m.thread`); | |||||
fetchQuery.append(SQL` | |||||
) AS unread_count | |||||
FROM notifications n | |||||
LEFT JOIN memberships m ON m.user = n.user | |||||
AND m.last_message > m.last_read_message | |||||
AND m.role > 0 | |||||
AND JSON_EXTRACT(subscription, ${notificationExtractString}) | |||||
AND JSON_EXTRACT(permissions, ${visPermissionExtractString}) | |||||
WHERE n.rescinded = 0 AND | |||||
`); | |||||
fetchQuery.append(notifCondition); | |||||
fetchQuery.append(SQL` GROUP BY n.id, m.user`); | |||||
const [fetchResult] = await dbQuery(fetchQuery); | |||||
const deliveryPromises = {}; | const deliveryPromises = {}; | ||||
const notifInfo = {}; | const notifInfo = {}; | ||||
const rescindedIDs = []; | const rescindedIDs = []; | ||||
for (const row of fetchResult) { | for (const row of unreadNotifs) { | ||||
const rawDelivery = JSON.parse(row.delivery); | const rawDelivery = JSON.parse(row.delivery); | ||||
const deliveries = Array.isArray(rawDelivery) ? rawDelivery : [rawDelivery]; | const deliveries = Array.isArray(rawDelivery) ? rawDelivery : [rawDelivery]; | ||||
const id = row.id.toString(); | const id = row.id; | ||||
const threadID = row.thread.toString(); | const threadID = row.thread; | ||||
notifInfo[id] = { | notifInfo[id] = { | ||||
userID: row.user.toString(), | userID: row.user, | ||||
threadID, | threadID, | ||||
messageID: row.message.toString(), | messageID: row.message, | ||||
}; | }; | ||||
for (const delivery of deliveries) { | for (const delivery of deliveries) { | ||||
if (delivery.iosID && delivery.iosDeviceTokens) { | if (delivery.iosID && delivery.iosDeviceTokens) { | ||||
// Old iOS | // Old iOS | ||||
const notification = prepareIOSNotification( | const notification = prepareIOSNotification( | ||||
delivery.iosID, | delivery.iosID, | ||||
row.unread_count, | row.unreadCount, | ||||
threadID, | threadID, | ||||
); | ); | ||||
deliveryPromises[id] = apnPush({ | deliveryPromises[id] = apnPush({ | ||||
notification, | notification, | ||||
deviceTokens: delivery.iosDeviceTokens, | deviceTokens: delivery.iosDeviceTokens, | ||||
codeVersion: null, | codeVersion: null, | ||||
}); | }); | ||||
} else if (delivery.androidID) { | } else if (delivery.androidID) { | ||||
// Old Android | // Old Android | ||||
const notification = prepareAndroidNotification( | const notification = prepareAndroidNotification( | ||||
row.collapse_key ? row.collapse_key : id, | row.collapseKey ? row.collapseKey : id, | ||||
row.unread_count, | row.unreadCount, | ||||
threadID, | threadID, | ||||
); | ); | ||||
deliveryPromises[id] = fcmPush({ | deliveryPromises[id] = fcmPush({ | ||||
notification, | notification, | ||||
deviceTokens: delivery.androidDeviceTokens, | deviceTokens: delivery.androidDeviceTokens, | ||||
codeVersion: null, | codeVersion: null, | ||||
}); | }); | ||||
} else if (delivery.deviceType === 'ios') { | } else if (delivery.deviceType === 'ios') { | ||||
// New iOS | // New iOS | ||||
const { iosID, deviceTokens, codeVersion } = delivery; | const { iosID, deviceTokens, codeVersion } = delivery; | ||||
const notification = prepareIOSNotification( | const notification = prepareIOSNotification( | ||||
iosID, | iosID, | ||||
row.unread_count, | row.unreadCount, | ||||
threadID, | threadID, | ||||
codeVersion, | codeVersion, | ||||
); | ); | ||||
deliveryPromises[id] = apnPush({ | deliveryPromises[id] = apnPush({ | ||||
notification, | notification, | ||||
deviceTokens, | deviceTokens, | ||||
codeVersion, | codeVersion, | ||||
}); | }); | ||||
} else if (delivery.deviceType === 'android') { | } else if (delivery.deviceType === 'android') { | ||||
// New Android | // New Android | ||||
const { deviceTokens, codeVersion } = delivery; | const { deviceTokens, codeVersion } = delivery; | ||||
const notification = prepareAndroidNotification( | const notification = prepareAndroidNotification( | ||||
row.collapse_key ? row.collapse_key : id, | row.collapseKey ? row.collapseKey : id, | ||||
row.unread_count, | row.unreadCount, | ||||
threadID, | threadID, | ||||
); | ); | ||||
deliveryPromises[id] = fcmPush({ | deliveryPromises[id] = fcmPush({ | ||||
notification, | notification, | ||||
deviceTokens, | deviceTokens, | ||||
codeVersion, | codeVersion, | ||||
}); | }); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 96 Lines • Show Last 20 Lines |