闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: follow_recommendations
  5. #
  6. # account_id :bigint(8) primary key
  7. # rank :decimal(, )
  8. # reason :text is an Array
  9. #
  10. class FollowRecommendation < ApplicationRecord
  11. self.primary_key = :account_id
  12. belongs_to :account_summary, foreign_key: :account_id
  13. belongs_to :account, foreign_key: :account_id
  14. scope :safe, -> { joins(:account_summary).merge(AccountSummary.safe) }
  15. scope :localized, ->(locale) { joins(:account_summary).merge(AccountSummary.localized(locale)) }
  16. scope :filtered, -> { joins(:account_summary).merge(AccountSummary.filtered) }
  17. def readonly?
  18. true
  19. end
  20. def self.get(account, limit, exclude_account_ids = [])
  21. account_ids = Redis.current.zrevrange("follow_recommendations:#{account.user_locale}", 0, -1).map(&:to_i) - exclude_account_ids - [account.id]
  22. return [] if account_ids.empty? || limit < 1
  23. accounts = Account.followable_by(account)
  24. .not_excluded_by_account(account)
  25. .not_domain_blocked_by_account(account)
  26. .where(id: account_ids)
  27. .limit(limit)
  28. .index_by(&:id)
  29. account_ids.map { |id| accounts[id] }.compact
  30. end
  31. end