Remove ShareWorker
This commit is contained in:
parent
d520755bc2
commit
06d7db7ff8
|
@ -1,43 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
class ShareWorker
|
|
||||||
include Sidekiq::Worker
|
|
||||||
|
|
||||||
sidekiq_options queue: :share, retry: 5
|
|
||||||
|
|
||||||
# @param user_id [Integer] the user id
|
|
||||||
# @param answer_id [Integer] the user id
|
|
||||||
# @param service [String] the service to post to
|
|
||||||
def perform(user_id, answer_id, service) # rubocop:disable Metrics/AbcSize
|
|
||||||
@user_service = User.find(user_id).services.find_by!(type: "Services::#{service.camelize}")
|
|
||||||
|
|
||||||
@user_service.post(Answer.find(answer_id))
|
|
||||||
rescue ActiveRecord::RecordNotFound
|
|
||||||
logger.info "Tried to post answer ##{answer_id} for user ##{user_id} to #{service.titleize} but the user/answer/service did not exist (likely deleted), will not retry."
|
|
||||||
# The question to be posted was deleted
|
|
||||||
rescue Twitter::Error::DuplicateStatus
|
|
||||||
logger.info "Tried to post answer ##{answer_id} from user ##{user_id} to Twitter but the status was already posted."
|
|
||||||
rescue Twitter::Error::Forbidden
|
|
||||||
# User's Twitter account is suspended
|
|
||||||
logger.info "Tried to post answer ##{answer_id} from user ##{user_id} to Twitter but the account is suspended."
|
|
||||||
rescue Twitter::Error::Unauthorized
|
|
||||||
# User's Twitter token has expired or been revoked
|
|
||||||
logger.info "Tried to post answer ##{answer_id} from user ##{user_id} to Twitter but the token has expired or been revoked."
|
|
||||||
revoke_and_notify(user_id, service)
|
|
||||||
rescue => e
|
|
||||||
logger.info "failed to post answer #{answer_id} to #{service} for user #{user_id}: #{e.message}"
|
|
||||||
Sentry.capture_exception(e)
|
|
||||||
raise
|
|
||||||
end
|
|
||||||
|
|
||||||
def revoke_and_notify(user_id, service)
|
|
||||||
@user_service.destroy
|
|
||||||
|
|
||||||
Notification::ServiceTokenExpired.create(
|
|
||||||
target_id: user_id,
|
|
||||||
target_type: "User::Expired#{service.camelize}ServiceConnection",
|
|
||||||
recipient_id: user_id,
|
|
||||||
new: true
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -25,13 +25,6 @@ describe Ajax::AnswerController, :ajax_controller, type: :controller do
|
||||||
expect { subject }.to(change { Answer.count }.by(1))
|
expect { subject }.to(change { Answer.count }.by(1))
|
||||||
end
|
end
|
||||||
|
|
||||||
it "enqueues a job for sharing the answer to social networks" do
|
|
||||||
subject
|
|
||||||
shared_services.each do |service|
|
|
||||||
expect(ShareWorker).to have_enqueued_sidekiq_job(user.id, Answer.last.id, service)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
include_examples "returns the expected response"
|
include_examples "returns the expected response"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,11 +33,6 @@ describe Ajax::AnswerController, :ajax_controller, type: :controller do
|
||||||
expect { subject }.not_to(change { Answer.count })
|
expect { subject }.not_to(change { Answer.count })
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does not enqueue a job for sharing the answer to social networks" do
|
|
||||||
subject
|
|
||||||
expect(ShareWorker).not_to have_enqueued_sidekiq_job(anything, anything, anything)
|
|
||||||
end
|
|
||||||
|
|
||||||
include_examples "returns the expected response"
|
include_examples "returns the expected response"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,88 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
require "rails_helper"
|
|
||||||
|
|
||||||
describe ShareWorker do
|
|
||||||
let(:user) { FactoryBot.create(:user) }
|
|
||||||
let(:answer) { FactoryBot.create(:answer, user: user) }
|
|
||||||
let!(:service) do
|
|
||||||
Services::Twitter.create!(type: "Services::Twitter",
|
|
||||||
user: user)
|
|
||||||
end
|
|
||||||
|
|
||||||
before do
|
|
||||||
stub_const("APP_CONFIG", {
|
|
||||||
"hostname" => "example.com",
|
|
||||||
"anonymous_name" => "Anonymous",
|
|
||||||
"https" => true,
|
|
||||||
"items_per_page" => 5,
|
|
||||||
"sharing" => {
|
|
||||||
"twitter" => {
|
|
||||||
"consumer_key" => ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#perform" do
|
|
||||||
before do
|
|
||||||
allow(Sidekiq.logger).to receive(:info)
|
|
||||||
end
|
|
||||||
|
|
||||||
subject do
|
|
||||||
Sidekiq::Testing.fake! do
|
|
||||||
ShareWorker.perform_async(user.id, answer.id, "twitter")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when answer doesn't exist" do
|
|
||||||
it "prevents the job from retrying and logs a message" do
|
|
||||||
answer.destroy!
|
|
||||||
expect { subject }.to change(ShareWorker.jobs, :size).by(1)
|
|
||||||
expect { ShareWorker.drain }.to change(ShareWorker.jobs, :size).by(-1)
|
|
||||||
expect(Sidekiq.logger).to have_received(:info).with("Tried to post answer ##{answer.id} for user ##{user.id} to Twitter but the user/answer/service did not exist (likely deleted), will not retry.")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when answer exists" do
|
|
||||||
it "handles Twitter::Error::DuplicateStatus" do
|
|
||||||
allow_any_instance_of(Services::Twitter).to receive(:post).with(answer).and_raise(Twitter::Error::DuplicateStatus)
|
|
||||||
subject
|
|
||||||
ShareWorker.drain
|
|
||||||
expect(Sidekiq.logger).to have_received(:info).with("Tried to post answer ##{answer.id} from user ##{user.id} to Twitter but the status was already posted.")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "handles Twitter::Error::Unauthorized" do
|
|
||||||
allow_any_instance_of(Services::Twitter).to receive(:post).with(answer).and_raise(Twitter::Error::Unauthorized)
|
|
||||||
subject
|
|
||||||
ShareWorker.drain
|
|
||||||
expect(Sidekiq.logger).to have_received(:info).with("Tried to post answer ##{answer.id} from user ##{user.id} to Twitter but the token has expired or been revoked.")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "revokes the service connection when Twitter::Error::Unauthorized is raised" do
|
|
||||||
allow_any_instance_of(Services::Twitter).to receive(:post).with(answer).and_raise(Twitter::Error::Unauthorized)
|
|
||||||
subject
|
|
||||||
expect { ShareWorker.drain }.to change { Services::Twitter.count }.by(-1)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "sends the user a notification when Twitter::Error::Unauthorized is raised" do
|
|
||||||
allow_any_instance_of(Services::Twitter).to receive(:post).with(answer).and_raise(Twitter::Error::Unauthorized)
|
|
||||||
subject
|
|
||||||
expect { ShareWorker.drain }.to change { Notification::ServiceTokenExpired.count }.by(1)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "handles Twitter::Error::Forbidden" do
|
|
||||||
allow_any_instance_of(Services::Twitter).to receive(:post).with(answer).and_raise(Twitter::Error::Forbidden)
|
|
||||||
subject
|
|
||||||
ShareWorker.drain
|
|
||||||
expect(Sidekiq.logger).to have_received(:info).with("Tried to post answer ##{answer.id} from user ##{user.id} to Twitter but the account is suspended.")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "retries on unhandled exceptions" do
|
|
||||||
expect { subject }.to change(ShareWorker.jobs, :size).by(1)
|
|
||||||
expect { ShareWorker.drain }.to raise_error(Twitter::Error::BadRequest)
|
|
||||||
expect(Sidekiq.logger).to have_received(:info)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
Reference in New Issue