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 #1224

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ def extra_params
}
}
end

def run_after_hooks
# A hook to destroy the follows of user on private non transparent assembly and its children
# when private user is destroyed
return unless resource.privatable_to_type == "Decidim::Assembly"

assembly = Decidim::Assembly.find(resource.privatable_to_id)
return unless assembly.private_space == true && assembly.is_transparent == false

user = Decidim::User.find(resource.decidim_user_id)
ids = []
ids << Decidim::Follow.where(user:)
.where(decidim_followable_type: "Decidim::Assembly")
.where(decidim_followable_id: assembly.id)
.first.id
children_ids = Decidim::Follow.where(user:)
.select { |follow| find_object(follow).respond_to?("decidim_component_id") }
.select { |follow| assembly.components.ids.include?(find_object(follow).decidim_component_id) }
.map(&:id)
ids << children_ids
Decidim::Follow.where(user:).where(id: ids.flatten).destroy_all if ids.present?
end

def find_object(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
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "spec_helper"
require "decidim/meetings/test/factories"

module Decidim::Admin
describe DestroyParticipatorySpacePrivateUser do
Expand Down Expand Up @@ -37,5 +38,33 @@ 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)
subject.call
expect(Decidim::Follow.where(user: normal_user).count).to eq(0)
end

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

it "destroys all follows" do
expect(Decidim::Follow.where(user: normal_user).count).to eq(2)
subject.call
expect(Decidim::Follow.where(user: normal_user).count).to eq(0)
end
end
end
end
end
end
Loading