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

Feat/apple feed subclass #1013

Merged
merged 9 commits into from
May 14, 2024
38 changes: 0 additions & 38 deletions app/models/apple/config.rb
Original file line number Diff line number Diff line change
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)
cavis marked this conversation as resolved.
Show resolved Hide resolved
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)
cavis marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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
cavis marked this conversation as resolved.
Show resolved Hide resolved

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
Original file line number Diff line number Diff line change
@@ -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
cavis marked this conversation as resolved.
Show resolved Hide resolved

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)],
cavis marked this conversation as resolved.
Show resolved Hide resolved
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)
cavis marked this conversation as resolved.
Show resolved Hide resolved
end
end
5 changes: 5 additions & 0 deletions app/policies/feed/apple_subscription_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Feed::AppleSubscriptionPolicy < ApplicationPolicy
def show?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe could leave this out until UI PR? i think this got added in a commit before realizing which branch i was in.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep - I think you can delete this. You shouldn't need it at all, since you set self.model_name to Feed in the model. That causes the child-model to use the policy of the parent.

true
end
end
7 changes: 7 additions & 0 deletions db/migrate/20240502172959_add_type_to_feeds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddTypeToFeeds < ActiveRecord::Migration[7.0]
def change
add_column :feeds, :type, :string

Feed.where(id: Apple::Config.select(:feed_id)).update_all(type: "Feed::AppleSubscription")
end
end
5 changes: 3 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading