From 6f36aee606a28c63d44aa385b3b4afc6821191bf Mon Sep 17 00:00:00 2001 From: stephanie rousset Date: Tue, 18 Jun 2024 09:52:49 +0200 Subject: [PATCH] fix: add rake task to delete unwanted follows of ex private users --- RELEASE_NOTES.md | 5 +++ ...lete_follows_of_ex_private_users_task.rake | 42 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 decidim-admin/lib/tasks/delete_follows_of_ex_private_users_task.rake diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index cc5a6d00c69d..9927d0c57eab 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -51,6 +51,11 @@ You can read more about this change on PR [#12616](https://github.com/decidim/de These are one time actions that need to be done after the code is updated in the production database. +To delete the follows of ex private users of non transparent assemblies or processes, run from decidim-admin +```console +bundle exec rake decidim:upgrade:fix_deleted_private_follows +``` + ### 3.1. CarrierWave removal Back in Decidim 0.25 we have added ActiveStorage (via [\#7902](https://github.com/decidim/decidim/pull/7902)) as main uploader instead of CarrierWave. diff --git a/decidim-admin/lib/tasks/delete_follows_of_ex_private_users_task.rake b/decidim-admin/lib/tasks/delete_follows_of_ex_private_users_task.rake new file mode 100644 index 000000000000..fe45adcd7cfa --- /dev/null +++ b/decidim-admin/lib/tasks/delete_follows_of_ex_private_users_task.rake @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +namespace :decidim do + namespace :upgrade do + desc "Delete follows of ex private users" + task fix_deleted_private_follows: :environment do + def find_object(follow) + follow.decidim_followable_type.constantize.find(follow.decidim_followable_id) + end + + def delete_unwanted_follows(model, elements, children_follows) + count = 0 + p "begin deleting follows" + elements.each do |element| + # for each element, find their private users + private_users_ids = Decidim::ParticipatorySpacePrivateUser.where(privatable_to_id: element.id, privatable_to_type: model.to_s).pluck(:decidim_user_id) + # delete follows from non private users + direct_follows_to_delete = Decidim::Follow.where(followable: element.id, decidim_followable_type: model.to_s) + .reject { |follow| private_users_ids.include?(follow.decidim_user_id) } + count += direct_follows_to_delete.size + direct_follows_to_delete.map { |follow| Decidim::Follow.delete(follow.id) } + # children of element + element_components_ids = element.components.ids + children_follows_to_delete = children_follows.select { |follow| element_components_ids.include?(find_object(follow).decidim_component_id) } + .reject { |follow| private_users_ids.include?(follow.decidim_user_id) } + count += children_follows_to_delete.size + children_follows_to_delete.map { |follow| Decidim::Follow.delete(follow.id) } + end + p "#{count} follows have been deleted for #{model} and their children" + end + children_follows = Decidim::Follow.select { |follow| find_object(follow).respond_to?(:decidim_component_id) } + # find private non transparent assemblies + assemblies = Decidim::Assembly.where(private_space: true, is_transparent: false).published + # delete unwanted follows + delete_unwanted_follows(Decidim::Assembly, assemblies, children_follows) + # find private processes + processes = Decidim::ParticipatoryProcess.where(private_space: true).published + # delete unwanted follows + delete_unwanted_follows(Decidim::ParticipatoryProcess, processes, children_follows) + end + end +end