@@ -39,6 +39,7 @@ import type {
39
39
} from '@dvcol/trakt-http-client/models' ;
40
40
41
41
import type { TvdbApiResponse } from '@dvcol/tvdb-http-client/models' ;
42
+ import type { ImagePayload } from '~/models/poster.model' ;
42
43
import type { ProgressItem } from '~/models/progress.model' ;
43
44
import type { SettingsAuth , UserSetting } from '~/models/trakt-service.model' ;
44
45
@@ -55,7 +56,7 @@ import { useUserSettingsStore } from '~/stores/settings/user.store';
55
56
import { createTab } from '~/utils/browser/browser.utils' ;
56
57
import { CachePrefix , ChromeCacheStore } from '~/utils/cache.utils' ;
57
58
58
- export const shouldEvict = ( date ?: string | number | Date , cache ?: CacheResponse < unknown > ) : boolean => {
59
+ const shouldEvict = ( cache ?: CacheResponse < unknown > , date ?: string | number | Date ) : boolean => {
59
60
// no cache skip
60
61
if ( ! cache ?. evict ) return false ;
61
62
// cached today skip
@@ -70,6 +71,12 @@ export const shouldEvict = (date?: string | number | Date, cache?: CacheResponse
70
71
return new Date ( date ) > new Date ( ) ;
71
72
} ;
72
73
74
+ const imageResponseEmpty = ( payload : ImagePayload ) => {
75
+ return Object . values ( payload )
76
+ . filter ( Array . isArray )
77
+ . every ( v => ! v ?. length ) ;
78
+ } ;
79
+
73
80
export class TraktService {
74
81
private static traktClient : TraktClient ;
75
82
private static tmdbClient : TmdbClient ;
@@ -240,22 +247,28 @@ export class TraktService {
240
247
const response = await TraktService . tmdbClient . v3 . movies . images . cached ( {
241
248
movie_id,
242
249
} ) ;
243
- return response . json ( ) ;
250
+ const data = await response . json ( ) ;
251
+ if ( imageResponseEmpty ( data ) && shouldEvict ( response . cache ) ) response . cache ?. evict ?.( ) ;
252
+ return data ;
244
253
} ,
245
254
246
255
async show ( series_id : string | number ) {
247
256
const response = await TraktService . tmdbClient . v3 . shows . images . cached ( {
248
257
series_id,
249
258
} ) ;
250
- return response . json ( ) ;
259
+ const data = await response . json ( ) ;
260
+ if ( imageResponseEmpty ( data ) && shouldEvict ( response . cache ) ) response . cache ?. evict ?.( ) ;
261
+ return data ;
251
262
} ,
252
263
253
264
async season ( series_id : string | number , season_number : number ) {
254
265
const response = await TraktService . tmdbClient . v3 . seasons . images . cached ( {
255
266
series_id,
256
267
season_number,
257
268
} ) ;
258
- return response . json ( ) ;
269
+ const data = await response . json ( ) ;
270
+ if ( imageResponseEmpty ( data ) && shouldEvict ( response . cache ) ) response . cache ?. evict ?.( ) ;
271
+ return data ;
259
272
} ,
260
273
261
274
async episode ( series_id : string | number , season_number : number , episode_number : number ) {
@@ -264,14 +277,18 @@ export class TraktService {
264
277
season_number,
265
278
episode_number,
266
279
} ) ;
267
- return response . json ( ) ;
280
+ const data = await response . json ( ) ;
281
+ if ( imageResponseEmpty ( data ) && shouldEvict ( response . cache ) ) response . cache ?. evict ?.( ) ;
282
+ return data ;
268
283
} ,
269
284
270
285
async person ( person_id : string | number ) {
271
286
const response = await TraktService . tmdbClient . v3 . people . images . cached ( {
272
287
person_id,
273
288
} ) ;
274
- return response . json ( ) ;
289
+ const data = await response . json ( ) ;
290
+ if ( imageResponseEmpty ( data ) && shouldEvict ( response . cache ) ) response . cache ?. evict ?.( ) ;
291
+ return data ;
275
292
} ,
276
293
} ;
277
294
@@ -414,36 +431,36 @@ export class TraktService {
414
431
async summary ( id : string | number ) {
415
432
const response = await TraktService . traktClient . shows . summary . cached ( { id, extended : 'full' } ) ;
416
433
const data = await response . json ( ) ;
417
- if ( shouldEvict ( data ?. first_aired , response ?. cache ) ) response . cache ?. evict ?.( ) ;
434
+ if ( shouldEvict ( response ?. cache , data ?. first_aired ) ) response . cache ?. evict ?.( ) ;
418
435
return data as TraktShowExtended ;
419
436
} ,
420
437
421
438
async season ( id : string | number , season : number ) {
422
439
const response = await TraktService . traktClient . seasons . season . cached ( { id, season } ) ;
423
440
const data = await response . json ( ) ;
424
- if ( data . some ( e => shouldEvict ( e ?. first_aired , response ?. cache ) ) ) response . cache ?. evict ?.( ) ;
441
+ if ( data . some ( e => shouldEvict ( response ?. cache , e ?. first_aired ) ) ) response . cache ?. evict ?.( ) ;
425
442
return data as TraktEpisodeShort [ ] ;
426
443
} ,
427
444
428
445
async seasons ( id : string | number ) {
429
446
const response = await TraktService . traktClient . seasons . summary . cached ( { id, extended : 'full' } ) ;
430
447
const data = await response . json ( ) ;
431
- if ( data . some ( s => shouldEvict ( s ?. first_aired , response ?. cache ) ) ) response . cache ?. evict ?.( ) ;
448
+ if ( data . some ( s => shouldEvict ( response ?. cache , s ?. first_aired ) ) ) response . cache ?. evict ?.( ) ;
432
449
return data as TraktSeasonExtended [ ] ;
433
450
} ,
434
451
435
452
async episode ( { id, season, episode } : { id : string | number ; season : number ; episode : number } ) {
436
453
const response = await TraktService . traktClient . episodes . summary . cached ( { id, season, episode, extended : 'full' } ) ;
437
454
const data = await response . json ( ) ;
438
- if ( shouldEvict ( data ?. first_aired , response ?. cache ) ) response . cache ?. evict ?.( ) ;
455
+ if ( shouldEvict ( response ?. cache , data ?. first_aired ) ) response . cache ?. evict ?.( ) ;
439
456
return data as TraktEpisodeExtended ;
440
457
} ,
441
458
} ;
442
459
443
460
static async movie ( id : string | number ) {
444
461
const response = await this . traktClient . movies . summary . cached ( { id, extended : 'full' } ) ;
445
462
const data = await response . json ( ) ;
446
- if ( shouldEvict ( data ?. released , response ?. cache ) ) response . cache ?. evict ?.( ) ;
463
+ if ( shouldEvict ( response ?. cache , data ?. released ) ) response . cache ?. evict ?.( ) ;
447
464
return data as TraktMovieExtended ;
448
465
}
449
466
0 commit comments