diff --git a/decidim-admin/lib/decidim/admin/engine.rb b/decidim-admin/lib/decidim/admin/engine.rb index 41e74daa767f..2bec21a7d0c5 100644 --- a/decidim-admin/lib/decidim/admin/engine.rb +++ b/decidim-admin/lib/decidim/admin/engine.rb @@ -279,7 +279,7 @@ class Engine < ::Rails::Engine initializer "decidim_admin.register_events" do config.to_prepare do - Decidim::EventsManager.subscribe("decidim.admin.block_user:after") do |_event_name, data| + ActiveSupport::Notifications.subscribe("decidim.admin.block_user:after") do |_event_name, data| Decidim::BlockUserMailer.notify(data[:resource], data.dig(:extra, :justification)).deliver_later end end diff --git a/decidim-comments/app/commands/decidim/comments/create_comment.rb b/decidim-comments/app/commands/decidim/comments/create_comment.rb index bbbba14019ab..dd66f43da258 100644 --- a/decidim-comments/app/commands/decidim/comments/create_comment.rb +++ b/decidim-comments/app/commands/decidim/comments/create_comment.rb @@ -21,20 +21,32 @@ def initialize(form, author) def call return broadcast(:invalid) if form.invalid? - create_comment + with_events do + create_comment + end broadcast(:ok, comment) end private - attr_reader :form, :comment + attr_reader :form, :comment, :author + + def event_arguments + { + resource: comment, + extra: { + event_author: form.current_user, + locale: + } + } + end def create_comment parsed = Decidim::ContentProcessor.parse(form.body, current_organization: form.current_organization) params = { - author: @author, + author:, commentable: form.commentable, root_commentable: root_commentable(form.commentable), body: { I18n.locale => parsed.rewrite }, @@ -45,7 +57,7 @@ def create_comment @comment = Decidim.traceability.create!( Comment, - @author, + author, params, visibility: "public-only" ) diff --git a/decidim-comments/app/commands/decidim/comments/update_comment.rb b/decidim-comments/app/commands/decidim/comments/update_comment.rb index b6de7ede4199..d4ddea98f7da 100644 --- a/decidim-comments/app/commands/decidim/comments/update_comment.rb +++ b/decidim-comments/app/commands/decidim/comments/update_comment.rb @@ -24,7 +24,9 @@ def initialize(comment, current_user, form) def call return broadcast(:invalid) if form.invalid? || !comment.authored_by?(current_user) - update_comment + with_events do + update_comment + end broadcast(:ok) end @@ -33,6 +35,16 @@ def call attr_reader :form, :comment, :current_user + def event_arguments + { + resource: comment, + extra: { + event_author: form.current_user, + locale: + } + } + end + def update_comment parsed = Decidim::ContentProcessor.parse(form.body, current_organization: form.current_organization) diff --git a/decidim-comments/lib/decidim/comments/engine.rb b/decidim-comments/lib/decidim/comments/engine.rb index 426887a9e349..ab60146e6176 100644 --- a/decidim-comments/lib/decidim/comments/engine.rb +++ b/decidim-comments/lib/decidim/comments/engine.rb @@ -82,7 +82,7 @@ class Engine < ::Rails::Engine initializer "decidim_comments.moderation_content" do config.to_prepare do - Decidim::EventsManager.subscribe("decidim.admin.block_user:after") do |_event_name, data| + ActiveSupport::Notifications.subscribe("decidim.admin.block_user:after") do |_event_name, data| Decidim::Comments::HideAllCreatedByAuthorJob.perform_later(**data) end end diff --git a/decidim-comments/spec/commands/create_comment_spec.rb b/decidim-comments/spec/commands/create_comment_spec.rb index 8a7314c034d6..f7c78ecf85c9 100644 --- a/decidim-comments/spec/commands/create_comment_spec.rb +++ b/decidim-comments/spec/commands/create_comment_spec.rb @@ -47,6 +47,9 @@ module Comments expect { command.call }.to broadcast(:ok) end + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.comments.create_comment:before" + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.comments.create_comment:after" + it "creates a new comment" do expect(Comment).to receive(:create!).with( { author:, diff --git a/decidim-comments/spec/commands/update_comment_spec.rb b/decidim-comments/spec/commands/update_comment_spec.rb index 143f4c541586..2f97e3628213 100644 --- a/decidim-comments/spec/commands/update_comment_spec.rb +++ b/decidim-comments/spec/commands/update_comment_spec.rb @@ -65,6 +65,9 @@ module Comments expect { command.call }.to broadcast(:ok) end + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.comments.update_comment:before" + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.comments.update_comment:after" + it "updates the comment" do command.call comment.reload diff --git a/decidim-debates/app/commands/decidim/debates/create_debate.rb b/decidim-debates/app/commands/decidim/debates/create_debate.rb index 508a40b18ff8..3d0a292a1182 100644 --- a/decidim-debates/app/commands/decidim/debates/create_debate.rb +++ b/decidim-debates/app/commands/decidim/debates/create_debate.rb @@ -15,12 +15,11 @@ def initialize(form) def call return broadcast(:invalid) if form.invalid? - transaction do + with_events(with_transaction: true) do create_debate - send_notification_to_author_followers - send_notification_to_space_followers end - + send_notification_to_author_followers + send_notification_to_space_followers follow_debate broadcast(:ok, debate) end @@ -29,6 +28,16 @@ def call attr_reader :debate, :form + def event_arguments + { + resource: debate, + extra: { + event_author: form.current_user, + locale: + } + } + end + def create_debate parsed_title = Decidim::ContentProcessor.parse_with_processor(:hashtag, form.title, current_organization: form.current_organization).rewrite parsed_description = Decidim::ContentProcessor.parse_with_processor(:hashtag, form.description, current_organization: form.current_organization).rewrite diff --git a/decidim-debates/app/commands/decidim/debates/update_debate.rb b/decidim-debates/app/commands/decidim/debates/update_debate.rb index 88188b95fd7f..c822c0930b19 100644 --- a/decidim-debates/app/commands/decidim/debates/update_debate.rb +++ b/decidim-debates/app/commands/decidim/debates/update_debate.rb @@ -21,7 +21,10 @@ def call return broadcast(:invalid) if form.invalid? return broadcast(:invalid) unless form.debate.editable_by?(form.current_user) - update_debate + with_events(with_transaction: true) do + update_debate + end + broadcast(:ok, @debate) end @@ -29,6 +32,16 @@ def call attr_reader :form + def event_arguments + { + resource: @debate, + extra: { + event_author: form.current_user, + locale: + } + } + end + def update_debate @debate = Decidim.traceability.update!( @form.debate, diff --git a/decidim-debates/lib/decidim/debates/engine.rb b/decidim-debates/lib/decidim/debates/engine.rb index 7f097c1436a0..5cadcbc84525 100644 --- a/decidim-debates/lib/decidim/debates/engine.rb +++ b/decidim-debates/lib/decidim/debates/engine.rb @@ -111,7 +111,7 @@ class Engine < ::Rails::Engine initializer "decidim_debates.moderation_content" do config.to_prepare do - Decidim::EventsManager.subscribe("decidim.admin.block_user:after") do |_event_name, data| + ActiveSupport::Notifications.subscribe("decidim.admin.block_user:after") do |_event_name, data| Decidim::Debates::HideAllCreatedByAuthorJob.perform_later(**data) end end diff --git a/decidim-debates/spec/commands/decidim/debates/create_debate_spec.rb b/decidim-debates/spec/commands/decidim/debates/create_debate_spec.rb index 37db10cdeaec..9c4b9f3b6989 100644 --- a/decidim-debates/spec/commands/decidim/debates/create_debate_spec.rb +++ b/decidim-debates/spec/commands/decidim/debates/create_debate_spec.rb @@ -41,6 +41,13 @@ expect { subject.call }.to change(Decidim::Debates::Debate, :count).by(1) end + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.debates.create_debate:before" do + let(:command) { subject } + end + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.debates.create_debate:after" do + let(:command) { subject } + end + it "sets the scope" do subject.call expect(debate.scope).to eq scope diff --git a/decidim-debates/spec/commands/decidim/debates/update_debate_spec.rb b/decidim-debates/spec/commands/decidim/debates/update_debate_spec.rb index 050d17aa9da2..1ba82c01ee33 100644 --- a/decidim-debates/spec/commands/decidim/debates/update_debate_spec.rb +++ b/decidim-debates/spec/commands/decidim/debates/update_debate_spec.rb @@ -68,6 +68,13 @@ end.to change(debate, :title) end + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.debates.update_debate:before" do + let(:command) { subject } + end + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.debates.update_debate:after" do + let(:command) { subject } + end + it "sets the scope" do subject.call debate.reload diff --git a/decidim-dev/lib/decidim/dummy_resources/engine.rb b/decidim-dev/lib/decidim/dummy_resources/engine.rb index a8c937661e35..d92b18fdd4f8 100644 --- a/decidim-dev/lib/decidim/dummy_resources/engine.rb +++ b/decidim-dev/lib/decidim/dummy_resources/engine.rb @@ -18,7 +18,7 @@ class DummyEngine < Rails::Engine initializer "dummy.moderation_content" do config.to_prepare do - Decidim::EventsManager.subscribe("decidim.admin.block_user:after") do |_event_name, data| + ActiveSupport::Notifications.subscribe("decidim.admin.block_user:after") do |_event_name, data| Decidim::DummyResources::HideAllCreatedByAuthorJob.perform_later(**data) end end diff --git a/decidim-meetings/app/commands/decidim/meetings/create_meeting.rb b/decidim-meetings/app/commands/decidim/meetings/create_meeting.rb index 841cf3bc4e3d..fee6b51136f6 100644 --- a/decidim-meetings/app/commands/decidim/meetings/create_meeting.rb +++ b/decidim-meetings/app/commands/decidim/meetings/create_meeting.rb @@ -15,13 +15,14 @@ def initialize(form) def call return broadcast(:invalid) if form.invalid? - transaction do + with_events(with_transaction: true) do create_meeting! - schedule_upcoming_meeting_notification - send_notification end create_follow_form_resource(form.current_user) + schedule_upcoming_meeting_notification + send_notification + broadcast(:ok, meeting) end @@ -29,6 +30,16 @@ def call attr_reader :meeting, :form + def event_arguments + { + resource: meeting, + extra: { + event_author: form.current_user, + locale: + } + } + end + def create_meeting! parsed_title = Decidim::ContentProcessor.parse_with_processor(:hashtag, form.title, current_organization: form.current_organization).rewrite parsed_description = Decidim::ContentProcessor.parse(form.description, current_organization: form.current_organization).rewrite diff --git a/decidim-meetings/app/commands/decidim/meetings/update_meeting.rb b/decidim-meetings/app/commands/decidim/meetings/update_meeting.rb index 3bc92672e171..9233f93ec5cc 100644 --- a/decidim-meetings/app/commands/decidim/meetings/update_meeting.rb +++ b/decidim-meetings/app/commands/decidim/meetings/update_meeting.rb @@ -22,12 +22,12 @@ def initialize(form, current_user, meeting) def call return broadcast(:invalid) if form.invalid? - transaction do + with_events(with_transaction: true) do update_meeting! - send_notification if should_notify_followers? - schedule_upcoming_meeting_notification if start_time_changed? end + send_notification if should_notify_followers? + schedule_upcoming_meeting_notification if start_time_changed? broadcast(:ok, meeting) end @@ -35,6 +35,16 @@ def call attr_reader :form, :current_user, :meeting + def event_arguments + { + resource: meeting, + extra: { + event_author: form.current_user, + locale: + } + } + end + def update_meeting! parsed_title = Decidim::ContentProcessor.parse_with_processor(:hashtag, form.title, current_organization: form.current_organization).rewrite parsed_description = Decidim::ContentProcessor.parse(form.description, current_organization: form.current_organization).rewrite diff --git a/decidim-meetings/lib/decidim/meetings/engine.rb b/decidim-meetings/lib/decidim/meetings/engine.rb index 51233e003075..6edcb2a5b46b 100644 --- a/decidim-meetings/lib/decidim/meetings/engine.rb +++ b/decidim-meetings/lib/decidim/meetings/engine.rb @@ -150,7 +150,7 @@ class Engine < ::Rails::Engine initializer "decidim_meetings.moderation_content" do config.to_prepare do - Decidim::EventsManager.subscribe("decidim.admin.block_user:after") do |_event_name, data| + ActiveSupport::Notifications.subscribe("decidim.admin.block_user:after") do |_event_name, data| Decidim::Meetings::HideAllCreatedByAuthorJob.perform_later(**data) end end diff --git a/decidim-meetings/spec/commands/create_meeting_spec.rb b/decidim-meetings/spec/commands/create_meeting_spec.rb index 35e34da377fb..363c91c49c2c 100644 --- a/decidim-meetings/spec/commands/create_meeting_spec.rb +++ b/decidim-meetings/spec/commands/create_meeting_spec.rb @@ -68,6 +68,13 @@ module Decidim::Meetings context "when everything is ok" do let(:meeting) { Meeting.last } + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.meetings.create_meeting:before" do + let(:command) { subject } + end + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.meetings.create_meeting:after" do + let(:command) { subject } + end + it "creates and publishes the meeting and log both actions" do subject.call meeting.reload @@ -156,7 +163,7 @@ module Decidim::Meetings end it "schedules a upcoming meeting notification job 48h before start time" do - meeting = instance_double(Meeting, id: 1, start_time:, participatory_space: participatory_process) + meeting = instance_double(Meeting, id: 1, start_time:, participatory_space: participatory_process, author: current_user) allow(Decidim.traceability) .to receive(:create!) .and_return(meeting) @@ -178,7 +185,7 @@ module Decidim::Meetings end it "does not schedule an upcoming meeting notification if start time is in the past" do - meeting = instance_double(Meeting, id: 1, start_time: 2.days.ago, participatory_space: participatory_process) + meeting = instance_double(Meeting, id: 1, start_time: 2.days.ago, participatory_space: participatory_process, author: current_user) allow(Decidim.traceability) .to receive(:create!) .and_return(meeting) diff --git a/decidim-meetings/spec/commands/update_meeting_spec.rb b/decidim-meetings/spec/commands/update_meeting_spec.rb index f87ac0a399d4..4fe98bc34bca 100644 --- a/decidim-meetings/spec/commands/update_meeting_spec.rb +++ b/decidim-meetings/spec/commands/update_meeting_spec.rb @@ -64,6 +64,14 @@ module Decidim::Meetings end context "when everything is ok" do + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.meetings.update_meeting:before" do + let(:command) { subject } + end + + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.meetings.update_meeting:after" do + let(:command) { subject } + end + it "updates the meeting" do subject.call diff --git a/decidim-proposals/app/commands/decidim/proposals/create_collaborative_draft.rb b/decidim-proposals/app/commands/decidim/proposals/create_collaborative_draft.rb index 3e7e786540a2..7844d0a3870a 100644 --- a/decidim-proposals/app/commands/decidim/proposals/create_collaborative_draft.rb +++ b/decidim-proposals/app/commands/decidim/proposals/create_collaborative_draft.rb @@ -31,7 +31,7 @@ def call return broadcast(:invalid) if attachments_invalid? end - transaction do + with_events(with_transaction: true) do create_collaborative_draft create_attachments if process_attachments? end @@ -43,6 +43,16 @@ def call attr_reader :form, :collaborative_draft, :attachment + def event_arguments + { + resource: collaborative_draft, + extra: { + event_author: form.current_user, + locale: + } + } + end + def create_collaborative_draft @collaborative_draft = Decidim.traceability.perform_action!( :create, diff --git a/decidim-proposals/app/commands/decidim/proposals/create_proposal.rb b/decidim-proposals/app/commands/decidim/proposals/create_proposal.rb index 6c7f73064e7f..c81814b7aefa 100644 --- a/decidim-proposals/app/commands/decidim/proposals/create_proposal.rb +++ b/decidim-proposals/app/commands/decidim/proposals/create_proposal.rb @@ -31,7 +31,7 @@ def call return broadcast(:invalid) end - transaction do + with_events(with_transaction: true) do create_proposal end @@ -42,6 +42,16 @@ def call attr_reader :form, :proposal, :attachment + def event_arguments + { + resource: proposal, + extra: { + event_author: form.current_user, + locale: + } + } + end + # Prevent PaperTrail from creating an additional version # in the proposal multi-step creation process (step 1: create) # diff --git a/decidim-proposals/app/commands/decidim/proposals/update_collaborative_draft.rb b/decidim-proposals/app/commands/decidim/proposals/update_collaborative_draft.rb index b02222e6440e..211cc141657b 100644 --- a/decidim-proposals/app/commands/decidim/proposals/update_collaborative_draft.rb +++ b/decidim-proposals/app/commands/decidim/proposals/update_collaborative_draft.rb @@ -27,7 +27,7 @@ def call return broadcast(:invalid) if form.invalid? return broadcast(:invalid) unless collaborative_draft.editable_by?(current_user) - transaction do + with_events(with_transaction: true) do update_collaborative_draft end @@ -38,6 +38,16 @@ def call attr_reader :form, :collaborative_draft, :current_user + def event_arguments + { + resource: collaborative_draft, + extra: { + event_author: form.current_user, + locale: + } + } + end + def update_collaborative_draft Decidim.traceability.update!( @collaborative_draft, diff --git a/decidim-proposals/app/commands/decidim/proposals/update_proposal.rb b/decidim-proposals/app/commands/decidim/proposals/update_proposal.rb index 726d5d3079d4..20e78e9de77e 100644 --- a/decidim-proposals/app/commands/decidim/proposals/update_proposal.rb +++ b/decidim-proposals/app/commands/decidim/proposals/update_proposal.rb @@ -33,7 +33,7 @@ def call return broadcast(:invalid) if attachments_invalid? end - transaction do + with_events(with_transaction: true) do if @proposal.draft? update_draft else @@ -52,6 +52,16 @@ def call attr_reader :form, :proposal, :current_user, :attachment + def event_arguments + { + resource: proposal, + extra: { + event_author: form.current_user, + locale: + } + } + end + def invalid? form.invalid? || !proposal.editable_by?(current_user) || proposal_limit_reached? end diff --git a/decidim-proposals/lib/decidim/proposals/engine.rb b/decidim-proposals/lib/decidim/proposals/engine.rb index 41c102ae6d05..d9214542e766 100644 --- a/decidim-proposals/lib/decidim/proposals/engine.rb +++ b/decidim-proposals/lib/decidim/proposals/engine.rb @@ -218,7 +218,7 @@ class Engine < ::Rails::Engine initializer "decidim_proposals.moderation_content" do config.to_prepare do - Decidim::EventsManager.subscribe("decidim.admin.block_user:after") do |_event_name, data| + ActiveSupport::Notifications.subscribe("decidim.admin.block_user:after") do |_event_name, data| Decidim::Proposals::HideAllCreatedByAuthorJob.perform_later(**data) end end diff --git a/decidim-proposals/spec/commands/decidim/proposals/create_collaborative_draft_spec.rb b/decidim-proposals/spec/commands/decidim/proposals/create_collaborative_draft_spec.rb index 8905f2765a61..8c9c7da289be 100644 --- a/decidim-proposals/spec/commands/decidim/proposals/create_collaborative_draft_spec.rb +++ b/decidim-proposals/spec/commands/decidim/proposals/create_collaborative_draft_spec.rb @@ -73,6 +73,9 @@ module Proposals expect { command.call }.to broadcast(:ok) end + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.proposals.create_collaborative_draft:before" + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.proposals.create_collaborative_draft:after" + it "creates a new collaborative draft" do expect do command.call diff --git a/decidim-proposals/spec/commands/decidim/proposals/create_proposal_spec.rb b/decidim-proposals/spec/commands/decidim/proposals/create_proposal_spec.rb index 3aa1ae848286..a39349d3f2ce 100644 --- a/decidim-proposals/spec/commands/decidim/proposals/create_proposal_spec.rb +++ b/decidim-proposals/spec/commands/decidim/proposals/create_proposal_spec.rb @@ -60,6 +60,9 @@ module Proposals expect { command.call }.to broadcast(:ok) end + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.proposals.create_proposal:before" + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.proposals.create_proposal:after" + it "creates a new proposal" do expect do command.call diff --git a/decidim-proposals/spec/commands/decidim/proposals/update_collaborative_draft_spec.rb b/decidim-proposals/spec/commands/decidim/proposals/update_collaborative_draft_spec.rb index a0758d1ac636..8c2f308b4a07 100644 --- a/decidim-proposals/spec/commands/decidim/proposals/update_collaborative_draft_spec.rb +++ b/decidim-proposals/spec/commands/decidim/proposals/update_collaborative_draft_spec.rb @@ -85,6 +85,9 @@ module Proposals expect { command.call }.to broadcast(:ok) end + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.proposals.update_collaborative_draft:before" + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.proposals.update_collaborative_draft:after" + it "updates the collaborative draft" do expect do command.call diff --git a/decidim-proposals/spec/commands/decidim/proposals/update_proposal_spec.rb b/decidim-proposals/spec/commands/decidim/proposals/update_proposal_spec.rb index ae21036d28bb..7cf09795b772 100644 --- a/decidim-proposals/spec/commands/decidim/proposals/update_proposal_spec.rb +++ b/decidim-proposals/spec/commands/decidim/proposals/update_proposal_spec.rb @@ -104,6 +104,9 @@ module Proposals expect { command.call }.to broadcast(:ok) end + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.proposals.update_proposal:before" + it_behaves_like "fires an ActiveSupport::Notification event", "decidim.proposals.update_proposal:after" + it "updates the proposal" do command.call proposal.reload