Skip to content

Commit

Permalink
Bumped RSS feed limit; fixed ordering (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
kieraneglin authored May 15, 2024
1 parent 2dfdb31 commit d575548
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/pinchflat/podcasts/podcast_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ defmodule Pinchflat.Podcasts.PodcastHelpers do
Returns: [%MediaItem{}]
"""
def persisted_media_items_for(source, opts \\ []) do
limit = Keyword.get(opts, :limit, 500)
limit = Keyword.get(opts, :limit, 1_000)

MediaQuery.new()
|> MediaQuery.for_source(source)
|> MediaQuery.with_media_filepath()
|> order_by(desc: :upload_date)
|> Repo.maybe_limit(limit)
|> Repo.all()
|> Enum.filter(fn media_item -> File.exists?(media_item.media_filepath) end)
Expand Down
4 changes: 2 additions & 2 deletions lib/pinchflat/podcasts/rss_feed_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ defmodule Pinchflat.Podcasts.RssFeedBuilder do
Only MediaItems that have been persisted will be included in the feed.
## Options:
- `:limit` - The maximum number of media items to include in the feed. Defaults to 300.
- `:limit` - The maximum number of media items to include in the feed. Defaults to 2,000.
Returns an XML document as a string.
"""
def build(source, opts \\ []) do
limit = Keyword.get(opts, :limit, 300)
limit = Keyword.get(opts, :limit, 2_000)
url_base = Keyword.get(opts, :url_base, PinchflatWeb.Endpoint.url())

media_items = PodcastHelpers.persisted_media_items_for(source, limit: limit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule PinchflatWeb.Podcasts.PodcastController do
def rss_feed(conn, %{"uuid" => uuid}) do
source = Repo.get_by!(Source, uuid: uuid)
url_base = url(conn, ~p"/")
xml = RssFeedBuilder.build(source, limit: 300, url_base: url_base)
xml = RssFeedBuilder.build(source, limit: 2_000, url_base: url_base)

conn
|> put_resp_content_type("application/rss+xml")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Pinchflat.Repo.Migrations.AddDateIndexesToMediaItems do
use Ecto.Migration

def change do
create(index(:media_items, [:media_downloaded_at]))
create(index(:media_items, [:media_redownloaded_at]))
end
end
10 changes: 10 additions & 0 deletions test/pinchflat/podcasts/podcast_helpers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ defmodule Pinchflat.Podcasts.PodcastHelpersTest do

assert [] = PodcastHelpers.persisted_media_items_for(source, limit: 0)
end

test "orders by upload date where newest is first" do
source = source_fixture()

oldest = media_item_with_attachments(%{source_id: source.id, upload_date: now_minus(2, :day)})
current = media_item_with_attachments(%{source_id: source.id, upload_date: now()})
older = media_item_with_attachments(%{source_id: source.id, upload_date: now_minus(1, :days)})

assert [^current, ^older, ^oldest] = PodcastHelpers.persisted_media_items_for(source)
end
end

describe "select_cover_image/2" do
Expand Down

0 comments on commit d575548

Please sign in to comment.