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

Fix unarchived episodes published #976

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions app/models/apple/publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,30 @@ def poll!(eps = episodes_to_sync)
end
end

def publish!(eps = episodes_to_sync)
Copy link
Member Author

Choose a reason for hiding this comment

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

Removing this default parameter fixes up the caching of episode state. So when we un-archive the episode, the subsequent call to episodes_to_sync then freshly filters episode state (derived from the private feed).

def publish!
show.sync!
raise "Missing Show!" unless show.apple_id.present?

# delete or unpublished episodes
# Archive deleted or unpublished episodes.
# These episodes are no longer in the private feed.
poll_episodes!(episodes_to_archive)
archive!(episodes_to_archive)
show.reload
Copy link
Member Author

Choose a reason for hiding this comment

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

These calls are only needed when episodes move from apple_new? == true to apple_new? == false. Or when an episode moves in and out of a private feed (which should not be happening during a publishing flow)


# Un-archive episodes that are re-published.
# These episodes are in the private feed.
# Unarchived episodes are converted to "DRAFTING" state.
poll_episodes!(episodes_to_unarchive)
unarchive!(episodes_to_unarchive)
show.reload

Rails.logger.tagged("Apple::Publisher#publish!") do
# Calculate the episodes_to_sync based on the current state of the private feed
deliver_and_publish!(episodes_to_sync)

# success
SyncLog.log!(feeder_id: public_feed.id, feeder_type: :feeds, external_id: show.apple_id, api_response: {success: true})
end

def deliver_and_publish!(eps)
Rails.logger.tagged("Apple::Publisher#deliver_and_publish!") do
eps.each_slice(PUBLISH_CHUNK_LEN) do |eps|
# Soft delete any existing delivery and delivery files
prepare_for_delivery!(eps)
Expand All @@ -145,9 +155,6 @@ def publish!(eps = episodes_to_sync)
raise_delivery_processing_errors(eps)
end
end

# success
SyncLog.log!(feeder_id: public_feed.id, feeder_type: :feeds, external_id: show.apple_id, api_response: {success: true})
end

def prepare_for_delivery!(eps)
Expand Down
51 changes: 51 additions & 0 deletions test/models/apple/publisher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,57 @@
assert_equal [], apple_publisher.episodes_to_unarchive.map(&:feeder_id)
end
end

describe "archived episodes with media already delivered to apple" do
let(:apple_episode_api_response) {
build(:apple_episode_api_response,
apple_hosted_audio_state: Apple::Episode::AUDIO_ASSET_SUCCESS,
publishing_state: "ARCHIVED",
apple_episode_id: "123")
}

let(:apple_episode) { build(:uploaded_apple_episode, show: apple_publisher.show, feeder_episode: episode, api: apple_api) }

it "includes the unarchived episode in the episodes to sync" do
assert apple_episode.audio_asset_state_success?
assert apple_episode.archived?
assert apple_episode.has_delivery?

assert_equal [apple_episode.feeder_id], apple_publisher.episodes_to_unarchive.map(&:feeder_id)
# we can't sync archived episodes
assert_equal [], apple_publisher.episodes_to_sync.map(&:feeder_id)

unarchiver = ->(eps) do
eps.map do |ep|
sl = ep.apple_sync_log
attrs = sl.api_response
attrs["api_response"]["val"]["data"]["attributes"]["publishingState"] = "DRAFTING"
sl.update!(api_response: attrs)
sl
end
end

# assert that this includes our unarchived episode
deliver_and_publish = ->(eps) do
assert_equal [apple_episode.feeder_id], eps.map(&:feeder_id)
true
end

# eliminate calls to the apple api
apple_publisher.show.stub(:sync!, []) do
apple_publisher.stub(:poll_episodes!, []) do
# 1) episodes are unarchived
apple_publisher.stub(:unarchive!, unarchiver) do
# 2) then the unarchived episodes are passed in ready to have
# media uploaded and episode published)
apple_publisher.stub(:deliver_and_publish!, deliver_and_publish) do
apple_publisher.publish!
end
end
end
end
end
end
end
end

Expand Down
Loading