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.

52 lines
1.6 KiB

  1. # frozen_string_literal: true
  2. class BootstrapTimelineService < BaseService
  3. def call(source_account)
  4. @source_account = source_account
  5. autofollow_inviter!
  6. autofollow_bootstrap_timeline_accounts! if Setting.enable_bootstrap_timeline_accounts
  7. end
  8. private
  9. def autofollow_inviter!
  10. return unless @source_account&.user&.invite&.autofollow?
  11. FollowService.new.call(@source_account, @source_account.user.invite.user.account)
  12. end
  13. def autofollow_bootstrap_timeline_accounts!
  14. bootstrap_timeline_accounts.each do |target_account|
  15. begin
  16. FollowService.new.call(@source_account, target_account)
  17. rescue ActiveRecord::RecordNotFound, Mastodon::NotPermittedError
  18. nil
  19. end
  20. end
  21. end
  22. def bootstrap_timeline_accounts
  23. return @bootstrap_timeline_accounts if defined?(@bootstrap_timeline_accounts)
  24. @bootstrap_timeline_accounts = bootstrap_timeline_accounts_usernames.empty? ? admin_accounts : local_unlocked_accounts(bootstrap_timeline_accounts_usernames)
  25. end
  26. def bootstrap_timeline_accounts_usernames
  27. @bootstrap_timeline_accounts_usernames ||= (Setting.bootstrap_timeline_accounts || '').split(',').map { |str| str.strip.gsub(/\A@/, '') }.reject(&:blank?)
  28. end
  29. def admin_accounts
  30. User.admins
  31. .includes(:account)
  32. .where(accounts: { locked: false })
  33. .map(&:account)
  34. end
  35. def local_unlocked_accounts(usernames)
  36. Account.local
  37. .without_suspended
  38. .where(username: usernames)
  39. .where(locked: false)
  40. .where(moved_to_account_id: nil)
  41. end
  42. end