add_accounts_to_list_service.rb 653 B

123456789101112131415161718192021222324252627282930313233
  1. # frozen_string_literal: true
  2. class AddAccountsToListService < BaseService
  3. def call(list, accounts)
  4. @list = list
  5. @accounts = accounts
  6. return if @accounts.empty?
  7. update_list!
  8. merge_into_list!
  9. end
  10. private
  11. def update_list!
  12. ApplicationRecord.transaction do
  13. @accounts.each do |account|
  14. @list.accounts << account
  15. end
  16. end
  17. end
  18. def merge_into_list!
  19. MergeWorker.push_bulk(merge_account_ids) do |account_id|
  20. [account_id, @list.id, 'list']
  21. end
  22. end
  23. def merge_account_ids
  24. ListAccount.where(list: @list, account: @accounts).where.not(follow_id: nil).pluck(:account_id)
  25. end
  26. end