diff --git a/app/models/apple/config.rb b/app/models/apple/config.rb index d37affa66..5849e7b1e 100644 --- a/app/models/apple/config.rb +++ b/app/models/apple/config.rb @@ -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 @@ -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 diff --git a/app/models/feed.rb b/app/models/feed.rb index 1b0ccdd4d..695411974 100644 --- a/app/models/feed.rb +++ b/app/models/feed.rb @@ -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 diff --git a/app/models/feed/apple_subscription.rb b/app/models/feed/apple_subscription.rb new file mode 100644 index 000000000..4d8224e52 --- /dev/null +++ b/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 diff --git a/app/policies/feed/apple_subscription_policy.rb b/app/policies/feed/apple_subscription_policy.rb new file mode 100644 index 000000000..cff088675 --- /dev/null +++ b/app/policies/feed/apple_subscription_policy.rb @@ -0,0 +1,5 @@ +class Feed::AppleSubscriptionPolicy < ApplicationPolicy + def show? + true + end +end