From 668a19a2f325c4e9fe2fd97d391ad3d2cd18c42a Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 2 May 2023 15:07:45 -0400 Subject: [PATCH] Fix Performance/DeletePrefix cop (#24796) --- .rubocop_todo.yml | 15 --------------- .../authorize_interactions_controller.rb | 2 +- .../concerns/signature_verification.rb | 2 +- app/controllers/intents_controller.rb | 2 +- app/lib/activitypub/case_transform.rb | 2 +- app/lib/permalink_redirector.rb | 2 +- app/lib/webfinger_resource.rb | 2 +- .../activitypub/fetch_remote_actor_service.rb | 2 +- app/services/resolve_account_service.rb | 2 +- app/services/tag_search_service.rb | 2 +- 10 files changed, 9 insertions(+), 24 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4410802d01..52b15c9bce 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -303,21 +303,6 @@ Performance/CollectionLiteralInLoop: - 'config/deploy.rb' - 'lib/mastodon/media_cli.rb' -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: SafeMultiline. -Performance/DeletePrefix: - Exclude: - - 'app/controllers/authorize_interactions_controller.rb' - - 'app/controllers/concerns/signature_verification.rb' - - 'app/controllers/intents_controller.rb' - - 'app/lib/activitypub/case_transform.rb' - - 'app/lib/permalink_redirector.rb' - - 'app/lib/webfinger_resource.rb' - - 'app/services/activitypub/fetch_remote_actor_service.rb' - - 'app/services/backup_service.rb' - - 'app/services/resolve_account_service.rb' - - 'app/services/tag_search_service.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). Performance/MapCompact: Exclude: diff --git a/app/controllers/authorize_interactions_controller.rb b/app/controllers/authorize_interactions_controller.rb index 02a6b6d06b..bf28d18423 100644 --- a/app/controllers/authorize_interactions_controller.rb +++ b/app/controllers/authorize_interactions_controller.rb @@ -59,7 +59,7 @@ class AuthorizeInteractionsController < ApplicationController end def uri_param - params[:uri] || params.fetch(:acct, '').gsub(/\Aacct:/, '') + params[:uri] || params.fetch(:acct, '').delete_prefix('acct:') end def set_body_classes diff --git a/app/controllers/concerns/signature_verification.rb b/app/controllers/concerns/signature_verification.rb index 9317259433..db3144adbe 100644 --- a/app/controllers/concerns/signature_verification.rb +++ b/app/controllers/concerns/signature_verification.rb @@ -244,7 +244,7 @@ module SignatureVerification end if key_id.start_with?('acct:') - stoplight_wrap_request { ResolveAccountService.new.call(key_id.gsub(/\Aacct:/, ''), suppress_errors: false) } + stoplight_wrap_request { ResolveAccountService.new.call(key_id.delete_prefix('acct:'), suppress_errors: false) } elsif !ActivityPub::TagManager.instance.local_uri?(key_id) account = ActivityPub::TagManager.instance.uri_to_actor(key_id) account ||= stoplight_wrap_request { ActivityPub::FetchRemoteKeyService.new.call(key_id, id: false, suppress_errors: false) } diff --git a/app/controllers/intents_controller.rb b/app/controllers/intents_controller.rb index ca89fc7fe6..ea024e30e6 100644 --- a/app/controllers/intents_controller.rb +++ b/app/controllers/intents_controller.rb @@ -9,7 +9,7 @@ class IntentsController < ApplicationController if uri.scheme == 'web+mastodon' case uri.host when 'follow' - return redirect_to authorize_interaction_path(uri: uri.query_values['uri'].gsub(/\Aacct:/, '')) + return redirect_to authorize_interaction_path(uri: uri.query_values['uri'].delete_prefix('acct:')) when 'share' return redirect_to share_path(text: uri.query_values['text']) end diff --git a/app/lib/activitypub/case_transform.rb b/app/lib/activitypub/case_transform.rb index d36e01b8f2..da2c5eb8b0 100644 --- a/app/lib/activitypub/case_transform.rb +++ b/app/lib/activitypub/case_transform.rb @@ -13,7 +13,7 @@ module ActivityPub::CaseTransform when Symbol then camel_lower(value.to_s).to_sym when String camel_lower_cache[value] ||= if value.start_with?('_:') - "_:#{value.gsub(/\A_:/, '').underscore.camelize(:lower)}" + "_:#{value.delete_prefix('_:').underscore.camelize(:lower)}" else value.underscore.camelize(:lower) end diff --git a/app/lib/permalink_redirector.rb b/app/lib/permalink_redirector.rb index 063a2188b5..0fcec683d9 100644 --- a/app/lib/permalink_redirector.rb +++ b/app/lib/permalink_redirector.rb @@ -52,7 +52,7 @@ class PermalinkRedirector end def path_segments - @path_segments ||= @path.gsub(/\A\//, '').split('/') + @path_segments ||= @path.delete_prefix('/').split('/') end def find_status_url_by_id(id) diff --git a/app/lib/webfinger_resource.rb b/app/lib/webfinger_resource.rb index 4209454859..7e1a7196d7 100644 --- a/app/lib/webfinger_resource.rb +++ b/app/lib/webfinger_resource.rb @@ -57,7 +57,7 @@ class WebfingerResource end def resource_without_acct_string - resource.gsub(/\Aacct:/, '') + resource.delete_prefix('acct:') end def local_username diff --git a/app/services/activitypub/fetch_remote_actor_service.rb b/app/services/activitypub/fetch_remote_actor_service.rb index c295700860..8df8c75876 100644 --- a/app/services/activitypub/fetch_remote_actor_service.rb +++ b/app/services/activitypub/fetch_remote_actor_service.rb @@ -67,7 +67,7 @@ class ActivityPub::FetchRemoteActorService < BaseService end def split_acct(acct) - acct.gsub(/\Aacct:/, '').split('@') + acct.delete_prefix('acct:').split('@') end def supported_context? diff --git a/app/services/resolve_account_service.rb b/app/services/resolve_account_service.rb index 870d67ec8d..6204fefd6f 100644 --- a/app/services/resolve_account_service.rb +++ b/app/services/resolve_account_service.rb @@ -100,7 +100,7 @@ class ResolveAccountService < BaseService end def split_acct(acct) - acct.gsub(/\Aacct:/, '').split('@') + acct.delete_prefix('acct:').split('@') end def fetch_account! diff --git a/app/services/tag_search_service.rb b/app/services/tag_search_service.rb index b66ccced9d..d5d1974275 100644 --- a/app/services/tag_search_service.rb +++ b/app/services/tag_search_service.rb @@ -2,7 +2,7 @@ class TagSearchService < BaseService def call(query, options = {}) - @query = query.strip.gsub(/\A#/, '') + @query = query.strip.delete_prefix('#') @offset = options.delete(:offset).to_i @limit = options.delete(:limit).to_i @options = options