diff --git a/decidim-admin/app/commands/decidim/admin/block_user.rb b/decidim-admin/app/commands/decidim/admin/block_user.rb index 2552a8791fadd..ea41d6b06545e 100644 --- a/decidim-admin/app/commands/decidim/admin/block_user.rb +++ b/decidim-admin/app/commands/decidim/admin/block_user.rb @@ -77,6 +77,7 @@ def block! form.user.block_id = @current_blocking.id form.user.extended_data["user_name"] = form.user.name form.user.name = "Blocked user" + form.user.notifications_sending_frequency = "none" form.user.save! end end diff --git a/decidim-admin/spec/commands/decidim/admin/block_user_spec.rb b/decidim-admin/spec/commands/decidim/admin/block_user_spec.rb index 16aef61852722..e2e9837fd9d1e 100644 --- a/decidim-admin/spec/commands/decidim/admin/block_user_spec.rb +++ b/decidim-admin/spec/commands/decidim/admin/block_user_spec.rb @@ -40,6 +40,7 @@ module Decidim::Admin expect(form.user.blocked).to be(true) expect(form.user.extended_data["user_name"]).to eq(user_name) expect(form.user.name).to eq("Blocked user") + expect(form.user.notifications_sending_frequency).to eq("none") end it "original username is stored in the action log entry's resource title" do diff --git a/decidim-core/app/services/decidim/email_notification_generator.rb b/decidim-core/app/services/decidim/email_notification_generator.rb index 64e5cd131f72b..bf4e1b29c3c0f 100644 --- a/decidim-core/app/services/decidim/email_notification_generator.rb +++ b/decidim-core/app/services/decidim/email_notification_generator.rb @@ -38,12 +38,14 @@ def generate followers.each do |recipient| next unless ["all", "followed-only"].include?(recipient.notification_types) + next if recipient.blocked? send_email_to(recipient, user_role: :follower) end affected_users.each do |recipient| next unless ["all", "own-only"].include?(recipient.notification_types) + next if recipient.blocked? send_email_to(recipient, user_role: :affected_user) end diff --git a/decidim-core/lib/tasks/upgrade/clean.rake b/decidim-core/lib/tasks/upgrade/clean.rake index f19b68898a6bb..5912bd348703d 100644 --- a/decidim-core/lib/tasks/upgrade/clean.rake +++ b/decidim-core/lib/tasks/upgrade/clean.rake @@ -10,7 +10,8 @@ namespace :decidim do :"decidim:upgrade:clean:follows", :"decidim:upgrade:clean:categories", :"decidim:upgrade:clean:action_logs", - :"decidim:upgrade:clean:clean_deleted_users" + :"decidim:upgrade:clean:clean_deleted_users", + :"decidim:upgrade:clean:fix_blocked_user_notification" ] desc "Remove data from deleted users" @@ -126,6 +127,19 @@ namespace :decidim do logger.info("===== Deleted #{invalid} invalid resources") end + desc "Update all blocked users notifications_sending_frequency setting" + task fix_blocked_user_notification: :environment do + logger.info("=== Updating all blocked users notifications_sending_frequency ...") + blocked_users = 0 + Decidim::User.blocked.where.not("notifications_sending_frequency = ?", "none").find_each do |blocked_user| + unless blocked_user.notifications_sending_frequency == "none" + blocked_user.update(notifications_sending_frequency: "none") + blocked_users += 1 + end + end + logger.info("===== Updated #{blocked_users} blocked users") + end + def logger @logger ||= Logger.new($stdout) end diff --git a/decidim-core/spec/services/decidim/email_notification_generator_spec.rb b/decidim-core/spec/services/decidim/email_notification_generator_spec.rb index 6dc42d2fd6a50..864ed74a321a7 100644 --- a/decidim-core/spec/services/decidim/email_notification_generator_spec.rb +++ b/decidim-core/spec/services/decidim/email_notification_generator_spec.rb @@ -105,6 +105,15 @@ it_behaves_like "does not enqueue the job" end + + context "when the user is blocked" do + before do + recipient.update!(blocked: true, blocked_at: Time.now.utc) + follower.update!(blocked: true, blocked_at: Time.now.utc) + end + + it_behaves_like "does not enqueue the job" + end end end diff --git a/decidim-core/spec/tasks/upgrade/clean_blocked_users_notifications_spec.rb b/decidim-core/spec/tasks/upgrade/clean_blocked_users_notifications_spec.rb new file mode 100644 index 0000000000000..c6d4ee863c79a --- /dev/null +++ b/decidim-core/spec/tasks/upgrade/clean_blocked_users_notifications_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe "rake decidim:upgrade:clean:fix_blocked_user_notification", type: :task do + context "when executing task" do + it "does not throw exceptions keys" do + expect do + Rake::Task[:"decidim:upgrade:clean:fix_blocked_user_notification"].invoke + end.not_to raise_exception + end + end + + context "when there are blocked users" do + let!(:organization) { create(:organization) } + let!(:users) { create_list(:user, 4, :blocked, organization:) } + + it "update all blocked users" do + expect(Decidim::User.blocked.where.not("notifications_sending_frequency = ?", "none").count).to eq(4) + expect { task.execute }.not_to raise_error + expect(Decidim::User.blocked.where.not("notifications_sending_frequency = ?", "none").count).to eq(0) + end + end +end