Skip to content

Commit

Permalink
refactor: destroy follows in a job
Browse files Browse the repository at this point in the history
  • Loading branch information
Stef-Rousset committed Aug 9, 2024
1 parent 5cfc85e commit 1a5e2b2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,12 @@ def run_after_hooks

return unless space.private_space?

return if space.respond_to?("is_transparent") && space.is_transparent?
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
decidim_user_id = resource.decidim_user_id
privatable_to_type = resource.privatable_to_type

def find_object_followed(follow)
follow.decidim_followable_type.constantize.find(follow.decidim_followable_id)
DestroyPrivateUsersFollowsJob.perform_later(decidim_user_id, privatable_to_type, space)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Decidim
module Admin
# Custom ApplicationJob scoped to the admin panel.
#
class DestroyPrivateUsersFollowsJob < ApplicationJob
queue_as :default

def perform(decidim_user_id, privatable_to_type, space)
follows = Decidim::Follow.where(user: decidim_user_id)
destroy_space_follow(follows, privatable_to_type, space)
destroy_children_follows(follows, space)
end

def destroy_space_follow(follows, privatable_to_type, space)
follows.where(decidim_followable_type: privatable_to_type)
.where(decidim_followable_id: space.id).destroy_all
end

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

follow.destroy if space.component_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

0 comments on commit 1a5e2b2

Please sign in to comment.