Skip to content

Commit b4221bb

Browse files
authored
Only support new sync tokens (#306)
1 parent 356f171 commit b4221bb

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

api/routes/profile.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,19 @@ function extractSyncToken(syncTokenParam: string | undefined) {
185185

186186
try {
187187
const tokenMap = JSON.parse(syncTokenParam) as { [component: string]: string | number };
188-
const tokens = Object.entries(tokenMap).reduce<{ [component: string]: Buffer | number }>(
188+
const tokens = Object.entries(tokenMap).reduce<{ [component: string]: number }>(
189189
(acc, [component, token]) => {
190-
acc[component] =
191-
typeof token === 'string' && !/^\d+$/.exec(token)
192-
? Buffer.from(token, 'base64')
193-
: Number(token);
190+
if (typeof token === 'number' || (typeof token === 'string' && /^\d+$/.exec(token))) {
191+
acc[component] = Number(token);
192+
}
194193
return acc;
195194
},
196195
{},
197196
);
198197

199198
if (
200199
Object.values(tokens).some(
201-
(t) =>
202-
typeof t === 'number' && Date.now() - new Date(t).getTime() > 30 * 24 * 60 * 60 * 1000,
200+
(t) => Date.now() - new Date(t).getTime() > 30 * 24 * 60 * 60 * 1000,
203201
)
204202
) {
205203
return undefined; // Don't accept sync tokens older than 30 days
@@ -218,7 +216,7 @@ async function loadProfile(
218216
bungieMembershipId: number,
219217
platformMembershipId: string | undefined,
220218
destinyVersion: DestinyVersion,
221-
incomingSyncTokens?: { [component: string]: Buffer | number },
219+
incomingSyncTokens?: { [component: string]: number },
222220
) {
223221
const response: ProfileResponse = {
224222
sync: Boolean(incomingSyncTokens),
@@ -231,12 +229,12 @@ async function loadProfile(
231229
syncTokens[name] = token.tokenData;
232230
}
233231
};
234-
const getSyncToken = <T extends number | Buffer>(name: string) => {
232+
const getSyncToken = (name: string) => {
235233
const tokenData = incomingSyncTokens?.[name];
236234
// if (incomingSyncTokens && !tokenData) {
237235
// throw new Error(`Missing sync token: ${name}`);
238236
// }
239-
return tokenData as T | undefined;
237+
return tokenData;
240238
};
241239

242240
// We'll accumulate promises and await them all at the end
@@ -248,7 +246,7 @@ async function loadProfile(
248246
(async () => {
249247
const start = new Date();
250248
const now = Date.now();
251-
const tokenData = getSyncToken<number>('s');
249+
const tokenData = getSyncToken('s');
252250
// TODO: Should add the token to the query to avoid fetching if unchanged
253251
const pgSettings = await readTransaction(async (pgClient) =>
254252
tokenData
@@ -286,7 +284,7 @@ async function loadProfile(
286284

287285
if (components.includes('loadouts')) {
288286
const start = new Date();
289-
const tokenData = getSyncToken<number>('loadouts');
287+
const tokenData = getSyncToken('loadouts');
290288
if (tokenData) {
291289
const { updated, deletedLoadoutIds } = await syncLoadoutsForProfile(
292290
client,
@@ -317,7 +315,7 @@ async function loadProfile(
317315

318316
if (components.includes('tags')) {
319317
const start = new Date();
320-
const tokenData = getSyncToken<number>('tags');
318+
const tokenData = getSyncToken('tags');
321319
if (tokenData) {
322320
const { updated, deletedItemIds } = await syncItemAnnotationsForProfile(
323321
client,
@@ -345,7 +343,7 @@ async function loadProfile(
345343

346344
if (components.includes('hashtags')) {
347345
const start = new Date();
348-
const tokenData = getSyncToken<number>('hashtags');
346+
const tokenData = getSyncToken('hashtags');
349347
if (tokenData) {
350348
const { updated, deletedItemHashes } = await syncItemHashTagsForProfile(
351349
client,
@@ -368,7 +366,7 @@ async function loadProfile(
368366

369367
if (components.includes('triumphs') && destinyVersion === 2) {
370368
const start = new Date();
371-
const tokenData = getSyncToken<number>('triumphs');
369+
const tokenData = getSyncToken('triumphs');
372370
if (tokenData) {
373371
const { updated, deleted } = await syncTrackedTriumphsForProfile(
374372
client,
@@ -391,7 +389,7 @@ async function loadProfile(
391389

392390
if (components.includes('searches')) {
393391
const start = new Date();
394-
const tokenData = getSyncToken<number>('searches');
392+
const tokenData = getSyncToken('searches');
395393
if (tokenData) {
396394
const { updated, deletedSearchHashes } = await syncSearchesForProfile(
397395
client,

0 commit comments

Comments
 (0)