Skip to content

Commit

Permalink
fix: add rake task to delete unwanted follows of ex private users
Browse files Browse the repository at this point in the history
  • Loading branch information
Stef-Rousset committed Jun 18, 2024
1 parent a7c3ab5 commit 6f36aee
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6f36aee

Please sign in to comment.