From 146704609d64ba13d1c8a7d80a80fd09e55bfd50 Mon Sep 17 00:00:00 2001 From: MrDynamo Date: Wed, 10 Apr 2024 23:48:53 -0500 Subject: [PATCH] fix(backend): user sync error on plex managed/guest users (#357) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: 🧽 debug statements * chore: 🧽 debug statements * fix: 🩹 lookup user by token instead of username lookup by token instead of username to handle Plex Managed and Guest users accounts * chore: 🧼 cleanup debug statements * fix(backend): 🩹 set db username to plex user title * fix(backend): 🐛 add managed user check for profile pic search * fix: 🐛 add execute function for update query https://stackoverflow.com/questions/31298469/python-peewee-update-query-is-not-working * fix: 🩹 managed user null username handling * chore: 🧽 cleanup debug statement * fix: 🩹 managed user check for profile picture lookup --- .../wizarr_backend/helpers/plex.py | 31 ++++++++++++------- .../wizarr_backend/helpers/users.py | 9 ++++-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/apps/wizarr-backend/wizarr_backend/helpers/plex.py b/apps/wizarr-backend/wizarr_backend/helpers/plex.py index 8034548fb..e4212a77f 100644 --- a/apps/wizarr-backend/wizarr_backend/helpers/plex.py +++ b/apps/wizarr-backend/wizarr_backend/helpers/plex.py @@ -284,9 +284,17 @@ def sync_plex_users(server_api_key: Optional[str] = None, server_url: Optional[s # If plex_users.id is not in database_users.token, add user to database for plex_user in plex_users: if str(plex_user.id) not in [str(database_user.token) for database_user in database_users]: - create_user(username=plex_user.username, token=plex_user.id, email=plex_user.email) - info(f"User {plex_user.username} successfully imported to database") - + create_user(username=plex_user.title, token=plex_user.id, email=plex_user.email) + info(f"User {plex_user.title} successfully imported to database") + + # Handle Plex Managed/Guest users. + # Update DB username to Plex user title. + # This value is the same as username for normal accounts. + # For managed accounts without a public username, + # this value is set to 'Guest' or local username + elif str(plex_user.username) == "" and plex_user.email is None: + create_user(username=plex_user.title, token=plex_user.id, email=plex_user.email) + info(f"Managed User {plex_user.title} successfully updated to database") # If database_users.token is not in plex_users.id, remove user from database for database_user in database_users: @@ -317,14 +325,15 @@ def get_plex_profile_picture(user_id: str, server_api_key: Optional[str] = None, # Get the user user = get_plex_user(user_id=user_id, server_api_key=server_api_key, server_url=server_url) - try: - # Get the profile picture from Plex - url = user.thumb - response = get(url=url, timeout=30) - except RequestException: - # Backup profile picture using ui-avatars.com if Jellyfin fails - username = f"{user.username}&length=1" if user else "ERROR&length=60&font-size=0.28" - response = get(url=f"https://ui-avatars.com/api/?uppercase=true&name={username}", timeout=30) + if str(user.email) != "": + try: + # Get the profile picture from Plex + url = user.thumb + response = get(url=url, timeout=30) + except RequestException: + # Backup profile picture using ui-avatars.com if Jellyfin fails + username = f"{user.username}&length=1" if user else "ERROR&length=60&font-size=0.28" + response = get(url=f"https://ui-avatars.com/api/?uppercase=true&name={username}", timeout=30) # Raise exception if either Jellyfin or ui-avatars.com fails if response.status_code != 200: diff --git a/apps/wizarr-backend/wizarr_backend/helpers/users.py b/apps/wizarr-backend/wizarr_backend/helpers/users.py index 0d4dc25a5..3fe298caf 100644 --- a/apps/wizarr-backend/wizarr_backend/helpers/users.py +++ b/apps/wizarr-backend/wizarr_backend/helpers/users.py @@ -155,9 +155,14 @@ def create_user(**kwargs) -> Users: form = UsersModel(**kwargs) user_model = form.model_dump() + # + # Lookup by token to fix Issue #322 and #352 + # https://github.com/wizarrrr/wizarr/issues/322 + # https://github.com/wizarrrr/wizarr/issues/352 + # # If user already exists raise error (maybe change this to update user) - if get_user_by_username(form.username, verify=False) is not None: - user: Users = Users.update(**user_model).where(Users.username == form.username) + if get_user_by_token(form.token, verify=False) is not None: + user: Users = Users.update(**user_model).where(Users.token == form.token).execute() else: user: Users = Users.create(**user_model)