Migrate new reaction job 5305 (#5401)

* Add New Reaction Worker

* Add Specs for the New Reaction Worker

* Update Worker Calls in Notification Model

* Update Notification Model Specs

* Fix Code Climate Issues

* Fix notification specs to remove instance variable and complex logic

* Fix Worker Spec

* Fix Review Comments

* Undo Job Specs

Co-authored-by: Mukesh Chaudhary <45253983+mukeshchdy@users.noreply.github.com>
This commit is contained in:
Mukesh Chaudhary 2020-01-09 20:21:56 +05:30 committed by Ben Halpern
parent 29a734b698
commit 48edaab5bc
4 changed files with 77 additions and 25 deletions

View file

@ -69,14 +69,14 @@ class Notification < ApplicationRecord
return if reaction.skip_notification_for?(receiver)
return if UserBlock.blocking?(receiver, reaction.user_id)
Notifications::NewReactionJob.perform_later(*reaction_notification_attributes(reaction, receiver))
Notifications::NewReactionWorker.perform_async(*reaction_notification_attributes(reaction, receiver))
end
def send_reaction_notification_without_delay(reaction, receiver)
return if reaction.skip_notification_for?(receiver)
return if UserBlock.blocking?(receiver, reaction.user_id)
Notifications::NewReactionJob.perform_now(*reaction_notification_attributes(reaction, receiver))
Notifications::NewReactionWorker.new.perform(*reaction_notification_attributes(reaction, receiver))
end
def send_mention_notification(mention)

View file

@ -0,0 +1,19 @@
module Notifications
class NewReactionWorker
include Sidekiq::Worker
sidekiq_options queue: :medium_priority, retry: 10
def perform(reaction_data, receiver_data)
# Sidekiq Parameters are hash with stringified keys, so we need to symbolize keys
receiver_data = receiver_data.symbolize_keys
reaction_data = reaction_data.symbolize_keys
receiver_klass = receiver_data.fetch(:klass)
return unless %w[User Organization].include?(receiver_klass)
receiver = receiver_klass.constantize.find_by(id: receiver_data.fetch(:id))
Notifications::Reactions::Send.call(reaction_data, receiver) if receiver
end
end
end

View file

@ -275,21 +275,21 @@ RSpec.describe Notification, type: :model do
it "sends a notification to the author of a comment" do
reaction = create(:reaction, reactable: comment, user: user)
perform_enqueued_jobs do
expect do
expect do
sidekiq_perform_enqueued_jobs do
described_class.send_reaction_notification(reaction, reaction.reactable.user)
end.to change(comment.user.notifications, :count).by(1)
end
end
end.to change(comment.user.notifications, :count).by(1)
end
it "sends a notification to the author of an article" do
reaction = create(:reaction, reactable: article, user: user2)
perform_enqueued_jobs do
expect do
expect do
sidekiq_perform_enqueued_jobs do
described_class.send_reaction_notification(reaction, reaction.reactable.user)
end.to change(article.user.notifications, :count).by(1)
end
end
end.to change(article.user.notifications, :count).by(1)
end
end
@ -328,17 +328,19 @@ RSpec.describe Notification, type: :model do
it "creates and destroys the notification properly" do
reaction = create(:reaction, user: user2, reactable: article, category: "like")
perform_enqueued_jobs do
expect do
expect do
sidekiq_perform_enqueued_jobs do
described_class.send_reaction_notification(reaction, reaction.reactable.user)
end.to change(user.notifications, :count).by(1)
end
end.to change(user.notifications, :count).by(1)
reaction.destroy!
reaction.destroy!
expect do
expect do
sidekiq_perform_enqueued_jobs do
described_class.send_reaction_notification(reaction, reaction.reactable.user)
end.to change(user.notifications, :count).by(-1)
end
end
end.to change(user.notifications, :count).by(-1)
end
end
@ -381,22 +383,22 @@ RSpec.describe Notification, type: :model do
it "creates a notification for a positive reaction" do
reaction = create(:reaction, reactable: article, user: user2, category: "like")
perform_enqueued_jobs do
expect do
expect do
sidekiq_perform_enqueued_jobs do
described_class.send_reaction_notification(reaction, reaction.reactable.user)
end.to change(article.notifications, :count).by(1)
end
end
end.to change(article.notifications, :count).by(1)
end
it "does not create a notification for a negative reaction" do
user2.add_role(:trusted)
reaction = create(:reaction, reactable: article, user: user2, category: "vomit")
perform_enqueued_jobs do
expect do
expect do
sidekiq_perform_enqueued_jobs do
described_class.send_reaction_notification(reaction, reaction.reactable.user)
end.to change(article.notifications, :count).by(0)
end
end
end.to change(article.notifications, :count).by(0)
end
end
end

View file

@ -0,0 +1,31 @@
require "rails_helper"
RSpec.describe Notifications::NewReactionWorker, type: :worker do
let(:reaction_data) { { reactable_type: "Comment", reactable_id: 1, reactable_user_id: 2 } }
let(:org) { create(:organization) }
let(:receiver_data) { { klass: "Organization", id: org.id } }
let(:worker) { subject }
include_examples "#enqueues_on_correct_queue", "medium_priority", [{}, {}]
describe "#perform" do
let(:reaction_service) { Notifications::Reactions::Send }
before { allow(reaction_service).to receive(:call) }
it "calls the service" do
subject.perform(reaction_data, receiver_data)
allow(reaction_service).to receive(:call).with(reaction_data, org).once
end
it "doesn't call if is a receiver is of a wrong class" do
subject.perform(reaction_data, { klass: "Tag", id: 10 })
expect(reaction_service).not_to have_received(:call)
end
it "doesn't call if is a receiver doesn't exist" do
subject.perform(reaction_data, { klass: "Organization", id: nil })
expect(reaction_service).not_to have_received(:call)
end
end
end