Move tag adjustment notification to Active Jobs (#2654)
This commit is contained in:
parent
4e256e509e
commit
ecdbee64ab
7 changed files with 144 additions and 38 deletions
12
app/jobs/notifications/tag_adjustment_notification_job.rb
Normal file
12
app/jobs/notifications/tag_adjustment_notification_job.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
module Notifications
|
||||
class TagAdjustmentNotificationJob < ApplicationJob
|
||||
queue_as :send_tag_adjustment_notification
|
||||
|
||||
def perform(tag_adjustment_id, service = Notifications::TagAdjustmentNotification::Send)
|
||||
tag_adjustment = TagAdjustment.find_by(id: tag_adjustment_id)
|
||||
return unless tag_adjustment
|
||||
|
||||
service.call(tag_adjustment)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -122,23 +122,12 @@ class Notification < ApplicationRecord
|
|||
end
|
||||
|
||||
def send_tag_adjustment_notification(tag_adjustment)
|
||||
article = tag_adjustment.article
|
||||
json_data = {
|
||||
article: { title: article.title, path: article.path },
|
||||
adjustment_type: tag_adjustment.adjustment_type,
|
||||
status: tag_adjustment.status,
|
||||
reason_for_adjustment: tag_adjustment.reason_for_adjustment,
|
||||
tag_name: tag_adjustment.tag_name
|
||||
}
|
||||
Notification.create(
|
||||
user_id: article.user_id,
|
||||
notifiable_id: tag_adjustment.id,
|
||||
notifiable_type: tag_adjustment.class.name,
|
||||
json_data: json_data,
|
||||
)
|
||||
article.user.update_column(:last_moderation_notification, Time.current)
|
||||
Notifications::TagAdjustmentNotificationJob.perform_later(tag_adjustment.id)
|
||||
end
|
||||
|
||||
def send_tag_adjustment_notification_without_delay(tag_adjustment)
|
||||
Notifications::TagAdjustmentNotificationJob.perform_now(tag_adjustment.id)
|
||||
end
|
||||
handle_asynchronously :send_tag_adjustment_notification
|
||||
|
||||
def send_milestone_notification(milestone_hash)
|
||||
milestone_hash[:next_milestone] = next_milestone(milestone_hash)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
# send notifications about a tag adjustment
|
||||
module Notifications
|
||||
module TagAdjustmentNotification
|
||||
class Send
|
||||
def initialize(tag_adjustment)
|
||||
@tag_adjustment = tag_adjustment
|
||||
end
|
||||
|
||||
def self.call(*args)
|
||||
new(*args).call
|
||||
end
|
||||
|
||||
def call
|
||||
article = tag_adjustment.article
|
||||
json_data = {
|
||||
article: { title: article.title, path: article.path },
|
||||
adjustment_type: tag_adjustment.adjustment_type,
|
||||
status: tag_adjustment.status,
|
||||
reason_for_adjustment: tag_adjustment.reason_for_adjustment,
|
||||
tag_name: tag_adjustment.tag_name
|
||||
}
|
||||
notification = Notification.create(
|
||||
user_id: article.user_id,
|
||||
notifiable_id: tag_adjustment.id,
|
||||
notifiable_type: tag_adjustment.class.name,
|
||||
json_data: json_data,
|
||||
)
|
||||
article.user.update_column(:last_moderation_notification, Time.current)
|
||||
notification
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :tag_adjustment
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe Notifications::TagAdjustmentNotificationJob do
|
||||
include_examples "#enqueues_job", "send_tag_adjustment_notification", 333
|
||||
|
||||
describe "#perform_now" do
|
||||
let(:id) { rand(1000) }
|
||||
let(:tag_adjustment_service) { double }
|
||||
let(:tag_adjustment) { double }
|
||||
|
||||
before do
|
||||
allow(tag_adjustment_service).to receive(:call)
|
||||
end
|
||||
|
||||
describe "When a tag is found" do
|
||||
it "calls the service" do
|
||||
allow(TagAdjustment).to receive(:find_by).and_return(tag_adjustment)
|
||||
perform
|
||||
expect(tag_adjustment_service).to have_received(:call)
|
||||
end
|
||||
end
|
||||
|
||||
describe "When no tag is found" do
|
||||
it "does not call the service" do
|
||||
allow(TagAdjustment).to receive(:find_by).and_return(nil)
|
||||
perform
|
||||
expect(tag_adjustment_service).not_to have_received(:call)
|
||||
end
|
||||
end
|
||||
|
||||
def perform
|
||||
described_class.perform_now(id, tag_adjustment_service)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -301,27 +301,6 @@ RSpec.describe Notification, type: :model do
|
|||
end
|
||||
end
|
||||
|
||||
describe "#send_tag_adjustment_notification" do
|
||||
let(:tag) { create(:tag) }
|
||||
let(:article) { create(:article, user: user2, body_markdown: "---\ntitle: Hellohnnnn#{rand(1000)}\npublished: true\ntags: heyheyhey,#{tag.name}\n---\n\nHello") }
|
||||
let(:tag_adjustment) { create(:tag_adjustment, tag: tag, user: user, article: article) }
|
||||
|
||||
before do
|
||||
user.add_role(:tag_moderator, tag)
|
||||
end
|
||||
|
||||
it "notifies the author of the article" do
|
||||
Notification.send_tag_adjustment_notification_without_delay(tag_adjustment)
|
||||
expect(Notification.first.user_id).to eq user2.id
|
||||
end
|
||||
|
||||
it "updates the author's last_moderation_notification" do
|
||||
original_last_moderation_notification_timestamp = user2.last_moderation_notification
|
||||
Notification.send_tag_adjustment_notification_without_delay(tag_adjustment)
|
||||
expect(user2.reload.last_moderation_notification).to be > original_last_moderation_notification_timestamp
|
||||
end
|
||||
end
|
||||
|
||||
describe "#send_milestone_notification" do
|
||||
# milestones = [64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144]
|
||||
let(:view_milestone_hash) { { type: "View", article: article } }
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe Notifications::TagAdjustmentNotification::Send do
|
||||
let(:user) { create(:user) }
|
||||
let(:user2) { create(:user) }
|
||||
let(:article) { create(:article, title: "My title", user: user2, body_markdown: "---\ntitle: Hellohnnnn#{rand(1000)}\npublished: true\ntags: heyheyhey,#{tag.name}\n---\n\nHello") }
|
||||
let(:tag) { create(:tag) }
|
||||
let(:mod_user) { create(:user) }
|
||||
let(:tag_adjustment) { create(:tag_adjustment, user_id: mod_user.id, article_id: article.id, tag_id: tag.id, adjustment_type: "removal") }
|
||||
let(:notification) { described_class.call(tag_adjustment) }
|
||||
|
||||
before do
|
||||
mod_user.add_role(:tag_moderator, tag)
|
||||
end
|
||||
|
||||
it "returns a valid notification" do
|
||||
expect(notification).to be_a(Notification)
|
||||
end
|
||||
|
||||
it "notifies the author of the article" do
|
||||
Notification.send_tag_adjustment_notification_without_delay(tag_adjustment)
|
||||
expect(Notification.first.user_id).to eq user2.id
|
||||
end
|
||||
|
||||
specify "notification to have valid attributes", :aggregate_failures do
|
||||
expect(notification.user_id).to eq(article.user_id)
|
||||
expect(notification.notifiable_id).to eq(tag_adjustment.id)
|
||||
expect(notification.notifiable_type).to eq(tag_adjustment.class.name)
|
||||
end
|
||||
|
||||
it "tests JSON data" do
|
||||
json = notification.json_data
|
||||
expect(json["article"]["title"]).to start_with("Hello")
|
||||
expect(json["adjustment_type"]). to eq "removal"
|
||||
end
|
||||
|
||||
specify "notification to be inserted on DB" do
|
||||
expect do
|
||||
described_class.call(tag_adjustment)
|
||||
end.to change(Notification, :count).by(1)
|
||||
end
|
||||
|
||||
it "checks that article user last mod notification updates" do
|
||||
expect do
|
||||
described_class.call(tag_adjustment)
|
||||
end.to change(tag_adjustment.article.user, :last_moderation_notification)
|
||||
end
|
||||
|
||||
it "updates the author's last_moderation_notification" do
|
||||
original_last_moderation_notification_timestamp = user2.last_moderation_notification
|
||||
Notification.send_tag_adjustment_notification_without_delay(tag_adjustment)
|
||||
expect(user2.reload.last_moderation_notification).to be > original_last_moderation_notification_timestamp
|
||||
end
|
||||
end
|
||||
|
|
@ -29,7 +29,7 @@ RSpec.describe TagAdjustmentCreationService do
|
|||
end
|
||||
|
||||
it "creates notification" do
|
||||
run_background_jobs_immediately do
|
||||
perform_enqueued_jobs do
|
||||
tag_adjustment = create_tag_adjustment
|
||||
expect(Notification.last.user_id).to eq(article.user_id)
|
||||
expect(Notification.last.json_data["adjustment_type"]).to eq(tag_adjustment.adjustment_type)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue