about summary refs log tree commit diff
path: root/app/models/follow_suggestion.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-10-16 17:04:13 +0200
committerEugen Rochko <eugen@zeonfederated.com>2016-10-16 17:04:13 +0200
commit447033038549495318f7c218941829e845aabb61 (patch)
treeae3b3ca628946a5cc64d92a933dac6e66976bb5a /app/models/follow_suggestion.rb
parentf9c9fef1575af3b97cbb300acae60272affc7730 (diff)
Backfill follow suggestions with fallback when not enough results. Cycling
through suggestions in UI
Diffstat (limited to 'app/models/follow_suggestion.rb')
-rw-r--r--app/models/follow_suggestion.rb39
1 files changed, 21 insertions, 18 deletions
diff --git a/app/models/follow_suggestion.rb b/app/models/follow_suggestion.rb
index 719270318..25d28f5ac 100644
--- a/app/models/follow_suggestion.rb
+++ b/app/models/follow_suggestion.rb
@@ -14,9 +14,17 @@ END
 
     results = neo.execute_query(query, id: for_account_id, limit: limit)
 
-    return fallback(for_account_id, limit) if results.empty? || results['data'].empty?
+    if results.empty?
+      results = fallback(for_account_id, limit)
+    elsif results['data'].size < limit
+      results['data'] = (results['data'] + fallback(for_account_id, limit - results['data'].size)['data']).uniq
+    end
 
-    map_to_accounts(for_account_id, results)
+    account_ids  = results['data'].map(&:first)
+    blocked_ids  = Block.where(account_id: for_account_id).pluck(:target_account_id)
+    accounts_map = Account.where(id: account_ids - blocked_ids).map { |a| [a.id, a] }.to_h
+
+    account_ids.map { |id| accounts_map[id] }.compact
   rescue Neography::NeographyError, Excon::Error::Socket => e
     Rails.logger.error e
     return []
@@ -25,23 +33,18 @@ END
   private
 
   def self.fallback(for_account_id, limit)
-    neo     = Neography::Rest.new
-    query   = 'MATCH (a) WHERE a.account_id <> {id} RETURN a.account_id ORDER BY a.nodeRank DESC LIMIT {limit}'
-    results = neo.execute_query(query, id: for_account_id, limit: limit)
-
-    map_to_accounts(for_account_id, results)
-  rescue Neography::NeographyError, Excon::Error::Socket => e
-    Rails.logger.error e
-    return []
-  end
-
-  def self.map_to_accounts(for_account_id, results)
-    return [] if results.empty? || results['data'].empty?
+    neo = Neography::Rest.new
 
-    account_ids  = results['data'].map(&:first)
-    blocked_ids  = Block.where(account_id: for_account_id).pluck(:target_account_id)
-    accounts_map = Account.where(id: account_ids - blocked_ids).map { |a| [a.id, a] }.to_h
+    query = <<END
+START a=node:account_index(Account={id})
+MATCH (b)
+WHERE a <> b
+AND NOT (a)-[:follows]->(b)
+RETURN b.account_id
+ORDER BY b.nodeRank DESC
+LIMIT {limit}
+END
 
-    account_ids.map { |id| accounts_map[id] }.compact
+    neo.execute_query(query, id: for_account_id, limit: limit)
   end
 end