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