Use generic history funcs

This commit is contained in:
WardPearce
2026-03-04 22:51:49 +13:00
parent 89e2097a84
commit c2be626ca5
8 changed files with 66 additions and 47 deletions
+5 -5
View File
@@ -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<VideoWatchHistory | undefined> {
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;
@@ -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<string, (result: VideoWatchHistory | undefined) => void>();
+49 -9
View File
@@ -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<Video[]> {
// 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<string[]> {}
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<VideoWatchHistory[]> {
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<VideoWatchHistory | undefined> {
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(
@@ -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 () => {
@@ -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';
+2 -5
View File
@@ -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 {
@@ -1,8 +1,8 @@
<script lang="ts">
import { deleteWatchHistory, getWatchHistory } from '$lib/api/backend/history';
import ItemsList from '$lib/components/layout/ItemsList.svelte';
import InfiniteLoading, { type InfiniteEvent } from 'svelte-infinite-loading';
import { _ } from '$lib/i18n';
import { deleteWatchHistory, getWatchHistory } from '$lib/api/index.js';
let { data } = $props();
@@ -1,5 +1,5 @@
import { resolve } from '$app/paths';
import { getWatchHistory } from '$lib/api/backend/history';
import { getWatchHistoryBackend } from '$lib/api/backend/history';
import { isOwnBackend } from '$lib/shared';
import { rawMasterKeyStore } from '$lib/store';
import { redirect } from '@sveltejs/kit';
@@ -9,5 +9,5 @@ export async function load() {
if (!isOwnBackend()?.internalAuth || !get(rawMasterKeyStore))
throw redirect(302, resolve('/', {}));
return { videos: await getWatchHistory() };
return { videos: await getWatchHistoryBackend() };
}