about summary refs log tree commit diff
path: root/app/workers
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2019-01-11 10:53:58 +0100
committerGitHub <noreply@github.com>2019-01-11 10:53:58 +0100
commit2cfa55185a5fc7d93a160a4e9a4730aae6725b0f (patch)
treec57169b5a3d717f4e68b8ec5d2d6e220d1456434 /app/workers
parentd1da0a1086fa25f22739277fbf32ba1b3745317d (diff)
parent394525e32994e605093c87d3a9fad2a4202f3401 (diff)
Merge pull request #885 from ThibG/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'app/workers')
-rw-r--r--app/workers/publish_scheduled_status_worker.rb24
-rw-r--r--app/workers/scheduler/scheduled_statuses_scheduler.rb19
-rw-r--r--app/workers/unfollow_follow_worker.rb18
3 files changed, 61 insertions, 0 deletions
diff --git a/app/workers/publish_scheduled_status_worker.rb b/app/workers/publish_scheduled_status_worker.rb
new file mode 100644
index 000000000..641fcc61c
--- /dev/null
+++ b/app/workers/publish_scheduled_status_worker.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+class PublishScheduledStatusWorker
+  include Sidekiq::Worker
+
+  def perform(scheduled_status_id)
+    scheduled_status = ScheduledStatus.find(scheduled_status_id)
+    scheduled_status.destroy!
+
+    PostStatusService.new.call(
+      scheduled_status.account,
+      options_with_objects(scheduled_status.params.with_indifferent_access)
+    )
+  rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid
+    true
+  end
+
+  def options_with_objects(options)
+    options.tap do |options_hash|
+      options_hash[:application] = Doorkeeper::Application.find(options_hash.delete(:application_id)) if options[:application_id]
+      options_hash[:thread]      = Status.find(options_hash.delete(:in_reply_to_id)) if options_hash[:in_reply_to_id]
+    end
+  end
+end
diff --git a/app/workers/scheduler/scheduled_statuses_scheduler.rb b/app/workers/scheduler/scheduled_statuses_scheduler.rb
new file mode 100644
index 000000000..1772a246b
--- /dev/null
+++ b/app/workers/scheduler/scheduled_statuses_scheduler.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+class Scheduler::ScheduledStatusesScheduler
+  include Sidekiq::Worker
+
+  sidekiq_options unique: :until_executed, retry: 0
+
+  def perform
+    due_statuses.find_each do |scheduled_status|
+      PublishScheduledStatusWorker.perform_at(scheduled_status.scheduled_at, scheduled_status.id)
+    end
+  end
+
+  private
+
+  def due_statuses
+    ScheduledStatus.where('scheduled_at <= ?', Time.now.utc + PostStatusService::MIN_SCHEDULE_OFFSET)
+  end
+end
diff --git a/app/workers/unfollow_follow_worker.rb b/app/workers/unfollow_follow_worker.rb
new file mode 100644
index 000000000..50d3bf034
--- /dev/null
+++ b/app/workers/unfollow_follow_worker.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class UnfollowFollowWorker
+  include Sidekiq::Worker
+
+  sidekiq_options queue: 'pull'
+
+  def perform(follower_account_id, old_target_account_id, new_target_account_id)
+    follower_account   = Account.find(follower_account_id)
+    old_target_account = Account.find(old_target_account_id)
+    new_target_account = Account.find(new_target_account_id)
+
+    FollowService.new.call(follower_account, new_target_account)
+    UnfollowService.new.call(follower_account, old_target_account)
+  rescue ActiveRecord::RecordNotFound, Mastodon::NotPermittedError
+    true
+  end
+end