forked from decidim/decidim
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add rake task to delete unwanted follows of ex private users
- Loading branch information
1 parent
a7c3ab5
commit 6f36aee
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
decidim-admin/lib/tasks/delete_follows_of_ex_private_users_task.rake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |