Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Author notification on proposal publication #620

Merged
merged 34 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
5d2076c
add notification with eventmanager
BarbaraOliveira13 Oct 30, 2024
b4ef566
base to watch the CI and see files on github
BarbaraOliveira13 Nov 4, 2024
490cc23
fix translation key & notififaction displaying
BarbaraOliveira13 Nov 6, 2024
f8b938b
add send_pubication_notification to right file
BarbaraOliveira13 Nov 6, 2024
a28f39a
start test rspec
BarbaraOliveira13 Nov 6, 2024
4c9be63
continuing rspec
BarbaraOliveira13 Nov 6, 2024
eb00e65
potential final test file
BarbaraOliveira13 Nov 6, 2024
df4935d
fix: Merge proposal command and anonymous proposals
Quentinchampenois Nov 7, 2024
cb8772a
fix: Change ProposalPublishedEvent to SimpleEvent
Quentinchampenois Nov 7, 2024
d4beb61
fix: Proposal Published Event
Quentinchampenois Nov 7, 2024
2fbbf92
fix: Push FR locales
Quentinchampenois Nov 7, 2024
899f214
test: Add specs for proposal_published_event
Quentinchampenois Nov 7, 2024
1b491c3
update methode & file name
BarbaraOliveira13 Nov 8, 2024
4f9de63
adjust trad key
BarbaraOliveira13 Nov 8, 2024
1c13724
adjust test file & update name of test file
BarbaraOliveira13 Nov 8, 2024
6331597
lint
BarbaraOliveira13 Nov 8, 2024
be6a5d2
Resolved merge conflict by keeping author_confirmation_proposal_event…
BarbaraOliveira13 Nov 8, 2024
1f69918
lint fr trad key
BarbaraOliveira13 Nov 8, 2024
5126005
lint
BarbaraOliveira13 Nov 8, 2024
19c986f
update trad key order
BarbaraOliveira13 Nov 8, 2024
1e9bf1a
lint
BarbaraOliveira13 Nov 12, 2024
d9dbd33
correct trad key link in method
BarbaraOliveira13 Nov 12, 2024
b62d525
correst rspec
BarbaraOliveira13 Nov 12, 2024
58cdd95
fix rspec
BarbaraOliveira13 Nov 12, 2024
faa18f9
fix: Notification small title
Quentinchampenois Nov 12, 2024
d70fa7b
lint(rubocop): Fix offenses
Quentinchampenois Nov 12, 2024
5957def
ci: Exclude BeEq Rubocop rule
Quentinchampenois Nov 12, 2024
1d89c03
clean
BarbaraOliveira13 Nov 13, 2024
2851810
Merge branch 'refactor/add_notification_after_adding_a_proposal' of g…
BarbaraOliveira13 Nov 13, 2024
502452b
fix CI
BarbaraOliveira13 Nov 13, 2024
76e36a6
delete test file
BarbaraOliveira13 Nov 13, 2024
d64a95b
add ignore trad key in i118n-tasks.yml to fix CI
BarbaraOliveira13 Nov 13, 2024
ae19d3c
update text syntaxe via trad key
BarbaraOliveira13 Nov 14, 2024
ad284ea
add '' in fr trad key
BarbaraOliveira13 Nov 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop_rails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ RSpec/MultipleMemoizedHelpers:

RSpec/AnyInstance:
Enabled: false

RSpec/BeEq:
Exclude:
- spec/events/decidim/proposals/author_confirmation_proposal_event_spec.rb
126 changes: 126 additions & 0 deletions app/commands/decidim/proposals/publish_proposal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# frozen_string_literal: true

module Decidim
module Proposals
# A command with all the business logic when a user publishes a draft proposal.
class PublishProposal < Decidim::Command
include Decidim::AnonymousProposals::AnonymousBehaviorCommandsConcern

# Public: Initializes the command.
#
# proposal - The proposal to publish.
# current_user - The current user.
# override: decidim-module-anonymous_proposals/app/commands/decidim/anonymous_proposals/publish_proposal_command_overrides.rb
def initialize(proposal, current_user)
@proposal = proposal
@is_anonymous = allow_anonymous_proposals? && (current_user.blank? || proposal.authored_by?(anonymous_group))
set_current_user(current_user)
end

# Executes the command. Broadcasts these events:
#
# - :ok when everything is valid and the proposal is published.
# - :invalid if the proposal's author is not the current user.
#
# Returns nothing.
def call
return broadcast(:invalid) unless @proposal.authored_by?(@current_user)

transaction do
publish_proposal
increment_scores
send_notification
send_notification_to_participatory_space
send_publication_notification
end

broadcast(:ok, @proposal)
end

private

# This will be the PaperTrail version that is
# shown in the version control feature (1 of 1)
#
# For an attribute to appear in the new version it has to be reset
# and reassigned, as PaperTrail only keeps track of object CHANGES.
def publish_proposal
title = reset(:title)
body = reset(:body)

