]> cat aescling's git repositories - mastodon.git/commitdiff
Adding common followers API, fixing fallback query again
authorEugen Rochko <eugen@zeonfederated.com>
Fri, 28 Oct 2016 23:29:19 +0000 (01:29 +0200)
committerEugen Rochko <eugen@zeonfederated.com>
Fri, 28 Oct 2016 23:29:19 +0000 (01:29 +0200)
app/controllers/api/v1/accounts_controller.rb
app/models/account.rb
app/models/follow_suggestion.rb
app/views/api/v1/accounts/followers.rabl [deleted file]
app/views/api/v1/accounts/following.rabl [deleted file]
app/views/api/v1/accounts/index.rabl [new file with mode: 0644]
app/views/api/v1/accounts/statuses.rabl
app/views/api/v1/accounts/suggestions.rabl [deleted file]
config/routes.rb

index bb3e54a89ba80e80283785d0ebe02cb82ad500f0..a74d6f979e82d4b8f682d9c385293f88006328fc 100644 (file)
@@ -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
index 8eba4da79f43171db626d375ccbd9edad54b6578..2e1f7a448898e57fe543f65d4f31773ef5bec9fc 100644 (file)
@@ -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
index 15f3b615648a48cb2494f572150ab6f4790ea4cd..ee76d4b6add7d1d5e7286c41f35ea0c327ec1b3f 100644 (file)
@@ -36,11 +36,7 @@ END
       neo = Neography::Rest.new
 
       query = <<END
-OPTIONAL MATCH (a {account_id: {id}})
-WITH a
 MATCH (b)
-WHERE b <> 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 (file)
index c54b048..0000000
+++ /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 (file)
index 87b454f..0000000
+++ /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 (file)
index 0000000..9f3b13a
--- /dev/null
@@ -0,0 +1,2 @@
+collection @accounts
+extends 'api/v1/accounts/show'
index 0a0ed13c5b248be54723c4417c38f8e7d2f17df1..44d29d91ba2c76e41eaf929338afb17d5041f78f 100644 (file)
@@ -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 (file)
index f4dc121..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-collection @accounts
-extends('api/v1/accounts/show')
index b5abd56af7cda7b959ba2945e684343c4b56917c..7ffb40339fe1fb09929be3e867c032dace82c0ed 100644 (file)
@@ -82,6 +82,7 @@ Rails.application.routes.draw do
           get :statuses
           get :followers
           get :following
+          get :common_followers
 
           post :follow
           post :unfollow