setting_source.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # frozen_string_literal: true
  2. class AccountSuggestions::SettingSource < AccountSuggestions::Source
  3. def key
  4. :staff
  5. end
  6. def get(account, skip_account_ids: [], limit: 40)
  7. return [] unless setting_enabled?
  8. as_ordered_suggestions(
  9. scope(account).where(setting_to_where_condition).where.not(id: skip_account_ids),
  10. usernames_and_domains
  11. ).take(limit)
  12. end
  13. def remove(_account, _target_account_id)
  14. nil
  15. end
  16. private
  17. def scope(account)
  18. Account.searchable
  19. .followable_by(account)
  20. .not_excluded_by_account(account)
  21. .not_domain_blocked_by_account(account)
  22. .where(locked: false)
  23. .where.not(id: account.id)
  24. end
  25. def usernames_and_domains
  26. @usernames_and_domains ||= setting_to_usernames_and_domains
  27. end
  28. def setting_enabled?
  29. setting.present?
  30. end
  31. def setting_to_where_condition
  32. usernames_and_domains.map do |(username, domain)|
  33. Arel::Nodes::Grouping.new(
  34. Account.arel_table[:username].lower.eq(username.downcase).and(
  35. Account.arel_table[:domain].lower.eq(domain&.downcase)
  36. )
  37. )
  38. end.reduce(:or)
  39. end
  40. def setting_to_usernames_and_domains
  41. setting.split(',').filter_map do |str|
  42. username, domain = str.strip.gsub(/\A@/, '').split('@', 2)
  43. domain = nil if TagManager.instance.local_domain?(domain)
  44. next if username.blank?
  45. [username.downcase, domain&.downcase]
  46. end
  47. end
  48. def setting
  49. Setting.bootstrap_timeline_accounts
  50. end
  51. def to_ordered_list_key(account)
  52. [account.username.downcase, account.domain&.downcase]
  53. end
  54. end