Skip to content

Commit

Permalink
Add custom events for created of updated resources (decidim#11117)
Browse files Browse the repository at this point in the history
* Add events for comments

* Add events for debates

* Add events for meetings

* Update the proposals commands

* Refactor with_events

* Apply review recommendations

* Revert event changes

* Revert event changes
  • Loading branch information
alecslupu authored Jul 22, 2023
1 parent 8b61be0 commit c58b304
Show file tree
Hide file tree
Showing 26 changed files with 182 additions and 28 deletions.
2 changes: 1 addition & 1 deletion decidim-admin/lib/decidim/admin/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions decidim-comments/app/commands/decidim/comments/create_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -45,7 +57,7 @@ def create_comment

@comment = Decidim.traceability.create!(
Comment,
@author,
author,
params,
visibility: "public-only"
)
Expand Down
14 changes: 13 additions & 1 deletion decidim-comments/app/commands/decidim/comments/update_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion decidim-comments/lib/decidim/comments/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions decidim-comments/spec/commands/create_comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:,
Expand Down
3 changes: 3 additions & 0 deletions decidim-comments/spec/commands/update_comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions decidim-debates/app/commands/decidim/debates/create_debate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 14 additions & 1 deletion decidim-debates/app/commands/decidim/debates/update_debate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,27 @@ 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

private

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,
Expand Down
2 changes: 1 addition & 1 deletion decidim-debates/lib/decidim/debates/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion decidim-dev/lib/decidim/dummy_resources/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions decidim-meetings/app/commands/decidim/meetings/create_meeting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,31 @@ 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

private

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
Expand Down
16 changes: 13 additions & 3 deletions decidim-meetings/app/commands/decidim/meetings/update_meeting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,29 @@ 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

private

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
Expand Down
2 changes: 1 addition & 1 deletion decidim-meetings/lib/decidim/meetings/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions decidim-meetings/spec/commands/create_meeting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions decidim-meetings/spec/commands/update_meeting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def call
return broadcast(:invalid)
end

transaction do
with_events(with_transaction: true) do
create_proposal
end

Expand All @@ -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)
#
Expand Down
Loading

0 comments on commit c58b304

Please sign in to comment.