From 4420f5d4250f0c81addf2f15ea7374b56dcaa742 Mon Sep 17 00:00:00 2001 From: Bastien Date: Thu, 11 Jan 2018 12:15:04 +0100 Subject: [PATCH 01/17] Set title field as obligatory --- decidim-admin/app/forms/decidim/admin/navbar_link_form.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decidim-admin/app/forms/decidim/admin/navbar_link_form.rb b/decidim-admin/app/forms/decidim/admin/navbar_link_form.rb index d73945ef5108d..cc4fa22dce140 100644 --- a/decidim-admin/app/forms/decidim/admin/navbar_link_form.rb +++ b/decidim-admin/app/forms/decidim/admin/navbar_link_form.rb @@ -12,7 +12,7 @@ class NavbarLinkForm < Form attribute :target, String validates :link, :format => { :with => URI::regexp(%w(http https)) }, presence: true - validates :title, translatable_presence: true + validates :title, translatable_presence: true, presence: true validates :organization_id, :link, presence: true end From 6ff1af195a069426243e19ae8858a9a7a480a8cc Mon Sep 17 00:00:00 2001 From: Bastien Date: Fri, 12 Jan 2018 10:08:52 +0100 Subject: [PATCH 02/17] Translation for navbar links in FR and EN locales --- .../app/views/decidim/admin/navbar_links/_form.html.erb | 8 ++++---- decidim-admin/config/locales/en.yml | 5 +++++ decidim-admin/config/locales/fr.yml | 5 +++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/decidim-admin/app/views/decidim/admin/navbar_links/_form.html.erb b/decidim-admin/app/views/decidim/admin/navbar_links/_form.html.erb index 8ac3ce0999cb4..a6b6046262702 100644 --- a/decidim-admin/app/views/decidim/admin/navbar_links/_form.html.erb +++ b/decidim-admin/app/views/decidim/admin/navbar_links/_form.html.erb @@ -1,14 +1,14 @@
- <%= form.translated :text_field, :title %> + <%= form.translated :text_field, :title, label: t(".title") %>
- <%= form.text_field :link, value: navbar_link.link || "http://" %> + <%= form.text_field :link, value: navbar_link.link || "http://", label: t(".link") %>
- <%= form.radio_button :target, "blank", label: "Ouvrir sur un nouvel onglet" %> - <%= form.radio_button :target, "", label: "Ouvrir dans le même onglet" %> + <%= form.radio_button :target, "blank", label: t(".new_tab") %> + <%= form.radio_button :target, "", label: t(".same_tab") %>
<%= form.hidden_field :organization_id, value: current_organization.id %>
diff --git a/decidim-admin/config/locales/en.yml b/decidim-admin/config/locales/en.yml index 2ed45f5ad68e3..02bdd619f7865 100644 --- a/decidim-admin/config/locales/en.yml +++ b/decidim-admin/config/locales/en.yml @@ -438,6 +438,11 @@ en: title: Invite user as administrator navbar_links: no_links: No links created + form: + title: Title + link: Link + new_tab: Open in a new tab + same_tab: Open in the same tab new: title: New navbar link create: Create diff --git a/decidim-admin/config/locales/fr.yml b/decidim-admin/config/locales/fr.yml index ee9cd4b293d6f..893e2825239fb 100644 --- a/decidim-admin/config/locales/fr.yml +++ b/decidim-admin/config/locales/fr.yml @@ -437,6 +437,11 @@ fr: title: Inviter l'utilisateur en tant qu'administrateur navbar_links: no_links: Vous n'avez pas créé de lien + form: + title: Titre + link: Lien + new_tab: Ouvrir sur un nouvel onglet + same_tab: Ouvrir dans le même onglet new: title: Nouveau lien de navigation create: Créer From 30b550ce1f4218b705852d92188020ae7730bf38 Mon Sep 17 00:00:00 2001 From: Bastien Date: Fri, 12 Jan 2018 12:31:45 +0100 Subject: [PATCH 03/17] Add weight to navbar links --- .../app/commands/decidim/admin/create_navbar_link.rb | 1 + .../app/commands/decidim/admin/update_navbar_link.rb | 1 + decidim-admin/app/forms/decidim/admin/navbar_link_form.rb | 2 ++ .../app/views/decidim/admin/navbar_links/_form.html.erb | 3 +++ .../app/views/decidim/admin/navbar_links/index.html.erb | 4 ++++ decidim-admin/config/locales/en.yml | 2 ++ decidim-admin/config/locales/fr.yml | 2 ++ .../db/migrate/20180112110646_add_weight_to_navbar_link.rb | 5 +++++ decidim-core/lib/decidim/engine_router.rb | 2 +- 9 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 decidim-core/db/migrate/20180112110646_add_weight_to_navbar_link.rb diff --git a/decidim-admin/app/commands/decidim/admin/create_navbar_link.rb b/decidim-admin/app/commands/decidim/admin/create_navbar_link.rb index c88e4986a5c17..70fc21950139d 100644 --- a/decidim-admin/app/commands/decidim/admin/create_navbar_link.rb +++ b/decidim-admin/app/commands/decidim/admin/create_navbar_link.rb @@ -32,6 +32,7 @@ def create_navbar_link Decidim::NavbarLink.create!( title: form.title, link: form.link, + weight: form.weight, target: form.target, decidim_organization_id: form.organization_id ) diff --git a/decidim-admin/app/commands/decidim/admin/update_navbar_link.rb b/decidim-admin/app/commands/decidim/admin/update_navbar_link.rb index 2dda370508c05..b291fa88fe34e 100644 --- a/decidim-admin/app/commands/decidim/admin/update_navbar_link.rb +++ b/decidim-admin/app/commands/decidim/admin/update_navbar_link.rb @@ -37,6 +37,7 @@ def attributes { title: form.title, link: form.link, + weight: form.weight, target: form.target, decidim_organization_id: form.organization_id } diff --git a/decidim-admin/app/forms/decidim/admin/navbar_link_form.rb b/decidim-admin/app/forms/decidim/admin/navbar_link_form.rb index cc4fa22dce140..52dd4e3b52de2 100644 --- a/decidim-admin/app/forms/decidim/admin/navbar_link_form.rb +++ b/decidim-admin/app/forms/decidim/admin/navbar_link_form.rb @@ -8,11 +8,13 @@ class NavbarLinkForm < Form translatable_attribute :title, String attribute :link, String + attribute :weight, Integer attribute :organization_id, Integer attribute :target, String validates :link, :format => { :with => URI::regexp(%w(http https)) }, presence: true validates :title, translatable_presence: true, presence: true + validates :weight, presence: true validates :organization_id, :link, presence: true end diff --git a/decidim-admin/app/views/decidim/admin/navbar_links/_form.html.erb b/decidim-admin/app/views/decidim/admin/navbar_links/_form.html.erb index a6b6046262702..5ce9ac0abd46a 100644 --- a/decidim-admin/app/views/decidim/admin/navbar_links/_form.html.erb +++ b/decidim-admin/app/views/decidim/admin/navbar_links/_form.html.erb @@ -6,6 +6,9 @@
<%= form.text_field :link, value: navbar_link.link || "http://", label: t(".link") %>
+
+ <%= form.number_field :weight, value: navbar_link.weight, label: t(".weight") %> +
<%= form.radio_button :target, "blank", label: t(".new_tab") %> <%= form.radio_button :target, "", label: t(".same_tab") %> diff --git a/decidim-admin/app/views/decidim/admin/navbar_links/index.html.erb b/decidim-admin/app/views/decidim/admin/navbar_links/index.html.erb index 60dc4886cb036..b5564a6596ec7 100644 --- a/decidim-admin/app/views/decidim/admin/navbar_links/index.html.erb +++ b/decidim-admin/app/views/decidim/admin/navbar_links/index.html.erb @@ -11,6 +11,7 @@ <%= t("models.navbar_link.fields.title", scope: "decidim.admin") %> + <%= t("models.navbar_link.fields.weight", scope: "decidim.admin") %> <%= t("models.navbar_link.fields.link", scope: "decidim.admin") %> @@ -21,6 +22,9 @@ <%= translated_attribute(navbar_link.title) %> + + <%= navbar_link.weight %> + <%= link_to navbar_link.link, navbar_link.link, target: :blank %> diff --git a/decidim-admin/config/locales/en.yml b/decidim-admin/config/locales/en.yml index 02bdd619f7865..5753301368861 100644 --- a/decidim-admin/config/locales/en.yml +++ b/decidim-admin/config/locales/en.yml @@ -282,6 +282,7 @@ en: fields: title: Title link: Link + weight: Weight moderations: index: title: Moderations @@ -441,6 +442,7 @@ en: form: title: Title link: Link + weight: Weight new_tab: Open in a new tab same_tab: Open in the same tab new: diff --git a/decidim-admin/config/locales/fr.yml b/decidim-admin/config/locales/fr.yml index 893e2825239fb..bfda45482f88c 100644 --- a/decidim-admin/config/locales/fr.yml +++ b/decidim-admin/config/locales/fr.yml @@ -281,6 +281,7 @@ fr: fields: title: Titre link: Lien + weight: Poids moderations: index: title: Modérations @@ -440,6 +441,7 @@ fr: form: title: Titre link: Lien + weight: Poids new_tab: Ouvrir sur un nouvel onglet same_tab: Ouvrir dans le même onglet new: diff --git a/decidim-core/db/migrate/20180112110646_add_weight_to_navbar_link.rb b/decidim-core/db/migrate/20180112110646_add_weight_to_navbar_link.rb new file mode 100644 index 0000000000000..e40c330916633 --- /dev/null +++ b/decidim-core/db/migrate/20180112110646_add_weight_to_navbar_link.rb @@ -0,0 +1,5 @@ +class AddWeightToNavbarLink < ActiveRecord::Migration[5.1] + def change + add_column :decidim_navbar_links, :weight, :integer + end +end diff --git a/decidim-core/lib/decidim/engine_router.rb b/decidim-core/lib/decidim/engine_router.rb index d9b61aeb3fb24..52a25ff6ae164 100644 --- a/decidim-core/lib/decidim/engine_router.rb +++ b/decidim-core/lib/decidim/engine_router.rb @@ -50,7 +50,7 @@ def method_missing(method_name, *args) private def route_helper?(method_name) - method_name.to_s.match?(/_(url|path)$/) + method_name.to_s.match(/_(url|path)$/) end end end From 1be88449896634e72d4973c8503310839bbd6815 Mon Sep 17 00:00:00 2001 From: Bastien Date: Fri, 12 Jan 2018 15:05:41 +0100 Subject: [PATCH 04/17] ReGex on model to validate the navbar_link link --- decidim-core/app/models/decidim/navbar_link.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/decidim-core/app/models/decidim/navbar_link.rb b/decidim-core/app/models/decidim/navbar_link.rb index 995b4ae4ba3d4..bf1154f9b7040 100644 --- a/decidim-core/app/models/decidim/navbar_link.rb +++ b/decidim-core/app/models/decidim/navbar_link.rb @@ -3,5 +3,14 @@ class NavbarLink < ApplicationRecord belongs_to :organization, foreign_key: "decidim_organization_id", class_name: "Decidim::Organization" + validate :validate_link_regex + + def validate_link_regex + link = URI.parse(self.link) + link.host.nil? + rescue URI::InvalidURIError + errors.add(:link) + end + end end From 76c64b33267414d295806163f83b3252446fbfe3 Mon Sep 17 00:00:00 2001 From: Bastien Date: Fri, 12 Jan 2018 15:13:01 +0100 Subject: [PATCH 05/17] Remove my fix of match? on engine router helper --- decidim-core/lib/decidim/engine_router.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decidim-core/lib/decidim/engine_router.rb b/decidim-core/lib/decidim/engine_router.rb index 52a25ff6ae164..d9b61aeb3fb24 100644 --- a/decidim-core/lib/decidim/engine_router.rb +++ b/decidim-core/lib/decidim/engine_router.rb @@ -50,7 +50,7 @@ def method_missing(method_name, *args) private def route_helper?(method_name) - method_name.to_s.match(/_(url|path)$/) + method_name.to_s.match?(/_(url|path)$/) end end end From 21eb2618bd7d60641921eed9ce1d6647ffc94179 Mon Sep 17 00:00:00 2001 From: Bastien Date: Fri, 12 Jan 2018 15:40:10 +0100 Subject: [PATCH 06/17] Fix PR #30 issues after review --- decidim-admin/config/locales/fr.yml | 2 +- decidim-core/app/models/decidim/navbar_link.rb | 2 +- decidim-proposals/config/locales/fr.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/decidim-admin/config/locales/fr.yml b/decidim-admin/config/locales/fr.yml index bfda45482f88c..6fc85d21e0b2c 100644 --- a/decidim-admin/config/locales/fr.yml +++ b/decidim-admin/config/locales/fr.yml @@ -281,7 +281,7 @@ fr: fields: title: Titre link: Lien - weight: Poids + weight: Position moderations: index: title: Modérations diff --git a/decidim-core/app/models/decidim/navbar_link.rb b/decidim-core/app/models/decidim/navbar_link.rb index bf1154f9b7040..5fbea1bd51ec8 100644 --- a/decidim-core/app/models/decidim/navbar_link.rb +++ b/decidim-core/app/models/decidim/navbar_link.rb @@ -3,7 +3,7 @@ class NavbarLink < ApplicationRecord belongs_to :organization, foreign_key: "decidim_organization_id", class_name: "Decidim::Organization" - validate :validate_link_regex + before_save :validate_link_regex def validate_link_regex link = URI.parse(self.link) diff --git a/decidim-proposals/config/locales/fr.yml b/decidim-proposals/config/locales/fr.yml index 3d0d71940c2f0..d5b51883f878c 100644 --- a/decidim-proposals/config/locales/fr.yml +++ b/decidim-proposals/config/locales/fr.yml @@ -116,7 +116,7 @@ fr: all: Toutes category: Catégorie category_prompt: Sélectionner une catégorie - citizens: Propositions citoyennes + citizens: Citoyens >> Propositions citoyennes evaluating: En cours d'évaluation official: Propositions officielles origin: Auteurs From 7b285324b8c2aec9e01f9c5be6d1406c13d19ffe Mon Sep 17 00:00:00 2001 From: Ludivine Date: Tue, 23 Jan 2018 16:13:25 +0100 Subject: [PATCH 07/17] fix recipient_ids in create comment --- .../app/commands/decidim/comments/create_comment.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decidim-comments/app/commands/decidim/comments/create_comment.rb b/decidim-comments/app/commands/decidim/comments/create_comment.rb index adda1a275e919..5f282a37be9c5 100644 --- a/decidim-comments/app/commands/decidim/comments/create_comment.rb +++ b/decidim-comments/app/commands/decidim/comments/create_comment.rb @@ -48,7 +48,7 @@ def send_notification_to_moderators event: "decidim.events.comments.comment_created", event_class: Decidim::Comments::CommentCreatedEvent, resource: @comment.root_commentable, - recipient_ids: recipient_ids.uniq, + recipient_ids: (@commentable.users_to_notify_on_comment_created - [@author]).pluck(:id), extra: { comment_id: @comment.id, moderation_event: @comment.moderation.upstream_activated? ? true : false, From a2a4eb1f5f10ee0a3d7ec6788830bfa12c342633 Mon Sep 17 00:00:00 2001 From: Ludivine Date: Fri, 26 Jan 2018 08:17:02 +0100 Subject: [PATCH 08/17] change upstream moderation request --- .../app/controllers/decidim/admin/moderations_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decidim-admin/app/controllers/decidim/admin/moderations_controller.rb b/decidim-admin/app/controllers/decidim/admin/moderations_controller.rb index fe5c38bb64eed..a7f41681ef16d 100644 --- a/decidim-admin/app/controllers/decidim/admin/moderations_controller.rb +++ b/decidim-admin/app/controllers/decidim/admin/moderations_controller.rb @@ -77,7 +77,7 @@ def downstream_moderations def upstream_moderations @upstream_moderations ||= begin if params[:moderated] && params[:moderation_type] == "upstream" - filtered_upstream_moderations.where.not(upstream_moderation: "unmoderate").order("created_at").reverse + filtered_upstream_moderations.where("upstream_moderation = ? OR upstream_moderation = ?", "refused", "authorized").order("created_at").reverse elsif params[:moderation_type] == "upstream" filtered_upstream_moderations.where(upstream_moderation: "unmoderate").order("created_at").reverse end From e324ab51a36058969f78e4d6ee0a0eb6d4ec143b Mon Sep 17 00:00:00 2001 From: Ludivine Coipel Date: Thu, 1 Feb 2018 12:02:08 +0100 Subject: [PATCH 09/17] Add extends files for moderation methods --- .../decidim/comments/create_comment.rb | 14 ++--- .../config/initializers/extends.rb | 3 ++ .../lib/decidim/comments/commentable.rb | 20 +------ .../lib/decidim/extends/commentable_extend.rb | 31 +++++++++++ .../decidim/extends/create_comment_extend.rb | 31 +++++++++++ .../decidim/extends/create_proposal_extend.rb | 30 +++++++++++ .../mailers/decidim/notification_mailer.rb | 45 ---------------- decidim-core/config/initializers/extends.rb | 3 ++ .../email_notification_generator_extend.rb | 37 +++++++++++++ .../extends/notification_mailer_extend.rb | 53 +++++++++++++++++++ .../decidim/proposals/create_proposal.rb | 15 ------ 11 files changed, 192 insertions(+), 90 deletions(-) create mode 100644 decidim-comments/config/initializers/extends.rb create mode 100644 decidim-comments/lib/decidim/extends/commentable_extend.rb create mode 100644 decidim-comments/lib/decidim/extends/create_comment_extend.rb create mode 100644 decidim-comments/lib/decidim/extends/create_proposal_extend.rb create mode 100644 decidim-core/config/initializers/extends.rb create mode 100644 decidim-core/lib/decidim/extends/email_notification_generator_extend.rb create mode 100644 decidim-core/lib/decidim/extends/notification_mailer_extend.rb diff --git a/decidim-comments/app/commands/decidim/comments/create_comment.rb b/decidim-comments/app/commands/decidim/comments/create_comment.rb index 5f282a37be9c5..166f77925f45f 100644 --- a/decidim-comments/app/commands/decidim/comments/create_comment.rb +++ b/decidim-comments/app/commands/decidim/comments/create_comment.rb @@ -24,7 +24,7 @@ def call transaction do create_comment - send_notification_to_moderators + send_notification end broadcast(:ok, @comment) @@ -43,17 +43,14 @@ def create_comment decidim_user_group_id: form.user_group_id) end - def send_notification_to_moderators + def send_notification Decidim::EventsManager.publish( event: "decidim.events.comments.comment_created", event_class: Decidim::Comments::CommentCreatedEvent, resource: @comment.root_commentable, recipient_ids: (@commentable.users_to_notify_on_comment_created - [@author]).pluck(:id), extra: { - comment_id: @comment.id, - moderation_event: @comment.moderation.upstream_activated? ? true : false, - new_content: true, - process_slug: @comment.root_commentable.feature.participatory_space.slug + comment_id: @comment.id } ) end @@ -62,11 +59,6 @@ def root_commentable(commentable) return commentable.root_commentable if commentable.is_a? Decidim::Comments::Comment commentable end - - def create_moderation - participatory_space_id = @comment.root_commentable.feature.participatory_space_id - Decidim::Moderation.create(decidim_participatory_space_id: participatory_space_id, decidim_reportable_type: @comment.class.name, decidim_reportable_id: @comment.id, decidim_participatory_space_type: "Decidim::ParticipatoryProcess") - end end end end diff --git a/decidim-comments/config/initializers/extends.rb b/decidim-comments/config/initializers/extends.rb new file mode 100644 index 0000000000000..099f60b04823c --- /dev/null +++ b/decidim-comments/config/initializers/extends.rb @@ -0,0 +1,3 @@ +require 'decidim/extends/create_comment_extend.rb' +require 'decidim/extends/create_proposal_extend.rb' +require 'decidim/extends/commentable_extend.rb' \ No newline at end of file diff --git a/decidim-comments/lib/decidim/comments/commentable.rb b/decidim-comments/lib/decidim/comments/commentable.rb index d08c2f5092950..a2d562bf0e724 100644 --- a/decidim-comments/lib/decidim/comments/commentable.rb +++ b/decidim-comments/lib/decidim/comments/commentable.rb @@ -38,29 +38,11 @@ def commentable_type self.class.name end - # Public: Defines which users will receive a notification when a comment is created. + # Public: Defines which users will receive a notification when a comment is created. # This method can be overridden at each resource model to include or exclude # other users, eg. admins. # Returns: a relation of Decidim::User objects. def users_to_notify_on_comment_created - get_all_users_with_role - end - - def get_all_users_with_role - participatory_process = feature.participatory_space - admins = feature.organization.admins - users_with_role = feature.organization.users_with_any_role - process_users_with_role = get_user_with_process_role(participatory_process.id) - users = admins + users_with_role + process_users_with_role - users.uniq - end - - def get_user_with_process_role(participatory_process_id) - Decidim::ParticipatoryProcessUserRole.where(decidim_participatory_process_id: participatory_process_id).map(&:user) - end - - # Public: Defines which users will receive a notification when a comment is authorized. - def users_to_notify_on_comment_authorized Decidim::User.none end end diff --git a/decidim-comments/lib/decidim/extends/commentable_extend.rb b/decidim-comments/lib/decidim/extends/commentable_extend.rb new file mode 100644 index 0000000000000..36a37e2005e7e --- /dev/null +++ b/decidim-comments/lib/decidim/extends/commentable_extend.rb @@ -0,0 +1,31 @@ +module CommentableExtend + def self.included(base) + base.send(:include, CommentableCustom) + end + + module CommentableCustom + def users_to_notify_on_comment_created + get_all_users_with_role + end + + def get_all_users_with_role + participatory_process = feature.participatory_space + admins = feature.organization.admins + users_with_role = feature.organization.users_with_any_role + process_users_with_role = get_user_with_process_role(participatory_process.id) + users = admins + users_with_role + process_users_with_role + users.uniq + end + + def get_user_with_process_role(participatory_process_id) + Decidim::ParticipatoryProcessUserRole.where(decidim_participatory_process_id: participatory_process_id).map(&:user) + end + + # Public: Defines which users will receive a notification when a comment is authorized. + def users_to_notify_on_comment_authorized + Decidim::User.none + end + end +end + +Decidim::Comments::Commentable.send(:include, CommentableExtend) diff --git a/decidim-comments/lib/decidim/extends/create_comment_extend.rb b/decidim-comments/lib/decidim/extends/create_comment_extend.rb new file mode 100644 index 0000000000000..c6346170e2ed8 --- /dev/null +++ b/decidim-comments/lib/decidim/extends/create_comment_extend.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true +# This module is use to add customs methods to the original "create_comment.rb" + +module CreateCommentExtend + def self.included(base) + base.send(:include, CommentsNotifications) + + base.class_eval do + alias_method :send_notification, :send_notification_to_moderators + end + end + + module CommentsNotifications + def send_notification_to_moderators + Decidim::EventsManager.publish( + event: "decidim.events.comments.comment_created", + event_class: Decidim::Comments::CommentCreatedEvent, + resource: @comment.root_commentable, + recipient_ids: (@commentable.users_to_notify_on_comment_created - [@author]).pluck(:id), + extra: { + comment_id: @comment.id, + moderation_event: @comment.moderation.upstream_activated? ? true : false, + new_content: true, + process_slug: @comment.root_commentable.feature.participatory_space.slug + } + ) + end + end +end + +Decidim::Comments::CreateComment.send(:include, CreateCommentExtend) diff --git a/decidim-comments/lib/decidim/extends/create_proposal_extend.rb b/decidim-comments/lib/decidim/extends/create_proposal_extend.rb new file mode 100644 index 0000000000000..8b0be686741ce --- /dev/null +++ b/decidim-comments/lib/decidim/extends/create_proposal_extend.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true +# This module is use to add customs methods to the original "create_proposal.rb" + +module CreateProposalExtend + def self.included(base) + base.send(:include, ProposalsNotifications) + + base.class_eval do + alias_method :send_notification, :send_notification_to_moderators + end + end + + module ProposalsNotifications + def send_notification_to_moderators + Decidim::EventsManager.publish( + event: "decidim.events.proposals.proposal_created", + event_class: Decidim::Proposals::ProposalCreatedEvent, + resource: @proposal, + recipient_ids: (@proposal.users_to_notify_on_proposal_created - [@proposal.author]).pluck(:id), + extra: { + moderation_event: @proposal.moderation.upstream_activated? ? true : false, + new_content: true, + process_slug: @proposal.feature.participatory_space.slug + } + ) + end + end +end + +Decidim::Proposals::CreateProposal.send(:include, CreateProposalExtend) diff --git a/decidim-core/app/mailers/decidim/notification_mailer.rb b/decidim-core/app/mailers/decidim/notification_mailer.rb index 2b8e9e5a69577..733eb146a8d9a 100644 --- a/decidim-core/app/mailers/decidim/notification_mailer.rb +++ b/decidim-core/app/mailers/decidim/notification_mailer.rb @@ -17,50 +17,5 @@ def event_received(event, event_class_name, resource, user, extra) mail(to: user.email, subject: subject) end end - - def new_content_received(event, event_class_name, resource, user, extra) - @moderation = extra[:moderation_event] - with_user(user) do - @organization = resource.organization - event_class = event_class_name.constantize - @event_instance = event_class.new(resource: resource, event_name: event, user: user, extra: extra) - @slug = extra[:process_slug] - @locale = locale.to_s - subject = @moderation ? @event_instance.email_moderation_subject : @event_instance.email_subject - @parent_title = parent_title(resource, event_class) - @resource_title = resource.title if is_proposal?(event_class) - @body = body(event_class, resource, extra) - object = is_proposal?(event_class) ? "proposal" : "comment" - @moderation_url = moderation_url - @is_proposal = is_proposal?(event_class) - mail(to: user.email, subject: subject) - end - end - - private - - def is_proposal?(event_class) - event_class == Decidim::Proposals::ProposalCreatedEvent - end - - def parent_title(resource, event_class) - if is_proposal?(event_class) - resource.feature.participatory_space.title - else - resource.title - end - end - - def body(event_class, resource, extra) - if is_proposal?(event_class) - resource.body - else - Decidim::Comments::Comment.find(extra[:comment_id]).body - end - end - - def moderation_url - "http://" + @organization.host + "/admin/participatory_processes/" + @slug + "/moderations?locale=" + @locale + "&moderation_type=upstream" - end end end diff --git a/decidim-core/config/initializers/extends.rb b/decidim-core/config/initializers/extends.rb new file mode 100644 index 0000000000000..7e53495bae379 --- /dev/null +++ b/decidim-core/config/initializers/extends.rb @@ -0,0 +1,3 @@ +require "decidim/extends/notification_mailer_extend.rb" +require "decidim/extends/email_notification_generator_extend.rb" +require "decidim/extends/commentable_extend.rb" \ No newline at end of file diff --git a/decidim-core/lib/decidim/extends/email_notification_generator_extend.rb b/decidim-core/lib/decidim/extends/email_notification_generator_extend.rb new file mode 100644 index 0000000000000..6a3d6ae8fed91 --- /dev/null +++ b/decidim-core/lib/decidim/extends/email_notification_generator_extend.rb @@ -0,0 +1,37 @@ +module EmailNotificationGeneratorExtend + def self.included(base) + base.send(:include, CustomEmailGenerator) + end + + module CustomEmailGenerator + def send_email_to(recipient_id) + recipient = Decidim::User.where(id: recipient_id).first + return unless recipient + return unless recipient.email_on_notification? + + if @extra[:new_content] + NotificationMailer + .new_content_received( + event, + event_class.name, + resource, + recipient, + extra + ) + .deliver_later + else + NotificationMailer + .event_received( + event, + event_class.name, + resource, + recipient, + extra + ) + .deliver_later + end + end + end +end + +Decidim::EmailNotificationGenerator.send(:include, EmailNotificationGeneratorExtend) diff --git a/decidim-core/lib/decidim/extends/notification_mailer_extend.rb b/decidim-core/lib/decidim/extends/notification_mailer_extend.rb new file mode 100644 index 0000000000000..bf92f53a634ca --- /dev/null +++ b/decidim-core/lib/decidim/extends/notification_mailer_extend.rb @@ -0,0 +1,53 @@ +module NotificationMailerExtend + def self.included(base) + base.send(:include, ModerationsNotifications) + end + + module ModerationsNotifications + def new_content_received(event, event_class_name, resource, user, extra) + @moderation = extra[:moderation_event] + with_user(user) do + @organization = resource.organization + event_class = event_class_name.constantize + @event_instance = event_class.new(resource: resource, event_name: event, user: user, extra: extra) + @slug = extra[:process_slug] + @locale = locale.to_s + subject = @moderation ? @event_instance.email_moderation_subject : @event_instance.email_subject + @parent_title = parent_title(resource, event_class) + @resource_title = resource.try(:title) + @body = body(event_class, resource, extra) + @moderation_url = moderation_url + @is_comment = is_comment?(event_class) + mail(to: user.email, subject: subject) + end + end + + private + + def is_comment?(event_class) + event_class == Decidim::Comments::CommentCreatedEvent + end + + def parent_title(resource, event_class) + if is_comment?(event_class) + resource.title + else + resource.feature.participatory_space.title + end + end + + def body(event_class, resource, extra) + if is_comment?(event_class) + Decidim::Comments::Comment.find(extra[:comment_id]).body + else + resource.body + end + end + + def moderation_url + "http://" + @organization.host + "/admin/participatory_processes/" + @slug + "/moderations?locale=" + @locale + "&moderation_type=upstream" + end + end +end + +Decidim::NotificationMailer.send(:include, NotificationMailerExtend) diff --git a/decidim-proposals/app/commands/decidim/proposals/create_proposal.rb b/decidim-proposals/app/commands/decidim/proposals/create_proposal.rb index 56b3227bb8237..d428e874b6280 100644 --- a/decidim-proposals/app/commands/decidim/proposals/create_proposal.rb +++ b/decidim-proposals/app/commands/decidim/proposals/create_proposal.rb @@ -38,7 +38,6 @@ def call send_notification end - send_notification_to_moderators broadcast(:ok, proposal) end @@ -72,20 +71,6 @@ def create_proposal ) end - def send_notification_to_moderators - Decidim::EventsManager.publish( - event: "decidim.events.proposals.proposal_created", - event_class: Decidim::Proposals::ProposalCreatedEvent, - resource: @proposal, - recipient_ids: (@proposal.users_to_notify_on_proposal_created - [@proposal.author]).pluck(:id), - extra: { - moderation_event: @proposal.moderation.upstream_activated? ? true : false, - new_content: true, - process_slug: @proposal.feature.participatory_space.slug - } - ) - end - def build_attachment @attachment = Attachment.new( title: form.attachment.title, From d1864768151ee4c76344163bae2bf1011d0a469e Mon Sep 17 00:00:00 2001 From: Ludivine Coipel Date: Thu, 1 Feb 2018 17:25:35 +0100 Subject: [PATCH 10/17] Add extend files --- .../comments/comment_authorized_event.rb | 62 +++++++++++++++ .../decidim/comments/comment_created_event.rb | 30 ------- .../app/models/decidim/comments/comment.rb | 32 -------- .../decidim/comments/sorted_comments.rb | 37 ++------- .../config/initializers/extends.rb | 5 +- decidim-comments/config/locales/en.yml | 23 ++++-- decidim-comments/config/locales/fr.yml | 22 ++++-- .../lib/decidim/comments/commentable.rb | 22 ++++++ .../extends/comment_created_event_extend.rb | 35 ++++++++ .../lib/decidim/extends/comment_extend.rb | 34 ++++++++ .../lib/decidim/extends/commentable_extend.rb | 41 +++++----- .../decidim/extends/create_comment_extend.rb | 40 ++++------ .../decidim/extends/create_proposal_extend.rb | 38 ++++----- .../decidim/extends/sorted_comments_extend.rb | 54 +++++++++++++ .../mailers/decidim/notification_mailer.rb | 2 + decidim-core/app/models/decidim/moderation.rb | 24 ------ .../decidim/email_notification_generator.rb | 31 +++----- decidim-core/config/initializers/extends.rb | 2 +- .../email_notification_generator_extend.rb | 59 +++++++------- .../lib/decidim/extends/moderation_extend.rb | 30 +++++++ .../extends/notification_mailer_extend.rb | 79 ++++++++----------- decidim-core/lib/decidim/reportable.rb | 2 +- .../decidim/proposals/proposals_controller.rb | 15 +--- .../app/models/decidim/proposals/proposal.rb | 33 ++------ .../config/initializers/extends.rb | 2 + .../lib/decidim/extends/proposal_extend.rb | 35 ++++++++ .../extends/proposals_controller_extend.rb | 27 +++++++ 27 files changed, 470 insertions(+), 346 deletions(-) create mode 100644 decidim-comments/app/events/decidim/comments/comment_authorized_event.rb create mode 100644 decidim-comments/lib/decidim/extends/comment_created_event_extend.rb create mode 100644 decidim-comments/lib/decidim/extends/comment_extend.rb create mode 100644 decidim-comments/lib/decidim/extends/sorted_comments_extend.rb create mode 100644 decidim-core/lib/decidim/extends/moderation_extend.rb create mode 100644 decidim-proposals/config/initializers/extends.rb create mode 100644 decidim-proposals/lib/decidim/extends/proposal_extend.rb create mode 100644 decidim-proposals/lib/decidim/extends/proposals_controller_extend.rb diff --git a/decidim-comments/app/events/decidim/comments/comment_authorized_event.rb b/decidim-comments/app/events/decidim/comments/comment_authorized_event.rb new file mode 100644 index 0000000000000..dbf96a3dfa47b --- /dev/null +++ b/decidim-comments/app/events/decidim/comments/comment_authorized_event.rb @@ -0,0 +1,62 @@ +# frozen-string_literal: true + +module Decidim + module Comments + class CommentAuthorizedEvent < Decidim::Events::BaseEvent + include Decidim::Events::EmailEvent + include Decidim::Events::NotificationEvent + + def email_subject + I18n.t( + "decidim.comments.events.comment_created.#{comment_type}.email_subject", + resource_title: resource_title, + resource_url: resource_locator.url(url_params), + author_name: author.name + ) + end + + def email_intro + I18n.t( + "decidim.comments.events.comment_created.#{comment_type}.email_intro", + resource_title: resource_title + ).html_safe + end + + def email_outro + I18n.t( + "decidim.comments.events.comment_created.#{comment_type}.email_outro", + resource_title: resource_title + ) + end + + def notification_title + I18n.t( + "decidim.comments.events.comment_created.#{comment_type}.notification_title", + resource_title: resource_title, + resource_path: resource_locator.path(url_params), + author_nickname: author.nickname, + author_name: author.name, + author_path: author.profile_path + ).html_safe + end + + private + + def author + @author ||= Decidim::UserPresenter.new(comment.author) + end + + def comment + @comment ||= Decidim::Comments::Comment.find(extra[:comment_id]) + end + + def comment_type + comment.depth.zero? ? :comment : :reply + end + + def url_params + comment_type == :comment ? {} : { anchor: "comment_#{comment.id}" } + end + end + end +end diff --git a/decidim-comments/app/events/decidim/comments/comment_created_event.rb b/decidim-comments/app/events/decidim/comments/comment_created_event.rb index 06411f00ec9b8..bd358be8b3e27 100644 --- a/decidim-comments/app/events/decidim/comments/comment_created_event.rb +++ b/decidim-comments/app/events/decidim/comments/comment_created_event.rb @@ -29,13 +29,6 @@ def email_outro ) end - def email_url - I18n.t( - "decidim.comments.events.comment_created.#{comment_type}.url", - resource_url: resource_locator.url(url_params) - ).html_safe - end - def notification_title I18n.t( "decidim.comments.events.comment_created.#{comment_type}.notification_title", @@ -47,29 +40,6 @@ def notification_title ).html_safe end - def email_moderation_intro - I18n.t( - "decidim.comments.events.comment_created.#{comment_type}.moderation.email_intro", - resource_title: resource_title, - author_name: comment.author.name - ).html_safe - end - - def email_moderation_subject - I18n.t( - "decidim.comments.events.comment_created.#{comment_type}.moderation.email_subject", - resource_title: resource_title, - author_name: comment.author.name - ).html_safe - end - - def email_moderation_url(moderation_url) - I18n.t( - "decidim.comments.events.comment_created.#{comment_type}.moderation.moderation_url", - moderation_url: moderation_url - ).html_safe - end - private def author diff --git a/decidim-comments/app/models/decidim/comments/comment.rb b/decidim-comments/app/models/decidim/comments/comment.rb index 22440a8ee78e5..aaa167089e109 100644 --- a/decidim-comments/app/models/decidim/comments/comment.rb +++ b/decidim-comments/app/models/decidim/comments/comment.rb @@ -33,9 +33,6 @@ class Comment < ApplicationRecord validate :commentable_can_have_comments before_save :compute_depth - after_create :create_comment_moderation - after_create :update_moderation - delegate :organization, :feature, to: :commentable @@ -45,9 +42,6 @@ def accepts_new_comments? depth < MAX_DEPTH end - # Public: Override Commentable concern method `users_to_notify_on_comment_created` - delegate :users_to_notify_on_comment_created, to: :root_commentable - # Public: Check if the user has upvoted the comment # # Returns a bool value to indicate if the condition is truthy or not @@ -67,32 +61,6 @@ def reported_content_url ResourceLocatorPresenter.new(root_commentable).url(anchor: "comment_#{id}") end - def send_notification - Decidim::EventsManager.publish( - event: "decidim.events.comments.comment_created", - event_class: Decidim::Comments::CommentCreatedEvent, - resource: self.root_commentable, - recipient_ids: (self.root_commentable.users_to_notify_on_comment_authorized - [author]).pluck(:id), - extra: { - comment_id: self.id - } - ) - end - - private - - def create_comment_moderation - participatory_space = self.root_commentable.feature.participatory_space - self.create_moderation!(participatory_space: participatory_space) - end - - def update_moderation - unless moderation.upstream_activated? - moderation.authorize! - end - end - - # Private: Check if commentable can have comments and if not adds # a validation error to the model def commentable_can_have_comments diff --git a/decidim-comments/app/queries/decidim/comments/sorted_comments.rb b/decidim-comments/app/queries/decidim/comments/sorted_comments.rb index d992bec52893f..dbc8185454fb2 100644 --- a/decidim-comments/app/queries/decidim/comments/sorted_comments.rb +++ b/decidim-comments/app/queries/decidim/comments/sorted_comments.rb @@ -29,8 +29,11 @@ def initialize(commentable, options = {}) # Finds the Comments for a resource that can have comments and eager # loads comments replies. It uses Comment's MAX_DEPTH to load a maximum # level of nested replies. - def query - scope = filter_comments + def query # this method has been extended + scope = Comment + .where(commentable: commentable) + .not_hidden + .includes(:author, :up_votes, :down_votes) scope = case @options[:order_by] when "older" @@ -50,36 +53,6 @@ def query private - def filter_comments - if admin_or_moderator? - Comment - .where(commentable: commentable) - .not_hidden - .includes(:author, :up_votes, :down_votes) - else - Comment - .where(commentable: commentable) - .authorized - .not_hidden - .includes(:author, :up_votes, :down_votes) - end - end - - def current_user - Thread.current[:current_user] - end - - def admin_or_moderator? - current_user && - (current_user.admin? || - @commentable.feature.organization.users_with_any_role.include?(current_user) || get_user_with_process_role(@commentable.feature.participatory_space.id).include?(current_user) - ) - end - - def get_user_with_process_role(participatory_process_id) - Decidim::ParticipatoryProcessUserRole.where(decidim_participatory_process_id: participatory_process_id).map(&:user) - end - def order_by_older(scope) scope.order(created_at: :asc) end diff --git a/decidim-comments/config/initializers/extends.rb b/decidim-comments/config/initializers/extends.rb index 099f60b04823c..fbcfcbe481e3d 100644 --- a/decidim-comments/config/initializers/extends.rb +++ b/decidim-comments/config/initializers/extends.rb @@ -1,3 +1,6 @@ require 'decidim/extends/create_comment_extend.rb' require 'decidim/extends/create_proposal_extend.rb' -require 'decidim/extends/commentable_extend.rb' \ No newline at end of file +require 'decidim/extends/commentable_extend.rb' +require 'decidim/extends/comment_extend.rb' +require 'decidim/extends/comment_created_event_extend.rb' +require 'decidim/extends/sorted_comments_extend.rb' \ No newline at end of file diff --git a/decidim-comments/config/locales/en.yml b/decidim-comments/config/locales/en.yml index 355ba69908606..4a7778a53c615 100644 --- a/decidim-comments/config/locales/en.yml +++ b/decidim-comments/config/locales/en.yml @@ -10,20 +10,12 @@ en: email_subject: There is a new comment from %{author_name} in %{resource_title} notification_title: There is a new comment from %{author_name} %{author_nickname} in %{resource_title} url: "Comment's url : %{resource_url}" - moderation: - email_intro: '%{author_name} published a comment on' - email_subject: "New comment from %{author_name} waiting for moderation" - moderation_url: "You can moderate it here : %{moderation_url}" reply: email_intro: The "%{resource_title}" has been commented email_outro: You have received this notification because you are following "%{resource_title}" or its author. You can unfollow it from the previous link. email_subject: There is a new reply of your comment from %{author_name} in %{resource_title} notification_title: There is a new reply of your comment from %{author_name} %{author_nickname} in %{resource_title} url: "Comment's url : %{resource_url}" - moderation: - email_intro: '%{author_name} published a comment on' - email_subject: 'New comment from %{author_name} waiting for moderation' - moderation_url: "You can moderate it here : %{moderation_url}" votes: create: error: There's been errors when voting the comment. @@ -78,3 +70,18 @@ en: errors: messages: cannot_have_comments: can't have comments +# moderation + comments: + events: + comment_created: + comment: + moderation: + email_intro: '%{author_name} published a comment on' + email_subject: "New comment from %{author_name} waiting for moderation" + moderation_url: "You can moderate it here : %{moderation_url}" + reply: + moderation: + email_intro: '%{author_name} published a comment on' + email_subject: 'New comment from %{author_name} waiting for moderation' + moderation_url: "You can moderate it here : %{moderation_url}" + diff --git a/decidim-comments/config/locales/fr.yml b/decidim-comments/config/locales/fr.yml index 035fc77ed9963..db8e5633231e0 100644 --- a/decidim-comments/config/locales/fr.yml +++ b/decidim-comments/config/locales/fr.yml @@ -9,20 +9,12 @@ fr: email_subject: Un nouveau commentaire a été publié par %{author_name} dans %{resource_title} notification_title: Il y a un nouveau commentaire de %{author_name} %{author_nickname} dans %{resource_title} url: "Url du commentaire : %{resource_url}" - moderation: - email_intro: "%{author_name} a publié un nouveau commentaire sur" - email_subject: 'Nouveau commentaire de %{author_name} en attente de modération' - moderation_url: "Vous pouvez modérer le contenu en suivant ce lien : %{moderation_url}" reply: email_intro: '"%{resource_title}" a reçu un commentaire' email_outro: Vous recevez cet notification car vous suivez « %{resource_title} ». Vous pouvez cesser de le suivre à partir du lien précédent. email_subject: Votre commentaire a reçu une réponse de %{author_name} dans %{resource_title} notification_title: Il y a une nouvelle réponse à votre commentaire de %{author_name} %{author_nickname} dans %{resource_title} url: "Url du commentaire : %{resource_url}" - moderation: - email_intro: "%{author_name} a publié un nouveau commentaire sur" - email_subject: 'Nouveau commentaire de %{author_name} en attente de modération' - moderation_url: Vous pouvez modérer le contenu en suivant ce lien %{moderation_url} votes: create: error: Une erreur s'est produite lors du vote sur le commentaire. @@ -77,3 +69,17 @@ fr: errors: messages: cannot_have_comments: ne peut pas être commenté +# moderation + comments: + events: + comment_created: + comment: + moderation: + email_intro: "%{author_name} a publié un nouveau commentaire sur" + email_subject: 'Nouveau commentaire de %{author_name} en attente de modération' + moderation_url: "Vous pouvez modérer le contenu en suivant ce lien : %{moderation_url}" + reply: + moderation: + email_intro: "%{author_name} a publié un nouveau commentaire sur" + email_subject: 'Nouveau commentaire de %{author_name} en attente de modération' + moderation_url: Vous pouvez modérer le contenu en suivant ce lien %{moderation_url} diff --git a/decidim-comments/lib/decidim/comments/commentable.rb b/decidim-comments/lib/decidim/comments/commentable.rb index a2d562bf0e724..2df10a2e79a08 100644 --- a/decidim-comments/lib/decidim/comments/commentable.rb +++ b/decidim-comments/lib/decidim/comments/commentable.rb @@ -42,7 +42,29 @@ def commentable_type # This method can be overridden at each resource model to include or exclude # other users, eg. admins. # Returns: a relation of Decidim::User objects. + # def users_to_notify_on_comment_created + # Decidim::User.none + # end + def users_to_notify_on_comment_created + get_all_users_with_role + end + + def get_all_users_with_role + participatory_process = feature.participatory_space + admins = feature.organization.admins + users_with_role = feature.organization.users_with_any_role + process_users_with_role = get_user_with_process_role(participatory_process.id) + users = admins + users_with_role + process_users_with_role + users.uniq + end + + def get_user_with_process_role(participatory_process_id) + Decidim::ParticipatoryProcessUserRole.where(decidim_participatory_process_id: participatory_process_id).map(&:user) + end + + # Public: Defines which users will receive a notification when a comment is authorized. + def users_to_notify_on_comment_authorized Decidim::User.none end end diff --git a/decidim-comments/lib/decidim/extends/comment_created_event_extend.rb b/decidim-comments/lib/decidim/extends/comment_created_event_extend.rb new file mode 100644 index 0000000000000..76075f1685cf7 --- /dev/null +++ b/decidim-comments/lib/decidim/extends/comment_created_event_extend.rb @@ -0,0 +1,35 @@ +# frozen-string_literal: true +module CommentCreatedEventExtend + module Decidim + module Comments + class CommentCreatedEvent < Decidim::Events::BaseEvent + def email_moderation_intro + I18n.t( + "decidim.comments.events.comment_created.#{comment_type}.moderation.email_intro", + resource_title: resource_title, + author_name: comment.author.name + ).html_safe + end + + def email_moderation_subject + I18n.t( + "decidim.comments.events.comment_created.#{comment_type}.moderation.email_subject", + resource_title: resource_title, + author_name: comment.author.name + ).html_safe + end + + def email_moderation_url(moderation_url) + I18n.t( + "decidim.comments.events.comment_created.#{comment_type}.moderation.moderation_url", + moderation_url: moderation_url + ).html_safe + end + end + end +end + +Decidim::Comments::CommentCreatedEvent.class_eval do + prepend(CommentCreatedEventExtend) +end + diff --git a/decidim-comments/lib/decidim/extends/comment_extend.rb b/decidim-comments/lib/decidim/extends/comment_extend.rb new file mode 100644 index 0000000000000..f092d84dd5460 --- /dev/null +++ b/decidim-comments/lib/decidim/extends/comment_extend.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true +# This module is use to add customs methods to the original "comment.rb" + +module CommentExtend + def send_notification # to followers. This method is called in "authorize!" method + Decidim::EventsManager.publish( + event: "decidim.events.comments.comment_authorized", + event_class: Decidim::Comments::CommentAuthorizedEvent, + resource: self.root_commentable, + recipient_ids: (self.root_commentable.users_to_notify_on_comment_authorized - [author]).pluck(:id), + extra: { + comment_id: self.id + } + ) + end + + def create_comment_moderation + participatory_space = self.root_commentable.feature.participatory_space + self.create_moderation!(participatory_space: participatory_space) + end + + def update_moderation + unless moderation.upstream_activated? + moderation.authorize! + end + end +end + +Decidim::Comments::Comment.class_eval do + prepend(CommentExtend) + after_create :create_comment_moderation + after_create :update_moderation + delegate :users_to_notify_on_comment_created, to: :root_commentable +end diff --git a/decidim-comments/lib/decidim/extends/commentable_extend.rb b/decidim-comments/lib/decidim/extends/commentable_extend.rb index 36a37e2005e7e..c5239272e9df4 100644 --- a/decidim-comments/lib/decidim/extends/commentable_extend.rb +++ b/decidim-comments/lib/decidim/extends/commentable_extend.rb @@ -1,31 +1,26 @@ module CommentableExtend - def self.included(base) - base.send(:include, CommentableCustom) + def users_to_notify_on_comment_created + get_all_users_with_role + super end - module CommentableCustom - def users_to_notify_on_comment_created - get_all_users_with_role - end - - def get_all_users_with_role - participatory_process = feature.participatory_space - admins = feature.organization.admins - users_with_role = feature.organization.users_with_any_role - process_users_with_role = get_user_with_process_role(participatory_process.id) - users = admins + users_with_role + process_users_with_role - users.uniq - end + def get_all_users_with_role + participatory_process = feature.participatory_space + admins = feature.organization.admins + users_with_role = feature.organization.users_with_any_role + process_users_with_role = get_user_with_process_role(participatory_process.id) + users = admins + users_with_role + process_users_with_role + users.uniq + end - def get_user_with_process_role(participatory_process_id) - Decidim::ParticipatoryProcessUserRole.where(decidim_participatory_process_id: participatory_process_id).map(&:user) - end + def get_user_with_process_role(participatory_process_id) + Decidim::ParticipatoryProcessUserRole.where(decidim_participatory_process_id: participatory_process_id).map(&:user) + end - # Public: Defines which users will receive a notification when a comment is authorized. - def users_to_notify_on_comment_authorized - Decidim::User.none - end + # Public: Defines which users will receive a notification when a comment is authorized. + def users_to_notify_on_comment_authorized + Decidim::User.none end end -Decidim::Comments::Commentable.send(:include, CommentableExtend) +Decidim::Comments::Commentable.module_eval { include CommentableExtend } \ No newline at end of file diff --git a/decidim-comments/lib/decidim/extends/create_comment_extend.rb b/decidim-comments/lib/decidim/extends/create_comment_extend.rb index c6346170e2ed8..d8b7c3f4982d5 100644 --- a/decidim-comments/lib/decidim/extends/create_comment_extend.rb +++ b/decidim-comments/lib/decidim/extends/create_comment_extend.rb @@ -2,30 +2,22 @@ # This module is use to add customs methods to the original "create_comment.rb" module CreateCommentExtend - def self.included(base) - base.send(:include, CommentsNotifications) - - base.class_eval do - alias_method :send_notification, :send_notification_to_moderators - end - end - - module CommentsNotifications - def send_notification_to_moderators - Decidim::EventsManager.publish( - event: "decidim.events.comments.comment_created", - event_class: Decidim::Comments::CommentCreatedEvent, - resource: @comment.root_commentable, - recipient_ids: (@commentable.users_to_notify_on_comment_created - [@author]).pluck(:id), - extra: { - comment_id: @comment.id, - moderation_event: @comment.moderation.upstream_activated? ? true : false, - new_content: true, - process_slug: @comment.root_commentable.feature.participatory_space.slug - } - ) - end + def send_notification # to moderators. + Decidim::EventsManager.publish( + event: "decidim.events.comments.comment_created", + event_class: Decidim::Comments::CommentCreatedEvent, + resource: @comment.root_commentable, + recipient_ids: (@commentable.users_to_notify_on_comment_created - [@author]).pluck(:id), + extra: { + comment_id: @comment.id, + moderation_event: @comment.moderation.upstream_activated? ? true : false, + new_content: true, + process_slug: @comment.root_commentable.feature.participatory_space.slug + } + ) end end -Decidim::Comments::CreateComment.send(:include, CreateCommentExtend) +Decidim::Comments::CreateComment.class_eval do + prepend(CreateCommentExtend) +end diff --git a/decidim-comments/lib/decidim/extends/create_proposal_extend.rb b/decidim-comments/lib/decidim/extends/create_proposal_extend.rb index 8b0be686741ce..7498b981b1ce9 100644 --- a/decidim-comments/lib/decidim/extends/create_proposal_extend.rb +++ b/decidim-comments/lib/decidim/extends/create_proposal_extend.rb @@ -2,29 +2,21 @@ # This module is use to add customs methods to the original "create_proposal.rb" module CreateProposalExtend - def self.included(base) - base.send(:include, ProposalsNotifications) - - base.class_eval do - alias_method :send_notification, :send_notification_to_moderators - end - end - - module ProposalsNotifications - def send_notification_to_moderators - Decidim::EventsManager.publish( - event: "decidim.events.proposals.proposal_created", - event_class: Decidim::Proposals::ProposalCreatedEvent, - resource: @proposal, - recipient_ids: (@proposal.users_to_notify_on_proposal_created - [@proposal.author]).pluck(:id), - extra: { - moderation_event: @proposal.moderation.upstream_activated? ? true : false, - new_content: true, - process_slug: @proposal.feature.participatory_space.slug - } - ) - end + def send_notification #to moderators + Decidim::EventsManager.publish( + event: "decidim.events.proposals.proposal_created", + event_class: Decidim::Proposals::ProposalCreatedEvent, + resource: @proposal, + recipient_ids: (@proposal.users_to_notify_on_proposal_created - [@proposal.author]).pluck(:id), + extra: { + moderation_event: @proposal.moderation.upstream_activated? ? true : false, + new_content: true, + process_slug: @proposal.feature.participatory_space.slug + } + ) end end -Decidim::Proposals::CreateProposal.send(:include, CreateProposalExtend) +Decidim::Proposals::CreateProposal.class_eval do + prepend(CreateProposalExtend) +end diff --git a/decidim-comments/lib/decidim/extends/sorted_comments_extend.rb b/decidim-comments/lib/decidim/extends/sorted_comments_extend.rb new file mode 100644 index 0000000000000..c079bd28b94f4 --- /dev/null +++ b/decidim-comments/lib/decidim/extends/sorted_comments_extend.rb @@ -0,0 +1,54 @@ +module SortedCommentsExtend + def query + scope = filter_comments + + scope = case @options[:order_by] + when "older" + order_by_older(scope) + when "recent" + order_by_recent(scope) + when "best_rated" + order_by_best_rated(scope) + when "most_discussed" + order_by_most_discussed(scope) + else + order_by_older(scope) + end + + scope + end + def filter_comments + if admin_or_moderator? + Comment + .where(commentable: commentable) + .not_hidden + .includes(:author, :up_votes, :down_votes) + else + Comment + .where(commentable: commentable) + .authorized + .not_hidden + .includes(:author, :up_votes, :down_votes) + end + end + + def current_user + Thread.current[:current_user] + end + + def admin_or_moderator? + current_user && + (current_user.admin? || + @commentable.feature.organization.users_with_any_role.include?(current_user) || get_user_with_process_role(@commentable.feature.participatory_space.id).include?(current_user) + ) + end + + def get_user_with_process_role(participatory_process_id) + Decidim::ParticipatoryProcessUserRole.where(decidim_participatory_process_id: participatory_process_id).map(&:user) + end +end + +Decidim::Comments::SortedComments.class_eval do + prepend(SortedCommentsExtend) +end + diff --git a/decidim-core/app/mailers/decidim/notification_mailer.rb b/decidim-core/app/mailers/decidim/notification_mailer.rb index 733eb146a8d9a..2719b257f07ed 100644 --- a/decidim-core/app/mailers/decidim/notification_mailer.rb +++ b/decidim-core/app/mailers/decidim/notification_mailer.rb @@ -8,6 +8,8 @@ class NotificationMailer < Decidim::ApplicationMailer helper Decidim::TranslationsHelper def event_received(event, event_class_name, resource, user, extra) + binding.pry + with_user(user) do @organization = resource.organization event_class = event_class_name.constantize diff --git a/decidim-core/app/models/decidim/moderation.rb b/decidim-core/app/models/decidim/moderation.rb index 6a29ae9dcee23..7c68232c1d2db 100644 --- a/decidim-core/app/models/decidim/moderation.rb +++ b/decidim-core/app/models/decidim/moderation.rb @@ -9,29 +9,5 @@ class Moderation < ApplicationRecord delegate :feature, :organization, to: :reportable - def authorized? - upstream_moderation == "authorized" - end - - def unmoderated? - upstream_moderation == "unmoderate" - end - - def authorize! - update_attributes(upstream_moderation: "authorized") - reportable.send_notification if reportable.class.name.demodulize == "Comment" - end - - def refuse! - update_attributes(upstream_moderation: "refused") - end - - def upstream_activated? - if reportable.is_a?(Decidim::Proposals::Proposal) - reportable.feature.settings.upstream_moderation_enabled - else - reportable.root_commentable.feature.settings.comments_upstream_moderation_enabled - end - end end end diff --git a/decidim-core/app/services/decidim/email_notification_generator.rb b/decidim-core/app/services/decidim/email_notification_generator.rb index b29a5e22c363a..a9f48a7a4534e 100644 --- a/decidim-core/app/services/decidim/email_notification_generator.rb +++ b/decidim-core/app/services/decidim/email_notification_generator.rb @@ -48,28 +48,15 @@ def send_email_to(recipient_id) recipient = Decidim::User.where(id: recipient_id).first return unless recipient return unless recipient.email_on_notification? - - if @extra[:new_content] - NotificationMailer - .new_content_received( - event, - event_class.name, - resource, - recipient, - extra - ) - .deliver_later - else - NotificationMailer - .event_received( - event, - event_class.name, - resource, - recipient, - extra - ) - .deliver_later - end + NotificationMailer + .event_received( + event, + event_class.name, + resource, + recipient, + extra + ) + .deliver_later end end end diff --git a/decidim-core/config/initializers/extends.rb b/decidim-core/config/initializers/extends.rb index 7e53495bae379..e7f1bef72acc5 100644 --- a/decidim-core/config/initializers/extends.rb +++ b/decidim-core/config/initializers/extends.rb @@ -1,3 +1,3 @@ require "decidim/extends/notification_mailer_extend.rb" require "decidim/extends/email_notification_generator_extend.rb" -require "decidim/extends/commentable_extend.rb" \ No newline at end of file +require "decidim/extends/moderation_extend.rb" \ No newline at end of file diff --git a/decidim-core/lib/decidim/extends/email_notification_generator_extend.rb b/decidim-core/lib/decidim/extends/email_notification_generator_extend.rb index 6a3d6ae8fed91..f157637ef514d 100644 --- a/decidim-core/lib/decidim/extends/email_notification_generator_extend.rb +++ b/decidim-core/lib/decidim/extends/email_notification_generator_extend.rb @@ -1,37 +1,32 @@ module EmailNotificationGeneratorExtend - def self.included(base) - base.send(:include, CustomEmailGenerator) - end - - module CustomEmailGenerator - def send_email_to(recipient_id) - recipient = Decidim::User.where(id: recipient_id).first - return unless recipient - return unless recipient.email_on_notification? - - if @extra[:new_content] - NotificationMailer - .new_content_received( - event, - event_class.name, - resource, - recipient, - extra - ) - .deliver_later - else - NotificationMailer - .event_received( - event, - event_class.name, - resource, - recipient, - extra - ) - .deliver_later - end + def send_email_to(recipient_id) + recipient = Decidim::User.where(id: recipient_id).first + return unless recipient + return unless recipient.email_on_notification? + if @extra[:new_content] + Decidim::NotificationMailer + .new_content_received( + event, + event_class.name, + resource, + recipient, + extra + ) + .deliver_later + else + Decidim::NotificationMailer + .event_received( + event, + event_class.name, + resource, + recipient, + extra + ) + .deliver_later end end end -Decidim::EmailNotificationGenerator.send(:include, EmailNotificationGeneratorExtend) +Decidim::EmailNotificationGenerator.class_eval do + prepend(EmailNotificationGeneratorExtend) +end diff --git a/decidim-core/lib/decidim/extends/moderation_extend.rb b/decidim-core/lib/decidim/extends/moderation_extend.rb new file mode 100644 index 0000000000000..35cc609b91235 --- /dev/null +++ b/decidim-core/lib/decidim/extends/moderation_extend.rb @@ -0,0 +1,30 @@ +module ModerationExtend + def authorized? + upstream_moderation == "authorized" + end + + def unmoderated? + upstream_moderation == "unmoderate" + end + + def authorize! + update_attributes(upstream_moderation: "authorized") + reportable.send_notification if reportable.class.name.demodulize == "Comment" + end + + def refuse! + update_attributes(upstream_moderation: "refused") + end + + def upstream_activated? + if reportable.is_a?(Decidim::Proposals::Proposal) + reportable.feature.settings.upstream_moderation_enabled + else + reportable.root_commentable.feature.settings.comments_upstream_moderation_enabled + end + end +end + +Decidim::Moderation.class_eval do + prepend(ModerationExtend) +end diff --git a/decidim-core/lib/decidim/extends/notification_mailer_extend.rb b/decidim-core/lib/decidim/extends/notification_mailer_extend.rb index bf92f53a634ca..ac9948fd16015 100644 --- a/decidim-core/lib/decidim/extends/notification_mailer_extend.rb +++ b/decidim-core/lib/decidim/extends/notification_mailer_extend.rb @@ -1,53 +1,44 @@ -module NotificationMailerExtend - def self.included(base) - base.send(:include, ModerationsNotifications) - end - - module ModerationsNotifications - def new_content_received(event, event_class_name, resource, user, extra) - @moderation = extra[:moderation_event] - with_user(user) do - @organization = resource.organization - event_class = event_class_name.constantize - @event_instance = event_class.new(resource: resource, event_name: event, user: user, extra: extra) - @slug = extra[:process_slug] - @locale = locale.to_s - subject = @moderation ? @event_instance.email_moderation_subject : @event_instance.email_subject - @parent_title = parent_title(resource, event_class) - @resource_title = resource.try(:title) - @body = body(event_class, resource, extra) - @moderation_url = moderation_url - @is_comment = is_comment?(event_class) - mail(to: user.email, subject: subject) - end +Decidim::NotificationMailer.class_eval do + def new_content_received(event, event_class_name, resource, user, extra) + @moderation = extra[:moderation_event] + with_user(user) do + @organization = resource.organization + event_class = event_class_name.constantize + @event_instance = event_class.new(resource: resource, event_name: event, user: user, extra: extra) + @slug = extra[:process_slug] + @locale = locale.to_s + subject = @moderation ? @event_instance.email_moderation_subject : @event_instance.email_subject + @parent_title = parent_title(resource, event_class) + @resource_title = resource.try(:title) + @body = body(event_class, resource, extra) + @moderation_url = moderation_url + @is_comment = is_comment?(event_class) + mail(to: user.email, subject: subject) end + end - private - - def is_comment?(event_class) - event_class == Decidim::Comments::CommentCreatedEvent - end - def parent_title(resource, event_class) - if is_comment?(event_class) - resource.title - else - resource.feature.participatory_space.title - end - end + def is_comment?(event_class) + event_class == Decidim::Comments::CommentCreatedEvent + end - def body(event_class, resource, extra) - if is_comment?(event_class) - Decidim::Comments::Comment.find(extra[:comment_id]).body - else - resource.body - end + def parent_title(resource, event_class) + if is_comment?(event_class) + resource.title + else + resource.feature.participatory_space.title end + end - def moderation_url - "http://" + @organization.host + "/admin/participatory_processes/" + @slug + "/moderations?locale=" + @locale + "&moderation_type=upstream" + def body(event_class, resource, extra) + if is_comment?(event_class) + Decidim::Comments::Comment.find(extra[:comment_id]).body + else + resource.body end end -end -Decidim::NotificationMailer.send(:include, NotificationMailerExtend) + def moderation_url + "http://" + @organization.host + "/admin/participatory_processes/" + @slug + "/moderations?locale=" + @locale + "&moderation_type=upstream" + end +end \ No newline at end of file diff --git a/decidim-core/lib/decidim/reportable.rb b/decidim-core/lib/decidim/reportable.rb index 1beadc5c251cb..202665061b032 100644 --- a/decidim-core/lib/decidim/reportable.rb +++ b/decidim-core/lib/decidim/reportable.rb @@ -13,8 +13,8 @@ module Reportable scope :reported, -> { left_outer_joins(:moderation).where(Decidim::Moderation.arel_table[:report_count].gt(0)) } scope :hidden, -> { left_outer_joins(:moderation).where.not(Decidim::Moderation.arel_table[:hidden_at].eq nil) } - scope :authorized, -> { left_outer_joins(:moderation).where(Decidim::Moderation.arel_table[:upstream_moderation].eq "authorized") } scope :not_hidden, -> { left_outer_joins(:moderation).where(Decidim::Moderation.arel_table[:hidden_at].eq nil) } + scope :authorized, -> { left_outer_joins(:moderation).where(Decidim::Moderation.arel_table[:upstream_moderation].eq "authorized") } # Public: Check if the user has reported the reportable. diff --git a/decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb b/decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb index 83af9fa4b9654..0dc52796a8f01 100644 --- a/decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb +++ b/decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb @@ -13,11 +13,10 @@ class ProposalsController < Decidim::Proposals::ApplicationController helper_method :geocoded_proposals before_action :authenticate_user!, only: [:new, :create] - def index + def index # this method has been extended @proposals = search .results .not_hidden - .authorized .includes(:author) .includes(:category) .includes(:scope) @@ -96,18 +95,6 @@ def update private - def get_user_with_process_role(participatory_process_id) - Decidim::ParticipatoryProcessUserRole.where(decidim_participatory_process_id: participatory_process_id).map(&:user) - end - - - def admin_or_moderator? - current_user && (current_user.admin? || - current_organization.users_with_any_role.include?(current_user) || - get_user_with_process_role(current_participatory_process.id).include?(current_user) - ) - end - def geocoded_proposals @geocoded_proposals ||= search.results.not_hidden.select(&:geocoded?) end diff --git a/decidim-proposals/app/models/decidim/proposals/proposal.rb b/decidim-proposals/app/models/decidim/proposals/proposal.rb index 59abedf86988b..bcc7e27b65ea5 100644 --- a/decidim-proposals/app/models/decidim/proposals/proposal.rb +++ b/decidim-proposals/app/models/decidim/proposals/proposal.rb @@ -26,8 +26,6 @@ class Proposal < Proposals::ApplicationRecord scope :accepted, -> { where(state: "accepted") } scope :rejected, -> { where(state: "rejected") } scope :evaluating, -> { where(state: "evaluating") } - after_create :create_proposal_moderation - after_create :update_moderation def self.order_randomly(seed) transaction do @@ -91,21 +89,17 @@ def comments_have_votes? true end + # Public: Override Commentable concern method `users_to_notify_on_comment_authorized` + def users_to_notify_on_comment_created + return (followers | feature.participatory_space.admins).uniq if official? + followers + end + # Public: Overrides the `reported_content_url` Reportable concern method. def reported_content_url ResourceLocatorPresenter.new(self).url end - def users_to_notify_on_proposal_created - get_all_users_with_role - end - - - # Public: Override Commentable concern method `users_to_notify_on_comment_authorized` - def users_to_notify_on_comment_authorized - return (followers | feature.participatory_space.admins).uniq if official? - followers - end # Public: Whether the proposal is official or not. def official? @@ -166,21 +160,6 @@ def within_edit_time_limit? limit = created_at + feature.settings.proposal_edit_before_minutes.minutes Time.current < limit end - - def create_proposal_moderation - participatory_space = self.feature.participatory_space - self.create_moderation!(participatory_space: participatory_space) - end - - def update_moderation - unless moderation.upstream_activated? - moderation.authorize! - end - end - - def upstream_moderation_activated? - feature.settings.upstream_moderation_enabled - end end end end diff --git a/decidim-proposals/config/initializers/extends.rb b/decidim-proposals/config/initializers/extends.rb new file mode 100644 index 0000000000000..1faa0239d87f4 --- /dev/null +++ b/decidim-proposals/config/initializers/extends.rb @@ -0,0 +1,2 @@ +require "decidim/extends/proposal_extend.rb" +require "decidim/extends/proposals_controller_extend.rb" \ No newline at end of file diff --git a/decidim-proposals/lib/decidim/extends/proposal_extend.rb b/decidim-proposals/lib/decidim/extends/proposal_extend.rb new file mode 100644 index 0000000000000..36455c2b73362 --- /dev/null +++ b/decidim-proposals/lib/decidim/extends/proposal_extend.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true +# This module is use to add customs methods to the original "proposal.rb" + +module ProposalExtend + def users_to_notify_on_proposal_created + get_all_users_with_role + end + + def users_to_notify_on_comment_created + get_all_users_with_role + end + + # Public: Override Commentable concern method `users_to_notify_on_comment_authorized` + def users_to_notify_on_comment_authorized + return (followers | feature.participatory_space.admins).uniq if official? + followers + end + + def create_proposal_moderation + participatory_space = self.feature.participatory_space + self.create_moderation!(participatory_space: participatory_space) + end + + def update_moderation + unless moderation.upstream_activated? + moderation.authorize! + end + end +end + +Decidim::Proposals::Proposal.class_eval do + prepend(ProposalExtend) + after_create :create_proposal_moderation + after_create :update_moderation +end diff --git a/decidim-proposals/lib/decidim/extends/proposals_controller_extend.rb b/decidim-proposals/lib/decidim/extends/proposals_controller_extend.rb new file mode 100644 index 0000000000000..23fd6ed5e2917 --- /dev/null +++ b/decidim-proposals/lib/decidim/extends/proposals_controller_extend.rb @@ -0,0 +1,27 @@ +module ProposalsControllerExtend + def index + @proposals = search + .results + .not_hidden + .authorized + .includes(:author) + .includes(:category) + .includes(:scope) + + @voted_proposals = if current_user + ProposalVote.where( + author: current_user, + proposal: @proposals.pluck(:id) + ).pluck(:decidim_proposal_id) + else + [] + end + + @proposals = paginate(@proposals) + @proposals = reorder(@proposals) + end +end + +Decidim::Proposals::ProposalsController.class_eval do + prepend(ProposalsControllerExtend) +end From e6c6d5e7f083e9ad85f84065122be4d1e21cd8b4 Mon Sep 17 00:00:00 2001 From: Ludivine Coipel Date: Thu, 1 Feb 2018 17:37:37 +0100 Subject: [PATCH 11/17] Extend moderation controller --- .../decidim/admin/moderations_controller.rb | 86 ++++-------------- decidim-admin/config/initializers/extends.rb | 1 + .../extends/moderations_controller_extend.rb | 88 +++++++++++++++++++ 3 files changed, 108 insertions(+), 67 deletions(-) create mode 100644 decidim-admin/config/initializers/extends.rb create mode 100644 decidim-admin/lib/decidim/extends/moderations_controller_extend.rb diff --git a/decidim-admin/app/controllers/decidim/admin/moderations_controller.rb b/decidim-admin/app/controllers/decidim/admin/moderations_controller.rb index a7f41681ef16d..72f5317df90ce 100644 --- a/decidim-admin/app/controllers/decidim/admin/moderations_controller.rb +++ b/decidim-admin/app/controllers/decidim/admin/moderations_controller.rb @@ -4,45 +4,10 @@ module Decidim module Admin # This controller allows admins to manage moderations in a participatory process. class ModerationsController < Decidim::Admin::ApplicationController - helper_method :downstream_moderations - helper_method :upstream_moderations + helper_method :moderations - def index + def index # This method has been extended authorize! :read, Decidim::Moderation - @upstream = true if params[:moderation_type] == "upstream" - @downstream = true if params[:moderation_type] == "downstream" - @moderated_or_hidden = true if params[:moderated] || params[:hidden] - end - - def authorize - authorize! :authorize, Decidim::Moderation - @moderation = Decidim::Moderation.find(params[:id]) - moderated = params[:moderated] - @moderation.authorize! - redirect_to moderations_path(moderation_type: "upstream", moderated: moderated) - end - - # This action is use to hide or to set as refused moderated object - def hide - authorize! :hide, reportable - if params[:refuse] - moderated = params[:moderated] - reportable.moderation.refuse! - redirect_to moderations_path(moderation_type: "upstream", moderated: moderated) - else - Admin::HideResource.call(reportable) do - on(:ok) do - flash[:notice] = I18n.t("reportable.hide.success", scope: "decidim.moderations.admin") - - redirect_to moderations_path - end - - on(:invalid) do - flash.now[:alert] = I18n.t("reportable.hide.invalid", scope: "decidim.moderations.admin") - redirect_to moderations_path - end - end - end end def unreport @@ -61,45 +26,32 @@ def unreport end end - private - + def hide # This method has been extended + authorize! :hide, reportable - def downstream_moderations - @downstream_moderations ||= begin - if params[:hidden] && params[:moderation_type] == "downstream" - participatory_space_moderations.where.not(hidden_at: nil, report_count: 0).order("created_at").reverse - elsif params[:moderation_type] == "downstream" - participatory_space_moderations.where(hidden_at: nil).where.not( report_count: 0).order("created_at").reverse + Admin::HideResource.call(reportable) do + on(:ok) do + flash[:notice] = I18n.t("reportable.hide.success", scope: "decidim.moderations.admin") + redirect_to moderations_path end - end - end - def upstream_moderations - @upstream_moderations ||= begin - if params[:moderated] && params[:moderation_type] == "upstream" - filtered_upstream_moderations.where("upstream_moderation = ? OR upstream_moderation = ?", "refused", "authorized").order("created_at").reverse - elsif params[:moderation_type] == "upstream" - filtered_upstream_moderations.where(upstream_moderation: "unmoderate").order("created_at").reverse + on(:invalid) do + flash.now[:alert] = I18n.t("reportable.hide.invalid", scope: "decidim.moderations.admin") + redirect_to moderations_path end end end - def filtered_upstream_moderations - moderation_ids = [] - participatory_space_moderations.each do |m| - if get_features.include?(m.feature) - moderation_ids << m.id + private + + def moderations + @moderations ||= begin + if params[:hidden] + participatory_space_moderations.where.not(hidden_at: nil) + else + participatory_space_moderations.where(hidden_at: nil) end end - Decidim::Moderation.where(id: moderation_ids) - end - - def get_features - features = Decidim::Feature.where("settings->'global' ->> 'upstream_moderation_enabled' = ?", "true") + - Decidim::Feature.where("settings->'global' ->> 'comments_upstream_moderation_enabled' = ?", "true") - features.uniq! - - features = Decidim::Feature.where(id: features.map(&:id), participatory_space: current_participatory_space) end def reportable diff --git a/decidim-admin/config/initializers/extends.rb b/decidim-admin/config/initializers/extends.rb new file mode 100644 index 0000000000000..aa3e1f7c5a6bf --- /dev/null +++ b/decidim-admin/config/initializers/extends.rb @@ -0,0 +1 @@ +require "extends/moderations_controller.rb" \ No newline at end of file diff --git a/decidim-admin/lib/decidim/extends/moderations_controller_extend.rb b/decidim-admin/lib/decidim/extends/moderations_controller_extend.rb new file mode 100644 index 0000000000000..2152bc148ce3a --- /dev/null +++ b/decidim-admin/lib/decidim/extends/moderations_controller_extend.rb @@ -0,0 +1,88 @@ +module ModerationsControllerExtend + helper_method :downstream_moderations + helper_method :upstream_moderations + + def index + authorize! :read, Decidim::Moderation + @upstream = true if params[:moderation_type] == "upstream" + @downstream = true if params[:moderation_type] == "downstream" + @moderated_or_hidden = true if params[:moderated] || params[:hidden] + end + + def authorize + authorize! :authorize, Decidim::Moderation + @moderation = Decidim::Moderation.find(params[:id]) + moderated = params[:moderated] + @moderation.authorize! + redirect_to moderations_path(moderation_type: "upstream", moderated: moderated) + end + + # This action is use to hide or to set as refused moderated object + def hide + authorize! :hide, reportable + if params[:refuse] + moderated = params[:moderated] + reportable.moderation.refuse! + redirect_to moderations_path(moderation_type: "upstream", moderated: moderated) + else + Admin::HideResource.call(reportable) do + on(:ok) do + flash[:notice] = I18n.t("reportable.hide.success", scope: "decidim.moderations.admin") + + redirect_to moderations_path + end + + on(:invalid) do + flash.now[:alert] = I18n.t("reportable.hide.invalid", scope: "decidim.moderations.admin") + redirect_to moderations_path + end + end + end + end + + private + + + def downstream_moderations + @downstream_moderations ||= begin + if params[:hidden] && params[:moderation_type] == "downstream" + participatory_space_moderations.where.not(hidden_at: nil, report_count: 0).order("created_at").reverse + elsif params[:moderation_type] == "downstream" + participatory_space_moderations.where(hidden_at: nil).where.not( report_count: 0).order("created_at").reverse + end + end + end + + def upstream_moderations + @upstream_moderations ||= begin + if params[:moderated] && params[:moderation_type] == "upstream" + filtered_upstream_moderations.where("upstream_moderation = ? OR upstream_moderation = ?", "refused", "authorized").order("created_at").reverse + elsif params[:moderation_type] == "upstream" + filtered_upstream_moderations.where(upstream_moderation: "unmoderate").order("created_at").reverse + end + end + end + + def filtered_upstream_moderations + moderation_ids = [] + participatory_space_moderations.each do |m| + if get_features.include?(m.feature) + moderation_ids << m.id + end + end + Decidim::Moderation.where(id: moderation_ids) + end + + def get_features + features = Decidim::Feature.where("settings->'global' ->> 'upstream_moderation_enabled' = ?", "true") + + Decidim::Feature.where("settings->'global' ->> 'comments_upstream_moderation_enabled' = ?", "true") + features.uniq! + + features = Decidim::Feature.where(id: features.map(&:id), participatory_space: current_participatory_space) + end +end + +Decidim::Admin::ModerationsController.class_eval do + prepend(ModerationsControllerExtend) +end + From 29e77900c198bf2465f1ec32fd556020dba5c6f4 Mon Sep 17 00:00:00 2001 From: Ludivine Coipel Date: Thu, 1 Feb 2018 17:54:33 +0100 Subject: [PATCH 12/17] Extends files for moderation --- decidim-admin/config/locales/en.yml | 1 + decidim-admin/config/locales/fr.yml | 1 + .../app/commands/decidim/comments/create_comment.rb | 5 ++++- .../models/decidim/comments/abilities/admin_ability.rb | 1 - decidim-comments/app/models/decidim/comments/comment.rb | 5 +++++ decidim-comments/config/initializers/extends.rb | 3 ++- .../lib/decidim/extends/admin_ability_extend.rb | 8 ++++++++ decidim-core/app/mailers/decidim/notification_mailer.rb | 2 -- decidim-core/app/models/decidim/moderation.rb | 1 - 9 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 decidim-comments/lib/decidim/extends/admin_ability_extend.rb diff --git a/decidim-admin/config/locales/en.yml b/decidim-admin/config/locales/en.yml index 4503330241911..66b3a0ab4859f 100644 --- a/decidim-admin/config/locales/en.yml +++ b/decidim-admin/config/locales/en.yml @@ -292,6 +292,7 @@ en: weight: Weight moderations: index: + title: Moderations title: upstream: Upstream moderation downstream: Downstream moderation diff --git a/decidim-admin/config/locales/fr.yml b/decidim-admin/config/locales/fr.yml index cec771115fa18..4f83daecd8bf3 100644 --- a/decidim-admin/config/locales/fr.yml +++ b/decidim-admin/config/locales/fr.yml @@ -285,6 +285,7 @@ fr: weight: Position moderations: index: + title: Modérations title: upstream: Modération à priori downstream: Modération à postériori diff --git a/decidim-comments/app/commands/decidim/comments/create_comment.rb b/decidim-comments/app/commands/decidim/comments/create_comment.rb index 166f77925f45f..96f7faa88b7ad 100644 --- a/decidim-comments/app/commands/decidim/comments/create_comment.rb +++ b/decidim-comments/app/commands/decidim/comments/create_comment.rb @@ -44,11 +44,14 @@ def create_comment end def send_notification + recipient_ids = (@commentable.users_to_notify_on_comment_created - [@author]).pluck(:id) + recipient_ids += @author.followers.pluck(:id) + Decidim::EventsManager.publish( event: "decidim.events.comments.comment_created", event_class: Decidim::Comments::CommentCreatedEvent, resource: @comment.root_commentable, - recipient_ids: (@commentable.users_to_notify_on_comment_created - [@author]).pluck(:id), + recipient_ids: recipient_ids.uniq, extra: { comment_id: @comment.id } diff --git a/decidim-comments/app/models/decidim/comments/abilities/admin_ability.rb b/decidim-comments/app/models/decidim/comments/abilities/admin_ability.rb index 7374aea10be58..fc090149ce9de 100644 --- a/decidim-comments/app/models/decidim/comments/abilities/admin_ability.rb +++ b/decidim-comments/app/models/decidim/comments/abilities/admin_ability.rb @@ -12,7 +12,6 @@ def define_abilities can :manage, Comment can :unreport, Comment can :hide, Comment - can :authorize, Comment end end end diff --git a/decidim-comments/app/models/decidim/comments/comment.rb b/decidim-comments/app/models/decidim/comments/comment.rb index aaa167089e109..f2620cb383323 100644 --- a/decidim-comments/app/models/decidim/comments/comment.rb +++ b/decidim-comments/app/models/decidim/comments/comment.rb @@ -42,6 +42,9 @@ def accepts_new_comments? depth < MAX_DEPTH end + # Public: Override Commentable concern method `users_to_notify_on_comment_created` + delegate :users_to_notify_on_comment_created, to: :root_commentable + # Public: Check if the user has upvoted the comment # # Returns a bool value to indicate if the condition is truthy or not @@ -61,6 +64,8 @@ def reported_content_url ResourceLocatorPresenter.new(root_commentable).url(anchor: "comment_#{id}") end + private + # Private: Check if commentable can have comments and if not adds # a validation error to the model def commentable_can_have_comments diff --git a/decidim-comments/config/initializers/extends.rb b/decidim-comments/config/initializers/extends.rb index fbcfcbe481e3d..a90758fe29423 100644 --- a/decidim-comments/config/initializers/extends.rb +++ b/decidim-comments/config/initializers/extends.rb @@ -3,4 +3,5 @@ require 'decidim/extends/commentable_extend.rb' require 'decidim/extends/comment_extend.rb' require 'decidim/extends/comment_created_event_extend.rb' -require 'decidim/extends/sorted_comments_extend.rb' \ No newline at end of file +require 'decidim/extends/sorted_comments_extend.rb' +require 'decidim/extends/admin_ability_extend.rb' \ No newline at end of file diff --git a/decidim-comments/lib/decidim/extends/admin_ability_extend.rb b/decidim-comments/lib/decidim/extends/admin_ability_extend.rb new file mode 100644 index 0000000000000..d732dcf9f0934 --- /dev/null +++ b/decidim-comments/lib/decidim/extends/admin_ability_extend.rb @@ -0,0 +1,8 @@ +module AdminAbilityExtend + can :authorize, Comment +end + +Decidim::Comments::Abilities::AdminAbility.class_eval do + prepend(AdminAbilityExtend) +end + diff --git a/decidim-core/app/mailers/decidim/notification_mailer.rb b/decidim-core/app/mailers/decidim/notification_mailer.rb index 2719b257f07ed..733eb146a8d9a 100644 --- a/decidim-core/app/mailers/decidim/notification_mailer.rb +++ b/decidim-core/app/mailers/decidim/notification_mailer.rb @@ -8,8 +8,6 @@ class NotificationMailer < Decidim::ApplicationMailer helper Decidim::TranslationsHelper def event_received(event, event_class_name, resource, user, extra) - binding.pry - with_user(user) do @organization = resource.organization event_class = event_class_name.constantize diff --git a/decidim-core/app/models/decidim/moderation.rb b/decidim-core/app/models/decidim/moderation.rb index 7c68232c1d2db..baf8767835e64 100644 --- a/decidim-core/app/models/decidim/moderation.rb +++ b/decidim-core/app/models/decidim/moderation.rb @@ -8,6 +8,5 @@ class Moderation < ApplicationRecord has_many :reports, foreign_key: "decidim_moderation_id", class_name: "Decidim::Report", dependent: :destroy delegate :feature, :organization, to: :reportable - end end From 448b40f8089b40d03cd581ccd3e44323daa5ef24 Mon Sep 17 00:00:00 2001 From: Ludivine Coipel Date: Thu, 1 Feb 2018 18:33:53 +0100 Subject: [PATCH 13/17] Add Extend files for moderation --- .../decidim/admin/moderations_controller.rb | 79 ++++++++++++++--- decidim-admin/config/initializers/extends.rb | 1 - .../extends/moderations_controller_extend.rb | 88 ------------------- .../decidim/api/queries_controller.rb | 14 +-- .../comments/abilities/admin_ability.rb | 1 + .../config/initializers/extends.rb | 3 +- .../decidim/extends/admin_ability_extend.rb | 8 -- .../extends/comment_created_event_extend.rb | 43 ++++----- .../lib/decidim/extends/comment_extend.rb | 1 - .../decidim/proposals/proposals_controller.rb | 1 + .../config/initializers/extends.rb | 3 +- .../extends/proposals_controller_extend.rb | 27 ------ 12 files changed, 99 insertions(+), 170 deletions(-) delete mode 100644 decidim-admin/config/initializers/extends.rb delete mode 100644 decidim-admin/lib/decidim/extends/moderations_controller_extend.rb delete mode 100644 decidim-comments/lib/decidim/extends/admin_ability_extend.rb delete mode 100644 decidim-proposals/lib/decidim/extends/proposals_controller_extend.rb diff --git a/decidim-admin/app/controllers/decidim/admin/moderations_controller.rb b/decidim-admin/app/controllers/decidim/admin/moderations_controller.rb index 72f5317df90ce..d3d2386641a8b 100644 --- a/decidim-admin/app/controllers/decidim/admin/moderations_controller.rb +++ b/decidim-admin/app/controllers/decidim/admin/moderations_controller.rb @@ -5,9 +5,23 @@ module Admin # This controller allows admins to manage moderations in a participatory process. class ModerationsController < Decidim::Admin::ApplicationController helper_method :moderations + helper_method :downstream_moderations + helper_method :upstream_moderations - def index # This method has been extended + + def index authorize! :read, Decidim::Moderation + @upstream = true if params[:moderation_type] == "upstream" + @downstream = true if params[:moderation_type] == "downstream" + @moderated_or_hidden = true if params[:moderated] || params[:hidden] + end + + def authorize + authorize! :authorize, Decidim::Moderation + @moderation = Decidim::Moderation.find(params[:id]) + moderated = params[:moderated] + @moderation.authorize! + redirect_to moderations_path(moderation_type: "upstream", moderated: moderated) end def unreport @@ -26,18 +40,25 @@ def unreport end end - def hide # This method has been extended + # This action is use to hide or to set as refused moderated object + def hide authorize! :hide, reportable + if params[:refuse] + moderated = params[:moderated] + reportable.moderation.refuse! + redirect_to moderations_path(moderation_type: "upstream", moderated: moderated) + else + Admin::HideResource.call(reportable) do + on(:ok) do + flash[:notice] = I18n.t("reportable.hide.success", scope: "decidim.moderations.admin") - Admin::HideResource.call(reportable) do - on(:ok) do - flash[:notice] = I18n.t("reportable.hide.success", scope: "decidim.moderations.admin") - redirect_to moderations_path - end + redirect_to moderations_path + end - on(:invalid) do - flash.now[:alert] = I18n.t("reportable.hide.invalid", scope: "decidim.moderations.admin") - redirect_to moderations_path + on(:invalid) do + flash.now[:alert] = I18n.t("reportable.hide.invalid", scope: "decidim.moderations.admin") + redirect_to moderations_path + end end end end @@ -61,6 +82,44 @@ def reportable def participatory_space_moderations @participatory_space_moderations ||= Decidim::Moderation.where(participatory_space: current_participatory_space) end + + def downstream_moderations + @downstream_moderations ||= begin + if params[:hidden] && params[:moderation_type] == "downstream" + participatory_space_moderations.where.not(hidden_at: nil, report_count: 0).order("created_at").reverse + elsif params[:moderation_type] == "downstream" + participatory_space_moderations.where(hidden_at: nil).where.not( report_count: 0).order("created_at").reverse + end + end + end + + def upstream_moderations + @upstream_moderations ||= begin + if params[:moderated] && params[:moderation_type] == "upstream" + filtered_upstream_moderations.where("upstream_moderation = ? OR upstream_moderation = ?", "refused", "authorized").order("created_at").reverse + elsif params[:moderation_type] == "upstream" + filtered_upstream_moderations.where(upstream_moderation: "unmoderate").order("created_at").reverse + end + end + end + + def filtered_upstream_moderations + moderation_ids = [] + participatory_space_moderations.each do |m| + if get_features.include?(m.feature) + moderation_ids << m.id + end + end + Decidim::Moderation.where(id: moderation_ids) + end + + def get_features + features = Decidim::Feature.where("settings->'global' ->> 'upstream_moderation_enabled' = ?", "true") + + Decidim::Feature.where("settings->'global' ->> 'comments_upstream_moderation_enabled' = ?", "true") + features.uniq! + + features = Decidim::Feature.where(id: features.map(&:id), participatory_space: current_participatory_space) + end end end end diff --git a/decidim-admin/config/initializers/extends.rb b/decidim-admin/config/initializers/extends.rb deleted file mode 100644 index aa3e1f7c5a6bf..0000000000000 --- a/decidim-admin/config/initializers/extends.rb +++ /dev/null @@ -1 +0,0 @@ -require "extends/moderations_controller.rb" \ No newline at end of file diff --git a/decidim-admin/lib/decidim/extends/moderations_controller_extend.rb b/decidim-admin/lib/decidim/extends/moderations_controller_extend.rb deleted file mode 100644 index 2152bc148ce3a..0000000000000 --- a/decidim-admin/lib/decidim/extends/moderations_controller_extend.rb +++ /dev/null @@ -1,88 +0,0 @@ -module ModerationsControllerExtend - helper_method :downstream_moderations - helper_method :upstream_moderations - - def index - authorize! :read, Decidim::Moderation - @upstream = true if params[:moderation_type] == "upstream" - @downstream = true if params[:moderation_type] == "downstream" - @moderated_or_hidden = true if params[:moderated] || params[:hidden] - end - - def authorize - authorize! :authorize, Decidim::Moderation - @moderation = Decidim::Moderation.find(params[:id]) - moderated = params[:moderated] - @moderation.authorize! - redirect_to moderations_path(moderation_type: "upstream", moderated: moderated) - end - - # This action is use to hide or to set as refused moderated object - def hide - authorize! :hide, reportable - if params[:refuse] - moderated = params[:moderated] - reportable.moderation.refuse! - redirect_to moderations_path(moderation_type: "upstream", moderated: moderated) - else - Admin::HideResource.call(reportable) do - on(:ok) do - flash[:notice] = I18n.t("reportable.hide.success", scope: "decidim.moderations.admin") - - redirect_to moderations_path - end - - on(:invalid) do - flash.now[:alert] = I18n.t("reportable.hide.invalid", scope: "decidim.moderations.admin") - redirect_to moderations_path - end - end - end - end - - private - - - def downstream_moderations - @downstream_moderations ||= begin - if params[:hidden] && params[:moderation_type] == "downstream" - participatory_space_moderations.where.not(hidden_at: nil, report_count: 0).order("created_at").reverse - elsif params[:moderation_type] == "downstream" - participatory_space_moderations.where(hidden_at: nil).where.not( report_count: 0).order("created_at").reverse - end - end - end - - def upstream_moderations - @upstream_moderations ||= begin - if params[:moderated] && params[:moderation_type] == "upstream" - filtered_upstream_moderations.where("upstream_moderation = ? OR upstream_moderation = ?", "refused", "authorized").order("created_at").reverse - elsif params[:moderation_type] == "upstream" - filtered_upstream_moderations.where(upstream_moderation: "unmoderate").order("created_at").reverse - end - end - end - - def filtered_upstream_moderations - moderation_ids = [] - participatory_space_moderations.each do |m| - if get_features.include?(m.feature) - moderation_ids << m.id - end - end - Decidim::Moderation.where(id: moderation_ids) - end - - def get_features - features = Decidim::Feature.where("settings->'global' ->> 'upstream_moderation_enabled' = ?", "true") + - Decidim::Feature.where("settings->'global' ->> 'comments_upstream_moderation_enabled' = ?", "true") - features.uniq! - - features = Decidim::Feature.where(id: features.map(&:id), participatory_space: current_participatory_space) - end -end - -Decidim::Admin::ModerationsController.class_eval do - prepend(ModerationsControllerExtend) -end - diff --git a/decidim-api/app/controllers/decidim/api/queries_controller.rb b/decidim-api/app/controllers/decidim/api/queries_controller.rb index 55829d678bd93..42fd9fe5e815e 100644 --- a/decidim-api/app/controllers/decidim/api/queries_controller.rb +++ b/decidim-api/app/controllers/decidim/api/queries_controller.rb @@ -17,13 +17,6 @@ def create private - def store_current_user - Thread.current[:current_user] = current_user - yield - ensure - Thread.current[:current_user] = nil - end - def context { current_organization: current_organization, @@ -40,6 +33,13 @@ def ensure_hash(query_variables) query_variables end end + + def store_current_user + Thread.current[:current_user] = current_user + yield + ensure + Thread.current[:current_user] = nil + end end end end diff --git a/decidim-comments/app/models/decidim/comments/abilities/admin_ability.rb b/decidim-comments/app/models/decidim/comments/abilities/admin_ability.rb index fc090149ce9de..7374aea10be58 100644 --- a/decidim-comments/app/models/decidim/comments/abilities/admin_ability.rb +++ b/decidim-comments/app/models/decidim/comments/abilities/admin_ability.rb @@ -12,6 +12,7 @@ def define_abilities can :manage, Comment can :unreport, Comment can :hide, Comment + can :authorize, Comment end end end diff --git a/decidim-comments/config/initializers/extends.rb b/decidim-comments/config/initializers/extends.rb index a90758fe29423..fbcfcbe481e3d 100644 --- a/decidim-comments/config/initializers/extends.rb +++ b/decidim-comments/config/initializers/extends.rb @@ -3,5 +3,4 @@ require 'decidim/extends/commentable_extend.rb' require 'decidim/extends/comment_extend.rb' require 'decidim/extends/comment_created_event_extend.rb' -require 'decidim/extends/sorted_comments_extend.rb' -require 'decidim/extends/admin_ability_extend.rb' \ No newline at end of file +require 'decidim/extends/sorted_comments_extend.rb' \ No newline at end of file diff --git a/decidim-comments/lib/decidim/extends/admin_ability_extend.rb b/decidim-comments/lib/decidim/extends/admin_ability_extend.rb deleted file mode 100644 index d732dcf9f0934..0000000000000 --- a/decidim-comments/lib/decidim/extends/admin_ability_extend.rb +++ /dev/null @@ -1,8 +0,0 @@ -module AdminAbilityExtend - can :authorize, Comment -end - -Decidim::Comments::Abilities::AdminAbility.class_eval do - prepend(AdminAbilityExtend) -end - diff --git a/decidim-comments/lib/decidim/extends/comment_created_event_extend.rb b/decidim-comments/lib/decidim/extends/comment_created_event_extend.rb index 76075f1685cf7..0657ab4e04bd5 100644 --- a/decidim-comments/lib/decidim/extends/comment_created_event_extend.rb +++ b/decidim-comments/lib/decidim/extends/comment_created_event_extend.rb @@ -1,31 +1,26 @@ # frozen-string_literal: true module CommentCreatedEventExtend - module Decidim - module Comments - class CommentCreatedEvent < Decidim::Events::BaseEvent - def email_moderation_intro - I18n.t( - "decidim.comments.events.comment_created.#{comment_type}.moderation.email_intro", - resource_title: resource_title, - author_name: comment.author.name - ).html_safe - end + def email_moderation_intro + I18n.t( + "decidim.comments.events.comment_created.#{comment_type}.moderation.email_intro", + resource_title: resource_title, + author_name: comment.author.name + ).html_safe + end - def email_moderation_subject - I18n.t( - "decidim.comments.events.comment_created.#{comment_type}.moderation.email_subject", - resource_title: resource_title, - author_name: comment.author.name - ).html_safe - end + def email_moderation_subject + I18n.t( + "decidim.comments.events.comment_created.#{comment_type}.moderation.email_subject", + resource_title: resource_title, + author_name: comment.author.name + ).html_safe + end - def email_moderation_url(moderation_url) - I18n.t( - "decidim.comments.events.comment_created.#{comment_type}.moderation.moderation_url", - moderation_url: moderation_url - ).html_safe - end - end + def email_moderation_url(moderation_url) + I18n.t( + "decidim.comments.events.comment_created.#{comment_type}.moderation.moderation_url", + moderation_url: moderation_url + ).html_safe end end diff --git a/decidim-comments/lib/decidim/extends/comment_extend.rb b/decidim-comments/lib/decidim/extends/comment_extend.rb index f092d84dd5460..138cb0152b513 100644 --- a/decidim-comments/lib/decidim/extends/comment_extend.rb +++ b/decidim-comments/lib/decidim/extends/comment_extend.rb @@ -30,5 +30,4 @@ def update_moderation prepend(CommentExtend) after_create :create_comment_moderation after_create :update_moderation - delegate :users_to_notify_on_comment_created, to: :root_commentable end diff --git a/decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb b/decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb index 0dc52796a8f01..d2c969909176a 100644 --- a/decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb +++ b/decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb @@ -17,6 +17,7 @@ def index # this method has been extended @proposals = search .results .not_hidden + .authorized .includes(:author) .includes(:category) .includes(:scope) diff --git a/decidim-proposals/config/initializers/extends.rb b/decidim-proposals/config/initializers/extends.rb index 1faa0239d87f4..47969b93c4b19 100644 --- a/decidim-proposals/config/initializers/extends.rb +++ b/decidim-proposals/config/initializers/extends.rb @@ -1,2 +1 @@ -require "decidim/extends/proposal_extend.rb" -require "decidim/extends/proposals_controller_extend.rb" \ No newline at end of file +require "decidim/extends/proposal_extend.rb" \ No newline at end of file diff --git a/decidim-proposals/lib/decidim/extends/proposals_controller_extend.rb b/decidim-proposals/lib/decidim/extends/proposals_controller_extend.rb deleted file mode 100644 index 23fd6ed5e2917..0000000000000 --- a/decidim-proposals/lib/decidim/extends/proposals_controller_extend.rb +++ /dev/null @@ -1,27 +0,0 @@ -module ProposalsControllerExtend - def index - @proposals = search - .results - .not_hidden - .authorized - .includes(:author) - .includes(:category) - .includes(:scope) - - @voted_proposals = if current_user - ProposalVote.where( - author: current_user, - proposal: @proposals.pluck(:id) - ).pluck(:decidim_proposal_id) - else - [] - end - - @proposals = paginate(@proposals) - @proposals = reorder(@proposals) - end -end - -Decidim::Proposals::ProposalsController.class_eval do - prepend(ProposalsControllerExtend) -end From 964bbadabd2450fdfa0fee58da968c01016c8338 Mon Sep 17 00:00:00 2001 From: Ludivine Coipel Date: Thu, 1 Feb 2018 19:17:25 +0100 Subject: [PATCH 14/17] Edit trads for moderation --- .../comments/comment_authorized_event.rb | 15 ++++++++--- decidim-comments/config/locales/en.yml | 25 +++++++------------ decidim-comments/config/locales/fr.yml | 14 +++++++++++ .../extends/comment_created_event_extend.rb | 9 +++++++ .../decidim/extends/sorted_comments_extend.rb | 4 +-- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/decidim-comments/app/events/decidim/comments/comment_authorized_event.rb b/decidim-comments/app/events/decidim/comments/comment_authorized_event.rb index dbf96a3dfa47b..a8415aaedef09 100644 --- a/decidim-comments/app/events/decidim/comments/comment_authorized_event.rb +++ b/decidim-comments/app/events/decidim/comments/comment_authorized_event.rb @@ -8,7 +8,7 @@ class CommentAuthorizedEvent < Decidim::Events::BaseEvent def email_subject I18n.t( - "decidim.comments.events.comment_created.#{comment_type}.email_subject", + "decidim.comments.events.comment_authorized.#{comment_type}.email_subject", resource_title: resource_title, resource_url: resource_locator.url(url_params), author_name: author.name @@ -17,21 +17,21 @@ def email_subject def email_intro I18n.t( - "decidim.comments.events.comment_created.#{comment_type}.email_intro", + "decidim.comments.events.comment_authorized.#{comment_type}.email_intro", resource_title: resource_title ).html_safe end def email_outro I18n.t( - "decidim.comments.events.comment_created.#{comment_type}.email_outro", + "decidim.comments.events.comment_authorized.#{comment_type}.email_outro", resource_title: resource_title ) end def notification_title I18n.t( - "decidim.comments.events.comment_created.#{comment_type}.notification_title", + "decidim.comments.events.comment_authorized.#{comment_type}.notification_title", resource_title: resource_title, resource_path: resource_locator.path(url_params), author_nickname: author.nickname, @@ -40,6 +40,13 @@ def notification_title ).html_safe end + def email_url + I18n.t( + "decidim.comments.events.comment_authorized.#{comment_type}.url", + resource_url: resource_locator.url(url_params) + ).html_safe + end + private def author diff --git a/decidim-comments/config/locales/en.yml b/decidim-comments/config/locales/en.yml index 4a7778a53c615..38964d898925b 100644 --- a/decidim-comments/config/locales/en.yml +++ b/decidim-comments/config/locales/en.yml @@ -10,12 +10,20 @@ en: email_subject: There is a new comment from %{author_name} in %{resource_title} notification_title: There is a new comment from %{author_name} %{author_nickname} in %{resource_title} url: "Comment's url : %{resource_url}" + moderation: + email_intro: '%{author_name} published a comment on' + email_subject: "New comment from %{author_name} waiting for moderation" + moderation_url: "You can moderate it here : %{moderation_url}" reply: email_intro: The "%{resource_title}" has been commented email_outro: You have received this notification because you are following "%{resource_title}" or its author. You can unfollow it from the previous link. email_subject: There is a new reply of your comment from %{author_name} in %{resource_title} notification_title: There is a new reply of your comment from %{author_name} %{author_nickname} in %{resource_title} url: "Comment's url : %{resource_url}" + moderation: + email_intro: '%{author_name} published a comment on' + email_subject: 'New comment from %{author_name} waiting for moderation' + moderation_url: "You can moderate it here : %{moderation_url}" votes: create: error: There's been errors when voting the comment. @@ -69,19 +77,4 @@ en: title: Featured comment errors: messages: - cannot_have_comments: can't have comments -# moderation - comments: - events: - comment_created: - comment: - moderation: - email_intro: '%{author_name} published a comment on' - email_subject: "New comment from %{author_name} waiting for moderation" - moderation_url: "You can moderate it here : %{moderation_url}" - reply: - moderation: - email_intro: '%{author_name} published a comment on' - email_subject: 'New comment from %{author_name} waiting for moderation' - moderation_url: "You can moderate it here : %{moderation_url}" - + cannot_have_comments: can't have comments \ No newline at end of file diff --git a/decidim-comments/config/locales/fr.yml b/decidim-comments/config/locales/fr.yml index db8e5633231e0..767cb4e8f7b39 100644 --- a/decidim-comments/config/locales/fr.yml +++ b/decidim-comments/config/locales/fr.yml @@ -83,3 +83,17 @@ fr: email_intro: "%{author_name} a publié un nouveau commentaire sur" email_subject: 'Nouveau commentaire de %{author_name} en attente de modération' moderation_url: Vous pouvez modérer le contenu en suivant ce lien %{moderation_url} + comment_authorized: + comment: + email_intro: '"%{resource_title}" a été commenté. Vous pouvez lire le commentaire de cette page:' + email_outro: Vous recevez cet notification car vous suivez « %{resource_title} ». Vous pouvez cesser de le suivre à partir du lien précédent. + email_subject: Un nouveau commentaire a été publié par %{author_name} dans %{resource_title} + notification_title: Il y a un nouveau commentaire de %{author_name} %{author_nickname} dans %{resource_title} + url: "Url du commentaire : %{resource_url}" + reply: + email_intro: '"%{resource_title}" a reçu un commentaire' + email_outro: Vous recevez cet notification car vous suivez « %{resource_title} ». Vous pouvez cesser de le suivre à partir du lien précédent. + email_subject: Votre commentaire a reçu une réponse de %{author_name} dans %{resource_title} + notification_title: Il y a une nouvelle réponse à votre commentaire de %{author_name} %{author_nickname} dans %{resource_title} + url: "Url du commentaire : %{resource_url}" + diff --git a/decidim-comments/lib/decidim/extends/comment_created_event_extend.rb b/decidim-comments/lib/decidim/extends/comment_created_event_extend.rb index 0657ab4e04bd5..20b05fa175e7f 100644 --- a/decidim-comments/lib/decidim/extends/comment_created_event_extend.rb +++ b/decidim-comments/lib/decidim/extends/comment_created_event_extend.rb @@ -22,6 +22,15 @@ def email_moderation_url(moderation_url) moderation_url: moderation_url ).html_safe end + + def email_url + I18n.t( + "decidim.comments.events.comment_created.#{comment_type}.url", + resource_url: resource_locator.url(url_params) + ).html_safe + end + + end Decidim::Comments::CommentCreatedEvent.class_eval do diff --git a/decidim-comments/lib/decidim/extends/sorted_comments_extend.rb b/decidim-comments/lib/decidim/extends/sorted_comments_extend.rb index c079bd28b94f4..5b93d4844df93 100644 --- a/decidim-comments/lib/decidim/extends/sorted_comments_extend.rb +++ b/decidim-comments/lib/decidim/extends/sorted_comments_extend.rb @@ -19,12 +19,12 @@ def query end def filter_comments if admin_or_moderator? - Comment + Decidim::Comments::Comment .where(commentable: commentable) .not_hidden .includes(:author, :up_votes, :down_votes) else - Comment + Decidim::Comments::Comment .where(commentable: commentable) .authorized .not_hidden From fa490c15e1b6509c7911459f556571ef09ebda4f Mon Sep 17 00:00:00 2001 From: Ludivine Coipel Date: Thu, 1 Feb 2018 20:21:34 +0100 Subject: [PATCH 15/17] Extend project and meetings + edit mailer moderation --- .../app/models/decidim/budgets/project.rb | 4 ++-- decidim-budgets/config/initializers/extends.rb | 1 + .../lib/decidim/extends/project_extend.rb | 13 +++++++++++++ .../new_content_received.html.erb | 8 ++++---- .../decidim/extends/notification_mailer_extend.rb | 10 ++++++++++ .../app/models/decidim/meetings/meeting.rb | 4 ++-- decidim-meetings/config/initializers/extends.rb | 1 + .../lib/decidim/extends/meeting_extend.rb | 13 +++++++++++++ .../app/models/decidim/proposals/proposal.rb | 2 +- 9 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 decidim-budgets/config/initializers/extends.rb create mode 100644 decidim-budgets/lib/decidim/extends/project_extend.rb create mode 100644 decidim-meetings/config/initializers/extends.rb create mode 100644 decidim-meetings/lib/decidim/extends/meeting_extend.rb diff --git a/decidim-budgets/app/models/decidim/budgets/project.rb b/decidim-budgets/app/models/decidim/budgets/project.rb index d338f8be2bf11..fac9215cc2881 100644 --- a/decidim-budgets/app/models/decidim/budgets/project.rb +++ b/decidim-budgets/app/models/decidim/budgets/project.rb @@ -33,8 +33,8 @@ def comments_have_votes? true end - # Public: Overrides the `users_to_notify_on_comment_authorized` Commentable concern method. - def users_to_notify_on_comment_authorized + # Public: Overrides the `users_to_notify_on_comment_created` Commentable concern method. + def users_to_notify_on_comment_created # this method has been extended followers end diff --git a/decidim-budgets/config/initializers/extends.rb b/decidim-budgets/config/initializers/extends.rb new file mode 100644 index 0000000000000..0d51ec8a0aae8 --- /dev/null +++ b/decidim-budgets/config/initializers/extends.rb @@ -0,0 +1 @@ +require "decidim/extends/project_extend.rb" \ No newline at end of file diff --git a/decidim-budgets/lib/decidim/extends/project_extend.rb b/decidim-budgets/lib/decidim/extends/project_extend.rb new file mode 100644 index 0000000000000..a76a998c1b93d --- /dev/null +++ b/decidim-budgets/lib/decidim/extends/project_extend.rb @@ -0,0 +1,13 @@ +module ProjectExtend + def users_to_notify_on_comment_created + get_all_users_with_role + end + + def users_to_notify_on_comment_authorized + followers + end +end + +Decidim::Budgets::Project.class_eval do + prepend(ProjectExtend) +end diff --git a/decidim-core/app/views/decidim/notification_mailer/new_content_received.html.erb b/decidim-core/app/views/decidim/notification_mailer/new_content_received.html.erb index a944d69a26875..34b15bef4fa5b 100644 --- a/decidim-core/app/views/decidim/notification_mailer/new_content_received.html.erb +++ b/decidim-core/app/views/decidim/notification_mailer/new_content_received.html.erb @@ -1,12 +1,12 @@

