From 53d5ca14617d0c76d980fd7a28855540fc71248d Mon Sep 17 00:00:00 2001 From: William Oldham Date: Wed, 6 Dec 2023 19:43:37 +0000 Subject: [PATCH 1/6] Add updatedAt saving into progress imports --- src/routes/users/progress.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/routes/users/progress.ts b/src/routes/users/progress.ts index 7575248..2a5b6ec 100644 --- a/src/routes/users/progress.ts +++ b/src/routes/users/progress.ts @@ -18,6 +18,7 @@ const progressItemSchema = z.object({ episodeId: z.string().optional(), seasonNumber: z.number().optional(), episodeNumber: z.number().optional(), + updatedAt: z.string().datetime({ offset: true }).optional(), }); export const userProgressRouter = makeRouter((app) => { @@ -59,7 +60,7 @@ export const userProgressRouter = makeRouter((app) => { duration: body.duration, watched: body.watched, meta: body.meta, - updatedAt: new Date(), + updatedAt: defaultAndCoerceDateTime(body.updatedAt), }); await em.persistAndFlush(progressItem); @@ -100,7 +101,9 @@ export const userProgressRouter = makeRouter((app) => { if (newItemIndex > -1) { const newItem = newItems[newItemIndex]; if (existingItem.watched < newItem.watched) { - existingItem.updatedAt = new Date(); + existingItem.updatedAt = defaultAndCoerceDateTime( + newItem.updatedAt, + ); existingItem.watched = newItem.watched; } itemsUpserted.push(existingItem); @@ -123,7 +126,7 @@ export const userProgressRouter = makeRouter((app) => { tmdbId: newItem.tmdbId, userId: params.uid, watched: newItem.watched, - updatedAt: new Date(), + updatedAt: defaultAndCoerceDateTime(newItem.updatedAt), }); } @@ -207,3 +210,14 @@ export const userProgressRouter = makeRouter((app) => { }), ); }); + +// 1st August 2021 - movie-web epoch +const minEpoch = 1627776000000; + +function defaultAndCoerceDateTime(dateTime: string | undefined) { + const epoch = dateTime ? new Date(dateTime).getTime() : Date.now(); + + const clampedEpoch = Math.max(minEpoch, Math.min(epoch, Date.now())); + + return new Date(clampedEpoch); +} From 4bf0285d06aceb348928b1629d723fbb986167ab Mon Sep 17 00:00:00 2001 From: William Oldham Date: Wed, 6 Dec 2023 19:43:57 +0000 Subject: [PATCH 2/6] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3b1561e..398f5bd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "backend", - "version": "1.1.4", + "version": "1.1.5", "private": true, "homepage": "https://github.com/movie-web/backend", "engines": { From e5c3cde51b128cfe1e64c4c9dba63a8b67cfbf00 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Wed, 6 Dec 2023 19:46:22 +0000 Subject: [PATCH 3/6] Only use user date on bulk import --- src/routes/users/progress.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/users/progress.ts b/src/routes/users/progress.ts index 2a5b6ec..53b285d 100644 --- a/src/routes/users/progress.ts +++ b/src/routes/users/progress.ts @@ -60,7 +60,7 @@ export const userProgressRouter = makeRouter((app) => { duration: body.duration, watched: body.watched, meta: body.meta, - updatedAt: defaultAndCoerceDateTime(body.updatedAt), + updatedAt: new Date(), }); await em.persistAndFlush(progressItem); From 4129b80828aa40cf27b3e1a7d5b4463a14b57a1e Mon Sep 17 00:00:00 2001 From: William Oldham Date: Wed, 6 Dec 2023 19:49:30 +0000 Subject: [PATCH 4/6] Use movie-web birthday --- src/routes/users/progress.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/users/progress.ts b/src/routes/users/progress.ts index 53b285d..cc3e649 100644 --- a/src/routes/users/progress.ts +++ b/src/routes/users/progress.ts @@ -211,8 +211,8 @@ export const userProgressRouter = makeRouter((app) => { ); }); -// 1st August 2021 - movie-web epoch -const minEpoch = 1627776000000; +// 13th July 2021 - movie-web epoch +const minEpoch = 1626134400000; function defaultAndCoerceDateTime(dateTime: string | undefined) { const epoch = dateTime ? new Date(dateTime).getTime() : Date.now(); From cf0125755ccf4b0d20a0ad0b9b2a3f46c4fc6485 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Wed, 6 Dec 2023 19:55:09 +0000 Subject: [PATCH 5/6] Use date to compare progress items --- src/routes/users/progress.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/routes/users/progress.ts b/src/routes/users/progress.ts index cc3e649..fbeb4e5 100644 --- a/src/routes/users/progress.ts +++ b/src/routes/users/progress.ts @@ -100,10 +100,9 @@ export const userProgressRouter = makeRouter((app) => { if (newItemIndex > -1) { const newItem = newItems[newItemIndex]; - if (existingItem.watched < newItem.watched) { - existingItem.updatedAt = defaultAndCoerceDateTime( - newItem.updatedAt, - ); + const newItemDate = defaultAndCoerceDateTime(newItem.updatedAt); + if (existingItem.updatedAt.getTime() < newItemDate.getTime()) { + existingItem.updatedAt = newItemDate; existingItem.watched = newItem.watched; } itemsUpserted.push(existingItem); From 05bf65193938ab35f0104b4271ad90ed66537d55 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Wed, 6 Dec 2023 19:59:50 +0000 Subject: [PATCH 6/6] Revert "Use date to compare progress items" This reverts commit cf0125755ccf4b0d20a0ad0b9b2a3f46c4fc6485. --- src/routes/users/progress.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/routes/users/progress.ts b/src/routes/users/progress.ts index fbeb4e5..cc3e649 100644 --- a/src/routes/users/progress.ts +++ b/src/routes/users/progress.ts @@ -100,9 +100,10 @@ export const userProgressRouter = makeRouter((app) => { if (newItemIndex > -1) { const newItem = newItems[newItemIndex]; - const newItemDate = defaultAndCoerceDateTime(newItem.updatedAt); - if (existingItem.updatedAt.getTime() < newItemDate.getTime()) { - existingItem.updatedAt = newItemDate; + if (existingItem.watched < newItem.watched) { + existingItem.updatedAt = defaultAndCoerceDateTime( + newItem.updatedAt, + ); existingItem.watched = newItem.watched; } itemsUpserted.push(existingItem);