diff --git a/app/jobs/notifications/mention_job.rb b/app/jobs/notifications/mention_job.rb new file mode 100644 index 000000000..3d89c10fa --- /dev/null +++ b/app/jobs/notifications/mention_job.rb @@ -0,0 +1,10 @@ +module Notifications + class MentionJob < ApplicationJob + queue_as :send_new_mention_notification + + def perform(mention_id, service = NewMention::Send) + mention = Mention.find_by(id: mention_id) + service.call(mention) if mention + end + end +end diff --git a/app/models/notification.rb b/app/models/notification.rb index 52b3f2b05..c0d8642df 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -73,20 +73,12 @@ class Notification < ApplicationRecord end def send_mention_notification(mention) - mentioner = mention.mentionable.user - json_data = { - user: user_data(mentioner) - } - json_data[:comment] = comment_data(mention.mentionable) if mention.mentionable_type == "Comment" - Notification.create( - user_id: mention.user_id, - notifiable_id: mention.id, - notifiable_type: "Mention", - action: nil, - json_data: json_data, - ) + Notifications::MentionJob.perform_later(mention.id) + end + + def send_mention_notification_without_delay(mention) + Notifications::MentionJob.perform_now(mention.id) end - handle_asynchronously :send_mention_notification def send_welcome_notification(receiver_id) Notifications::WelcomeNotificationJob.perform_later(receiver_id) diff --git a/app/services/notifications/moderation/send.rb b/app/services/notifications/moderation/send.rb index bcbda6304..59ec8b327 100644 --- a/app/services/notifications/moderation/send.rb +++ b/app/services/notifications/moderation/send.rb @@ -19,7 +19,7 @@ module Notifications json_data = { user: user_data(User.dev_account) } json_data[notifiable.class.name.downcase] = public_send "#{notifiable.class.name.downcase}_data", notifiable - new_notification = Notification.create( + new_notification = Notification.create!( user_id: moderator.id, notifiable_id: notifiable.id, notifiable_type: notifiable.class.name, diff --git a/app/services/notifications/new_mention/send.rb b/app/services/notifications/new_mention/send.rb new file mode 100644 index 000000000..3b80fb8d9 --- /dev/null +++ b/app/services/notifications/new_mention/send.rb @@ -0,0 +1,36 @@ +module Notifications + module NewMention + class Send + delegate :user_data, to: Notifications + delegate :comment_data, to: Notifications + + def initialize(mention) + @mention = mention + end + + def self.call(*args) + new(*args).call + end + + def call + Notification.create( + user_id: mention.user_id, + notifiable_id: mention.id, + notifiable_type: "Mention", + action: nil, + json_data: json_data, + ) + end + + private + + attr_reader :mention + + def json_data + data = { user: user_data(mention.user) } + data[:comment] = comment_data(mention.mentionable) if mention.mentionable_type == "Comment" + data + end + end + end +end diff --git a/spec/jobs/notifications/mention_job_spec.rb b/spec/jobs/notifications/mention_job_spec.rb new file mode 100644 index 000000000..a49aff121 --- /dev/null +++ b/spec/jobs/notifications/mention_job_spec.rb @@ -0,0 +1,24 @@ +require "rails_helper" + +RSpec.describe Notifications::MentionJob, type: :job do + include_examples "#enqueues_job", "send_new_mention_notification", 1 + + describe "#perform_now" do + let(:new_mention_service) { double } + let(:mention) { create(:mention, mentionable: create(:comment, commentable: create(:article))) } + + before do + allow(new_mention_service).to receive(:call) + end + + it "calls the service" do + described_class.perform_now(mention.id, new_mention_service) + expect(new_mention_service).to have_received(:call) + end + + it "doesn't call the service when there's no mention" do + described_class.perform_now(mention.id + 1, new_mention_service) + expect(new_mention_service).not_to have_received(:call) + end + end +end diff --git a/spec/services/notifications/new_mention/send_spec.rb b/spec/services/notifications/new_mention/send_spec.rb new file mode 100644 index 000000000..83e288815 --- /dev/null +++ b/spec/services/notifications/new_mention/send_spec.rb @@ -0,0 +1,20 @@ +require "rails_helper" + +RSpec.describe Notifications::NewMention::Send, type: :service do + let(:user) { create(:user) } + let(:comment) { create(:comment, commentable: create(:article)) } + let(:mention) { create(:mention, mentionable: comment, user: user) } + + it "creates a mention notification" do + expect do + described_class.call(mention) + end.to change(Notification, :count).by(1) + end + + it "creates a correct mention notification" do + notification = described_class.call(mention) + expect(notification.user_id).to eq(user.id) + expect(notification.notifiable).to eq(mention) + expect(notification.json_data["comment"]["path"]).to eq(comment.path) + end +end