about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-10-14 23:10:07 +0200
committerEugen Rochko <eugen@zeonfederated.com>2016-10-14 23:10:07 +0200
commite21a3fe0cd91dfeae76bce4d58c7611e78b60fbf (patch)
tree6a4f6ceda687bbd079845daf2b1c90d15225893b /app/models
parent91144d46ecc1a6e2d39abe8bea2d62c5cb57aca3 (diff)
Adding sync of follow relationships to Neo4J, accounts/suggestions API
Diffstat (limited to 'app/models')
-rw-r--r--app/models/follow.rb24
-rw-r--r--app/models/follow_suggestion.rb7
2 files changed, 31 insertions, 0 deletions
diff --git a/app/models/follow.rb b/app/models/follow.rb
index 4f47580e1..1c52f24c0 100644
--- a/app/models/follow.rb
+++ b/app/models/follow.rb
@@ -22,4 +22,28 @@ class Follow < ApplicationRecord
   def title
     destroyed? ? "#{account.acct} is no longer following #{target_account.acct}" : "#{account.acct} started following #{target_account.acct}"
   end
+
+  after_create  :add_to_graph
+  after_destroy :remove_from_graph
+
+  private
+
+  def add_to_graph
+    neo = Neography::Rest.new
+
+    a = neo.create_unique_node('account_index', 'Account', account_id.to_s, account_id: account_id)
+    b = neo.create_unique_node('account_index', 'Account', target_account_id.to_s, account_id: target_account_id)
+
+    neo.create_unique_relationship('follow_index', 'Follow', id.to_s, 'follows', a, b)
+  rescue Neography::NeographyError => e
+    Rails.logger.error e
+  end
+
+  def remove_from_graph
+    neo = Neography::Rest.new
+    rel = neo.get_relationship_index('follow_index', 'Follow', id.to_s)
+    neo.delete_relationship(rel)
+  rescue Neography::NeographyError => e
+    Rails.logger.error e
+  end
 end
diff --git a/app/models/follow_suggestion.rb b/app/models/follow_suggestion.rb
new file mode 100644
index 000000000..2f291bc49
--- /dev/null
+++ b/app/models/follow_suggestion.rb
@@ -0,0 +1,7 @@
+class FollowSuggestion
+  def self.get(for_account_id)
+    neo = Neography::Rest.new
+    account_ids = neo.execute_query('START a=node:account_index(Account={id}) MATCH (a)-[:follows]->(b)-[:follows]->(c) WHERE a <> c AND NOT (a)-[:follows]->(c) RETURN DISTINCT c.account_id', id: for_account_id)
+    Account.where(id: account_ids['data'].first) unless account_ids.empty?
+  end
+end