<%= @event_instance.email_moderation_intro %> - <% if @is_proposal %> - <%= translated_attribute(@parent_title) %> + <% if @translatable %> + <%= translated_attribute(@parent_title) %> <% else %> - <%= @parent_title %> + <%= @parent_title %> <% end %>

diff --git a/decidim-core/lib/decidim/extends/notification_mailer_extend.rb b/decidim-core/lib/decidim/extends/notification_mailer_extend.rb index ac9948fd16015..ccb21907e0de4 100644 --- a/decidim-core/lib/decidim/extends/notification_mailer_extend.rb +++ b/decidim-core/lib/decidim/extends/notification_mailer_extend.rb @@ -13,6 +13,16 @@ def new_content_received(event, event_class_name, resource, user, extra) @body = body(event_class, resource, extra) @moderation_url = moderation_url @is_comment = is_comment?(event_class) + @translatable = + if resource.is_a?(Decidim::Budgets::Project) || resource.is_a?(Decidim::Meetings::Meeting) + true + elsif resource.is_a?(Decidim::Proposals::Proposal) + if event_class_name == "Decidim::Proposals::ProposalCreatedEvent" + true + else + false + end + end mail(to: user.email, subject: subject) end end diff --git a/decidim-meetings/app/models/decidim/meetings/meeting.rb b/decidim-meetings/app/models/decidim/meetings/meeting.rb index 34290f3d9dcdb..f9b44fce92337 100644 --- a/decidim-meetings/app/models/decidim/meetings/meeting.rb +++ b/decidim-meetings/app/models/decidim/meetings/meeting.rb @@ -59,8 +59,8 @@ def comments_have_votes? true end - # Public: Override Commentable concern method `users_to_notify_on_comment_authorized` - def users_to_notify_on_comment_authorized + # Public: Override Commentable concern method `users_to_notify_on_comment_created` + def users_to_notify_on_comment_created followers end end diff --git a/decidim-meetings/config/initializers/extends.rb b/decidim-meetings/config/initializers/extends.rb new file mode 100644 index 0000000000000..d21051530181d --- /dev/null +++ b/decidim-meetings/config/initializers/extends.rb @@ -0,0 +1 @@ +require "decidim/extends/meeting_extend.rb" \ No newline at end of file diff --git a/decidim-meetings/lib/decidim/extends/meeting_extend.rb b/decidim-meetings/lib/decidim/extends/meeting_extend.rb new file mode 100644 index 0000000000000..72b7f1a9a9a9c --- /dev/null +++ b/decidim-meetings/lib/decidim/extends/meeting_extend.rb @@ -0,0 +1,13 @@ +module MeetingExtend + def users_to_notify_on_comment_created + get_all_users_with_role + end + + def users_to_notify_on_comment_authorized + followers + end +end + +Decidim::Meetings::Meeting.class_eval do + prepend(MeetingExtend) +end diff --git a/decidim-proposals/app/models/decidim/proposals/proposal.rb b/decidim-proposals/app/models/decidim/proposals/proposal.rb index bcc7e27b65ea5..de04faf382eef 100644 --- a/decidim-proposals/app/models/decidim/proposals/proposal.rb +++ b/decidim-proposals/app/models/decidim/proposals/proposal.rb @@ -89,7 +89,7 @@ def comments_have_votes? true end - # Public: Override Commentable concern method `users_to_notify_on_comment_authorized` + # Public: Override Commentable concern method `users_to_notify_on_comment_created` def users_to_notify_on_comment_created return (followers | feature.participatory_space.admins).uniq if official? followers From 3cda971139b6922d2ae343d5b0334b45c0398017 Mon Sep 17 00:00:00 2001 From: Ludivine Coipel Date: Thu, 1 Feb 2018 20:44:46 +0100 Subject: [PATCH 16/17] #29 moderation translations --- .../controllers/decidim/proposals/proposals_controller.rb | 6 +++++- decidim-proposals/config/locales/en.yml | 4 +++- decidim-proposals/config/locales/fr.yml | 4 +++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb b/decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb index d2c969909176a..9836837024b24 100644 --- a/decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb +++ b/decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb @@ -58,7 +58,11 @@ def create CreateProposal.call(@form, current_user) do on(:ok) do |proposal| - flash[:notice] = I18n.t("proposals.create.success", scope: "decidim") + if proposal.feature.settings.upstream_moderation_enabled + flash[:notice] = I18n.t("proposals.create.moderation.success", scope: "decidim") + else + flash[:notice] = I18n.t("proposals.create.success", scope: "decidim") + end redirect_to proposal_path(proposal) end diff --git a/decidim-proposals/config/locales/en.yml b/decidim-proposals/config/locales/en.yml index 3c74b66a4a517..1b0ab39e49dd3 100644 --- a/decidim-proposals/config/locales/en.yml +++ b/decidim-proposals/config/locales/en.yml @@ -93,7 +93,9 @@ en: rejected: Rejected create: error: There's been errors when saving the proposal. - success: Proposal successfully created. It will be published as soon as an administrator will have authorized it. + success: Proposal successfully created. + moderation: + success: Proposal successfully created. It will be published as soon as an administrator will have authorized it. events: create_proposal_event: email_intro: |- diff --git a/decidim-proposals/config/locales/fr.yml b/decidim-proposals/config/locales/fr.yml index addf40190b3d7..46b4e2c1cca72 100644 --- a/decidim-proposals/config/locales/fr.yml +++ b/decidim-proposals/config/locales/fr.yml @@ -92,7 +92,9 @@ fr: rejected: Refusée create: error: Il y a eu des erreurs lors de la sauvegarde de la proposition. - success: Proposition créée avec succès. Elle sera publiée dès qu'un administrateur l'aura validée. + success: Proposition créée avec succès. + moderation: + success: Proposition créée avec succès. Elle sera publiée dès qu'un administrateur l'aura validée. events: create_proposal_event: email_intro: |- From f6a7ca0369f3e7bd060fb0a25ad9ee696717207a Mon Sep 17 00:00:00 2001 From: Ludivine Coipel Date: Thu, 1 Feb 2018 21:01:11 +0100 Subject: [PATCH 17/17] Add script to update existing data => moderation --- .../migrate/20180201195211_add_data_for_moderation.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 decidim-comments/db/migrate/20180201195211_add_data_for_moderation.rb diff --git a/decidim-comments/db/migrate/20180201195211_add_data_for_moderation.rb b/decidim-comments/db/migrate/20180201195211_add_data_for_moderation.rb new file mode 100644 index 0000000000000..446adf5aad310 --- /dev/null +++ b/decidim-comments/db/migrate/20180201195211_add_data_for_moderation.rb @@ -0,0 +1,11 @@ +class AddDataForModeration < ActiveRecord::Migration[5.1] + def up + Decidim::Comments::Comment.all.each {|c|Decidim::Moderation.find_or_create_by!(reportable: c, participatory_space: c.feature.participatory_space, upstream_moderation: "authorized")} + + Decidim::Proposals::Proposal.all.each {|p|Decidim::Moderation.find_or_create_by!(reportable: p, participatory_space: p.feature.participatory_space, upstream_moderation: "authorized")} + + Decidim::Moderation.all.each {|m| m.update_attributes(upstream_moderation: "authorized")} + end + + def down; end +end