From eb51e43fb4386120f77f2ff99581f15018a81bd4 Mon Sep 17 00:00:00 2001 From: luigi <007.lva@gmail.com> Date: Fri, 22 Jan 2021 04:09:08 -0500 Subject: Optimize some regex matching (#15528) * Use Regex#match? * Replace =~ too * Avoid to call match? from Nil * Keep value of Regexp.last_match --- app/services/account_search_service.rb | 2 +- app/services/search_service.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'app/services') diff --git a/app/services/account_search_service.rb b/app/services/account_search_service.rb index 43e596040..6fe4b6593 100644 --- a/app/services/account_search_service.rb +++ b/app/services/account_search_service.rb @@ -175,7 +175,7 @@ class AccountSearchService < BaseService end def username_complete? - query.include?('@') && "@#{query}" =~ /\A#{Account::MENTION_RE}\Z/ + query.include?('@') && "@#{query}".match?(/\A#{Account::MENTION_RE}\Z/) end def likely_acct? diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 19500a8d4..1a76cbb38 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -72,7 +72,7 @@ class SearchService < BaseService end def url_query? - @resolve && @query =~ /\Ahttps?:\/\// + @resolve && /\Ahttps?:\/\//.match?(@query) end def url_resource_results -- cgit From 7ea9588520c664f17a02265d8456b2288476cb9b Mon Sep 17 00:00:00 2001 From: luigi <007.lva@gmail.com> Date: Fri, 22 Jan 2021 10:28:15 -0500 Subject: Use Enumerable#filter_map in more places (#15527) --- app/lib/spam_check.rb | 4 ++-- app/presenters/status_relationships_presenter.rb | 2 +- app/services/activitypub/fetch_featured_collection_service.rb | 7 ++----- 3 files changed, 5 insertions(+), 8 deletions(-) (limited to 'app/services') diff --git a/app/lib/spam_check.rb b/app/lib/spam_check.rb index 68e586d00..dcb2db9ca 100644 --- a/app/lib/spam_check.rb +++ b/app/lib/spam_check.rb @@ -186,9 +186,9 @@ class SpamCheck def matching_status_ids if nilsimsa? - other_digests.select { |record| record.start_with?('nilsimsa') && nilsimsa_compare_value(digest, record.split(':')[1]) >= NILSIMSA_COMPARE_THRESHOLD }.filter_map { |record| record.split(':')[2] } + other_digests.filter_map { |record| record.split(':')[2] if record.start_with?('nilsimsa') && nilsimsa_compare_value(digest, record.split(':')[1]) >= NILSIMSA_COMPARE_THRESHOLD } else - other_digests.select { |record| record.start_with?('md5') && record.split(':')[1] == digest }.filter_map { |record| record.split(':')[2] } + other_digests.filter_map { |record| record.split(':')[2] if record.start_with?('md5') && record.split(':')[1] == digest } end end diff --git a/app/presenters/status_relationships_presenter.rb b/app/presenters/status_relationships_presenter.rb index f4849d245..70fb2ba90 100644 --- a/app/presenters/status_relationships_presenter.rb +++ b/app/presenters/status_relationships_presenter.rb @@ -15,7 +15,7 @@ class StatusRelationshipsPresenter statuses = statuses.compact status_ids = statuses.flat_map { |s| [s.id, s.reblog_of_id] }.uniq.compact conversation_ids = statuses.filter_map(&:conversation_id).uniq - pinnable_status_ids = statuses.map(&:proper).select { |s| s.account_id == current_account_id && %w(public unlisted).include?(s.visibility) }.map(&:id) + pinnable_status_ids = statuses.map(&:proper).filter_map { |s| s.id if s.account_id == current_account_id && %w(public unlisted).include?(s.visibility) } @reblogs_map = Status.reblogs_map(status_ids, current_account_id).merge(options[:reblogs_map] || {}) @favourites_map = Status.favourites_map(status_ids, current_account_id).merge(options[:favourites_map] || {}) diff --git a/app/services/activitypub/fetch_featured_collection_service.rb b/app/services/activitypub/fetch_featured_collection_service.rb index 82c861f5b..72352aca6 100644 --- a/app/services/activitypub/fetch_featured_collection_service.rb +++ b/app/services/activitypub/fetch_featured_collection_service.rb @@ -23,11 +23,8 @@ class ActivityPub::FetchFeaturedCollectionService < BaseService def process_items(items) status_ids = items.map { |item| value_or_id(item) } - .reject { |uri| ActivityPub::TagManager.instance.local_uri?(uri) } - .filter_map { |uri| ActivityPub::FetchRemoteStatusService.new.call(uri) } - .select { |status| status.account_id == @account.id } - .map(&:id) - + .filter_map { |uri| ActivityPub::FetchRemoteStatusService.new.call(uri) unless ActivityPub::TagManager.instance.local_uri?(uri) } + .filter_map { |status| status.id if status.account_id == @account.id } to_remove = [] to_add = status_ids -- cgit From 7f1c56954b46b26b4dadfa92047f1ee5d7f9ad0a Mon Sep 17 00:00:00 2001 From: Takeshi Umeda Date: Mon, 25 Jan 2021 17:22:41 +0900 Subject: Fix first return value of FetchLinkCardService.html method (#15630) --- app/services/fetch_link_card_service.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/services') diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb index 255490d5c..74fe9a0a5 100644 --- a/app/services/fetch_link_card_service.rb +++ b/app/services/fetch_link_card_service.rb @@ -47,11 +47,11 @@ class FetchLinkCardService < BaseService Request.new(:get, @url).add_headers('Accept' => 'text/html', 'User-Agent' => Mastodon::Version.user_agent + ' Bot').perform do |res| if res.code == 200 && res.mime_type == 'text/html' - @html = res.body_with_limit @html_charset = res.charset + @html = res.body_with_limit else - @html = nil @html_charset = nil + @html = nil end end end -- cgit