diff --git a/materialious/src/lib/api/backend.ts b/materialious/src/lib/api/backend.ts index c18b82f1..8f5d7ed8 100644 --- a/materialious/src/lib/api/backend.ts +++ b/materialious/src/lib/api/backend.ts @@ -263,7 +263,24 @@ export async function getKeyValue(key: string): Promise { return (await decryptWithMasterKey(respJson.valueNonce, respJson.valueCipher)) ?? null; } -export async function saveHistoryToBackend(video: VideoPlay, progress: number = 0) { +export async function updateWatchHistory(videoId: string, progress: number) { + await sodium.ready; + const rawKey = await getRawKey(); + if (!rawKey) return; + + const videoHash = await getSecureHash(videoId, rawKey); + + await fetch(`/api/user/history/${videoHash}`, { + method: 'POST', + credentials: 'same-origin', + body: JSON.stringify({ + watched: new Date(), + progress + }) + }); +} + +export async function saveWatchHistory(video: VideoPlay, progress: number = 0) { await sodium.ready; const rawKey = await getRawKey(); if (!rawKey) return; @@ -277,6 +294,7 @@ export async function saveHistoryToBackend(video: VideoPlay, progress: number = await fetch('/api/user/history', { method: 'POST', + credentials: 'same-origin', body: JSON.stringify({ id: videoHash, watched: new Date(), diff --git a/materialious/src/lib/watch.ts b/materialious/src/lib/watch.ts index fa9b12a8..7de56282 100644 --- a/materialious/src/lib/watch.ts +++ b/materialious/src/lib/watch.ts @@ -19,7 +19,7 @@ import { error } from '@sveltejs/kit'; import { get } from 'svelte/store'; import { _ } from './i18n'; import { isOwnBackend } from './shared'; -import { saveHistoryToBackend } from './api/backend'; +import { saveWatchHistory } from './api/backend'; export async function getWatchDetails(videoId: string, url: URL) { const playerStateRetrieved = get(playerState); @@ -49,7 +49,7 @@ export async function getWatchDetails(videoId: string, url: URL) { } if (isOwnBackend()?.internalAuth && get(rawMasterKeyStore)) { - saveHistoryToBackend(video); + saveWatchHistory(video); } let comments;