2017-04-05 03:21:37 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class FeedInsertWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
2022-01-28 10:43:56 +11:00
|
|
|
def perform(status_id, id, type = 'home', options = {})
|
2023-07-09 03:45:36 +10:00
|
|
|
ApplicationRecord.connected_to(role: :primary) do
|
|
|
|
@type = type.to_sym
|
|
|
|
@status = Status.find(status_id)
|
|
|
|
@options = options.symbolize_keys
|
2017-11-18 10:16:48 +11:00
|
|
|
|
2023-07-09 03:45:36 +10:00
|
|
|
case @type
|
|
|
|
when :home, :tags
|
|
|
|
@follower = Account.find(id)
|
|
|
|
when :list
|
|
|
|
@list = List.find(id)
|
|
|
|
@follower = @list.account
|
|
|
|
end
|
2017-11-18 10:16:48 +11:00
|
|
|
end
|
2017-05-11 04:32:05 +10:00
|
|
|
|
2023-07-09 03:45:36 +10:00
|
|
|
ApplicationRecord.connected_to(role: :read, prevent_writes: true) do
|
|
|
|
check_and_insert
|
|
|
|
end
|
2017-11-18 10:16:48 +11:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
true
|
2017-05-11 04:32:05 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_and_insert
|
2022-01-20 08:37:27 +11:00
|
|
|
if feed_filtered?
|
|
|
|
perform_unpush if update?
|
|
|
|
else
|
|
|
|
perform_push
|
|
|
|
perform_notify if notify?
|
|
|
|
end
|
2017-05-11 04:32:05 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def feed_filtered?
|
2020-09-08 11:41:16 +10:00
|
|
|
case @type
|
|
|
|
when :home
|
|
|
|
FeedManager.instance.filter?(:home, @status, @follower)
|
2022-07-17 21:49:29 +10:00
|
|
|
when :tags
|
|
|
|
FeedManager.instance.filter?(:tags, @status, @follower)
|
2020-09-08 11:41:16 +10:00
|
|
|
when :list
|
|
|
|
FeedManager.instance.filter?(:list, @status, @list)
|
|
|
|
end
|
2017-05-11 04:32:05 +10:00
|
|
|
end
|
2017-04-05 03:21:37 +10:00
|
|
|
|
2020-09-19 01:26:45 +10:00
|
|
|
def notify?
|
|
|
|
return false if @type != :home || @status.reblog? || (@status.reply? && @status.in_reply_to_account_id != @status.account_id)
|
|
|
|
|
|
|
|
Follow.find_by(account: @follower, target_account: @status.account)&.notify?
|
|
|
|
end
|
|
|
|
|
2017-05-11 04:32:05 +10:00
|
|
|
def perform_push
|
2017-11-18 10:16:48 +11:00
|
|
|
case @type
|
2022-07-17 21:49:29 +10:00
|
|
|
when :home, :tags
|
2022-01-20 08:37:27 +11:00
|
|
|
FeedManager.instance.push_to_home(@follower, @status, update: update?)
|
|
|
|
when :list
|
|
|
|
FeedManager.instance.push_to_list(@list, @status, update: update?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_unpush
|
|
|
|
case @type
|
2022-07-17 21:49:29 +10:00
|
|
|
when :home, :tags
|
2022-01-20 08:37:27 +11:00
|
|
|
FeedManager.instance.unpush_from_home(@follower, @status, update: true)
|
2017-11-18 10:16:48 +11:00
|
|
|
when :list
|
2022-01-20 08:37:27 +11:00
|
|
|
FeedManager.instance.unpush_from_list(@list, @status, update: true)
|
2017-11-18 10:16:48 +11:00
|
|
|
end
|
2017-04-05 03:21:37 +10:00
|
|
|
end
|
2020-09-19 01:26:45 +10:00
|
|
|
|
|
|
|
def perform_notify
|
2022-03-04 11:06:33 +11:00
|
|
|
LocalNotificationWorker.perform_async(@follower.id, @status.id, 'Status', 'status')
|
2020-09-19 01:26:45 +10:00
|
|
|
end
|
2022-01-20 08:37:27 +11:00
|
|
|
|
|
|
|
def update?
|
|
|
|
@options[:update]
|
|
|
|
end
|
2017-04-05 03:21:37 +10:00
|
|
|
end
|