docbrown/app/services/users/delete_podcasts.rb
Jeremy Friesen 0ef5bc6351
Ensuring we delete podcasts when we banish a user (#18063)
Prior to this commit, we may have left lingering podcast entries that
required manual cleanup.  As a bonus, I also added deleting podcasts
when we delete users.

Closes forem/forem#18041

In addition I adjusted rubocop to not complain about the require method call

```
spec/services/users/delete_spec.rb:1:1: C: \
  Style/TopLevelMethodDefinition: \
  Do not define methods at the top-level. \
  (https://rubystyle.guide#top-level-methods)
require "rails_helper"
^^^^^^^^^^^^^^^^^^^^^^
```
2022-07-12 09:18:06 -04:00

31 lines
830 B
Ruby

module Users
module DeletePodcasts
def self.call(user)
return unless user
# We'll want to bust the podcast paths that match our criteria.
paths = []
user.podcast_ownerships.includes(:podcast).find_each do |ownership|
ownership.destroy
podcast = ownership.podcast
# Guard against ownership without podcast
next if podcast.blank?
# We have another owner, don't delete the podcast.
next if PodcastOwnership.where(podcast: podcast).where.not(user_id: user.id).exists?
# No sense keeping the roles around.
Role.where(resource: podcast).destroy_all
paths << podcast.path if podcast.path.present?
podcast.destroy
end
paths.each do |path|
EdgeCache::BustPodcast.call(path)
end
end
end
end