@@ -19,7 +19,7 @@ import type { TraktWatchlistGetQuery } from '~/models/trakt/trakt-watchlist.mode
19
19
import type { SettingsAuth , UserSetting } from '~/models/trakt-service.model' ;
20
20
import type { TvdbApiResponse } from '~/models/tvdb/tvdb-client.model' ;
21
21
22
- import { type BaseCacheOption , getCachedFunction , type TypedResponse } from '~/services/common/base-client' ;
22
+ import { type BaseCacheOption , type CacheResponse , getCachedFunction , type TypedResponse } from '~/services/common/base-client' ;
23
23
import { LoadingBarService } from '~/services/loading-bar.service' ;
24
24
import { tmdbApi } from '~/services/tmdb-client/api/tmdb-api.endpoints' ;
25
25
import { TmdbClient } from '~/services/tmdb-client/clients/tmdb-client' ;
@@ -36,6 +36,20 @@ import { useUserSettingsStore } from '~/stores/settings/user.store';
36
36
import { createTab } from '~/utils/browser/browser.utils' ;
37
37
import { CacheRetention , ChromeCacheStore } from '~/utils/cache.utils' ;
38
38
39
+ export const shouldEvict = ( date ?: string | number | Date , cache ?: CacheResponse < unknown > ) : boolean => {
40
+ // no date or cache skip
41
+ if ( ! date || ! cache ?. evict ) return false ;
42
+ // date in the past skip
43
+ if ( new Date ( date ) <= new Date ( ) ) return false ;
44
+ // cached today skip
45
+ if ( cache ?. current ?. cachedAt ) {
46
+ const _cachedAt = new Date ( cache . current . cachedAt ) . toLocaleDateString ( ) ;
47
+ const _today = new Date ( ) . toLocaleDateString ( ) ;
48
+ if ( _cachedAt === _today ) return false ;
49
+ }
50
+ return true ;
51
+ } ;
52
+
39
53
export class TraktService {
40
54
private static traktClient : TraktClient ;
41
55
private static tmdbClient : TmdbClient ;
@@ -352,40 +366,50 @@ export class TraktService {
352
366
static show = {
353
367
async summary ( id : string | number ) {
354
368
const response = await TraktService . traktClient . shows . summary . cached ( { id, extended : 'full' } ) ;
355
- return response . json ( ) as Promise < TraktShowExtended > ;
369
+ const data = await response . json ( ) ;
370
+ if ( shouldEvict ( data ?. first_aired , response ?. cache ) ) response . cache ?. evict ?.( ) ;
371
+ return data as TraktShowExtended ;
356
372
} ,
357
373
358
374
async season ( id : string | number , season : number ) {
359
375
const response = await TraktService . traktClient . seasons . season . cached ( { id, season } ) ;
360
- return response . json ( ) as Promise < TraktEpisodeShort [ ] > ;
376
+ const data = await response . json ( ) ;
377
+ if ( data . some ( e => shouldEvict ( e ?. first_aired , response ?. cache ) ) ) response . cache ?. evict ?.( ) ;
378
+ return data as TraktEpisodeShort [ ] ;
361
379
} ,
362
380
363
381
async seasons ( id : string | number ) {
364
382
const response = await TraktService . traktClient . seasons . summary . cached ( { id, extended : 'full' } ) ;
365
- return response . json ( ) as Promise < TraktSeasonExtended [ ] > ;
383
+ const data = await response . json ( ) ;
384
+ if ( data . some ( s => shouldEvict ( s ?. first_aired , response ?. cache ) ) ) response . cache ?. evict ?.( ) ;
385
+ return data as TraktSeasonExtended [ ] ;
366
386
} ,
367
387
368
388
async episode ( { id, season, episode } : { id : string | number ; season : number ; episode : number } ) {
369
389
const response = await TraktService . traktClient . episodes . summary . cached ( { id, season, episode, extended : 'full' } ) ;
370
- return response . json ( ) as Promise < TraktEpisodeExtended > ;
390
+ const data = await response . json ( ) ;
391
+ if ( shouldEvict ( data ?. first_aired , response ?. cache ) ) response . cache ?. evict ?.( ) ;
392
+ return data as TraktEpisodeExtended ;
371
393
} ,
372
394
} ;
373
395
374
- static async activity ( ) {
375
- const response = await this . traktClient . sync . lastActivities ( ) ;
376
- return response . json ( ) ;
377
- }
378
-
379
396
static async movie ( id : string | number ) {
380
397
const response = await this . traktClient . movies . summary . cached ( { id, extended : 'full' } ) ;
381
- return response . json ( ) as Promise < TraktMovieExtended > ;
398
+ const data = await response . json ( ) ;
399
+ if ( shouldEvict ( data ?. released , response ?. cache ) ) response . cache ?. evict ?.( ) ;
400
+ return data as TraktMovieExtended ;
382
401
}
383
402
384
403
static async person ( id : string | number ) {
385
404
const response = await this . traktClient . people . summary . cached ( { id, extended : 'full' } ) ;
386
405
return response . json ( ) as Promise < TraktPersonExtended > ;
387
406
}
388
407
408
+ static async activity ( ) {
409
+ const response = await this . traktClient . sync . lastActivities ( ) ;
410
+ return response . json ( ) ;
411
+ }
412
+
389
413
static evict = {
390
414
tmdb : ( ) => TraktService . tmdbClient . clearCache ( ) ,
391
415
trakt : ( ) => TraktService . traktClient . clearCache ( ) ,
0 commit comments