From 087ed84367537ac168ed3e00bb7eb4bd582dc3d0 Mon Sep 17 00:00:00 2001 From: luigi <007.lva@gmail.com> Date: Sat, 9 Jan 2021 18:32:01 -0500 Subject: Optimize map { ... }.compact calls (#15513) * Optimize map { ... }.compact using Enumerable#filter_map, supported since Ruby 2.7 * Add poyfill for Enumerable#filter_map --- app/services/activitypub/fetch_featured_collection_service.rb | 3 +-- app/services/activitypub/process_collection_service.rb | 2 +- app/services/activitypub/process_poll_service.rb | 2 +- app/services/activitypub/synchronize_followers_service.rb | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) (limited to 'app/services/activitypub') diff --git a/app/services/activitypub/fetch_featured_collection_service.rb b/app/services/activitypub/fetch_featured_collection_service.rb index 2c2770466..82c861f5b 100644 --- a/app/services/activitypub/fetch_featured_collection_service.rb +++ b/app/services/activitypub/fetch_featured_collection_service.rb @@ -24,8 +24,7 @@ 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) } - .map { |uri| ActivityPub::FetchRemoteStatusService.new.call(uri) } - .compact + .filter_map { |uri| ActivityPub::FetchRemoteStatusService.new.call(uri) } .select { |status| status.account_id == @account.id } .map(&:id) diff --git a/app/services/activitypub/process_collection_service.rb b/app/services/activitypub/process_collection_service.rb index f1d175dac..170e6709c 100644 --- a/app/services/activitypub/process_collection_service.rb +++ b/app/services/activitypub/process_collection_service.rb @@ -37,7 +37,7 @@ class ActivityPub::ProcessCollectionService < BaseService end def process_items(items) - items.reverse_each.map { |item| process_item(item) }.compact + items.reverse_each.filter_map { |item| process_item(item) } end def supported_context? diff --git a/app/services/activitypub/process_poll_service.rb b/app/services/activitypub/process_poll_service.rb index 903b6a78a..d83e614d8 100644 --- a/app/services/activitypub/process_poll_service.rb +++ b/app/services/activitypub/process_poll_service.rb @@ -30,7 +30,7 @@ class ActivityPub::ProcessPollService < BaseService voters_count = @json['votersCount'] - latest_options = items.map { |item| item['name'].presence || item['content'] }.compact + latest_options = items.filter_map { |item| item['name'].presence || item['content'] } # If for some reasons the options were changed, it invalidates all previous # votes, so we need to remove them diff --git a/app/services/activitypub/synchronize_followers_service.rb b/app/services/activitypub/synchronize_followers_service.rb index d83fcf55e..93cd60253 100644 --- a/app/services/activitypub/synchronize_followers_service.rb +++ b/app/services/activitypub/synchronize_followers_service.rb @@ -14,7 +14,7 @@ class ActivityPub::SynchronizeFollowersService < BaseService # should never happen in practice, since in almost all cases we keep an # Account record, and should we not do that, we should have sent a Delete. # In any case there is not much we can do if that occurs. - @expected_followers = items.map { |uri| ActivityPub::TagManager.instance.uri_to_resource(uri, Account) }.compact + @expected_followers = items.filter_map { |uri| ActivityPub::TagManager.instance.uri_to_resource(uri, Account) } remove_unexpected_local_followers! handle_unexpected_outgoing_follows! -- cgit