diff --git a/materialious/src/lib/api/backend/history.ts b/materialious/src/lib/api/backend/history.ts index 01801139..103dec9d 100644 --- a/materialious/src/lib/api/backend/history.ts +++ b/materialious/src/lib/api/backend/history.ts @@ -5,7 +5,7 @@ import { getBestThumbnail } from '$lib/images'; import { get } from 'svelte/store'; import { watchHistoryEnabledStore } from '$lib/store'; -export async function updateWatchHistory(videoId: string, progress: number) { +export async function updateWatchHistoryBackend(videoId: string, progress: number) { await sodium.ready; const rawKey = await getRawKey(); if (!rawKey) return; @@ -55,7 +55,7 @@ async function decryptWatchHistory( }; } -export async function getVideoWatchHistory( +export async function getVideoWatchHistoryBackend( videoId: string ): Promise { await sodium.ready; @@ -74,7 +74,7 @@ export async function getVideoWatchHistory( return await decryptWatchHistory(await resp.json()); } -export async function getWatchHistory( +export async function getWatchHistoryBackend( options: { page?: number; videoIds?: string[]; fetchOptions?: RequestInit } = { page: undefined, videoIds: undefined, @@ -116,11 +116,11 @@ export async function getWatchHistory( return history; } -export async function deleteWatchHistory() { +export async function deleteWatchHistoryBackend() { await fetch('/api/user/history', { method: 'DELETE' }); } -export async function saveWatchHistory(video: VideoPlay, progress: number = 0) { +export async function saveWatchHistoryBackend(video: VideoPlay, progress: number = 0) { if (!get(watchHistoryEnabledStore)) return; await sodium.ready; diff --git a/materialious/src/lib/api/backend/historyPool.ts b/materialious/src/lib/api/historyPool.ts similarity index 93% rename from materialious/src/lib/api/backend/historyPool.ts rename to materialious/src/lib/api/historyPool.ts index eda26eac..4710d7b0 100644 --- a/materialious/src/lib/api/backend/historyPool.ts +++ b/materialious/src/lib/api/historyPool.ts @@ -1,5 +1,5 @@ -import type { VideoWatchHistory } from '../model'; -import { getWatchHistory } from './history'; +import type { VideoWatchHistory } from './model'; +import { getWatchHistory } from './index'; const videoIds: string[] = []; const pendingResolves = new Map void>(); diff --git a/materialious/src/lib/api/index.ts b/materialious/src/lib/api/index.ts index 09de48fd..2055c621 100644 --- a/materialious/src/lib/api/index.ts +++ b/materialious/src/lib/api/index.ts @@ -1,6 +1,6 @@ import { getVideoYTjs } from '$lib/api/youtubejs/video'; import { get } from 'svelte/store'; -import { playerYouTubeJsAlways, rawMasterKeyStore } from '../store'; +import { invidiousAuthStore, playerYouTubeJsAlways, rawMasterKeyStore } from '../store'; import type { ChannelPage, Comments, @@ -15,7 +15,8 @@ import type { SearchResults, CommentsOptions, ChannelOptions, - ChannelContent + ChannelContent, + VideoWatchHistory } from './model'; import { commentsSetDefaults, searchSetDefaults, useEngineFallback } from './misc'; import { getSearchYTjs } from './youtubejs/search'; @@ -66,6 +67,13 @@ import { postPersonalPlaylistInvidious, removePlaylistVideoInvidious } from './invidious/playlist'; +import { + deleteWatchHistoryBackend, + getVideoWatchHistoryBackend, + getWatchHistoryBackend, + saveWatchHistoryBackend, + updateWatchHistoryBackend +} from './backend/history'; export async function getPopular(fetchOptions?: RequestInit): Promise { // Doesn't exist in YTjs. @@ -270,16 +278,48 @@ export async function deleteUnsubscribe(authorId: string, fetchOptions: RequestI return deleteUnsubscribeInvidious(authorId, fetchOptions); } -export async function getHistory(page: number = 1, maxResults: number = 20): Promise {} - -export async function deleteHistory(videoId: string | undefined = undefined) { - if (isYTBackend()) return; +export async function getWatchHistory( + options: { page?: number; videoIds?: string[]; fetchOptions?: RequestInit } = { + page: undefined, + videoIds: undefined, + fetchOptions: undefined + } +): Promise { + if (isOwnBackend()?.internalAuth && get(rawMasterKeyStore)) { + return getWatchHistoryBackend(options); + } } -export async function postHistory(videoId: string, fetchOptions: RequestInit = {}) { - if (isYTBackend()) return; +export async function getVideoWatchHistory( + videoId: string +): Promise { + if (isOwnBackend()?.internalAuth && get(rawMasterKeyStore)) { + return getVideoWatchHistoryBackend(videoId); + } +} - return postHistoryInvidious(videoId, fetchOptions); +export async function deleteWatchHistory() { + if (isOwnBackend()?.internalAuth && get(rawMasterKeyStore)) { + return deleteWatchHistoryBackend(); + } +} + +export async function updateWatchHistory( + videoId: string, + progress: number, + fetchOptions: RequestInit = {} +) { + if (isOwnBackend()?.internalAuth && get(rawMasterKeyStore)) { + return updateWatchHistoryBackend(videoId, progress); + } + + if (get(invidiousAuthStore)) postHistoryInvidious(videoId, fetchOptions); +} + +export async function saveWatchHistory(video: VideoPlay, progress: number = 0) { + if (isOwnBackend()?.internalAuth && get(rawMasterKeyStore)) { + return saveWatchHistoryBackend(video, progress); + } } export async function getPlaylist( diff --git a/materialious/src/lib/components/player/Player.svelte b/materialious/src/lib/components/player/Player.svelte index b8ec33be..9d4cb723 100644 --- a/materialious/src/lib/components/player/Player.svelte +++ b/materialious/src/lib/components/player/Player.svelte @@ -61,7 +61,7 @@ import { Network, type ConnectionStatus } from '@capacitor/network'; import { ScreenOrientation, type ScreenOrientationResult } from '@capacitor/screen-orientation'; import ClosedCaptions from './ClosedCaptions.svelte'; - import { getVideoWatchHistory, updateWatchHistory } from '$lib/api/backend/history'; + import { getVideoWatchHistory, updateWatchHistory } from '$lib/api'; interface Props { data: { video: VideoPlay; content: ParsedDescription; playlistId: string | null }; @@ -737,10 +737,8 @@ // Continue regardless of error } - if (isOwnBackend()?.internalAuth && get(rawMasterKeyStore)) { - const watchHistory = await getVideoWatchHistory(data.video.videoId); - if (watchHistory && watchHistory.progress > toSetTime) toSetTime = watchHistory.progress; - } + const watchHistory = await getVideoWatchHistory(data.video.videoId); + if (watchHistory && watchHistory.progress > toSetTime) toSetTime = watchHistory.progress; return toSetTime; } @@ -748,23 +746,7 @@ function savePlayerbackHistory() { if (data.video.liveNow || !$playerSavePlaybackPositionStore || !playerElement) return; - if (playerElement.currentTime < playerElement.duration - 10 && playerElement.currentTime > 10) { - try { - localStorage.setItem(`v_${data.video.videoId}`, playerElement.currentTime.toString()); - } catch { - // Continue regardless of error - } - } else { - try { - localStorage.removeItem(`v_${data.video.videoId}`); - } catch { - // Continue regardless of error - } - } - - if (isOwnBackend()?.internalAuth && get(rawMasterKeyStore) && playerElement.currentTime > 10) { - updateWatchHistory(data.video.videoId, playerElement.currentTime); - } + updateWatchHistory(data.video.videoId, playerElement.currentTime); } onDestroy(async () => { diff --git a/materialious/src/lib/components/thumbnail/VideoThumbnail.svelte b/materialious/src/lib/components/thumbnail/VideoThumbnail.svelte index 348ec50d..0eb24234 100644 --- a/materialious/src/lib/components/thumbnail/VideoThumbnail.svelte +++ b/materialious/src/lib/components/thumbnail/VideoThumbnail.svelte @@ -22,7 +22,7 @@ rawMasterKeyStore } from '$lib/store'; import { relativeTimestamp } from '$lib/time'; - import { queueGetWatchHistory } from '$lib/api/backend/historyPool'; + import { queueGetWatchHistory } from '$lib/api/historyPool'; import { page } from '$app/state'; import { isOwnBackend } from '$lib/shared'; import { getDeArrow, getThumbnailDeArrow } from '$lib/api/dearrow'; diff --git a/materialious/src/lib/watch.ts b/materialious/src/lib/watch.ts index 25540a81..532b31e7 100644 --- a/materialious/src/lib/watch.ts +++ b/materialious/src/lib/watch.ts @@ -1,4 +1,4 @@ -import { getComments, getPersonalPlaylists, getVideo } from '$lib/api/index'; +import { getComments, getPersonalPlaylists, getVideo, saveWatchHistory } from '$lib/api/index'; import { loadEntirePlaylist } from '$lib/playlist'; import { invidiousAuthStore, @@ -13,7 +13,6 @@ import { error } from '@sveltejs/kit'; import { get } from 'svelte/store'; import { _ } from './i18n'; import { isOwnBackend } from './shared'; -import { saveWatchHistory } from './api/backend/history'; import { getDislikesRYD } from './api/ytd'; export async function getWatchDetails(videoId: string, url: URL) { @@ -42,9 +41,7 @@ export async function getWatchDetails(videoId: string, url: URL) { personalPlaylists = null; } - if (isOwnBackend()?.internalAuth && get(rawMasterKeyStore)) { - saveWatchHistory(video); - } + saveWatchHistory(video); let comments; try { diff --git a/materialious/src/routes/(app)/history/+page.svelte b/materialious/src/routes/(app)/history/+page.svelte index 1ca7a160..fb5ec7cc 100644 --- a/materialious/src/routes/(app)/history/+page.svelte +++ b/materialious/src/routes/(app)/history/+page.svelte @@ -1,8 +1,8 @@