about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authoralpaca-tc <alpaca-tc@alpaca.tc>2017-05-16 19:06:38 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-05-16 12:06:38 +0200
commit682b68438e7b9c1755151777bbb6849c00ca98e4 (patch)
tree4139bb574a23ce5c98a81bf3880b14e3f0538faa /app/models
parent09ec6e504be96a8bef72c9d988099191501dde67 (diff)
Improve Account#triadic_closures (#3079)
Diffstat (limited to 'app/models')
-rw-r--r--app/models/account.rb20
1 files changed, 13 insertions, 7 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index bf3d92a51..bd47ce8a9 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -259,24 +259,30 @@ class Account < ApplicationRecord
       nil
     end
 
-    def triadic_closures(account, limit = 5)
+    def triadic_closures(account, limit: 5, offset: 0)
       sql = <<-SQL.squish
         WITH first_degree AS (
-            SELECT target_account_id
-            FROM follows
-            WHERE account_id = :account_id
-          )
+          SELECT target_account_id
+          FROM follows
+          WHERE account_id = :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 <> :account_id
+        WHERE
+          account_id IN (SELECT * FROM first_degree)
+          AND target_account_id NOT IN (SELECT * FROM first_degree)
+          AND target_account_id NOT IN (:excluded_account_ids)
         GROUP BY target_account_id, accounts.id
         ORDER BY count(account_id) DESC
+        OFFSET :offset
         LIMIT :limit
       SQL
 
+      excluded_account_ids = account.excluded_from_timeline_account_ids + [account.id]
+
       find_by_sql(
-        [sql, { account_id: account.id, limit: limit }]
+        [sql, { account_id: account.id, excluded_account_ids: excluded_account_ids, limit: limit, offset: offset }]
       )
     end