about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-03-16 20:10:51 +0100
committerEugen Rochko <eugen@zeonfederated.com>2017-03-16 20:10:51 +0100
commit017350e0ead4715ef4180eb7b1f6fecb67fbf554 (patch)
tree2d0150fbe64123f3cd2acdf0b939afb1f3d30134 /app/models
parenta2696cf54284107b3ac846d5ac311f5d471022fd (diff)
Add method for retrieving triadic closures
Diffstat (limited to 'app/models')
-rw-r--r--app/models/account.rb19
1 files changed, 19 insertions, 0 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index 078078945..1eb886ee3 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -193,6 +193,25 @@ class Account < ApplicationRecord
       nil
     end
 
+    def triadic_closures(account, limit = 5)
+      sql = <<SQL
+        WITH first_degree AS (
+            SELECT target_account_id
+            FROM follows
+            WHERE account_id = ?
+          )
+        SELECT accounts.*
+        FROM follows
+        INNER JOIN accounts ON follows.target_account_id = accounts.id
+        WHERE account_id IN (SELECT * FROM first_degree) AND target_account_id NOT IN (SELECT * FROM first_degree) AND target_account_id <> ?
+        GROUP BY target_account_id, accounts.id
+        ORDER BY count(account_id) DESC
+        LIMIT ?
+SQL
+
+      Account.find_by_sql([sql, account.id, account.id, limit])
+    end
+
     def following_map(target_account_ids, account_id)
       follow_mapping(Follow.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id)
     end