Decidim.traceability.perform_action!(
"publish",
@proposal,
@current_user,
visibility: "public-only"
) do
@proposal.update title: title, body: body, published_at: Time.current
end
end

# Reset the attribute to an empty string and return the old value
def reset(attribute)
attribute_value = @proposal[attribute]
PaperTrail.request(enabled: false) do
# rubocop:disable Rails/SkipsModelValidations
@proposal.update_attribute attribute, ""
# rubocop:enable Rails/SkipsModelValidations
end
attribute_value
end

def send_notification
return if @proposal.coauthorships.empty?

Decidim::EventsManager.publish(
event: "decidim.events.proposals.proposal_published",
event_class: Decidim::Proposals::PublishProposalEvent,
resource: @proposal,
followers: coauthors_followers
)
end

def send_publication_notification
Decidim::EventsManager.publish(
event: "decidim.events.proposals.author_confirmation_proposal_event",
event_class: Decidim::Proposals::AuthorConfirmationProposalEvent,
resource: @proposal,
affected_users: [@proposal.creator_identity],
extra: { force_email: true },
force_send: true
)
end

def send_notification_to_participatory_space
Decidim::EventsManager.publish(
event: "decidim.events.proposals.proposal_published",
event_class: Decidim::Proposals::PublishProposalEvent,
resource: @proposal,
followers: @proposal.participatory_space.followers - coauthors_followers,
extra: {
participatory_space: true
}
)
end

def coauthors_followers
@coauthors_followers ||= @proposal.authors.flat_map(&:followers)
end

def increment_scores
@proposal.coauthorships.find_each do |coauthorship|
if coauthorship.user_group
Decidim::Gamification.increment_score(coauthorship.user_group, :proposals)
else
Decidim::Gamification.increment_score(coauthorship.author, :proposals)
end
end
end

# override: decidim-module-anonymous_proposals/app/commands/decidim/anonymous_proposals/publish_proposal_command_overrides.rb
def component
@component ||= @proposal.component
end
end
end
end
15 changes: 15 additions & 0 deletions app/events/decidim/proposals/author_confirmation_proposal_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Decidim
module Proposals
class AuthorConfirmationProposalEvent < Decidim::Events::SimpleEvent
def self.model_name
ActiveModel::Name.new(self, nil, I18n.t("decidim.events.proposals.author_confirmation_proposal_event.email_subject"))
end

def resource_title
translated_attribute(resource.title)
end
end
end
end
3 changes: 3 additions & 0 deletions config/i18n-tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,8 @@ ignore_unused:
- decidim.newsletters.unsubscribe.error
- decidim.newsletters.unsubscribe.token_error
- decidim.half_signup.quick_auth.sms_verification.text_message
- decidim.events.proposals.author_confirmation_proposal_event.email_intro
- decidim.events.proposals.author_confirmation_proposal_event.email_outro
- decidim.events.proposals.author_confirmation_proposal_event.notification_title


6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ en:
email_outro: You have received this notification because you are following the initiative "%{resource_title}".
email_subject: Initiative "%{resource_title}" has been answered
notification_title: The initiative <a href="%{resource_path}">%{resource_title}</a> has been answered.
proposals:
author_confirmation_proposal_event:
email_intro: 'Your proposal " %{resource_title} " was successfully received and is now public. Thank you for participating ! You can view it here:'
email_outro: You received this notification because you are the author of the proposal. You can unfollow it by visiting the proposal page (" %{resource_title} ") and clicking on " Unfollow ".
email_subject: Your proposal has been published!
notification_title: Your proposal <a href="%{resource_path}">%{resource_title}</a> is now live.
users:
user_officialized:
email_intro: Participant %{name} (%{nickname}) has been officialized.
Expand Down
6 changes: 6 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ fr:
email_outro: Vous avez reçu cette notification parce que vous suivez la pétition "%{resource_title}".
email_subject: La pétition "%{resource_title}" a reçu une réponse.
notification_title: La pétition <a href="%{resource_path}">%{resource_title}</a> a reçu une réponse.
proposals:
author_confirmation_proposal_event:
email_intro: 'Votre proposition « %{resource_title} » a été reçue avec succès et est maintenant publique. Merci pour votre participation ! Vous pouvez la consulter ici :'
email_outro: Vous recevez cette notification car vous êtes l’auteur de la proposition. Vous pouvez vous désabonner en visitant la page de la proposition (« %{resource_title} ») et en cliquant sur « Ne plus suivre ».
email_subject: Votre proposition a été publiée !
notification_title: Votre proposition <a href="%{resource_path}">%{resource_title}</a> est maintenant en ligne.
users:
user_officialized:
email_intro: Le participant %{name} (%{nickname}) a été officialisé.
Expand Down
Loading