Removed collection_type user input instead inferring from yt-dlp response

This commit is contained in:
Kieran Eglin 2024-02-24 16:07:57 -08:00
parent 52c0a1742a
commit 0e61e6000d
No known key found for this signature in database
GPG key ID: 193984967FCF432D
4 changed files with 71 additions and 57 deletions

View file

@ -118,24 +118,20 @@ defmodule Pinchflat.Sources do
defp add_source_details_by_collection_type(source, changeset, source_details) do defp add_source_details_by_collection_type(source, changeset, source_details) do
%Ecto.Changeset{changes: changes} = changeset %Ecto.Changeset{changes: changes} = changeset
collection_type = Ecto.Changeset.get_field(changeset, :collection_type)
collection_changes = collection_changes =
case collection_type do if source_details.playlist_id == source_details.channel_id do
:channel ->
%{ %{
collection_type: :channel,
collection_id: source_details.channel_id, collection_id: source_details.channel_id,
collection_name: source_details.channel_name collection_name: source_details.channel_name
} }
else
:playlist ->
%{ %{
collection_type: :playlist,
collection_id: source_details.playlist_id, collection_id: source_details.playlist_id,
collection_name: source_details.playlist_name collection_name: source_details.playlist_name
} }
_ ->
%{}
end end
change_source(source, Map.merge(changes, collection_changes)) change_source(source, Map.merge(changes, collection_changes))

View file

@ -24,11 +24,4 @@ defmodule PinchflatWeb.Sources.SourceHTML do
{"Monthly", 30 * 24 * 60} {"Monthly", 30 * 24 * 60}
] ]
end end
def friendly_collection_types do
[
{"Channel", "channel"},
{"Playlist", "playlist"}
]
end
end end

View file

@ -19,8 +19,6 @@
label="Media Profile" label="Media Profile"
/> />
<.input field={f[:collection_type]} options={friendly_collection_types()} type="select" label="Source Type" />
<.input <.input
field={f[:index_frequency_minutes]} field={f[:index_frequency_minutes]}
options={friendly_index_frequencies()} options={friendly_index_frequencies()}

View file

