diff --git a/src/components/home/ContinueWatchingSection.tsx b/src/components/home/ContinueWatchingSection.tsx index 4d19e3c65..05bca2aa4 100644 --- a/src/components/home/ContinueWatchingSection.tsx +++ b/src/components/home/ContinueWatchingSection.tsx @@ -227,21 +227,34 @@ const ContinueWatchingSection = React.forwardRef((props, re try { const shouldFetchMeta = await stremioService.isValidContentId(type, id); - const [metadata, basicContent, addonContent] = await Promise.all([ + const [metadata, basicContent, addonSpecificMeta] = await Promise.all([ shouldFetchMeta ? stremioService.getMetaDetails(type, id) : Promise.resolve(null), catalogService.getBasicContentDetails(type, id), - addonId ? stremioService.getMetaDetails(type, id, addonId).catch(() => null) : Promise.resolve(null) + + addonId + ? stremioService.getMetaDetails(type, id, addonId).catch(() => null) + : Promise.resolve(null) ]); + const preferredAddonMeta = addonSpecificMeta || metadata; + + const finalContent = basicContent ? { ...basicContent, - ...(addonContent?.name && { name: addonContent.name }), - ...(addonContent?.poster && { poster: addonContent.poster }), - ...(addonContent?.description && { description: addonContent.description }), + ...(preferredAddonMeta?.name && { name: preferredAddonMeta.name }), + ...(preferredAddonMeta?.poster && { poster: preferredAddonMeta.poster }), + ...(preferredAddonMeta?.description && { description: preferredAddonMeta.description }), } : null; + if (finalContent) { - const result = { metadata, basicContent: finalContent, addonContent, timestamp: now }; + const result = { + metadata, + basicContent: finalContent, + addonContent: preferredAddonMeta, + timestamp: now + }; + metadataCache.current[cacheKey] = result; return result; } @@ -500,23 +513,46 @@ const ContinueWatchingSection = React.forwardRef((props, re } } - if (currentSeason !== undefined && currentEpisode !== undefined && metadata?.videos) { - const nextEpisodeVideo = findNextEpisode(currentSeason, currentEpisode, metadata.videos); + if (currentSeason !== undefined && currentEpisode !== undefined) { + const traktService = TraktService.getInstance(); + let nextEpisode: any = null; - if (nextEpisodeVideo) { + try { + const isAuthed = await traktService.isAuthenticated(); + if (isAuthed && typeof (traktService as any).getShowWatchedProgress === 'function') { + const showProgress = await (traktService as any).getShowWatchedProgress(group.id); + + if (showProgress && !showProgress.completed && showProgress.next_episode) { + nextEpisode = showProgress.next_episode; + } + } + } catch { + + } + + if (!nextEpisode && metadata?.videos) { + nextEpisode = findNextEpisode( + currentSeason, + currentEpisode, + metadata.videos + ); + } + + if (nextEpisode) { batch.push({ ...basicContent, id: group.id, type: group.type, progress: 0, lastUpdated: progress.lastUpdated, - season: nextEpisodeVideo.season, - episode: nextEpisodeVideo.episode, - episodeTitle: `Episode ${nextEpisodeVideo.episode}`, + season: nextEpisode.season, + episode: nextEpisode.number ?? nextEpisode.episode, + episodeTitle: nextEpisode.title || `Episode ${nextEpisode.number ?? nextEpisode.episode}`, addonId: progress.addonId, } as ContinueWatchingItem); } } + } continue; } @@ -769,24 +805,36 @@ const ContinueWatchingSection = React.forwardRef((props, re if (!cachedData?.basicContent) continue; const { metadata, basicContent } = cachedData; - if (metadata?.videos) { - const nextEpisodeVideo = findNextEpisode(info.season, info.episode, metadata.videos); - if (nextEpisodeVideo) { - logger.log(`➕ [TraktSync] Adding next episode for ${showId}: S${nextEpisodeVideo.season}E${nextEpisodeVideo.episode}`); - traktBatch.push({ - ...basicContent, - id: showId, - type: 'series', - progress: 0, // Next episode, not started - lastUpdated: info.watchedAt, - season: nextEpisodeVideo.season, - episode: nextEpisodeVideo.episode, - episodeTitle: `Episode ${nextEpisodeVideo.episode}`, - addonId: undefined, - } as ContinueWatchingItem); - } + const traktService = TraktService.getInstance(); + let showProgress: any = null; + + try { + showProgress = await (traktService as any).getShowWatchedProgress?.(showId); + } catch { + showProgress = null; } + if (!showProgress || showProgress.completed || !showProgress.next_episode) { + logger.log(`🚫 [TraktSync] Skipping completed show: ${showId}`); + continue; + } + + const nextEp = showProgress.next_episode; + + logger.log(`➕ [TraktSync] Adding next episode for ${showId}: S${nextEp.season}E${nextEp.number}`); + + traktBatch.push({ + ...basicContent, + id: showId, + type: 'series', + progress: 0, + lastUpdated: info.watchedAt, + season: nextEp.season, + episode: nextEp.number, + episodeTitle: nextEp.title || `Episode ${nextEp.number}`, + addonId: undefined, + } as ContinueWatchingItem); + // Persist "watched" progress for the episode that Trakt reported if (!recentlyRemovedRef.current.has(showKey)) { const watchedEpisodeId = `${showId}:${info.season}:${info.episode}`; diff --git a/src/services/traktService.ts b/src/services/traktService.ts index f3874a4a4..eb94f8fd0 100644 --- a/src/services/traktService.ts +++ b/src/services/traktService.ts @@ -1099,6 +1099,55 @@ export class TraktService { return this.apiRequest('/sync/watched/shows'); } + + public async isMovieWatchedAccurate(imdbId: string): Promise { + try { + const history = await this.client.get( + `/sync/history/movies/${imdbId}?limit=1` + ); + + const history = response.data; + return Array.isArray(history) && history.length > 0; + } catch { + return false; + } + } + + public async isEpisodeWatchedAccurate( + showId: string, + season: number, + episode: number + ): Promise { + try { + const history = await this.client.get( + `/sync/history/episodes/${showId}`, + { params: { limit: 20 } } + ); + + const history = response.data; + if (!Array.isArray(history)) return false; + + for (const entry of history) { + if ( + entry.episode?.season === season && + entry.episode?.number === episode + ) { + + if (entry.reset_at) { + const watchedAt = new Date(entry.watched_at).getTime(); + const resetAt = new Date(entry.reset_at).getTime(); + if (watchedAt < resetAt) return false; + } + return true; + } + } + + return false; + } catch { + return false; + } + } + /** * Get the user's watchlist movies */ @@ -2923,4 +2972,4 @@ export class TraktService { } // Export a singleton instance -export const traktService = TraktService.getInstance(); \ No newline at end of file +export const traktService = TraktService.getInstance(); diff --git a/src/services/watchedService.ts b/src/services/watchedService.ts index 34364960d..4bfca17d5 100644 --- a/src/services/watchedService.ts +++ b/src/services/watchedService.ts @@ -301,52 +301,60 @@ class WatchedService { * Check if a movie is marked as watched (locally) */ public async isMovieWatched(imdbId: string): Promise { - try { - // First check local watched flag - const localWatched = await mmkvStorage.getItem(`watched:movie:${imdbId}`); - if (localWatched === 'true') { - return true; - } + try { + const isAuthed = await this.traktService.isAuthenticated(); - // Check local progress - const progress = await storageService.getWatchProgress(imdbId, 'movie'); - if (progress) { - const progressPercent = (progress.currentTime / progress.duration) * 100; - if (progressPercent >= 85) { - return true; - } - } - - return false; - } catch (error) { - logger.error('[WatchedService] Error checking movie watched status:', error); - return false; + if (isAuthed) { + const traktWatched = + await this.traktService.isMovieWatchedAccurate(imdbId); + if (traktWatched) return true; } + + const local = await mmkvStorage.getItem(`watched:movie:${imdbId}`); + return local === 'true'; + } catch { + return false; + } } + /** * Check if an episode is marked as watched (locally) */ - public async isEpisodeWatched(showId: string, season: number, episode: number): Promise { - try { - const episodeId = `${showId}:${season}:${episode}`; + public async isEpisodeWatched( + showId: string, + season: number, + episode: number + ): Promise { + try { + const isAuthed = await this.traktService.isAuthenticated(); - // Check local progress - const progress = await storageService.getWatchProgress(showId, 'series', episodeId); - if (progress) { - const progressPercent = (progress.currentTime / progress.duration) * 100; - if (progressPercent >= 85) { - return true; - } - } - - return false; - } catch (error) { - logger.error('[WatchedService] Error checking episode watched status:', error); - return false; + if (isAuthed) { + const traktWatched = + await this.traktService.isEpisodeWatchedAccurate( + showId, + season, + episode + ); + if (traktWatched) return true; } - } + const episodeId = `${showId}:${season}:${episode}`; + const progress = await storageService.getWatchProgress( + showId, + 'series', + episodeId + ); + + if (!progress) return false; + + const pct = (progress.currentTime / progress.duration) * 100; + return pct >= 99; + } catch { + return false; + } + } + /** * Set local watched status by creating a "completed" progress entry */