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.

75 lines
1.7 KiB

  1. # frozen_string_literal: true
  2. class FeedInsertWorker
  3. include Sidekiq::Worker
  4. def perform(status_id, id, type = 'home', options = {})
  5. @type = type.to_sym
  6. @status = Status.find(status_id)
  7. @options = options.symbolize_keys
  8. case @type
  9. when :home
  10. @follower = Account.find(id)
  11. when :list
  12. @list = List.find(id)
  13. @follower = @list.account
  14. end
  15. check_and_insert
  16. rescue ActiveRecord::RecordNotFound
  17. true
  18. end
  19. private
  20. def check_and_insert
  21. if feed_filtered?
  22. perform_unpush if update?
  23. else
  24. perform_push
  25. perform_notify if notify?
  26. end
  27. end
  28. def feed_filtered?
  29. case @type
  30. when :home
  31. FeedManager.instance.filter?(:home, @status, @follower)
  32. when :list
  33. FeedManager.instance.filter?(:list, @status, @list)
  34. end
  35. end
  36. def notify?
  37. return false if @type != :home || @status.reblog? || (@status.reply? && @status.in_reply_to_account_id != @status.account_id)
  38. Follow.find_by(account: @follower, target_account: @status.account)&.notify?
  39. end
  40. def perform_push
  41. case @type
  42. when :home
  43. FeedManager.instance.push_to_home(@follower, @status, update: update?)
  44. when :list
  45. FeedManager.instance.push_to_list(@list, @status, update: update?)
  46. end
  47. end
  48. def perform_unpush
  49. case @type
  50. when :home
  51. FeedManager.instance.unpush_from_home(@follower, @status, update: true)
  52. when :list
  53. FeedManager.instance.unpush_from_list(@list, @status, update: true)
  54. end
  55. end
  56. def perform_notify
  57. LocalNotificationWorker.perform_async(@follower.id, @status.id, 'Status', 'status')
  58. end
  59. def update?
  60. @options[:update]
  61. end
  62. end