Skip to content

Commit

Permalink
Extract the apple delivery status off the episode model
Browse files Browse the repository at this point in the history
  • Loading branch information
svevang committed Oct 3, 2023
1 parent a0887b7 commit e5d6cf4
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/models/apple/episode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ def needs_delivery?
return true if missing_container?

# TODO: probe for episode media version
podcast_container&.needs_delivery? || feeder_episode.needs_apple_delivery?
podcast_container&.needs_delivery? || feeder_episode.apple_needs_delivery?
end

def has_delivery?
Expand Down
5 changes: 5 additions & 0 deletions app/models/apple/episode_delivery_status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Apple
class EpisodeDeliveryStatus < ApplicationRecord
belongs_to :episode, class_name: "::Episode"
end
end
2 changes: 1 addition & 1 deletion app/models/apple/publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def mark_delivery_files_uploaded!(eps)
# update the feeder episode to indicate that delivery is no longer needed
eps.each do |ep|
Rails.logger.info("Marking episode as no longer needing delivery", {episode_id: ep.feeder_episode.id})
ep.feeder_episode.update!(needs_apple_delivery: false)
ep.feeder_episode.apple_has_delivery!
end

Rails.logger.info("Updated remote container references for episodes.", {count: res.length})
Expand Down
23 changes: 20 additions & 3 deletions app/models/episode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Episode < ApplicationRecord
class_name: "Apple::PodcastDelivery"
has_many :apple_podcast_delivery_files, through: :apple_podcast_deliveries, source: :podcast_delivery_files,
class_name: "Apple::PodcastDeliveryFile"
has_many :apple_episode_delivery_statuses, dependent: :destroy, class_name: "Apple::EpisodeDeliveryStatus"

validates :podcast_id, :guid, presence: true
validates :title, presence: true
Expand Down Expand Up @@ -113,6 +114,24 @@ def apple_delivery_file_errors
apple_delivery_files.map { |p| p.asset_processing_state["errors"] }.flatten
end

def apple_episode_delivery_status
apple_episode_delivery_statuses.reset.order(created_at: :desc).first
end

def apple_needs_delivery?
return true if apple_episode_delivery_status.nil?

apple_episode_delivery_status.delivered == false
end

def apple_needs_delivery!
apple_episode_delivery_statuses.create!(delivered: false)
end

def apple_has_delivery!
apple_episode_delivery_statuses.create!(delivered: true)
end

def self.generate_item_guid(podcast_id, episode_guid)
"prx_#{podcast_id}_#{episode_guid}"
end
Expand Down Expand Up @@ -234,9 +253,7 @@ def apple_mark_for_reupload!
apple_podcast_deliveries.map(&:destroy)
apple_podcast_deliveries.reset
apple_podcast_container&.podcast_deliveries&.reset
# Skip rails validations in case we are in e.g. a controller delete action
# with invalid media
update_column(:needs_apple_delivery, true)
apple_needs_delivery!
end

def publish!
Expand Down
12 changes: 10 additions & 2 deletions db/migrate/20230920153421_add_needs_apple_delivery_to_episode.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
class AddNeedsAppleDeliveryToEpisode < ActiveRecord::Migration[7.0]
def change
add_column :episodes, :needs_apple_delivery, :boolean, default: true
create_table :apple_episode_delivery_statuses do |t|
t.references :episode, null: false, foreign_key: true
t.boolean :delivered, default: false

t.datetime :created_at, null: false
end

execute('CREATE INDEX "index_apple_episode_delivery_statuses_on_episode_id_created_at" ON "apple_episode_delivery_statuses" ("episode_id", "created_at") INCLUDE (delivered);')

reversible do |dir|
dir.up do
Expand All @@ -13,9 +20,10 @@ def change
needs_delivery_episodes = []
apple_episodes.each do |apple_episode|
if (apple_episode.podcast_container.nil? || apple_episode.podcast_container.needs_delivery?) || apple_episode.apple_hosted_audio_asset_container_id.blank?
apple_episode.feeder_episode.apple_needs_delivery!
needs_delivery_episodes << apple_episode.feeder_episode
else
apple_episode.feeder_episode.update!(needs_apple_delivery: false)
apple_episode.feeder_episode.apple_has_delivery!
end
end

Expand Down
12 changes: 10 additions & 2 deletions db/schema.rb

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

2 changes: 1 addition & 1 deletion test/factories/apple_episode_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# set a complete episode factory varient
factory :uploaded_apple_episode do
feeder_episode { create(:episode, needs_apple_delivery: false) }
feeder_episode { create(:episode, apple_episode_delivery_statuses: [Apple::EpisodeDeliveryStatus.new(delivered: true)]) }
transient do
api_response do
build(:apple_episode_api_response,
Expand Down
22 changes: 22 additions & 0 deletions test/models/episode_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,26 @@
assert_empty episode.apple_podcast_deliveries
end
end

describe "#apple_needs_delivery?" do
let(:episode) { create(:episode) }
it "is true by default" do
assert_nil episode.apple_episode_delivery_status
assert episode.apple_needs_delivery?
end

it "can be set to false" do
episode.apple_has_delivery!
refute episode.apple_needs_delivery?
end

it "can be set to true" do
episode.apple_has_delivery!
refute episode.apple_needs_delivery?

# now set it to true
episode.apple_needs_delivery!
assert episode.apple_needs_delivery?
end
end
end

0 comments on commit e5d6cf4

Please sign in to comment.