2016-10-14 22:10:07 +01:00
|
|
|
class FollowSuggestion
|
2016-10-15 11:58:34 +01:00
|
|
|
def self.get(for_account_id, limit = 6)
|
2016-10-14 22:10:07 +01:00
|
|
|
neo = Neography::Rest.new
|
2016-10-15 15:43:52 +01:00
|
|
|
|
|
|
|
query = <<END
|
|
|
|
START a=node:account_index(Account={id})
|
|
|
|
MATCH (a)-[:follows]->(b)-[:follows]->(c)
|
|
|
|
WHERE a <> c
|
|
|
|
AND NOT (a)-[:follows]->(c)
|
2016-10-15 16:19:18 +01:00
|
|
|
RETURN DISTINCT c.account_id, count(b), c.nodeRank
|
|
|
|
ORDER BY count(b) DESC, c.nodeRank DESC
|
2016-10-15 15:43:52 +01:00
|
|
|
LIMIT {limit}
|
|
|
|
END
|
|
|
|
|
|
|
|
results = neo.execute_query(query, id: for_account_id, limit: limit)
|
|
|
|
|
2016-10-15 16:34:00 +01:00
|
|
|
return fallback(for_account_id, limit) if results.empty? || results['data'].empty?
|
2016-10-15 15:43:52 +01:00
|
|
|
|
|
|
|
map_to_accounts(for_account_id, results)
|
2016-10-15 11:06:30 +01:00
|
|
|
rescue Neography::NeographyError, Excon::Error::Socket => e
|
|
|
|
Rails.logger.error e
|
2016-10-15 15:43:52 +01:00
|
|
|
return []
|
|
|
|
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)
|
2016-10-15 16:34:00 +01:00
|
|
|
return [] if results.empty? || results['data'].empty?
|
2016-10-15 15:43:52 +01:00
|
|
|
|
|
|
|
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
|
2016-10-14 22:10:07 +01:00
|
|
|
end
|
|
|
|
end
|