From 0623baa4b7e2576fd7d4175eefc90bc6df535b89 Mon Sep 17 00:00:00 2001 From: 4gray Date: Mon, 16 Feb 2026 22:16:12 +0100 Subject: [PATCH] fix(playlists): update timestamp of recently viewed items --- .../src/app/xtream-tauri/with-recent-items.ts | 26 ++++++++++++++-- libs/services/src/lib/playlists.service.ts | 30 +++++++++++++++---- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/apps/web/src/app/xtream-tauri/with-recent-items.ts b/apps/web/src/app/xtream-tauri/with-recent-items.ts index 68753ad31..3f3c36f45 100644 --- a/apps/web/src/app/xtream-tauri/with-recent-items.ts +++ b/apps/web/src/app/xtream-tauri/with-recent-items.ts @@ -62,15 +62,37 @@ export const withRecentItems = function () { pipe( switchMap(async ({ contentId, playlist }) => { // contentId is actually xtream_id, need to look up the database content.id + const playlistId = playlist().id; const content = await dbService.getContentByXtreamId( contentId, - playlist().id + playlistId ); if (content) { await dbService.addRecentItem( content.id, - playlist().id + playlistId ); + + // Reload after add/update so re-watched items + // immediately move to the top in recently-viewed. + const items = + await dbService.getRecentItems(playlistId); + patchState(store, { + recentItems: items.map((item) => ({ + id: item.id, + title: item.title, + type: item.type as + | 'live' + | 'movie' + | 'series', + poster_url: item.poster_url, + content_id: item.id, + playlist_id: playlistId, + viewed_at: item.viewed_at || '', + xtream_id: item.xtream_id, + category_id: item.category_id, + })), + }); } }) ) diff --git a/libs/services/src/lib/playlists.service.ts b/libs/services/src/lib/playlists.service.ts index 6343f4579..f8cad31e6 100644 --- a/libs/services/src/lib/playlists.service.ts +++ b/libs/services/src/lib/playlists.service.ts @@ -9,7 +9,7 @@ import { createPlaylistObject, } from 'm3u-utils'; import { NgxIndexedDBService } from 'ngx-indexed-db'; -import { combineLatest, EMPTY, map, switchMap } from 'rxjs'; +import { combineLatest, map, switchMap } from 'rxjs'; import { Channel, DbStores, @@ -397,14 +397,34 @@ export class PlaylistsService { } return this.getPlaylistById(portalId).pipe( switchMap((portal) => { - // Check if item already exists in recently viewed - if (portal.recentlyViewed?.some((i: any) => i.id === item.id)) { - return EMPTY; + const nowIso = new Date().toISOString(); + const recentItems = portal.recentlyViewed ?? []; + const existingIndex = recentItems.findIndex( + (i: any) => String(i?.id) === String(item.id) + ); + + if (existingIndex >= 0) { + const updatedRecentItems = [...recentItems]; + updatedRecentItems[existingIndex] = { + ...updatedRecentItems[existingIndex], + ...item, + added_at: nowIso, + }; + return this.dbService.update(DbStores.Playlists, { + ...portal, + recentlyViewed: updatedRecentItems, + }); } return this.dbService.update(DbStores.Playlists, { ...portal, - recentlyViewed: [...(portal.recentlyViewed ?? []), item], + recentlyViewed: [ + ...recentItems, + { + ...item, + added_at: (item as any).added_at ?? nowIso, + }, + ], }); }) );