2016-11-16 00:56:29 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-15 06:10:07 +09:00
|
|
|
class FollowSuggestion
|
2016-10-23 18:56:04 +09:00
|
|
|
class << self
|
|
|
|
def get(for_account_id, limit = 10)
|
|
|
|
neo = Neography::Rest.new
|
2016-10-15 23:43:52 +09:00
|
|
|
|
2016-10-23 18:56:04 +09:00
|
|
|
query = <<END
|
2016-10-29 03:35:30 +09:00
|
|
|
MATCH (a {account_id: {id}})-[:follows]->(b)-[:follows]->(c)
|
2016-10-15 23:43:52 +09:00
|
|
|
WHERE a <> c
|
|
|
|
AND NOT (a)-[:follows]->(c)
|
2016-10-16 00:19:18 +09:00
|
|
|
RETURN DISTINCT c.account_id, count(b), c.nodeRank
|
|
|
|
ORDER BY count(b) DESC, c.nodeRank DESC
|
2016-10-15 23:43:52 +09:00
|
|
|
LIMIT {limit}
|
|
|
|
END
|
|
|
|
|
2016-10-23 18:56:04 +09:00
|
|
|
results = neo.execute_query(query, id: for_account_id, limit: limit)
|
2016-10-15 23:43:52 +09:00
|
|
|
|
2016-10-23 18:56:04 +09:00
|
|
|
if results.empty? || results['data'].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
|
2016-10-15 23:43:52 +09:00
|
|
|
|
2016-10-23 18:56:04 +09: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).with_counters.map { |a| [a.id, a] }.to_h
|
2016-10-17 00:04:13 +09:00
|
|
|
|
2016-10-23 18:56:04 +09:00
|
|
|
account_ids.map { |id| accounts_map[id] }.compact
|
|
|
|
rescue Neography::NeographyError, Excon::Error::Socket => e
|
|
|
|
Rails.logger.error e
|
|
|
|
return []
|
|
|
|
end
|
2016-10-15 23:43:52 +09:00
|
|
|
|
2016-10-23 18:56:04 +09:00
|
|
|
private
|
2016-10-15 23:43:52 +09:00
|
|
|
|
2016-10-23 18:56:04 +09:00
|
|
|
def fallback(for_account_id, limit)
|
|
|
|
neo = Neography::Rest.new
|
2016-10-15 23:43:52 +09:00
|
|
|
|
2016-10-23 18:56:04 +09:00
|
|
|
query = <<END
|
2016-10-17 00:04:13 +09:00
|
|
|
MATCH (b)
|
|
|
|
RETURN b.account_id
|
|
|
|
ORDER BY b.nodeRank DESC
|
|
|
|
LIMIT {limit}
|
|
|
|
END
|
2016-10-15 23:43:52 +09:00
|
|
|
|
2016-10-23 18:56:04 +09:00
|
|
|
neo.execute_query(query, id: for_account_id, limit: limit)
|
|
|
|
end
|
2016-10-15 06:10:07 +09:00
|
|
|
end
|
|
|
|
end
|