From d2f91a8253d79d08ed3e9b9c682f6cea6bd2f13c Mon Sep 17 00:00:00 2001 From: Kieran Date: Thu, 23 May 2024 10:24:52 -0700 Subject: [PATCH] [Enhancement] Added "other" tab to see media that's not set for download (#258) * Added tab for other (non-downloaded and non-pending) media * Added column for tracking if media was manually ignored --- .../source_html/media_item_table_live.ex | 37 ++++++++++++++++++- .../sources/source_html/show.html.heex | 13 +++++-- .../sources/media_item_table_live_test.exs | 30 +++++++++++++-- 3 files changed, 71 insertions(+), 9 deletions(-) diff --git a/lib/pinchflat_web/controllers/sources/source_html/media_item_table_live.ex b/lib/pinchflat_web/controllers/sources/source_html/media_item_table_live.ex index 7ffac3c..7e732c1 100644 --- a/lib/pinchflat_web/controllers/sources/source_html/media_item_table_live.ex +++ b/lib/pinchflat_web/controllers/sources/source_html/media_item_table_live.ex @@ -30,6 +30,9 @@ defmodule Pinchflat.Sources.MediaItemTableLive do <%= StringUtils.truncate(media_item.title, 50) %> + <:col :let={media_item} :if={@media_state == "other"} label="Manually Ignored?"> + <.icon name={if media_item.prevent_download, do: "hero-check", else: "hero-x-mark"} /> + <:col :let={media_item} label="" class="flex justify-end"> <.icon_link href={~p"/sources/#{@source.id}/media/#{media_item.id}/edit"} icon="hero-pencil-square" class="mr-4" /> @@ -42,13 +45,25 @@ defmodule Pinchflat.Sources.MediaItemTableLive do end def mount(_params, session, socket) do + PinchflatWeb.Endpoint.subscribe("media_table") + page = 1 media_state = session["media_state"] source = Sources.get_source!(session["source_id"]) base_query = generate_base_query(source, media_state) pagination_attrs = fetch_pagination_attributes(base_query, page) - {:ok, assign(socket, Map.merge(pagination_attrs, %{base_query: base_query, source: source}))} + new_assigns = + Map.merge( + pagination_attrs, + %{ + base_query: base_query, + source: source, + media_state: media_state + } + ) + + {:ok, assign(socket, new_assigns)} end def handle_event("page_change", %{"direction" => direction}, %{assigns: assigns} = socket) do @@ -59,7 +74,13 @@ defmodule Pinchflat.Sources.MediaItemTableLive do {:noreply, assign(socket, new_assigns)} end - def handle_event("reload_page", _params, %{assigns: assigns} = socket) do + def handle_event("reload_page", _params, socket) do + PinchflatWeb.Endpoint.broadcast("media_table", "reload", nil) + + {:noreply, socket} + end + + def handle_info(%{topic: "media_table", event: "reload"}, %{assigns: assigns} = socket) do new_assigns = fetch_pagination_attributes(assigns.base_query, assigns.page) {:noreply, assign(socket, new_assigns)} @@ -95,4 +116,16 @@ defmodule Pinchflat.Sources.MediaItemTableLive do |> where(^dynamic(^MediaQuery.for_source(source) and ^MediaQuery.downloaded())) |> order_by(desc: :id) end + + defp generate_base_query(source, "other") do + MediaQuery.new() + |> MediaQuery.require_assoc(:media_profile) + |> where( + ^dynamic( + ^MediaQuery.for_source(source) and + (not (^MediaQuery.downloaded()) and not (^MediaQuery.pending())) + ) + ) + |> order_by(desc: :id) + end end diff --git a/lib/pinchflat_web/controllers/sources/source_html/show.html.heex b/lib/pinchflat_web/controllers/sources/source_html/show.html.heex index 00ad375..26fdc96 100644 --- a/lib/pinchflat_web/controllers/sources/source_html/show.html.heex +++ b/lib/pinchflat_web/controllers/sources/source_html/show.html.heex @@ -36,21 +36,28 @@ <.list_items_from_map map={Map.from_struct(@source)} /> - <:tab title="Pending Media" id="pending"> + <:tab title="Pending" id="pending"> <%= live_render( @conn, Pinchflat.Sources.MediaItemTableLive, session: %{"source_id" => @source.id, "media_state" => "pending"} ) %> - <:tab title="Downloaded Media" id="downloaded"> + <:tab title="Downloaded" id="downloaded"> <%= live_render( @conn, Pinchflat.Sources.MediaItemTableLive, session: %{"source_id" => @source.id, "media_state" => "downloaded"} ) %> - <:tab title="Pending Tasks" id="tasks"> + <:tab title="Other" id="other"> + <%= live_render( + @conn, + Pinchflat.Sources.MediaItemTableLive, + session: %{"source_id" => @source.id, "media_state" => "other"} + ) %> + + <:tab title="Tasks" id="tasks"> <%= if match?([_|_], @pending_tasks) do %> <.table rows={@pending_tasks} table_class="text-black dark:text-white"> <:col :let={task} label="Worker"> diff --git a/test/pinchflat_web/controllers/sources/media_item_table_live_test.exs b/test/pinchflat_web/controllers/sources/media_item_table_live_test.exs index da873d6..f584377 100644 --- a/test/pinchflat_web/controllers/sources/media_item_table_live_test.exs +++ b/test/pinchflat_web/controllers/sources/media_item_table_live_test.exs @@ -4,6 +4,7 @@ defmodule PinchflatWeb.Sources.MediaItemTableLiveTest do import Phoenix.LiveViewTest import Pinchflat.MediaFixtures import Pinchflat.SourcesFixtures + import Pinchflat.ProfilesFixtures alias Pinchflat.Sources.MediaItemTableLive @@ -34,10 +35,8 @@ defmodule PinchflatWeb.Sources.MediaItemTableLiveTest do describe "media_state" do test "shows pending media when pending", %{conn: conn, source: source} do - downloaded_media_item = media_item_fixture(source_id: source.id, title: "DL-#{Enum.random(0..9999)}") - - pending_media_item = - media_item_fixture(source_id: source.id, media_filepath: nil, title: "P-#{Enum.random(0..9999)}") + downloaded_media_item = media_item_fixture(source_id: source.id) + pending_media_item = media_item_fixture(source_id: source.id, media_filepath: nil) {:ok, _view, html} = live_isolated(conn, MediaItemTableLive, session: create_session(source, "pending")) @@ -54,6 +53,29 @@ defmodule PinchflatWeb.Sources.MediaItemTableLiveTest do assert html =~ downloaded_media_item.title refute html =~ pending_media_item.title end + + test "shows records that aren't pending or downloaded when other", %{conn: conn} do + media_profile = media_profile_fixture(shorts_behaviour: :exclude) + source = source_fixture(media_profile_id: media_profile.id) + + downloaded_media_item = media_item_fixture(source_id: source.id) + pending_media_item = media_item_fixture(source_id: source.id, media_filepath: nil) + other_media_item = media_item_fixture(source_id: source.id, media_filepath: nil, short_form_content: true) + + {:ok, _view, html} = live_isolated(conn, MediaItemTableLive, session: create_session(source, "other")) + + assert html =~ other_media_item.title + refute html =~ downloaded_media_item.title + refute html =~ pending_media_item.title + end + + test "shows 'Manually Ignored' column when other", %{conn: conn, source: source} do + _media_item = media_item_fixture(source_id: source.id, prevent_download: true, media_filepath: nil) + + {:ok, _view, html} = live_isolated(conn, MediaItemTableLive, session: create_session(source, "other")) + + assert html =~ "Manually Ignored?" + end end defp create_session(source, media_state \\ "pending") do