added what im watching

This commit is contained in:
2026-11-03 19:32:08 +00:00
parent 7e8f329109
commit 1c2c577280
19 changed files with 255 additions and 124 deletions

View File

@@ -16,6 +16,28 @@ interface PlaylistItemsResponse {
TotalRecordCount: number;
}
interface JellyfinEpisode {
Id: string;
Name: string;
SeriesName?: string;
SeriesId?: string;
SeasonName?: string;
ParentIndexNumber?: number;
IndexNumber?: number;
Overview?: string;
ProductionYear?: number;
UserData?: {
Played?: boolean;
LastPlayedDate?: string;
PlaybackPositionTicks?: number;
};
}
interface EpisodesResponse {
Items: JellyfinEpisode[];
TotalRecordCount: number;
}
export interface LatestSong {
id: string;
name: string;
@@ -25,15 +47,35 @@ export interface LatestSong {
coverArt: string | null;
}
export interface LatestSeries {
id: string;
name: string;
year: number | null;
poster: string | null;
episode: {
id: string;
name: string;
season: string;
seasonNumber: number | null;
episodeNumber: number | null;
overview: string | null;
dateWatched: string | null;
coverArt: string | null;
} | null;
}
interface CacheEntry<T> {
value: T;
expiresAt: number;
}
const playlistCache = new Map<string, CacheEntry<LatestSong | null>>();
const pendingRequests = new Map<string, Promise<LatestSong | null>>();
const seriesCache = new Map<string, CacheEntry<LatestSeries | null>>();
const pendingSeriesRequests = new Map<string, Promise<LatestSeries | null>>();
async function fetchLatestPlaylistSong(
fetch: typeof globalThis.fetch,
playlistId: string
@@ -119,6 +161,97 @@ export async function getLatestPlaylistSong(
return request;
}
async function fetchLatestWatchedSeries(
fetch: typeof globalThis.fetch,
libraryId: string
): Promise<LatestSeries | null> {
const url = new URL(`${JELLYFIN_URL}/Users/${JELLYFIN_USER_ID}/Items`);
url.searchParams.set('ParentId', libraryId);
url.searchParams.set('Recursive', 'true');
url.searchParams.set('IncludeItemTypes', 'Episode');
url.searchParams.set('Filters', 'IsPlayed');
url.searchParams.set('SortBy', 'DatePlayed');
url.searchParams.set('SortOrder', 'Descending');
url.searchParams.set('Limit', '1');
const headers = {
'X-Emby-Token': JELLYFIN_API_KEY
};
const response = await fetch(url, { headers });
if (!response.ok) {
const body = await response.text();
throw new Error(`Jellyfin error: ${response.status} - ${body}`);
}
const data: EpisodesResponse = await response.json();
const episode = data.Items?.[0];
if (!episode || !episode.SeriesId) return null;
return {
id: episode.SeriesId,
name: episode.SeriesName ?? 'Unknown series',
year: episode.ProductionYear ?? null,
// Series poster
poster: `/posters/${encodeURIComponent(episode.SeriesId)}`,
// Most recently watched episode
episode: {
id: episode.Id,
name: episode.Name,
season: episode.SeasonName ?? 'Unknown season',
seasonNumber: episode.ParentIndexNumber ?? null,
episodeNumber: episode.IndexNumber ?? null,
overview: episode.Overview ?? null,
dateWatched: episode.UserData?.LastPlayedDate ?? null,
// Episode artwork
coverArt: `/artwork/${encodeURIComponent(episode.Id)}`
}
};
}
export async function getLatestWatchedSeries(
fetch: typeof globalThis.fetch,
libraryId: string
): Promise<LatestSeries | null> {
const cached = seriesCache.get(libraryId);
if (cached && cached.expiresAt > Date.now()) {
return cached.value;
}
const pending = pendingSeriesRequests.get(libraryId);
if (pending) return pending;
const request = fetchLatestWatchedSeries(fetch, libraryId)
.then((series) => {
seriesCache.set(libraryId, {
value: series,
expiresAt: Date.now() + CACHE_TTL
});
return series;
})
.finally(() => {
pendingSeriesRequests.delete(libraryId);
});
pendingSeriesRequests.set(libraryId, request);
return request;
}
export function invalidatePlaylistCache(playlistId: string): void {
playlistCache.delete(playlistId);
}
export function invalidateSeriesCache(libraryId: string): void {
seriesCache.delete(libraryId);
}