Skip to content

Commit

Permalink
fix(backend): user sync error on plex managed/guest users (#357)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
MrDynamo authored Apr 11, 2024
1 parent 5bd5be2 commit 1467046
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
31 changes: 20 additions & 11 deletions apps/wizarr-backend/wizarr_backend/helpers/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions apps/wizarr-backend/wizarr_backend/helpers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 1467046

Please sign in to comment.