feed_insert_worker.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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, :tags
  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 :tags
  33. FeedManager.instance.filter?(:tags, @status, @follower)
  34. when :list
  35. FeedManager.instance.filter?(:list, @status, @list)
  36. end
  37. end
  38. def notify?
  39. return false if @type != :home || @status.reblog? || (@status.reply? && @status.in_reply_to_account_id != @status.account_id)
  40. Follow.find_by(account: @follower, target_account: @status.account)&.notify?
  41. end
  42. def perform_push
  43. case @type
  44. when :home, :tags
  45. FeedManager.instance.push_to_home(@follower, @status, update: update?)
  46. when :list
  47. FeedManager.instance.push_to_list(@list, @status, update: update?)
  48. end
  49. end
  50. def perform_unpush
  51. case @type
  52. when :home, :tags
  53. FeedManager.instance.unpush_from_home(@follower, @status, update: true)
  54. when :list
  55. FeedManager.instance.unpush_from_list(@list, @status, update: true)
  56. end
  57. end
  58. def perform_notify
  59. LocalNotificationWorker.perform_async(@follower.id, @status.id, 'Status', 'status')
  60. end
  61. def update?
  62. @options[:update]
  63. end
  64. end