From 312b6ce987694ee7896868fa875de543616dbd28 Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Wed, 30 Oct 2024 16:44:51 +0000 Subject: [PATCH] fix(plex): Handle plex local user * Identify when session is for the local user (no user is logged in) * Add Local User as default allowed when no usersAllow settings * Allow user config to allow/block local user * Update plex docs Fixes #218 --- docsite/docs/configuration/configuration.mdx | 56 +++++++++++++++++--- src/backend/sources/PlexApiSource.ts | 10 +++- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/docsite/docs/configuration/configuration.mdx b/docsite/docs/configuration/configuration.mdx index 4d58374..9f07594 100644 --- a/docsite/docs/configuration/configuration.mdx +++ b/docsite/docs/configuration/configuration.mdx @@ -244,18 +244,58 @@ If your Spotify player has [Automix](https://community.spotify.com/t5/FAQs/What- -:::tip[Important Defaults] +Find your [**Plex Token**](https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/) and make note of the **URL** and **Port** used to connect to your Plex instance. -By default... +
-* multi-scrobbler will **only** scrobble for the user authenticated with the Plex Token. - * Allowed Users (`usersAllow` or `PLEX_USERS_ALLOW`) are only necessary if you want to scrobble for additional users. -* multi-scrobbler will only scrobble media found in Plex libraries that are labelled as **Music.** - * `librariesAllow` or `PLEX_LIBRARIES_ALLOW` will override this +Allowed Users and Defaults -::: +**Multi-scrobbler will automatically scrobble for these users by default:** -Find your [**Plex Token**](https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/) and make note of the **URL** and **Port** used to connect to your Plex instance. +* The User authenticated with the Plex Token +* and the **Local User** + +The Local User (`PLEX_LOCAL_USER`) is how Plex identifies anyone directly accessing the Plex UI from a local IP (who does not need to login). + +To allow MS to scrobble for other users use `usersAllow` or `PLEX_USERS_ALLOW` (env) from the below configuration docs. However, because you are overriding the default settings you must also explicitly list the authenticated user and the Local User if you want them to also be able to scrobble. + +
+ +Examples + +###### Defaults + +If `usersallow` and `PLEX_USERS_ALLOW` are not defined then the Plex Token authenticated User and Local User will be scrobbled for. + + +###### Only A Specific User + +* `"usersallow": ["SomeUser"]` or +* `PLEX_USERS_ALLOW: SomeUser` + +Only the Plex user `SomeUser` will be scrobbled for. The Plex Token authenticated user and the Local User will not be scrobbled for. + +###### A Specific User + Defaults + +(Assuming the plex authenticated user is `FoxxMD`) + +* `"usersallow": ["FoxxMD", "PLEX_LOCAL_USER", "SomeUser"]` or +* `PLEX_USERS_ALLOW: FoxxMD,PLEX_LOCAL_USER,SomeUser` + +The Plex user SomeUser, the Plex Token authenticated user (FoxxMD) and the Local User will be scrobbled for. + +
+ +
+ +
+ +Allowed Libraries and Defaults + +By default multi-scrobbler will only scrobble media found in Plex libraries that are labelled as **Music.** + * `librariesAllow` or `PLEX_LIBRARIES_ALLOW` will override this + +
#### Configuration diff --git a/src/backend/sources/PlexApiSource.ts b/src/backend/sources/PlexApiSource.ts index 5c0fcab..79d904a 100644 --- a/src/backend/sources/PlexApiSource.ts +++ b/src/backend/sources/PlexApiSource.ts @@ -6,6 +6,7 @@ import { asPlayerStateDataMaybePlay, FormatPlayObjectOptions, InternalConfig, + NO_USER, PlayerStateData, PlayerStateDataMaybePlay, PlayPlatformId, REPORTED_PLAYER_STATUSES @@ -31,6 +32,8 @@ import { FixedSizeList } from 'fixed-size-list'; const shortDeviceId = truncateStringToLength(10, ''); +export const LOCAL_USER = 'PLEX_LOCAL_USER'; + const THUMB_REGEX = new RegExp(/\/library\/metadata\/(?\d+)\/thumb\/\d+/) export default class PlexApiSource extends MemoryPositionalSource { @@ -162,6 +165,7 @@ export default class PlexApiSource extends MemoryPositionalSource { if(this.usersAllow.length === 0) { this.usersAllow.push(this.plexUser.toLocaleLowerCase()); + this.usersAllow.push(LOCAL_USER.toLocaleLowerCase()); } this.logger.info(`Authenticated on behalf of user ${this.plexUser} on Server ${server.object.mediaContainer.friendlyName} (version ${server.object.mediaContainer.version})`); @@ -299,7 +303,7 @@ export default class PlexApiSource extends MemoryPositionalSource { machineIdentifier } = {}, user: { - title: userTitle + title: userTitle, } = {} // plex returns the track artist as originalTitle (when there is an album artist) // otherwise this is undefined @@ -315,7 +319,9 @@ export default class PlexApiSource extends MemoryPositionalSource { duration: duration / 1000 }, meta: { - user: userTitle, + // If a user does not have to login to Plex (local IP and no Home Management(?)) then the User node is never populated + // in this case we will use a special constant to signal this is the local user + user: userTitle ?? LOCAL_USER, trackId: guid, // server: ServerId, mediaType: type,