-
Notifications
You must be signed in to change notification settings - Fork 29
/
rescind.js
384 lines (358 loc) · 11.5 KB
/
rescind.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// @flow
import apn from '@parse/node-apn';
import type { ResponseFailure } from '@parse/node-apn';
import type { FirebaseError } from 'firebase-admin';
import invariant from 'invariant';
import { createAndroidNotificationRescind } from 'lib/push/android-notif-creators.js';
import { getAPNsNotificationTopic } from 'lib/shared/notif-utils.js';
import type { PlatformDetails } from 'lib/types/device-types.js';
import type {
NotificationTargetDevice,
TargetedAndroidNotification,
SenderDeviceDescriptor,
EncryptedNotifUtilsAPI,
} from 'lib/types/notif-types.js';
import { threadSubscriptions } from 'lib/types/subscription-types.js';
import { threadPermissions } from 'lib/types/thread-permission-types.js';
import { promiseAll } from 'lib/utils/promises.js';
import { tID } from 'lib/utils/validation-utils.js';
import { prepareEncryptedIOSNotificationRescind } from './crypto.js';
import encryptedNotifUtilsAPI from './encrypted-notif-utils-api.js';
import type { TargetedAPNsNotification } from './types.js';
import {
apnPush,
fcmPush,
type APNPushResult,
type FCMPushResult,
} from './utils.js';
import createIDs from '../creators/id-creator.js';
import { dbQuery, SQL } from '../database/database.js';
import type { SQLStatementType } from '../database/types.js';
import { thisKeyserverID } from '../user/identity.js';
import { validateOutput } from '../utils/validation-utils.js';
type ParsedDelivery = {
+platform: 'ios' | 'macos' | 'android',
+codeVersion: ?number,
+stateVersion: ?number,
+notificationID: string,
+deviceTokens: $ReadOnlyArray<string>,
};
type RescindDelivery = {
source: 'rescind',
rescindedID: string,
errors?:
| $ReadOnlyArray<FirebaseError | mixed>
| $ReadOnlyArray<ResponseFailure>,
};
async function rescindPushNotifs(
notifCondition: SQLStatementType,
inputCountCondition?: SQLStatementType,
) {
const notificationExtractString = `$.${threadSubscriptions.home}`;
const visPermissionExtractString = `$.${threadPermissions.VISIBLE}.value`;
const fetchQuery = SQL`
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], keyserverID] = await Promise.all([
dbQuery(fetchQuery),
thisKeyserverID(),
]);
const allDeviceTokens = new Set<string>();
const parsedDeliveries: { [string]: $ReadOnlyArray<ParsedDelivery> } = {};
for (const row of fetchResult) {
const rawDelivery = JSON.parse(row.delivery);
const deliveries = Array.isArray(rawDelivery) ? rawDelivery : [rawDelivery];
const id = row.id.toString();
const rowParsedDeliveries = [];
for (const delivery of deliveries) {
if (
delivery.iosID ||
delivery.deviceType === 'ios' ||
delivery.deviceType === 'macos'
) {
const deviceTokens = delivery.iosDeviceTokens ?? delivery.deviceTokens;
rowParsedDeliveries.push({
notificationID: delivery.iosID,
codeVersion: delivery.codeVersion,
stateVersion: delivery.stateVersion,
platform: delivery.deviceType ?? 'ios',
deviceTokens,
});
deviceTokens.forEach(deviceToken => allDeviceTokens.add(deviceToken));
} else if (delivery.androidID || delivery.deviceType === 'android') {
const deviceTokens =
delivery.androidDeviceTokens ?? delivery.deviceTokens;
rowParsedDeliveries.push({
notificationID: row.collapse_key ? row.collapse_key : id,
codeVersion: delivery.codeVersion,
stateVersion: delivery.stateVersion,
platform: 'android',
deviceTokens,
});
deviceTokens.forEach(deviceToken => allDeviceTokens.add(deviceToken));
}
}
parsedDeliveries[id] = rowParsedDeliveries;
}
const deviceTokenToCookieID = await getDeviceTokenToCookieID(allDeviceTokens);
const deliveryPromises: {
[string]: Promise<APNPushResult> | Promise<FCMPushResult>,
} = {};
const notifInfo = {};
const rescindedIDs = [];
for (const row of fetchResult) {
const id = row.id.toString();
const threadID = row.thread.toString();
notifInfo[id] = {
userID: row.user.toString(),
threadID,
messageID: row.message.toString(),
};
for (const delivery of parsedDeliveries[id]) {
let platformDetails: PlatformDetails = { platform: delivery.platform };
if (delivery.codeVersion) {
platformDetails = {
...platformDetails,
codeVersion: delivery.codeVersion,
};
}
if (delivery.stateVersion) {
platformDetails = {
...platformDetails,
stateVersion: delivery.stateVersion,
};
}
if (delivery.platform === 'ios') {
const devices = delivery.deviceTokens.map(deviceToken => ({
deliveryID: deviceToken,
cryptoID: deviceTokenToCookieID[deviceToken],
}));
const deliveryPromise = (async () => {
const targetedNotifications = await prepareIOSNotification(
keyserverID,
delivery.notificationID,
row.unread_count,
threadID,
platformDetails,
devices,
);
return await apnPush({
targetedNotifications,
platformDetails: {
platform: 'ios',
codeVersion: delivery.codeVersion,
},
});
})();
deliveryPromises[id] = deliveryPromise;
} else if (delivery.platform === 'android') {
const devices = delivery.deviceTokens.map(deviceToken => ({
deliveryID: deviceToken,
cryptoID: deviceTokenToCookieID[deviceToken],
}));
const deliveryPromise = (async () => {
const targetedNotifications = await prepareAndroidNotification(
keyserverID,
delivery.notificationID,
row.unread_count,
threadID,
platformDetails,
devices,
);
return await fcmPush({
targetedNotifications,
codeVersion: delivery.codeVersion,
});
})();
deliveryPromises[id] = deliveryPromise;
}
}
rescindedIDs.push(id);
}
const numRescinds = Object.keys(deliveryPromises).length;
const dbIDsPromise: Promise<?Array<string>> = (async () => {
if (numRescinds === 0) {
return undefined;
}
return await createIDs('notifications', numRescinds);
})();
const rescindPromise: Promise<mixed> = (async () => {
if (rescindedIDs.length === 0) {
return undefined;
}
const rescindQuery = SQL`
UPDATE notifications SET rescinded = 1 WHERE id IN (${rescindedIDs})
`;
return await dbQuery(rescindQuery);
})();
const [deliveryResults, dbIDs] = await Promise.all([
promiseAll(deliveryPromises),
dbIDsPromise,
rescindPromise,
]);
const newNotifRows = [];
if (numRescinds > 0) {
invariant(dbIDs, 'dbIDs should be set');
for (const rescindedID in deliveryResults) {
const delivery: RescindDelivery = {
source: 'rescind',
rescindedID,
};
const { errors } = deliveryResults[rescindedID];
if (errors) {
delivery.errors = errors;
}
const dbID = dbIDs.shift();
const { userID, threadID, messageID } = notifInfo[rescindedID];
newNotifRows.push([
dbID,
userID,
threadID,
messageID,
null,
JSON.stringify([delivery]),
1,
]);
}
}
if (newNotifRows.length > 0) {
const insertQuery = SQL`
INSERT INTO notifications
(id, user, thread, message, collapse_key, delivery, rescinded)
VALUES ${newNotifRows}
`;
await dbQuery(insertQuery);
}
}
async function getDeviceTokenToCookieID(
deviceTokens: Set<string>,
): Promise<{ +[string]: string }> {
if (deviceTokens.size === 0) {
return {};
}
const deviceTokenToCookieID = {};
const fetchCookiesQuery = SQL`
SELECT id, device_token FROM cookies
WHERE device_token IN (${[...deviceTokens]})
`;
const [fetchResult] = await dbQuery(fetchCookiesQuery);
for (const row of fetchResult) {
deviceTokenToCookieID[row.device_token.toString()] = row.id.toString();
}
return deviceTokenToCookieID;
}
async function conditionallyEncryptNotification<T>(
encryptedNotifUtilsAPIInstance: EncryptedNotifUtilsAPI,
senderDeviceDescriptor: SenderDeviceDescriptor,
notification: T,
codeVersion: ?number,
devices: $ReadOnlyArray<NotificationTargetDevice>,
encryptCallback: (
encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI,
senderDeviceDescriptor: SenderDeviceDescriptor,
devices: $ReadOnlyArray<NotificationTargetDevice>,
notification: T,
codeVersion?: ?number,
) => Promise<
$ReadOnlyArray<{
+notification: T,
+cryptoID: string,
+deliveryID: string,
+encryptionOrder?: number,
}>,
>,
): Promise<$ReadOnlyArray<{ +deliveryID: string, +notification: T }>> {
const shouldBeEncrypted = codeVersion && codeVersion >= 233;
if (!shouldBeEncrypted) {
return devices.map(({ deliveryID }) => ({
notification,
deliveryID,
}));
}
const notifications = await encryptCallback(
encryptedNotifUtilsAPI,
senderDeviceDescriptor,
devices,
notification,
codeVersion,
);
return notifications.map(({ deliveryID, notification: notif }) => ({
deliveryID,
notification: notif,
}));
}
async function prepareIOSNotification(
keyserverID: string,
iosID: string,
unreadCount: number,
threadID: string,
platformDetails: PlatformDetails,
devices: $ReadOnlyArray<NotificationTargetDevice>,
): Promise<$ReadOnlyArray<TargetedAPNsNotification>> {
threadID = await validateOutput(platformDetails, tID, threadID);
const { codeVersion } = platformDetails;
const notification = new apn.Notification();
notification.topic = getAPNsNotificationTopic({
platform: 'ios',
codeVersion,
});
if (codeVersion && codeVersion > 198) {
notification.mutableContent = true;
notification.pushType = 'alert';
notification.badge = unreadCount;
} else {
notification.priority = 5;
notification.contentAvailable = true;
notification.pushType = 'background';
}
notification.payload = {
backgroundNotifType: 'CLEAR',
notificationId: iosID,
setUnreadStatus: true,
threadID,
keyserverID,
};
return await conditionallyEncryptNotification(
encryptedNotifUtilsAPI,
{ keyserverID },
notification,
codeVersion,
devices,
prepareEncryptedIOSNotificationRescind,
);
}
async function prepareAndroidNotification(
keyserverID: string,
notifID: string,
unreadCount: number,
threadID: string,
platformDetails: PlatformDetails,
devices: $ReadOnlyArray<NotificationTargetDevice>,
): Promise<$ReadOnlyArray<TargetedAndroidNotification>> {
threadID = await validateOutput(platformDetails, tID, threadID);
return await createAndroidNotificationRescind(
encryptedNotifUtilsAPI,
{
senderDeviceDescriptor: { keyserverID },
badge: unreadCount.toString(),
platformDetails,
rescindID: notifID,
threadID,
},
devices,
);
}
export { rescindPushNotifs };