Skip to content

Commit

Permalink
Merge branch 'main' into feat/apple_config_ui
Browse files Browse the repository at this point in the history
  • Loading branch information
kookster committed Mar 12, 2024
2 parents 234de0a + 6181a4b commit f3cd774
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ GEM
erubi (~> 1.4)
parser (>= 2.4)
smart_properties
bindata (2.4.15)
bindata (2.5.0)
bindex (0.8.1)
bootsnap (1.15.0)
msgpack (~> 1.2)
Expand Down Expand Up @@ -202,7 +202,7 @@ GEM
fuzzyurl (= 0.2.2)
json
uri_template (>= 0.5.2)
i18n (1.14.1)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
importmap-rails (1.1.5)
actionpack (>= 6.0.0)
Expand All @@ -215,7 +215,7 @@ GEM
activesupport (>= 5.0.0)
jmespath (1.6.2)
json (2.7.1)
json-jwt (1.15.3)
json-jwt (1.15.3.1)
activesupport (>= 4.2)
aes_key_wrap
bindata
Expand Down Expand Up @@ -253,7 +253,7 @@ GEM
method_source (1.0.0)
mini_mime (1.1.5)
mini_portile2 (2.8.5)
minitest (5.21.2)
minitest (5.22.2)
minitest-around (0.5.0)
minitest (~> 5.0)
minitest-spec-rails (7.2.0)
Expand Down Expand Up @@ -318,7 +318,7 @@ GEM
pundit (2.3.0)
activesupport (>= 3.0.0)
racc (1.7.3)
rack (2.2.8)
rack (2.2.8.1)
rack-cors (1.1.1)
rack (>= 2.0.0)
rack-test (2.1.0)
Expand Down
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)
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

# 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
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ en:
title:
episode_image: Cover Image
feed_image: Thumbnail Image
itunes_image: Profile Image
itunes_image: Cover Image
imports:
create:
success: Beginning import.
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

0 comments on commit f3cd774

Please sign in to comment.