Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/deleted users follow #1226

Closed
wants to merge 9 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,41 @@ def extra_params
}
}
end

def run_after_hooks
# When private user is destroyed, a hook to destroy the follows of user on private non transparent assembly
# or private participatory process and the follows of their children
space = resource.privatable_to_type.constantize.find(resource.privatable_to_id)

return unless space.private_space?

return if space.respond_to?("is_transparent") && space.is_transparent?

ids = []
follows = Decidim::Follow.where(user: resource.decidim_user_id)
ids << find_space_follow_id(follows, resource, space)
ids << find_children_follows_ids(follows, space)
follows.where(id: ids.flatten.compact).destroy_all if ids.present?
end

def find_space_follow_id(follows, resource, space)
follows.where(decidim_followable_type: resource.privatable_to_type)
.where(decidim_followable_id: space.id)
&.first&.id
end

def find_children_follows_ids(follows, space)
follows.map do |follow|
object = find_object_followed(follow).presence
next unless object.respond_to?("decidim_component_id")

follow.id if space.components.ids.include?(object.decidim_component_id)
end
end

def find_object_followed(follow)
follow.decidim_followable_type.constantize.find(follow.decidim_followable_id)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,69 @@ module Decidim::Admin
action_log = Decidim::ActionLog.last
expect(action_log.version).to be_nil
end

context "when assembly is not transparent" do
let(:normal_user) { create(:user, organization:) }
let(:assembly) { create(:assembly, :private, :opaque, :published, organization: user.organization) }
let!(:participatory_space_private_user) { create(:participatory_space_private_user, user: normal_user, privatable_to: assembly) }

context "and user follows assembly" do
let!(:follow) { create(:follow, followable: assembly, user: normal_user) }

it "destroys the follow" do
expect(Decidim::Follow.where(user: normal_user).count).to eq(1)
expect do
subject.call
end.to change(Decidim::Follow, :count).by(-1)
end

context "and user follows meeting belonging to assembly" do
let(:meetings_component) { create(:component, manifest_name: "meetings", participatory_space: assembly) }

it "destroys all follows" do
meeting = Decidim::Meetings::Meeting.create!(title: generate_localized_title(:meeting_title, skip_injection: false),
description: generate_localized_description(:meeting_description, skip_injection: false),
component: meetings_component, author: user)
create(:follow, followable: meeting, user: normal_user)
expect(Decidim::Follow.where(user: normal_user).count).to eq(2)
expect do
subject.call
end.to change(Decidim::Follow, :count).by(-2)
end
end
end
end

context "when participatory process is private" do
let(:normal_user) { create(:user, organization:) }
let(:participatory_process) { create(:participatory_process, :private, :published, organization: user.organization) }
let!(:participatory_space_private_user) { create(:participatory_space_private_user, user: normal_user, privatable_to: participatory_process) }

context "and user follows process" do
let!(:follow) { create(:follow, followable: participatory_process, user: normal_user) }

it "destroys the follow" do
expect(Decidim::Follow.where(user: normal_user).count).to eq(1)
expect do
subject.call
end.to change(Decidim::Follow, :count).by(-1)
end

context "and user follows meeting belonging to process" do
let(:meetings_component) { create(:component, manifest_name: "meetings", participatory_space: participatory_process) }

it "destroys all follows" do
meeting = Decidim::Meetings::Meeting.create!(title: generate_localized_title(:meeting_title, skip_injection: false),
description: generate_localized_description(:meeting_description, skip_injection: false),
component: meetings_component, author: user)
create(:follow, followable: meeting, user: normal_user)
expect(Decidim::Follow.where(user: normal_user).count).to eq(2)
expect do
subject.call
end.to change(Decidim::Follow, :count).by(-2)
end
end
end
end
end
end
Loading