From 1c5a89ab35a5c6b9d12119694dfc7977ec357784 Mon Sep 17 00:00:00 2001 From: Jorenn92 Date: Sun, 7 Jan 2024 18:39:17 +0100 Subject: [PATCH] feat(rules): Added new rule: Plex - [list] Labels --- .../plex-api/interfaces/library.interfaces.ts | 1 + .../plex-api/interfaces/media.interface.ts | 1 + .../rules/constants/rules.constants.ts | 7 +++ .../rules/getter/plex-getter.service.ts | 47 ++++++++++--------- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/server/src/modules/api/plex-api/interfaces/library.interfaces.ts b/server/src/modules/api/plex-api/interfaces/library.interfaces.ts index 536fbec0..03f0b760 100644 --- a/server/src/modules/api/plex-api/interfaces/library.interfaces.ts +++ b/server/src/modules/api/plex-api/interfaces/library.interfaces.ts @@ -37,6 +37,7 @@ export interface PlexLibraryItem { index?: number; parentIndex?: number; Collection?: { tag: string }[]; + Label?: { tag: string }[]; } export interface PlexLibraryResponse { diff --git a/server/src/modules/api/plex-api/interfaces/media.interface.ts b/server/src/modules/api/plex-api/interfaces/media.interface.ts index 3951b0cf..e07cb95a 100644 --- a/server/src/modules/api/plex-api/interfaces/media.interface.ts +++ b/server/src/modules/api/plex-api/interfaces/media.interface.ts @@ -21,6 +21,7 @@ export interface PlexMetadata { updatedAt: number; media: Media[]; parentData?: PlexMetadata; + Label?: { tag: string }[]; } export interface Media { diff --git a/server/src/modules/rules/constants/rules.constants.ts b/server/src/modules/rules/constants/rules.constants.ts index 63993cbf..8c97809c 100644 --- a/server/src/modules/rules/constants/rules.constants.ts +++ b/server/src/modules/rules/constants/rules.constants.ts @@ -279,6 +279,13 @@ export class RuleConstants { mediaType: MediaType.BOTH, type: RuleType.NUMBER, } as Property, + { + id: 24, + name: 'labels', + humanName: '[list] Labels', + mediaType: MediaType.BOTH, + type: RuleType.TEXT, + } as Property, ], }, { diff --git a/server/src/modules/rules/getter/plex-getter.service.ts b/server/src/modules/rules/getter/plex-getter.service.ts index cf1c440e..87af5122 100644 --- a/server/src/modules/rules/getter/plex-getter.service.ts +++ b/server/src/modules/rules/getter/plex-getter.service.ts @@ -12,6 +12,7 @@ import { } from '../constants/rules.constants'; import { EPlexDataType } from 'src/modules/api/plex-api/enums/plex-data-type-enum'; import { RulesDto } from '../dtos/rules.dto'; +import { PlexMetadata } from 'src/modules/api/plex-api/interfaces/media.interface'; @Injectable() export class PlexGetterService { @@ -33,6 +34,7 @@ export class PlexGetterService { ) { try { const prop = this.plexProperties.find((el) => el.id === id); + const metadata: PlexMetadata = await this.plexApi.getMetadata(libItem.ratingKey) // fetch metadata from cache, this data is more complete switch (prop.name) { case 'addDate': { return libItem.addedAt ? new Date(+libItem.addedAt * 1000) : null; @@ -75,21 +77,22 @@ export class PlexGetterService { const count = await this.plexApi.getWatchHistory(libItem.ratingKey); return count ? count.length : 0; } + case 'labels': { + return metadata.Label ? metadata.Label.map(l => l.tag) : [] + } case 'collections': { // fetch metadata because collections in plexLibrary object are wrong - return libItem.Collection - ? ( - await this.plexApi.getMetadata(libItem.ratingKey) - ).Collection.filter( - (el) => - el.tag.toLowerCase().trim() !== - (ruleGroup.manualCollectionName - ? ruleGroup.manualCollectionName - : ruleGroup.name - ) - .toLowerCase() - .trim(), - ).length + return metadata.Collection + ? metadata.Collection.filter( + (el) => + el.tag.toLowerCase().trim() !== + (ruleGroup.manualCollectionName + ? ruleGroup.manualCollectionName + : ruleGroup.name + ) + .toLowerCase() + .trim(), + ).length : 0; } case 'playlists': { @@ -159,10 +162,8 @@ export class PlexGetterService { } } case 'collection_names': { - // fetch metadata because collections in plexLibrary object are wrong - const moreData = await this.plexApi.getMetadata(libItem.ratingKey); - return moreData.Collection - ? moreData.Collection.map((el) => el.tag.trim()) + return metadata.Collection + ? metadata.Collection.map((el) => el.tag.trim()) : null; } case 'lastViewedAt': { @@ -201,13 +202,13 @@ export class PlexGetterService { const item = libItem.type === 'episode' ? ((await this.plexApi.getMetadata( - libItem.grandparentRatingKey, - )) as unknown as PlexLibraryItem) + libItem.grandparentRatingKey, + )) as unknown as PlexLibraryItem) : libItem.type === 'season' - ? ((await this.plexApi.getMetadata( + ? ((await this.plexApi.getMetadata( libItem.parentRatingKey, )) as unknown as PlexLibraryItem) - : libItem; + : libItem; return item.Genre ? item.Genre.map((el) => el.tag) : null; } case 'sw_allEpisodesSeenBy': { @@ -347,8 +348,8 @@ export class PlexGetterService { const seasons = libItem.type !== 'season' ? ( - await this.plexApi.getChildrenMetadata(libItem.ratingKey) - ).sort((a, b) => a.index - b.index) + await this.plexApi.getChildrenMetadata(libItem.ratingKey) + ).sort((a, b) => a.index - b.index) : [libItem]; const lastEpDate = await this.plexApi