From 4974d2cddbc5cc3be3321b13d30e3e78756c8fad Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Sat, 10 Feb 2024 16:29:03 +1300 Subject: [PATCH] remove func.lower which was bad for DB perf --- app/activitypub/routes.py | 2 +- app/activitypub/util.py | 10 +++++----- app/community/util.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py index 0690f215..40746ebc 100644 --- a/app/activitypub/routes.py +++ b/app/activitypub/routes.py @@ -801,7 +801,7 @@ def process_delete_request(request_json, activitypublog_id, ip_address): with current_app.app_context(): activity_log = ActivityPubLog.query.get(activitypublog_id) if 'type' in request_json and request_json['type'] == 'Delete': - actor_to_delete = request_json['object'] + actor_to_delete = request_json['object'].lower() user = User.query.filter_by(ap_profile_id=actor_to_delete).first() if user: # check that the user really has been deleted, to avoid spoofing attacks diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 2b9b77d1..d4aa0d09 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -183,16 +183,16 @@ def instance_allowed(host: str) -> bool: def find_actor_or_create(actor: str) -> Union[User, Community, None]: - actor = actor.strip() + actor = actor.strip().lower() user = None # actor parameter must be formatted as https://server/u/actor or https://server/c/actor # Initially, check if the user exists in the local DB already if current_app.config['SERVER_NAME'] + '/c/' in actor: - return Community.query.filter(func.lower(Community.ap_profile_id) == func.lower(actor)).first() # finds communities formatted like https://localhost/c/* + return Community.query.filter(Community.ap_profile_id == actor).first() # finds communities formatted like https://localhost/c/* if current_app.config['SERVER_NAME'] + '/u/' in actor: - user = User.query.filter(func.lower(User.user_name) == func.lower(actor.split('/')[-1])).filter_by(ap_id=None, banned=False).first() # finds local users + user = User.query.filter(func.lower(User.user_name) == actor.split('/')[-1]).filter_by(ap_id=None, banned=False).first() # finds local users if user is None: return None elif actor.startswith('https://'): @@ -203,11 +203,11 @@ def find_actor_or_create(actor: str) -> Union[User, Community, None]: else: if instance_blocked(server): return None - user = User.query.filter(func.lower(User.ap_profile_id) == func.lower(actor)).first() # finds users formatted like https://kbin.social/u/tables + user = User.query.filter(User.ap_profile_id == actor).first() # finds users formatted like https://kbin.social/u/tables if (user and user.banned) or (user and user.deleted) : return None if user is None: - user = Community.query.filter(func.lower(Community.ap_profile_id) == func.lower(actor)).first() + user = Community.query.filter(Community.ap_profile_id == actor).first() if user is not None: if not user.is_local() and user.ap_fetched_at < utcnow() - timedelta(days=7): diff --git a/app/community/util.py b/app/community/util.py index 90dc0706..b297beda 100644 --- a/app/community/util.py +++ b/app/community/util.py @@ -116,7 +116,7 @@ def retrieve_mods_and_backfill(community_id: int): def community_url_exists(url) -> bool: - community = Community.query.filter(func.lower(Community.ap_profile_id) == func.lower(url)).first() + community = Community.query.filter(Community.ap_profile_id == url.lower()).first() return community is not None