From e0a197650a30bec9dae26d714168700bc9ce93ed Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 29 Oct 2016 01:29:19 +0200 Subject: [PATCH] Adding common followers API, fixing fallback query again --- app/controllers/api/v1/accounts_controller.rb | 12 ++++++++++-- app/models/account.rb | 9 +++++++++ app/models/follow_suggestion.rb | 4 ---- app/views/api/v1/accounts/followers.rabl | 2 -- app/views/api/v1/accounts/following.rabl | 2 -- app/views/api/v1/accounts/index.rabl | 2 ++ app/views/api/v1/accounts/statuses.rabl | 2 +- app/views/api/v1/accounts/suggestions.rabl | 2 -- config/routes.rb | 1 + 9 files changed, 23 insertions(+), 13 deletions(-) delete mode 100644 app/views/api/v1/accounts/followers.rabl delete mode 100644 app/views/api/v1/accounts/following.rabl create mode 100644 app/views/api/v1/accounts/index.rabl delete mode 100644 app/views/api/v1/accounts/suggestions.rabl diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb index bb3e54a89ba..a74d6f979e8 100644 --- a/app/controllers/api/v1/accounts_controller.rb +++ b/app/controllers/api/v1/accounts_controller.rb @@ -14,15 +14,23 @@ class Api::V1::AccountsController < ApiController end def following - @following = @account.following + @accounts = @account.following + render action: :index end def followers - @followers = @account.followers + @accounts = @account.followers + render action: :index + end + + def common_followers + @accounts = @account.common_followers_with(current_user.account) + render action: :index end def suggestions @accounts = FollowSuggestion.get(current_user.account_id) + render action: :index end def statuses diff --git a/app/models/account.rb b/app/models/account.rb index 8eba4da79f4..2e1f7a44889 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -122,6 +122,15 @@ class Account < ApplicationRecord username end + def common_followers_with(other_account) + results = Neography::Rest.new.execute_query('MATCH (a {account_id: {a_id}})-[:follows]->(b)-[:follows]->(c {account_id: {c_id}}) RETURN b.account_id', a_id: id, c_id: other_account.id) + ids = results['data'].map(&:first) + accounts = self.where(id: ids).with_counters.map { |a| [a.id, a] }.to_h + ids.map { |id| accounts[id] }.compact + rescue Neography::NeographyError, Excon::Error::Socket + [] + end + def self.find_local!(username) find_remote!(username, nil) end diff --git a/app/models/follow_suggestion.rb b/app/models/follow_suggestion.rb index 15f3b615648..ee76d4b6add 100644 --- a/app/models/follow_suggestion.rb +++ b/app/models/follow_suggestion.rb @@ -36,11 +36,7 @@ END neo = Neography::Rest.new query = < a -AND NOT (a)-[:follows]->(b) RETURN b.account_id ORDER BY b.nodeRank DESC LIMIT {limit} diff --git a/app/views/api/v1/accounts/followers.rabl b/app/views/api/v1/accounts/followers.rabl deleted file mode 100644 index c54b0487e08..00000000000 --- a/app/views/api/v1/accounts/followers.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @followers -extends('api/v1/accounts/show') diff --git a/app/views/api/v1/accounts/following.rabl b/app/views/api/v1/accounts/following.rabl deleted file mode 100644 index 87b454ffadd..00000000000 --- a/app/views/api/v1/accounts/following.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @following -extends('api/v1/accounts/show') diff --git a/app/views/api/v1/accounts/index.rabl b/app/views/api/v1/accounts/index.rabl new file mode 100644 index 00000000000..9f3b13a53d9 --- /dev/null +++ b/app/views/api/v1/accounts/index.rabl @@ -0,0 +1,2 @@ +collection @accounts +extends 'api/v1/accounts/show' diff --git a/app/views/api/v1/accounts/statuses.rabl b/app/views/api/v1/accounts/statuses.rabl index 0a0ed13c5b2..44d29d91ba2 100644 --- a/app/views/api/v1/accounts/statuses.rabl +++ b/app/views/api/v1/accounts/statuses.rabl @@ -1,2 +1,2 @@ collection @statuses -extends('api/v1/statuses/show') +extends 'api/v1/statuses/show' diff --git a/app/views/api/v1/accounts/suggestions.rabl b/app/views/api/v1/accounts/suggestions.rabl deleted file mode 100644 index f4dc121ea5b..00000000000 --- a/app/views/api/v1/accounts/suggestions.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @accounts -extends('api/v1/accounts/show') diff --git a/config/routes.rb b/config/routes.rb index b5abd56af7c..7ffb40339fe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -82,6 +82,7 @@ Rails.application.routes.draw do get :statuses get :followers get :following + get :common_followers post :follow post :unfollow