@ -32,12 +32,11 @@ defmodule Pinchflat.SourcesTest do
describe "create_source/1" do describe "create_source/1" do
test "creates a source and adds name + ID from runner response for channels" do test "creates a source and adds name + ID from runner response for channels" do
expect(YtDlpRunnerMock, :run, &runner_function_mock/3) expect(YtDlpRunnerMock, :run, &channel_mock/3)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
original_url: "https://www.youtube.com/channel/abc123", original_url: "https://www.youtube.com/channel/abc123"
collection_type: "channel"
} }
assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs) assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs)
@ -46,12 +45,11 @@ defmodule Pinchflat.SourcesTest do
end end
test "creates a source and adds name + ID for playlists" do test "creates a source and adds name + ID for playlists" do
expect(YtDlpRunnerMock, :run, &runner_function_mock/3) expect(YtDlpRunnerMock, :run, &playlist_mock/3)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
original_url: "https://www.youtube.com/playlist?list=abc123", original_url: "https://www.youtube.com/playlist?list=abc123"
collection_type: "playlist"
} }
assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs) assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs)
@ -60,12 +58,11 @@ defmodule Pinchflat.SourcesTest do
end end
test "you can specify a custom friendly_name" do test "you can specify a custom friendly_name" do
expect(YtDlpRunnerMock, :run, &runner_function_mock/3) expect(YtDlpRunnerMock, :run, &channel_mock/3)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
original_url: "https://www.youtube.com/channel/abc123", original_url: "https://www.youtube.com/channel/abc123",
collection_type: "channel",
friendly_name: "some custom name" friendly_name: "some custom name"
} }
@ -75,12 +72,11 @@ defmodule Pinchflat.SourcesTest do
end end
test "friendly name is pulled from collection_name if not specified" do test "friendly name is pulled from collection_name if not specified" do
expect(YtDlpRunnerMock, :run, &runner_function_mock/3) expect(YtDlpRunnerMock, :run, &channel_mock/3)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
original_url: "https://www.youtube.com/channel/abc123", original_url: "https://www.youtube.com/channel/abc123"
collection_type: "channel"
} }
assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs) assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs)
@ -88,6 +84,22 @@ defmodule Pinchflat.SourcesTest do
assert source.friendly_name == "some channel name" assert source.friendly_name == "some channel name"
end end
test "collection_type is inferred from source details" do
expect(YtDlpRunnerMock, :run, &channel_mock/3)
expect(YtDlpRunnerMock, :run, &playlist_mock/3)
valid_attrs = %{
media_profile_id: media_profile_fixture().id,
original_url: "https://www.youtube.com/channel/abc123"
}
assert {:ok, %Source{} = source_1} = Sources.create_source(valid_attrs)
assert {:ok, %Source{} = source_2} = Sources.create_source(valid_attrs)
assert source_1.collection_type == :channel
assert source_2.collection_type == :playlist
end
test "creation with invalid data returns error changeset" do test "creation with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Sources.create_source(@invalid_source_attrs) assert {:error, %Ecto.Changeset{}} = Sources.create_source(@invalid_source_attrs)
end end
@ -97,14 +109,15 @@ defmodule Pinchflat.SourcesTest do
{:ok, {:ok,
Phoenix.json_library().encode!(%{ Phoenix.json_library().encode!(%{
channel: "some channel name", channel: "some channel name",
channel_id: "some_channel_id_12345678" channel_id: "some_channel_id_12345678",
playlist_id: "some_channel_id_12345678",
playlist_title: "some channel name - videos"
})} })}
end) end)
valid_once_attrs = %{ valid_once_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
original_url: "https://www.youtube.com/channel/abc123", original_url: "https://www.youtube.com/channel/abc123"
collection_type: "channel"
} }
assert {:ok, %Source{}} = Sources.create_source(valid_once_attrs) assert {:ok, %Source{}} = Sources.create_source(valid_once_attrs)
@ -116,14 +129,15 @@ defmodule Pinchflat.SourcesTest do
{:ok, {:ok,
Phoenix.json_library().encode!(%{ Phoenix.json_library().encode!(%{
channel: "some channel name", channel: "some channel name",
channel_id: "some_channel_id_12345678" channel_id: "some_channel_id_12345678",
playlist_id: "some_channel_id_12345678",
playlist_title: "some channel name - videos"
})} })}
end) end)
valid_attrs = %{ valid_attrs = %{
name: "some name", name: "some name",
original_url: "https://www.youtube.com/channel/abc123", original_url: "https://www.youtube.com/channel/abc123"
collection_type: "channel"
} }
source_1_attrs = Map.merge(valid_attrs, %{media_profile_id: media_profile_fixture().id}) source_1_attrs = Map.merge(valid_attrs, %{media_profile_id: media_profile_fixture().id})
@ -134,12 +148,11 @@ defmodule Pinchflat.SourcesTest do
end end
test "creation will schedule the indexing task" do test "creation will schedule the indexing task" do
expect(YtDlpRunnerMock, :run, &runner_function_mock/3) expect(YtDlpRunnerMock, :run, &channel_mock/3)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
original_url: "https://www.youtube.com/channel/abc123", original_url: "https://www.youtube.com/channel/abc123"
collection_type: "channel"
} }
assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs) assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs)
@ -158,7 +171,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "updating the original_url will re-fetch the source details for channels" do test "updating the original_url will re-fetch the source details for channels" do
expect(YtDlpRunnerMock, :run, &runner_function_mock/3) expect(YtDlpRunnerMock, :run, &channel_mock/3)
source = source_fixture() source = source_fixture()
update_attrs = %{original_url: "https://www.youtube.com/channel/abc123"} update_attrs = %{original_url: "https://www.youtube.com/channel/abc123"}
@ -169,9 +182,9 @@ defmodule Pinchflat.SourcesTest do
end end
test "updating the original_url will re-fetch the source details for playlists" do test "updating the original_url will re-fetch the source details for playlists" do
expect(YtDlpRunnerMock, :run, &runner_function_mock/3) expect(YtDlpRunnerMock, :run, &playlist_mock/3)
source = source_fixture(collection_type: "playlist") source = source_fixture()
update_attrs = %{original_url: "https://www.youtube.com/playlist?list=abc123"} update_attrs = %{original_url: "https://www.youtube.com/playlist?list=abc123"}
assert {:ok, %Source{} = source} = Sources.update_source(source, update_attrs) assert {:ok, %Source{} = source} = Sources.update_source(source, update_attrs)
@ -180,7 +193,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "not updating the original_url will not re-fetch the source details" do test "not updating the original_url will not re-fetch the source details" do
expect(YtDlpRunnerMock, :run, 0, &runner_function_mock/3) expect(YtDlpRunnerMock, :run, 0, &channel_mock/3)
source = source_fixture() source = source_fixture()
update_attrs = %{name: "some updated name"} update_attrs = %{name: "some updated name"}
@ -267,14 +280,14 @@ defmodule Pinchflat.SourcesTest do
describe "change_source_from_url/2" do describe "change_source_from_url/2" do
test "it returns a changeset" do test "it returns a changeset" do
stub(YtDlpRunnerMock, :run, &runner_function_mock/3) stub(YtDlpRunnerMock, :run, &channel_mock/3)
source = source_fixture() source = source_fixture()
assert %Ecto.Changeset{} = Sources.change_source_from_url(source, %{}) assert %Ecto.Changeset{} = Sources.change_source_from_url(source, %{})
end end
test "it does not fetch source details if the original_url isn't in the changeset" do test "it does not fetch source details if the original_url isn't in the changeset" do
expect(YtDlpRunnerMock, :run, 0, &runner_function_mock/3) expect(YtDlpRunnerMock, :run, 0, &channel_mock/3)
changeset = Sources.change_source_from_url(%Source{}, %{name: "some updated name"}) changeset = Sources.change_source_from_url(%Source{}, %{name: "some updated name"})
@ -282,7 +295,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "it fetches source details if the original_url is in the changeset" do test "it fetches source details if the original_url is in the changeset" do
expect(YtDlpRunnerMock, :run, &runner_function_mock/3) expect(YtDlpRunnerMock, :run, &channel_mock/3)
changeset = changeset =
Sources.change_source_from_url(%Source{}, %{ Sources.change_source_from_url(%Source{}, %{
@ -293,13 +306,13 @@ defmodule Pinchflat.SourcesTest do
end end
test "it adds source details to the changeset, keeping the orignal details" do test "it adds source details to the changeset, keeping the orignal details" do
expect(YtDlpRunnerMock, :run, &runner_function_mock/3) expect(YtDlpRunnerMock, :run, &channel_mock/3)
media_profile = media_profile_fixture() media_profile = media_profile_fixture()
media_profile_id = media_profile.id media_profile_id = media_profile.id
changeset = changeset =
Sources.change_source_from_url(%Source{collection_type: :channel}, %{ Sources.change_source_from_url(%Source{}, %{
original_url: "https://www.youtube.com/channel/abc123", original_url: "https://www.youtube.com/channel/abc123",
media_profile_id: media_profile.id media_profile_id: media_profile.id
}) })
@ -329,15 +342,29 @@ defmodule Pinchflat.SourcesTest do
end end
end end
defp runner_function_mock(_url, _opts, _ot) do defp playlist_mock(_url, _opts, _ot) do
{ {
:ok, :ok,
Phoenix.json_library().encode!(%{ Phoenix.json_library().encode!(%{
channel: "some channel name", channel: nil,
channel_id: "some_channel_id_#{:rand.uniform(1_000_000)}", channel_id: nil,
playlist_id: "some_playlist_id_#{:rand.uniform(1_000_000)}", playlist_id: "some_playlist_id_#{:rand.uniform(1_000_000)}",
playlist_title: "some playlist name" playlist_title: "some playlist name"
}) })
} }
end end
defp channel_mock(_url, _opts, _ot) do
channel_id = "some_channel_id_#{:rand.uniform(1_000_000)}"
{
:ok,
Phoenix.json_library().encode!(%{
channel: "some channel name",
channel_id: channel_id,
playlist_id: channel_id,
playlist_title: "some channel name - videos"
})
}
end
end end