follow_recommendations_v01.sql 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. SELECT
  2. account_id,
  3. sum(rank) AS rank,
  4. array_agg(reason) AS reason
  5. FROM (
  6. SELECT
  7. accounts.id AS account_id,
  8. count(follows.id) / (1.0 + count(follows.id)) AS rank,
  9. 'most_followed' AS reason
  10. FROM follows
  11. INNER JOIN accounts ON accounts.id = follows.target_account_id
  12. INNER JOIN users ON users.account_id = follows.account_id
  13. WHERE users.current_sign_in_at >= (now() - interval '30 days')
  14. AND accounts.suspended_at IS NULL
  15. AND accounts.moved_to_account_id IS NULL
  16. AND accounts.silenced_at IS NULL
  17. AND accounts.locked = 'f'
  18. AND accounts.discoverable = 't'
  19. GROUP BY accounts.id
  20. HAVING count(follows.id) >= 5
  21. UNION ALL
  22. SELECT accounts.id AS account_id,
  23. sum(status_stats.reblogs_count + status_stats.favourites_count) / (1.0 + sum(status_stats.reblogs_count + status_stats.favourites_count)) AS rank,
  24. 'most_interactions' AS reason
  25. FROM status_stats
  26. INNER JOIN statuses ON statuses.id = status_stats.status_id
  27. INNER JOIN accounts ON accounts.id = statuses.account_id
  28. WHERE statuses.id >= ((date_part('epoch', now() - interval '30 days') * 1000)::bigint << 16)
  29. AND accounts.suspended_at IS NULL
  30. AND accounts.moved_to_account_id IS NULL
  31. AND accounts.silenced_at IS NULL
  32. AND accounts.locked = 'f'
  33. AND accounts.discoverable = 't'
  34. GROUP BY accounts.id
  35. HAVING sum(status_stats.reblogs_count + status_stats.favourites_count) >= 5
  36. ) t0
  37. GROUP BY account_id
  38. ORDER BY rank DESC