Skip to content

Commit

Permalink
perf: 🚀 Only update Emby/Jellyfin users when data is different
Browse files Browse the repository at this point in the history
  • Loading branch information
JamsRepos committed May 5, 2024
1 parent 8f18d98 commit 44fe2f1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
4 changes: 1 addition & 3 deletions apps/wizarr-backend/wizarr_backend/app/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ def check_expiring_users():
# Get all users that have an expiration date set and are expired
expiring = get_users_by_expiring()

print(expiring)

# Delete all expired users
for user in expiring:
global_delete_user_from_media_server(user.id)
info(f"Deleting user { user.email if user.email else user.username } due to expired invite.")
info(f"Deleting user { user.email if user.email is not None else user.username } due to expired invite.")


@schedule.task("interval", id="clearRevokedSessions", hours=1, misfire_grace_time=900)
Expand Down
13 changes: 7 additions & 6 deletions apps/wizarr-backend/wizarr_backend/helpers/emby.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ def sync_emby_users(server_api_key: Optional[str] = None, server_url: Optional[s
# Get users from Emby
emby_users = get_emby_users(server_api_key=server_api_key, server_url=server_url)


# Get users from database
database_users = get_users(False)

Expand All @@ -393,13 +392,15 @@ def sync_emby_users(server_api_key: Optional[str] = None, server_url: Optional[s
create_user(username=emby_user["Name"], token=emby_user["Id"], email=email)
info(f"User {emby_user['Name']} successfully imported to database.")

# If the user does exist then update their username and email
# If the user does exist then update their username and email if it has changed
else:
user = get_user_by_token(emby_user["Id"], verify=False)
user.username = emby_user["Name"]
user.email = emby_user["ConnectUserName"] if "ConnectUserName" in emby_user else None
user.save()
info(f"User {emby_user['Name']} successfully updated in database.")
email = emby_user["ConnectUserName"] if "ConnectUserName" in emby_user else None
if (emby_user["Name"] != user.username or (email is not None and email != user.email)):
user.username = emby_user["Name"]
user.email = email
user.save()
info(f"User {emby_user['Name']} successfully updated in database.")

# If database_users.token not in emby_users.id, delete from database
for database_user in database_users:
Expand Down
11 changes: 6 additions & 5 deletions apps/wizarr-backend/wizarr_backend/helpers/jellyfin.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ def sync_jellyfin_users(server_api_key: Optional[str] = None, server_url: Option
# Get users from Jellyfin
jellyfin_users = get_jellyfin_users(server_api_key=server_api_key, server_url=server_url)


# Get users from database
database_users = get_users(False)

Expand All @@ -388,12 +387,14 @@ def sync_jellyfin_users(server_api_key: Optional[str] = None, server_url: Option
create_user(username=jellyfin_user["Name"], token=jellyfin_user["Id"])
info(f"User {jellyfin_user['Name']} successfully imported to database.")

# If the user does exist then update their username
# If database_users.token in jellyfin_users.id, update the users name in database
else:
user = get_user_by_token(jellyfin_user["Id"], verify=False)
user.username = jellyfin_user["Name"]
user.save()
info(f"User {jellyfin_user['Name']} successfully updated in database.")

if (jellyfin_user["Name"] != user.username):
user.username = jellyfin_user["Name"]
user.save()
info(f"User {jellyfin_user['Name']} successfully updated in database.")

# If database_users.token not in jellyfin_users.id, delete from database
for database_user in database_users:
Expand Down
4 changes: 2 additions & 2 deletions apps/wizarr-backend/wizarr_backend/helpers/users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from app.models.database.users import Users
from app.models.users import UsersModel
from playhouse.shortcuts import model_to_dict
from datetime import datetime
from datetime import datetime, timezone

# ANCHOR - Get Users
def get_users(as_dict: bool = True) -> list[Users]:
Expand Down Expand Up @@ -124,7 +124,7 @@ def get_users_by_expiring() -> list[Users]:
"""

# Get all users by expiring
users: list[Users] = Users.select().where(Users.expires <= datetime.utcnow())
users: list[Users] = Users.select().where(Users.expires <= datetime.now(timezone.utc))

return users

Expand Down

0 comments on commit 44fe2f1

Please sign in to comment.