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