Skip to content

Commit

Permalink
Add apple feed subclass and policy
Browse files Browse the repository at this point in the history
  • Loading branch information
radical-ube committed May 2, 2024
1 parent f1fe963 commit f82da83
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 40 deletions.
38 changes: 0 additions & 38 deletions app/models/apple/config.rb
Expand Up @@ -2,10 +2,6 @@

module Apple
class Config < ApplicationRecord
DEFAULT_FEED_SLUG = "apple-delegated-delivery-subscriptions"
DEFAULT_TITLE = "Apple Delegated Delivery Subscriptions"
DEFAULT_AUDIO_FORMAT = {"f" => "flac", "b" => 16, "c" => 2, "s" => 44100}.freeze

belongs_to :feed
belongs_to :key, class_name: "Apple::Key", optional: true, validate: true, autosave: true

Expand All @@ -24,40 +20,6 @@ class Config < ApplicationRecord
delegate :public_feed, to: :podcast, allow_nil: true
alias_method :private_feed, :feed

def self.find_or_build_private_feed(podcast)
if (existing = podcast.feeds.find_by(slug: DEFAULT_FEED_SLUG, title: DEFAULT_TITLE))
# TODO, handle partitions on apple models via the apple_config
# Until then it's not safe to have multiple apple_configs for the same podcast
Rails.logger.error("Found existing private feed for #{podcast.title}!")
Rails.logger.error("Do you want to continue? (y/N)")
raise "Stopping find_or_build_private_feed" if $stdin.gets.chomp.downcase != "y"

return existing
end
default_feed = podcast.default_feed

Feed.new(
display_episodes_count: default_feed.display_episodes_count,
slug: DEFAULT_FEED_SLUG,
title: DEFAULT_TITLE,
audio_format: DEFAULT_AUDIO_FORMAT,
include_zones: ["billboard", "sonic_id"],
tokens: [FeedToken.new(label: DEFAULT_TITLE)],
podcast: podcast
)
end

# TODO: this a helper for onboarding via console, retrofit when the UX catches up
def self.build_apple_config(podcast, key)
if podcast.apple_config
Rails.logger.error("Found existing apple config for #{podcast.title}!")
Rails.logger.error("Do you want to continue? (y/N)")
raise "Stopping build_apple_config" if $stdin.gets.chomp.downcase != "y"
end

Apple::Config.new(feed: find_or_build_private_feed(podcast), key: key)
end

def self.mark_as_delivered!(apple_publisher)
apple_publisher.episodes_to_sync.each do |episode|
if episode.podcast_container&.needs_delivery? == false
Expand Down
2 changes: 0 additions & 2 deletions app/models/feed.rb
Expand Up @@ -25,8 +25,6 @@ class Feed < ApplicationRecord
alias_attribute :tokens, :feed_tokens
accepts_nested_attributes_for :feed_tokens, allow_destroy: true, reject_if: ->(ft) { ft[:token].blank? }

has_one :apple_config, class_name: "::Apple::Config", dependent: :destroy, autosave: true, validate: true

has_many :feed_images, -> { order("created_at DESC") }, autosave: true, dependent: :destroy, inverse_of: :feed
has_many :itunes_images, -> { order("created_at DESC") }, autosave: true, dependent: :destroy, inverse_of: :feed
has_many :itunes_categories, validate: true, autosave: true, dependent: :destroy
Expand Down
53 changes: 53 additions & 0 deletions app/models/feed/apple_subscription.rb
@@ -0,0 +1,53 @@
class Feed::AppleSubscription < Feed
DEFAULT_FEED_SLUG = "apple-delegated-delivery-subscriptions"
DEFAULT_TITLE = "Apple Delegated Delivery Subscriptions"
DEFAULT_AUDIO_FORMAT = {"f" => "flac", "b" => 16, "c" => 2, "s" => 44100}.freeze

has_one :apple_config, class_name: "::Apple::Config", dependent: :destroy, autosave: true, validate: true
has_one :apple_key,
through: :apple_config,
class_name: "Apple::Key",
dependent: :destroy,
foreign_key: :key_id

accepts_nested_attributes_for :apple_config, allow_destroy: true, reject_if: :all_blank
accepts_nested_attributes_for :apple_key, allow_destroy: true, reject_if: :all_blank

def self.model_name
Feed.model_name
end

def self.find_or_build_private_feed(podcast)
if (existing = podcast.feeds.find_by(slug: DEFAULT_FEED_SLUG, title: DEFAULT_TITLE))
# TODO, handle partitions on apple models via the apple_config
# Until then it's not safe to have multiple apple_configs for the same podcast
Rails.logger.error("Found existing private feed for #{podcast.title}!")
Rails.logger.error("Do you want to continue? (y/N)")
raise "Stopping find_or_build_private_feed" if $stdin.gets.chomp.downcase != "y"

return existing
end
default_feed = podcast.default_feed

Feed.new(
display_episodes_count: default_feed.display_episodes_count,
slug: DEFAULT_FEED_SLUG,
title: DEFAULT_TITLE,
audio_format: DEFAULT_AUDIO_FORMAT,
include_zones: ["billboard", "sonic_id"],
tokens: [FeedToken.new(label: DEFAULT_TITLE)],
podcast: podcast
)
end

# TODO: this a helper for onboarding via console, retrofit when the UX catches up
def self.build_apple_config(podcast, key)
if podcast.apple_config
Rails.logger.error("Found existing apple config for #{podcast.title}!")
Rails.logger.error("Do you want to continue? (y/N)")
raise "Stopping build_apple_config" if $stdin.gets.chomp.downcase != "y"
end

Apple::Config.new(feed: find_or_build_private_feed(podcast), key: key)
end
end
5 changes: 5 additions & 0 deletions app/policies/feed/apple_subscription_policy.rb
@@ -0,0 +1,5 @@
class Feed::AppleSubscriptionPolicy < ApplicationPolicy
def show?
true
end
end

0 comments on commit f82da83

Please sign in to comment.