about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorluigi <007.lva@gmail.com>2021-01-09 18:32:01 -0500
committerGitHub <noreply@github.com>2021-01-10 00:32:01 +0100
commit087ed84367537ac168ed3e00bb7eb4bd582dc3d0 (patch)
tree2374e63ef1aabe4b9991dd30c4eba8adfec89839 /app/models
parent93951431269403201550e914dee508a522becf8b (diff)
Optimize map { ... }.compact calls (#15513)
* Optimize map { ... }.compact

using Enumerable#filter_map, supported since Ruby 2.7

* Add poyfill for Enumerable#filter_map
Diffstat (limited to 'app/models')
-rw-r--r--app/models/account.rb4
-rw-r--r--app/models/concerns/status_threading_concern.rb2
-rw-r--r--app/models/custom_filter.rb2
-rw-r--r--app/models/notification.rb2
-rw-r--r--app/models/status.rb4
5 files changed, 7 insertions, 7 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index 1f723e206..75ebfb2f3 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -508,7 +508,7 @@ class Account < ApplicationRecord
     def from_text(text)
       return [] if text.blank?
 
-      text.scan(MENTION_RE).map { |match| match.first.split('@', 2) }.uniq.map do |(username, domain)|
+      text.scan(MENTION_RE).map { |match| match.first.split('@', 2) }.uniq.filter_map do |(username, domain)|
         domain = begin
           if TagManager.instance.local_domain?(domain)
             nil
@@ -517,7 +517,7 @@ class Account < ApplicationRecord
           end
         end
         EntityCache.instance.mention(username, domain)
-      end.compact
+      end
     end
 
     private
diff --git a/app/models/concerns/status_threading_concern.rb b/app/models/concerns/status_threading_concern.rb
index a0ead1995..5c04108e4 100644
--- a/app/models/concerns/status_threading_concern.rb
+++ b/app/models/concerns/status_threading_concern.rb
@@ -83,7 +83,7 @@ module StatusThreadingConcern
   def find_statuses_from_tree_path(ids, account, promote: false)
     statuses    = Status.with_accounts(ids).to_a
     account_ids = statuses.map(&:account_id).uniq
-    domains     = statuses.map(&:account_domain).compact.uniq
+    domains     = statuses.filter_map(&:account_domain).uniq
     relations   = relations_map_for_account(account, account_ids, domains)
 
     statuses.reject! { |status| StatusFilter.new(status, account, relations).filtered? }
diff --git a/app/models/custom_filter.rb b/app/models/custom_filter.rb
index 8df8a4fbf..9d0f3729b 100644
--- a/app/models/custom_filter.rb
+++ b/app/models/custom_filter.rb
@@ -46,7 +46,7 @@ class CustomFilter < ApplicationRecord
   private
 
   def clean_up_contexts
-    self.context = Array(context).map(&:strip).map(&:presence).compact
+    self.context = Array(context).map(&:strip).filter_map(&:presence)
   end
 
   def remove_cache
diff --git a/app/models/notification.rb b/app/models/notification.rb
index e83123c97..b6db37d6d 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -92,7 +92,7 @@ class Notification < ApplicationRecord
     end
 
     def reload_stale_associations!(cached_items)
-      account_ids = (cached_items.map(&:from_account_id) + cached_items.map { |item| item.target_status&.account_id }.compact).uniq
+      account_ids = (cached_items.map(&:from_account_id) + cached_items.filter_map { |item| item.target_status&.account_id }).uniq
 
       return if account_ids.empty?
 
diff --git a/app/models/status.rb b/app/models/status.rb
index b426f9d5b..558a37f99 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -334,7 +334,7 @@ class Status < ApplicationRecord
     def from_text(text)
       return [] if text.blank?
 
-      text.scan(FetchLinkCardService::URL_PATTERN).map(&:first).uniq.map do |url|
+      text.scan(FetchLinkCardService::URL_PATTERN).map(&:first).uniq.filter_map do |url|
         status = begin
           if TagManager.instance.local_url?(url)
             ActivityPub::TagManager.instance.uri_to_resource(url, Status)
@@ -343,7 +343,7 @@ class Status < ApplicationRecord
           end
         end
         status&.distributable? ? status : nil
-      end.compact
+      end
     end
   end