about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEugen <eugen@zeonfederated.com>2017-01-08 04:14:54 +0100
committerGitHub <noreply@github.com>2017-01-08 04:14:54 +0100
commit69643338f6810d1ae36249e49b910212b1ecccf4 (patch)
tree88019fe0f4dbe6ddbec21f97dc12a42988bd5962
parente8d6f6c8c15d68d735a6b4b15fe013054451953a (diff)
parentabe3ae1cc2c21243b570dbe34f54ea7b568cc629 (diff)
Merge pull request #437 from krainboltgreene/patch-1
Simplifying followers mappings
-rw-r--r--app/models/account.rb12
1 files changed, 8 insertions, 4 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index db110a9e1..3a7763a64 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -175,19 +175,23 @@ class Account < ApplicationRecord
     end
 
     def following_map(target_account_ids, account_id)
-      Follow.where(target_account_id: target_account_ids).where(account_id: account_id).map { |f| [f.target_account_id, true] }.to_h
+      follow_mapping(Follow.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id)
     end
 
     def followed_by_map(target_account_ids, account_id)
-      Follow.where(account_id: target_account_ids).where(target_account_id: account_id).map { |f| [f.account_id, true] }.to_h
+      follow_mapping(Follow.where(account_id: target_account_ids, target_account_id: account_id), :account_id)
     end
 
     def blocking_map(target_account_ids, account_id)
-      Block.where(target_account_id: target_account_ids).where(account_id: account_id).map { |b| [b.target_account_id, true] }.to_h
+      follow_mapping(Block.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id)
     end
 
     def requested_map(target_account_ids, account_id)
-      FollowRequest.where(target_account_id: target_account_ids).where(account_id: account_id).map { |r| [r.target_account_id, true] }.to_h
+      follow_mapping(FollowRequest.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id)
+    end
+    
+    private def follow_mapping(query, field)
+      query.pluck(field).inject({}) { |mapping, id| mapping[id] = true }
     end
   end