feed_insert_worker.rb 1.9 KB

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