From 69743262ae42460cb0a447d0d2afb67ee68721ac Mon Sep 17 00:00:00 2001 From: trueChazza Date: Tue, 10 Oct 2023 17:26:35 +1300 Subject: [PATCH 01/18] test: refactor webhooks --- .../controllers/webhooks_controller_test.exs | 44 +++++-------------- 1 file changed, 12 insertions(+), 32 deletions(-) diff --git a/test/media_server_web/controllers/webhooks_controller_test.exs b/test/media_server_web/controllers/webhooks_controller_test.exs index 9a426c2c..7c38eea0 100644 --- a/test/media_server_web/controllers/webhooks_controller_test.exs +++ b/test/media_server_web/controllers/webhooks_controller_test.exs @@ -5,59 +5,39 @@ defmodule MediaServerWeb.WebhooksControllerTest do %{user: MediaServer.AccountsFixtures.user_fixture()} end - test "it should halt", %{conn: conn} do - conn = - post(conn, Routes.webhooks_path(conn, :create, "movie", %{"someKey" => "someValue"}), - token: "someToken" - ) + test "movie should halt", %{conn: conn} do + conn = post(conn, ~p"/api/webhooks/movie?token=someToken", %{"someKey" => "someValue"}) assert conn.status === 403 assert conn.halted end - test "it should fall through", %{conn: conn, user: user} do - conn = - post(conn, Routes.webhooks_path(conn, :create, "movie", %{"someKey" => "someValue"}), - token: user.api_token.token - ) + test "movie should fall through", %{conn: conn, user: user} do + conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"someKey" => "someValue"}) assert conn.status === 200 end - test "it should fall through again", %{conn: conn, user: user} do - conn = - post(conn, Routes.webhooks_path(conn, :create, "movie", %{"eventType" => "someValue"}), - token: user.api_token.token - ) + test "movie should fall through again", %{conn: conn, user: user} do + conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "someValue"}) assert conn.status === 200 end - test "it should create", %{conn: conn, user: user} do - conn = - post(conn, Routes.webhooks_path(conn, :create, "movie", %{"eventType" => "Download"}), - token: user.api_token.token - ) + test "movie should create", %{conn: conn, user: user} do + conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "Download"}) assert conn.status === 201 end - test "it should create on delete", %{conn: conn, user: user} do - conn = - post(conn, Routes.webhooks_path(conn, :create, "movie", %{"eventType" => "MovieDelete"}), - token: user.api_token.token - ) + test "it should movie on delete", %{conn: conn, user: user} do + conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "MovieDelete"}) assert conn.status === 201 end - test "it should create on file delete", %{conn: conn, user: user} do - conn = - post( - conn, - Routes.webhooks_path(conn, :create, "movie", %{"eventType" => "MovieFileDelete"}), - token: user.api_token.token - ) + test "it should movie on file delete", %{conn: conn, user: user} do + conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "MovieFileDelete"}) assert conn.status === 201 end From c6901d039b922577047c72c16f25dcba48d5f4a6 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Wed, 11 Oct 2023 18:02:35 +1300 Subject: [PATCH 02/18] feat: add movie action added --- lib/media_server/application.ex | 5 +++-- lib/media_server/movie_actions.ex | 21 +++++++++++++++++++ .../controllers/webhooks_controller.ex | 2 +- .../controllers/webhooks_controller_test.exs | 6 +++++- 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 lib/media_server/movie_actions.ex diff --git a/lib/media_server/application.ex b/lib/media_server/application.ex index 4c6975d7..b083b2bf 100644 --- a/lib/media_server/application.ex +++ b/lib/media_server/application.ex @@ -20,9 +20,10 @@ defmodule MediaServer.Application do # {MediaServer.Worker, arg} MediaServerWeb.Presence, {DynamicSupervisor, name: MediaServer.DynamicSupervisor}, - MediaServer.UserActions, MediaServer.MoviesIndex, - MediaServer.SeriesIndex + MediaServer.SeriesIndex, + MediaServer.UserActions, + MediaServer.MovieActions ] # See https://hexdocs.pm/elixir/Supervisor.html diff --git a/lib/media_server/movie_actions.ex b/lib/media_server/movie_actions.ex new file mode 100644 index 00000000..77c4f387 --- /dev/null +++ b/lib/media_server/movie_actions.ex @@ -0,0 +1,21 @@ +defmodule MediaServer.MovieActions do + use GenServer + + require Logger + + def start_link(_args) do + GenServer.start_link(__MODULE__, %{}) + end + + def init(state) do + Phoenix.PubSub.subscribe(MediaServer.PubSub, "movie") + + {:ok, state} + end + + def handle_info({:added}, state) do + MediaServer.MoviesIndex.reset() + + {:noreply, state} + end +end \ No newline at end of file diff --git a/lib/media_server_web/controllers/webhooks_controller.ex b/lib/media_server_web/controllers/webhooks_controller.ex index 328095e7..c8a7ea40 100644 --- a/lib/media_server_web/controllers/webhooks_controller.ex +++ b/lib/media_server_web/controllers/webhooks_controller.ex @@ -2,7 +2,7 @@ defmodule MediaServerWeb.WebhooksController do use MediaServerWeb, :controller def create(conn, %{"id" => "movie", "eventType" => "Download"}) do - MediaServer.MoviesIndex.reset() + Phoenix.PubSub.broadcast(MediaServer.PubSub, "movie", {:added}) conn |> send_resp(201, "Ok") diff --git a/test/media_server_web/controllers/webhooks_controller_test.exs b/test/media_server_web/controllers/webhooks_controller_test.exs index 7c38eea0..2709aa6f 100644 --- a/test/media_server_web/controllers/webhooks_controller_test.exs +++ b/test/media_server_web/controllers/webhooks_controller_test.exs @@ -24,9 +24,13 @@ defmodule MediaServerWeb.WebhooksControllerTest do assert conn.status === 200 end - test "movie should create", %{conn: conn, user: user} do + test "it should add movie", %{conn: conn, user: user} do + Phoenix.PubSub.subscribe(MediaServer.PubSub, "movie") + conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "Download"}) + assert_received {:added} + assert conn.status === 201 end From eb214086cb9f1b2d929607f09e923139e511465f Mon Sep 17 00:00:00 2001 From: trueChazza Date: Wed, 11 Oct 2023 18:06:03 +1300 Subject: [PATCH 03/18] feat: add movie action deleted --- lib/media_server/movie_actions.ex | 6 ++++++ lib/media_server_web/controllers/webhooks_controller.ex | 2 +- .../controllers/webhooks_controller_test.exs | 6 +++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/media_server/movie_actions.ex b/lib/media_server/movie_actions.ex index 77c4f387..df9a8729 100644 --- a/lib/media_server/movie_actions.ex +++ b/lib/media_server/movie_actions.ex @@ -18,4 +18,10 @@ defmodule MediaServer.MovieActions do {:noreply, state} end + + def handle_info({:deleted}, state) do + MediaServer.MoviesIndex.reset() + + {:noreply, state} + end end \ No newline at end of file diff --git a/lib/media_server_web/controllers/webhooks_controller.ex b/lib/media_server_web/controllers/webhooks_controller.ex index c8a7ea40..a802462f 100644 --- a/lib/media_server_web/controllers/webhooks_controller.ex +++ b/lib/media_server_web/controllers/webhooks_controller.ex @@ -9,7 +9,7 @@ defmodule MediaServerWeb.WebhooksController do end def create(conn, %{"id" => "movie", "eventType" => "MovieDelete"}) do - MediaServer.MoviesIndex.reset() + Phoenix.PubSub.broadcast(MediaServer.PubSub, "movie", {:deleted}) conn |> send_resp(201, "Ok") diff --git a/test/media_server_web/controllers/webhooks_controller_test.exs b/test/media_server_web/controllers/webhooks_controller_test.exs index 2709aa6f..cd4186d2 100644 --- a/test/media_server_web/controllers/webhooks_controller_test.exs +++ b/test/media_server_web/controllers/webhooks_controller_test.exs @@ -34,9 +34,13 @@ defmodule MediaServerWeb.WebhooksControllerTest do assert conn.status === 201 end - test "it should movie on delete", %{conn: conn, user: user} do + test "it should delete movie", %{conn: conn, user: user} do + Phoenix.PubSub.subscribe(MediaServer.PubSub, "movie") + conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "MovieDelete"}) + assert_received {:deleted} + assert conn.status === 201 end From 119f2883cfc92e371410c137b57a5bf963037d76 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Wed, 11 Oct 2023 18:09:04 +1300 Subject: [PATCH 04/18] feat: add movie action deleted file --- lib/media_server/movie_actions.ex | 6 ++++++ lib/media_server_web/controllers/webhooks_controller.ex | 2 +- .../controllers/webhooks_controller_test.exs | 6 +++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/media_server/movie_actions.ex b/lib/media_server/movie_actions.ex index df9a8729..4ef85525 100644 --- a/lib/media_server/movie_actions.ex +++ b/lib/media_server/movie_actions.ex @@ -24,4 +24,10 @@ defmodule MediaServer.MovieActions do {:noreply, state} end + + def handle_info({:deleted_file}, state) do + MediaServer.MoviesIndex.reset() + + {:noreply, state} + end end \ No newline at end of file diff --git a/lib/media_server_web/controllers/webhooks_controller.ex b/lib/media_server_web/controllers/webhooks_controller.ex index a802462f..b45c4671 100644 --- a/lib/media_server_web/controllers/webhooks_controller.ex +++ b/lib/media_server_web/controllers/webhooks_controller.ex @@ -16,7 +16,7 @@ defmodule MediaServerWeb.WebhooksController do end def create(conn, %{"id" => "movie", "eventType" => "MovieFileDelete"}) do - MediaServer.MoviesIndex.reset() + Phoenix.PubSub.broadcast(MediaServer.PubSub, "movie", {:deleted_file}) conn |> send_resp(201, "Ok") diff --git a/test/media_server_web/controllers/webhooks_controller_test.exs b/test/media_server_web/controllers/webhooks_controller_test.exs index cd4186d2..63b3c163 100644 --- a/test/media_server_web/controllers/webhooks_controller_test.exs +++ b/test/media_server_web/controllers/webhooks_controller_test.exs @@ -44,9 +44,13 @@ defmodule MediaServerWeb.WebhooksControllerTest do assert conn.status === 201 end - test "it should movie on file delete", %{conn: conn, user: user} do + test "it should delete movie file", %{conn: conn, user: user} do + Phoenix.PubSub.subscribe(MediaServer.PubSub, "movie") + conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "MovieFileDelete"}) + assert_received {:deleted_file} + assert conn.status === 201 end From 802b4650f42b2a07b040912bfd2e007c7fb2b759 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Wed, 11 Oct 2023 18:19:25 +1300 Subject: [PATCH 05/18] feat: add series actions --- lib/media_server/application.ex | 3 +- lib/media_server/series_actions.ex | 33 +++++++++++++ .../controllers/webhooks_controller.ex | 6 +-- .../controllers/webhooks_controller_test.exs | 48 ++++++++----------- 4 files changed, 58 insertions(+), 32 deletions(-) create mode 100644 lib/media_server/series_actions.ex diff --git a/lib/media_server/application.ex b/lib/media_server/application.ex index b083b2bf..7e95b212 100644 --- a/lib/media_server/application.ex +++ b/lib/media_server/application.ex @@ -23,7 +23,8 @@ defmodule MediaServer.Application do MediaServer.MoviesIndex, MediaServer.SeriesIndex, MediaServer.UserActions, - MediaServer.MovieActions + MediaServer.MovieActions, + MediaServer.SeriesActions ] # See https://hexdocs.pm/elixir/Supervisor.html diff --git a/lib/media_server/series_actions.ex b/lib/media_server/series_actions.ex new file mode 100644 index 00000000..92dca9d8 --- /dev/null +++ b/lib/media_server/series_actions.ex @@ -0,0 +1,33 @@ +defmodule MediaServer.SeriesActions do + use GenServer + + require Logger + + def start_link(_args) do + GenServer.start_link(__MODULE__, %{}) + end + + def init(state) do + Phoenix.PubSub.subscribe(MediaServer.PubSub, "series") + + {:ok, state} + end + + def handle_info({:added}, state) do + MediaServer.SeriesIndex.reset() + + {:noreply, state} + end + + def handle_info({:deleted}, state) do + MediaServer.SeriesIndex.reset() + + {:noreply, state} + end + + def handle_info({:deleted_episode_file}, state) do + MediaServer.SeriesIndex.reset() + + {:noreply, state} + end +end \ No newline at end of file diff --git a/lib/media_server_web/controllers/webhooks_controller.ex b/lib/media_server_web/controllers/webhooks_controller.ex index b45c4671..09616779 100644 --- a/lib/media_server_web/controllers/webhooks_controller.ex +++ b/lib/media_server_web/controllers/webhooks_controller.ex @@ -23,21 +23,21 @@ defmodule MediaServerWeb.WebhooksController do end def create(conn, %{"id" => "series", "eventType" => "Download"}) do - MediaServer.SeriesIndex.reset() + Phoenix.PubSub.broadcast(MediaServer.PubSub, "series", {:added}) conn |> send_resp(201, "Ok") end def create(conn, %{"id" => "series", "eventType" => "SeriesDelete"}) do - MediaServer.SeriesIndex.reset() + Phoenix.PubSub.broadcast(MediaServer.PubSub, "series", {:deleted}) conn |> send_resp(201, "Ok") end def create(conn, %{"id" => "series", "eventType" => "EpisodeFileDelete"}) do - MediaServer.SeriesIndex.reset() + Phoenix.PubSub.broadcast(MediaServer.PubSub, "series", {:deleted_episode_file}) conn |> send_resp(201, "Ok") diff --git a/test/media_server_web/controllers/webhooks_controller_test.exs b/test/media_server_web/controllers/webhooks_controller_test.exs index 63b3c163..c25e1393 100644 --- a/test/media_server_web/controllers/webhooks_controller_test.exs +++ b/test/media_server_web/controllers/webhooks_controller_test.exs @@ -55,58 +55,50 @@ defmodule MediaServerWeb.WebhooksControllerTest do end test "series should halt", %{conn: conn} do - conn = - post(conn, Routes.webhooks_path(conn, :create, "series", %{"someKey" => "someValue"}), - token: "someToken" - ) + conn = post(conn, ~p"/api/webhooks/series?token=someToken", %{"someKey" => "someValue"}) assert conn.status === 403 assert conn.halted end test "series should fall through", %{conn: conn, user: user} do - conn = - post(conn, Routes.webhooks_path(conn, :create, "series", %{"someKey" => "someValue"}), - token: user.api_token.token - ) + conn = post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{"someKey" => "someValue"}) assert conn.status === 200 end test "series should fall through again", %{conn: conn, user: user} do - conn = - post(conn, Routes.webhooks_path(conn, :create, "series", %{"eventType" => "someValue"}), - token: user.api_token.token - ) + conn = post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{"eventType" => "someValue"}) assert conn.status === 200 end - test "series should create", %{conn: conn, user: user} do - conn = - post(conn, Routes.webhooks_path(conn, :create, "series", %{"eventType" => "Download"}), - token: user.api_token.token - ) + test "it should add series", %{conn: conn, user: user} do + Phoenix.PubSub.subscribe(MediaServer.PubSub, "series") + + conn = post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{"eventType" => "Download"}) + + assert_received {:added} assert conn.status === 201 end - test "series should create on delete", %{conn: conn, user: user} do - conn = - post(conn, Routes.webhooks_path(conn, :create, "series", %{"eventType" => "SeriesDelete"}), - token: user.api_token.token - ) + test "it should delete series", %{conn: conn, user: user} do + Phoenix.PubSub.subscribe(MediaServer.PubSub, "series") + + conn = post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{"eventType" => "SeriesDelete"}) + + assert_received {:deleted} assert conn.status === 201 end test "series should create on episode file delete", %{conn: conn, user: user} do - conn = - post( - conn, - Routes.webhooks_path(conn, :create, "series", %{"eventType" => "EpisodeFileDelete"}), - token: user.api_token.token - ) + Phoenix.PubSub.subscribe(MediaServer.PubSub, "series") + + conn = post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{"eventType" => "EpisodeFileDelete"}) + + assert_received {:deleted_episode_file} assert conn.status === 201 end From abeed56ef41db98a0dd16c981235f497589f5fb6 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Fri, 13 Oct 2023 10:31:38 +1300 Subject: [PATCH 06/18] perf: add task movie actions --- lib/media_server/application.ex | 1 + lib/media_server/media_actions.ex | 9 ++++++ lib/media_server/movie_actions.ex | 35 ++++++++++++++++++++---- test/media_server/movie_actions_test.exs | 14 ++++++++++ 4 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 test/media_server/movie_actions_test.exs diff --git a/lib/media_server/application.ex b/lib/media_server/application.ex index 7e95b212..472c223b 100644 --- a/lib/media_server/application.ex +++ b/lib/media_server/application.ex @@ -20,6 +20,7 @@ defmodule MediaServer.Application do # {MediaServer.Worker, arg} MediaServerWeb.Presence, {DynamicSupervisor, name: MediaServer.DynamicSupervisor}, + {Task.Supervisor, name: MediaServer.TaskSupervisor}, MediaServer.MoviesIndex, MediaServer.SeriesIndex, MediaServer.UserActions, diff --git a/lib/media_server/media_actions.ex b/lib/media_server/media_actions.ex index 9a591ea3..5723765c 100644 --- a/lib/media_server/media_actions.ex +++ b/lib/media_server/media_actions.ex @@ -1,6 +1,7 @@ defmodule MediaServer.MediaActions do use Ecto.Schema import Ecto.Changeset + import Ecto.Query alias MediaServer.Repo @@ -61,4 +62,12 @@ defmodule MediaServer.MediaActions do Repo.get_by(__MODULE__, attrs) |> Repo.delete end + + def movie(id) do + from this in __MODULE__, where: this.media_type_id == ^MediaServer.MediaTypes.get_movie_id() and this.media_id == ^id + end + + def followers(query) do + MediaServer.Repo.all(from this in query, where: this.action_id == ^MediaServer.Actions.get_followed_id(), preload: [user: [:push_subscriptions]]) + end end diff --git a/lib/media_server/movie_actions.ex b/lib/media_server/movie_actions.ex index 4ef85525..c6f8c2ff 100644 --- a/lib/media_server/movie_actions.ex +++ b/lib/media_server/movie_actions.ex @@ -3,20 +3,43 @@ defmodule MediaServer.MovieActions do require Logger - def start_link(_args) do - GenServer.start_link(__MODULE__, %{}) + def start_link(opts) do + GenServer.start_link(__MODULE__, opts) end - def init(state) do + def init(_opts) do Phoenix.PubSub.subscribe(MediaServer.PubSub, "movie") - {:ok, state} + {:ok, %{}} + end + + defp notify_followers(state) do +# followers = MediaServer.MediaActions.movie(3) |> MediaServer.MediaActions.followers() + + task = + Task.Supervisor.async_nolink(MediaServer.TaskSupervisor, fn -> + Enum.each([], fn x -> x end) + end) + + Map.put(state, :notify_followers, task.ref) + end + + def handle_info({ref, :ok}, state) do + Process.demonitor(ref, [:flush]) + + {_value, state} = Map.pop(state, :notify_followers) + + {:noreply, state} + end + + def handle_info({:DOWN, _ref, :process, _pid, _reason}, state) do + {:noreply, state} end def handle_info({:added}, state) do MediaServer.MoviesIndex.reset() - {:noreply, state} + {:noreply, state |> notify_followers} end def handle_info({:deleted}, state) do @@ -30,4 +53,4 @@ defmodule MediaServer.MovieActions do {:noreply, state} end -end \ No newline at end of file +end diff --git a/test/media_server/movie_actions_test.exs b/test/media_server/movie_actions_test.exs new file mode 100644 index 00000000..d5c1db01 --- /dev/null +++ b/test/media_server/movie_actions_test.exs @@ -0,0 +1,14 @@ +defmodule MediaServer.MovieActionsTest do + use ExUnit.Case + + setup do + {:ok, pid} = MediaServer.MovieActions.start_link(%{}) + %{pid: pid} + end + + test "it should notify on added", %{pid: pid} do + send(pid, {:added}) + + %{:notify_followers => _ref} = :sys.get_state(pid) + end +end \ No newline at end of file From a6b219b7a8dd951540b56104c400032b52a8edab Mon Sep 17 00:00:00 2001 From: trueChazza Date: Fri, 13 Oct 2023 17:58:57 +1300 Subject: [PATCH 07/18] refactor: inline movie notify --- lib/media_server/application.ex | 2 - lib/media_server/movie_actions.ex | 56 ------------------- .../controllers/webhooks_controller.ex | 16 ++++-- test/media_server/movie_actions_test.exs | 14 ----- .../controllers/webhooks_controller_test.exs | 25 +++++---- test/support/mock_server.ex | 5 ++ 6 files changed, 31 insertions(+), 87 deletions(-) delete mode 100644 lib/media_server/movie_actions.ex delete mode 100644 test/media_server/movie_actions_test.exs diff --git a/lib/media_server/application.ex b/lib/media_server/application.ex index 472c223b..aeed533b 100644 --- a/lib/media_server/application.ex +++ b/lib/media_server/application.ex @@ -20,11 +20,9 @@ defmodule MediaServer.Application do # {MediaServer.Worker, arg} MediaServerWeb.Presence, {DynamicSupervisor, name: MediaServer.DynamicSupervisor}, - {Task.Supervisor, name: MediaServer.TaskSupervisor}, MediaServer.MoviesIndex, MediaServer.SeriesIndex, MediaServer.UserActions, - MediaServer.MovieActions, MediaServer.SeriesActions ] diff --git a/lib/media_server/movie_actions.ex b/lib/media_server/movie_actions.ex deleted file mode 100644 index c6f8c2ff..00000000 --- a/lib/media_server/movie_actions.ex +++ /dev/null @@ -1,56 +0,0 @@ -defmodule MediaServer.MovieActions do - use GenServer - - require Logger - - def start_link(opts) do - GenServer.start_link(__MODULE__, opts) - end - - def init(_opts) do - Phoenix.PubSub.subscribe(MediaServer.PubSub, "movie") - - {:ok, %{}} - end - - defp notify_followers(state) do -# followers = MediaServer.MediaActions.movie(3) |> MediaServer.MediaActions.followers() - - task = - Task.Supervisor.async_nolink(MediaServer.TaskSupervisor, fn -> - Enum.each([], fn x -> x end) - end) - - Map.put(state, :notify_followers, task.ref) - end - - def handle_info({ref, :ok}, state) do - Process.demonitor(ref, [:flush]) - - {_value, state} = Map.pop(state, :notify_followers) - - {:noreply, state} - end - - def handle_info({:DOWN, _ref, :process, _pid, _reason}, state) do - {:noreply, state} - end - - def handle_info({:added}, state) do - MediaServer.MoviesIndex.reset() - - {:noreply, state |> notify_followers} - end - - def handle_info({:deleted}, state) do - MediaServer.MoviesIndex.reset() - - {:noreply, state} - end - - def handle_info({:deleted_file}, state) do - MediaServer.MoviesIndex.reset() - - {:noreply, state} - end -end diff --git a/lib/media_server_web/controllers/webhooks_controller.ex b/lib/media_server_web/controllers/webhooks_controller.ex index 09616779..1b748ac2 100644 --- a/lib/media_server_web/controllers/webhooks_controller.ex +++ b/lib/media_server_web/controllers/webhooks_controller.ex @@ -1,22 +1,30 @@ defmodule MediaServerWeb.WebhooksController do use MediaServerWeb, :controller - def create(conn, %{"id" => "movie", "eventType" => "Download"}) do - Phoenix.PubSub.broadcast(MediaServer.PubSub, "movie", {:added}) + def create(conn, %{"id" => "movie", "eventType" => "Download", "movie" => %{"id" => id, "title" => title}}) do + MediaServer.MoviesIndex.reset() + + followers = MediaServer.MediaActions.movie(id) |> MediaServer.MediaActions.followers() + + Enum.each(followers, fn media_action -> + Enum.each(media_action.user.push_subscriptions, fn push_subscription -> + WebPushElixir.send_notification(push_subscription.push_subscription, "#{ title } is now available") + end) + end) conn |> send_resp(201, "Ok") end def create(conn, %{"id" => "movie", "eventType" => "MovieDelete"}) do - Phoenix.PubSub.broadcast(MediaServer.PubSub, "movie", {:deleted}) + MediaServer.MoviesIndex.reset() conn |> send_resp(201, "Ok") end def create(conn, %{"id" => "movie", "eventType" => "MovieFileDelete"}) do - Phoenix.PubSub.broadcast(MediaServer.PubSub, "movie", {:deleted_file}) + MediaServer.MoviesIndex.reset() conn |> send_resp(201, "Ok") diff --git a/test/media_server/movie_actions_test.exs b/test/media_server/movie_actions_test.exs deleted file mode 100644 index d5c1db01..00000000 --- a/test/media_server/movie_actions_test.exs +++ /dev/null @@ -1,14 +0,0 @@ -defmodule MediaServer.MovieActionsTest do - use ExUnit.Case - - setup do - {:ok, pid} = MediaServer.MovieActions.start_link(%{}) - %{pid: pid} - end - - test "it should notify on added", %{pid: pid} do - send(pid, {:added}) - - %{:notify_followers => _ref} = :sys.get_state(pid) - end -end \ No newline at end of file diff --git a/test/media_server_web/controllers/webhooks_controller_test.exs b/test/media_server_web/controllers/webhooks_controller_test.exs index c25e1393..0a01d884 100644 --- a/test/media_server_web/controllers/webhooks_controller_test.exs +++ b/test/media_server_web/controllers/webhooks_controller_test.exs @@ -1,6 +1,8 @@ defmodule MediaServerWeb.WebhooksControllerTest do use MediaServerWeb.ConnCase + @subscription "#{'{"endpoint":"http://localhost:8081/some-push-service","keys":{"p256dh":"BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM=","auth":"tBHItJI5svbpez7KI4CCXg=="}}'}" + setup do %{user: MediaServer.AccountsFixtures.user_fixture()} end @@ -25,32 +27,33 @@ defmodule MediaServerWeb.WebhooksControllerTest do end test "it should add movie", %{conn: conn, user: user} do - Phoenix.PubSub.subscribe(MediaServer.PubSub, "movie") - conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "Download"}) + {:ok, _struct} = MediaServer.MediaActions.create(%{ + media_id: 3, + user_id: user.id, + action_id: MediaServer.Actions.get_followed_id(), + media_type_id: MediaServer.MediaTypes.get_type_id("movie") + }) - assert_received {:added} + {:ok, _struct} = MediaServer.PushSubscriptions.create(%{ + user_id: user.id, + push_subscription: @subscription + }) + + conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "Download", "movie" => %{"id" => 3, "title" => "Some Movie"}}) assert conn.status === 201 end test "it should delete movie", %{conn: conn, user: user} do - Phoenix.PubSub.subscribe(MediaServer.PubSub, "movie") - conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "MovieDelete"}) - assert_received {:deleted} - assert conn.status === 201 end test "it should delete movie file", %{conn: conn, user: user} do - Phoenix.PubSub.subscribe(MediaServer.PubSub, "movie") - conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "MovieFileDelete"}) - assert_received {:deleted_file} - assert conn.status === 201 end diff --git a/test/support/mock_server.ex b/test/support/mock_server.ex index 15be6bc8..c15e664d 100644 --- a/test/support/mock_server.ex +++ b/test/support/mock_server.ex @@ -18,4 +18,9 @@ defmodule MediaServer.MockServer do conn |> Plug.Conn.send_resp(200, Jason.encode!(%{name: "someName", email: "someEmail"})) end + + post "/some-push-service" do + conn + |> Plug.Conn.send_resp(201, "ok") + end end From d7c9291b196f982a578170de0e83063631ba685e Mon Sep 17 00:00:00 2001 From: trueChazza Date: Fri, 13 Oct 2023 18:05:04 +1300 Subject: [PATCH 08/18] test: assert on push service error --- .../controllers/webhooks_controller_test.exs | 6 ++++++ test/support/mock_server.ex | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/test/media_server_web/controllers/webhooks_controller_test.exs b/test/media_server_web/controllers/webhooks_controller_test.exs index 0a01d884..2cce0185 100644 --- a/test/media_server_web/controllers/webhooks_controller_test.exs +++ b/test/media_server_web/controllers/webhooks_controller_test.exs @@ -2,6 +2,7 @@ defmodule MediaServerWeb.WebhooksControllerTest do use MediaServerWeb.ConnCase @subscription "#{'{"endpoint":"http://localhost:8081/some-push-service","keys":{"p256dh":"BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM=","auth":"tBHItJI5svbpez7KI4CCXg=="}}'}" + @subscription_with_error "#{'{"endpoint":"http://localhost:8081/some-push-service-with-error","keys":{"p256dh":"BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM=","auth":"tBHItJI5svbpez7KI4CCXg=="}}'}" setup do %{user: MediaServer.AccountsFixtures.user_fixture()} @@ -40,6 +41,11 @@ defmodule MediaServerWeb.WebhooksControllerTest do push_subscription: @subscription }) + {:ok, _struct} = MediaServer.PushSubscriptions.create(%{ + user_id: user.id, + push_subscription: @subscription_with_error + }) + conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "Download", "movie" => %{"id" => 3, "title" => "Some Movie"}}) assert conn.status === 201 diff --git a/test/support/mock_server.ex b/test/support/mock_server.ex index c15e664d..44e9bd62 100644 --- a/test/support/mock_server.ex +++ b/test/support/mock_server.ex @@ -23,4 +23,9 @@ defmodule MediaServer.MockServer do conn |> Plug.Conn.send_resp(201, "ok") end + + post "/some-push-service-with-error" do + conn + |> Plug.Conn.send_resp(401, "unauthorized") + end end From e8a7fb7602b9739f4fc5cb8b505522deb717f649 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Fri, 13 Oct 2023 18:07:14 +1300 Subject: [PATCH 09/18] build: v4.2.0-beta.2 --- lib/media_server_web/components/footer_component.html.heex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/media_server_web/components/footer_component.html.heex b/lib/media_server_web/components/footer_component.html.heex index 014f09ab..fce9b0e7 100644 --- a/lib/media_server_web/components/footer_component.html.heex +++ b/lib/media_server_web/components/footer_component.html.heex @@ -3,7 +3,7 @@

Copyright © 2023 Midarr Labs

- v4.2.0-beta.1 + v4.2.0-beta.2

From 98cde732e2238889f20ce1af268286b8381eaba8 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Fri, 13 Oct 2023 18:10:40 +1300 Subject: [PATCH 10/18] ci: otp 24 --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 698abc1c..06c87842 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -40,7 +40,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - otp: [ '25' ] + otp: [ '24' ] elixir: [ '1.15' ] services: From a5be1a9ab7abbec8185d492013515976040cb779 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Sun, 15 Oct 2023 10:22:00 +1300 Subject: [PATCH 11/18] test: fix json fixture --- .../controllers/webhooks_controller_test.exs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/media_server_web/controllers/webhooks_controller_test.exs b/test/media_server_web/controllers/webhooks_controller_test.exs index 2cce0185..204bfc4a 100644 --- a/test/media_server_web/controllers/webhooks_controller_test.exs +++ b/test/media_server_web/controllers/webhooks_controller_test.exs @@ -1,8 +1,8 @@ defmodule MediaServerWeb.WebhooksControllerTest do use MediaServerWeb.ConnCase - @subscription "#{'{"endpoint":"http://localhost:8081/some-push-service","keys":{"p256dh":"BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM=","auth":"tBHItJI5svbpez7KI4CCXg=="}}'}" - @subscription_with_error "#{'{"endpoint":"http://localhost:8081/some-push-service-with-error","keys":{"p256dh":"BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM=","auth":"tBHItJI5svbpez7KI4CCXg=="}}'}" + @subscription %{"endpoint" => "http://localhost:8081/some-push-service","keys" => %{"p256dh" => "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM=","auth" => "tBHItJI5svbpez7KI4CCXg=="}} + @subscription_with_error %{"endpoint" => "http://localhost:8081/some-push-service-with-error","keys" => %{"p256dh" => "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM=","auth" => "tBHItJI5svbpez7KI4CCXg=="}} setup do %{user: MediaServer.AccountsFixtures.user_fixture()} @@ -38,12 +38,12 @@ defmodule MediaServerWeb.WebhooksControllerTest do {:ok, _struct} = MediaServer.PushSubscriptions.create(%{ user_id: user.id, - push_subscription: @subscription + push_subscription: Jason.encode!(@subscription) }) {:ok, _struct} = MediaServer.PushSubscriptions.create(%{ user_id: user.id, - push_subscription: @subscription_with_error + push_subscription: Jason.encode!(@subscription_with_error) }) conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "Download", "movie" => %{"id" => 3, "title" => "Some Movie"}}) From ac84f9d60370c4ce5032c680ce95323ea3f03dc6 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Sun, 15 Oct 2023 10:43:45 +1300 Subject: [PATCH 12/18] ci: add vapid keys --- .github/workflows/test.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 06c87842..ec2abd89 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -30,6 +30,10 @@ env: SONARR_BASE_URL: http://localhost:8989 SONARR_API_KEY: 1accda4476394bfcaddefe8c4fd77d4a + VAPID_PUBLIC_KEY: BDntLA3k5K1tsrFOXXAuS_9Ey30jxy-R2CAosC2DOQnTs8LpQGxpTEx3AcPXinVYFFpJI6tT_RJC8pHgUsdbhOk + VAPID_PRIVATE_KEY: RVPPDBVNmJtSLoZ28jE1SumpG4HyhhCPfcix3bvxbLw + VAPID_SUBJECT: mailto:admin@email.com + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} From bb445dd4f1a0b39304fd211c30dac41527b22e79 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Sun, 15 Oct 2023 13:58:05 +1300 Subject: [PATCH 13/18] feat: add series notify --- lib/media_server/media_actions.ex | 4 ++++ lib/media_server/media_types.ex | 4 ++++ .../controllers/webhooks_controller.ex | 12 +++++++++-- .../controllers/webhooks_controller_test.exs | 20 ++++++++++++++++--- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/media_server/media_actions.ex b/lib/media_server/media_actions.ex index 5723765c..5a2f8495 100644 --- a/lib/media_server/media_actions.ex +++ b/lib/media_server/media_actions.ex @@ -67,6 +67,10 @@ defmodule MediaServer.MediaActions do from this in __MODULE__, where: this.media_type_id == ^MediaServer.MediaTypes.get_movie_id() and this.media_id == ^id end + def series(id) do + from this in __MODULE__, where: this.media_type_id == ^MediaServer.MediaTypes.get_series_id() and this.media_id == ^id + end + def followers(query) do MediaServer.Repo.all(from this in query, where: this.action_id == ^MediaServer.Actions.get_followed_id(), preload: [user: [:push_subscriptions]]) end diff --git a/lib/media_server/media_types.ex b/lib/media_server/media_types.ex index 3b73b2c7..f3ad3cf8 100644 --- a/lib/media_server/media_types.ex +++ b/lib/media_server/media_types.ex @@ -25,6 +25,10 @@ defmodule MediaServer.MediaTypes do Repo.get_by!(__MODULE__, type: "movie").id end + def get_series_id() do + Repo.get_by!(__MODULE__, type: "series").id + end + def get_episode_id() do Repo.get_by!(__MODULE__, type: "episode").id end diff --git a/lib/media_server_web/controllers/webhooks_controller.ex b/lib/media_server_web/controllers/webhooks_controller.ex index 1b748ac2..01340336 100644 --- a/lib/media_server_web/controllers/webhooks_controller.ex +++ b/lib/media_server_web/controllers/webhooks_controller.ex @@ -30,8 +30,16 @@ defmodule MediaServerWeb.WebhooksController do |> send_resp(201, "Ok") end - def create(conn, %{"id" => "series", "eventType" => "Download"}) do - Phoenix.PubSub.broadcast(MediaServer.PubSub, "series", {:added}) + def create(conn, %{"id" => "series", "eventType" => "Download", "series" => %{"id" => id, "title" => title}}) do + MediaServer.SeriesIndex.reset() + + followers = MediaServer.MediaActions.series(id) |> MediaServer.MediaActions.followers() + + Enum.each(followers, fn media_action -> + Enum.each(media_action.user.push_subscriptions, fn push_subscription -> + WebPushElixir.send_notification(push_subscription.push_subscription, "#{ title } is now available") + end) + end) conn |> send_resp(201, "Ok") diff --git a/test/media_server_web/controllers/webhooks_controller_test.exs b/test/media_server_web/controllers/webhooks_controller_test.exs index 204bfc4a..98f92e55 100644 --- a/test/media_server_web/controllers/webhooks_controller_test.exs +++ b/test/media_server_web/controllers/webhooks_controller_test.exs @@ -83,11 +83,25 @@ defmodule MediaServerWeb.WebhooksControllerTest do end test "it should add series", %{conn: conn, user: user} do - Phoenix.PubSub.subscribe(MediaServer.PubSub, "series") - conn = post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{"eventType" => "Download"}) + {:ok, _struct} = MediaServer.MediaActions.create(%{ + media_id: 1, + user_id: user.id, + action_id: MediaServer.Actions.get_followed_id(), + media_type_id: MediaServer.MediaTypes.get_type_id("series") + }) + + {:ok, _struct} = MediaServer.PushSubscriptions.create(%{ + user_id: user.id, + push_subscription: Jason.encode!(@subscription) + }) + + {:ok, _struct} = MediaServer.PushSubscriptions.create(%{ + user_id: user.id, + push_subscription: Jason.encode!(@subscription_with_error) + }) - assert_received {:added} + conn = post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{"eventType" => "Download", "series" => %{"id" => 1, "title" => "Some Series"}}) assert conn.status === 201 end From d1a80b6c62a31f44153d1fa36bbb5306c6136ff9 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Sun, 15 Oct 2023 14:04:21 +1300 Subject: [PATCH 14/18] docs: update README webhook --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8bffce1a..7c94f3b2 100644 --- a/README.md +++ b/README.md @@ -205,7 +205,7 @@ To keep your media in sync, Midarr webhooks are required in your integrations. Any updates to your media library via your integrations, a POST request will be made to the following endpoints to update your Midarr cache. -Add these webhook urls to Radarr / Sonarr under `Settings -> Connect`: +Add these webhook urls to Radarr / Sonarr under `Settings -> Connect -> Webhook`: #### Radarr example ``` From 9b787852f78bc731ea6943b61075641410ee35d0 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Sun, 15 Oct 2023 15:36:26 +1300 Subject: [PATCH 15/18] refactor: use task series actions --- lib/media_server/series_actions.ex | 28 +++++++++---------- .../controllers/webhooks_controller.ex | 12 ++------ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/lib/media_server/series_actions.ex b/lib/media_server/series_actions.ex index 92dca9d8..59087c81 100644 --- a/lib/media_server/series_actions.ex +++ b/lib/media_server/series_actions.ex @@ -1,33 +1,33 @@ defmodule MediaServer.SeriesActions do - use GenServer + use Task require Logger - def start_link(_args) do - GenServer.start_link(__MODULE__, %{}) + def start_link(arg) do + Task.start_link(__MODULE__, :run, [arg]) end - def init(state) do + def run(_arg) do Phoenix.PubSub.subscribe(MediaServer.PubSub, "series") - - {:ok, state} end - def handle_info({:added}, state) do + def handle_info({:added, %{"series" => %{"id" => id, "title" => title}}}) do MediaServer.SeriesIndex.reset() - {:noreply, state} + followers = MediaServer.MediaActions.series(id) |> MediaServer.MediaActions.followers() + + Enum.each(followers, fn media_action -> + Enum.each(media_action.user.push_subscriptions, fn push_subscription -> + WebPushElixir.send_notification(push_subscription.push_subscription, "#{ title } is now available") + end) + end) end - def handle_info({:deleted}, state) do + def handle_info({:deleted}) do MediaServer.SeriesIndex.reset() - - {:noreply, state} end - def handle_info({:deleted_episode_file}, state) do + def handle_info({:deleted_episode_file}) do MediaServer.SeriesIndex.reset() - - {:noreply, state} end end \ No newline at end of file diff --git a/lib/media_server_web/controllers/webhooks_controller.ex b/lib/media_server_web/controllers/webhooks_controller.ex index 01340336..480e0f3c 100644 --- a/lib/media_server_web/controllers/webhooks_controller.ex +++ b/lib/media_server_web/controllers/webhooks_controller.ex @@ -30,16 +30,8 @@ defmodule MediaServerWeb.WebhooksController do |> send_resp(201, "Ok") end - def create(conn, %{"id" => "series", "eventType" => "Download", "series" => %{"id" => id, "title" => title}}) do - MediaServer.SeriesIndex.reset() - - followers = MediaServer.MediaActions.series(id) |> MediaServer.MediaActions.followers() - - Enum.each(followers, fn media_action -> - Enum.each(media_action.user.push_subscriptions, fn push_subscription -> - WebPushElixir.send_notification(push_subscription.push_subscription, "#{ title } is now available") - end) - end) + def create(conn, %{"id" => "series", "eventType" => "Download"} = params) do + Phoenix.PubSub.broadcast(MediaServer.PubSub, "series", {:added, params}) conn |> send_resp(201, "Ok") From 421d50846dfcbd75ebb65cc039248491f67b2a69 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Sun, 15 Oct 2023 15:45:51 +1300 Subject: [PATCH 16/18] refactor: mix format --- lib/media_server/media_actions.ex | 15 +- lib/media_server/push_subscriptions.ex | 2 +- lib/media_server/series_actions.ex | 13 +- lib/media_server/user_actions.ex | 20 ++- .../components/follow_component.ex | 15 +- .../components/follow_component.html.heex | 2 +- .../controllers/webhooks_controller.ex | 11 +- .../live/movies_live/show.html.heex | 22 +-- .../live/series_live/show.html.heex | 2 +- .../templates/layout/live.html.heex | 6 +- .../controllers/webhooks_controller_test.exs | 146 ++++++++++++------ test/media_server_web/user_follow_test.exs | 42 ++++- 12 files changed, 199 insertions(+), 97 deletions(-) diff --git a/lib/media_server/media_actions.ex b/lib/media_server/media_actions.ex index 5a2f8495..fc2f4628 100644 --- a/lib/media_server/media_actions.ex +++ b/lib/media_server/media_actions.ex @@ -60,18 +60,25 @@ defmodule MediaServer.MediaActions do def delete(attrs) do Repo.get_by(__MODULE__, attrs) - |> Repo.delete + |> Repo.delete() end def movie(id) do - from this in __MODULE__, where: this.media_type_id == ^MediaServer.MediaTypes.get_movie_id() and this.media_id == ^id + from this in __MODULE__, + where: this.media_type_id == ^MediaServer.MediaTypes.get_movie_id() and this.media_id == ^id end def series(id) do - from this in __MODULE__, where: this.media_type_id == ^MediaServer.MediaTypes.get_series_id() and this.media_id == ^id + from this in __MODULE__, + where: + this.media_type_id == ^MediaServer.MediaTypes.get_series_id() and this.media_id == ^id end def followers(query) do - MediaServer.Repo.all(from this in query, where: this.action_id == ^MediaServer.Actions.get_followed_id(), preload: [user: [:push_subscriptions]]) + MediaServer.Repo.all( + from this in query, + where: this.action_id == ^MediaServer.Actions.get_followed_id(), + preload: [user: [:push_subscriptions]] + ) end end diff --git a/lib/media_server/push_subscriptions.ex b/lib/media_server/push_subscriptions.ex index 2ed44309..be3f567b 100644 --- a/lib/media_server/push_subscriptions.ex +++ b/lib/media_server/push_subscriptions.ex @@ -34,6 +34,6 @@ defmodule MediaServer.PushSubscriptions do def delete(attrs) do Repo.get_by(__MODULE__, attrs) - |> Repo.delete + |> Repo.delete() end end diff --git a/lib/media_server/series_actions.ex b/lib/media_server/series_actions.ex index 59087c81..a58b76e1 100644 --- a/lib/media_server/series_actions.ex +++ b/lib/media_server/series_actions.ex @@ -16,11 +16,14 @@ defmodule MediaServer.SeriesActions do followers = MediaServer.MediaActions.series(id) |> MediaServer.MediaActions.followers() - Enum.each(followers, fn media_action -> - Enum.each(media_action.user.push_subscriptions, fn push_subscription -> - WebPushElixir.send_notification(push_subscription.push_subscription, "#{ title } is now available") - end) + Enum.each(followers, fn media_action -> + Enum.each(media_action.user.push_subscriptions, fn push_subscription -> + WebPushElixir.send_notification( + push_subscription.push_subscription, + "#{title} is now available" + ) end) + end) end def handle_info({:deleted}) do @@ -30,4 +33,4 @@ defmodule MediaServer.SeriesActions do def handle_info({:deleted_episode_file}) do MediaServer.SeriesIndex.reset() end -end \ No newline at end of file +end diff --git a/lib/media_server/user_actions.ex b/lib/media_server/user_actions.ex index b94a3cbd..41efef0e 100644 --- a/lib/media_server/user_actions.ex +++ b/lib/media_server/user_actions.ex @@ -24,7 +24,6 @@ defmodule MediaServer.UserActions do end def handle_info({:followed, params}, state) do - media_type_id = MediaServer.MediaTypes.get_type_id(params["media_type"]) MediaServer.MediaActions.create(%{ @@ -34,13 +33,14 @@ defmodule MediaServer.UserActions do media_type_id: media_type_id }) - Logger.info("user_id:#{ params["user_id"] }:followed:media_type_id:#{ media_type_id }:media_id:#{ params["media_id"] }") + Logger.info( + "user_id:#{params["user_id"]}:followed:media_type_id:#{media_type_id}:media_id:#{params["media_id"]}" + ) {:noreply, state} end def handle_info({:unfollowed, params}, state) do - media_type_id = MediaServer.MediaTypes.get_type_id(params["media_type"]) MediaServer.MediaActions.delete(%{ @@ -50,31 +50,35 @@ defmodule MediaServer.UserActions do media_type_id: media_type_id }) - Logger.info("user_id:#{ params["user_id"] }:unfollowed:media_type_id:#{ media_type_id }:media_id:#{ params["media_id"] }") + Logger.info( + "user_id:#{params["user_id"]}:unfollowed:media_type_id:#{media_type_id}:media_id:#{params["media_id"]}" + ) {:noreply, state} end def handle_info({:granted_push_notifications, params}, state) do - MediaServer.PushSubscriptions.create(%{ user_id: params["user_id"], push_subscription: params["push_subscription"] }) - Logger.info("user_id:#{ params["user_id"] }:granted_push_notifications:media_type_id:#{ params["media_type_id"] }:media_id:#{ params["media_id"] }") + Logger.info( + "user_id:#{params["user_id"]}:granted_push_notifications:media_type_id:#{params["media_type_id"]}:media_id:#{params["media_id"]}" + ) {:noreply, state} end def handle_info({:denied_push_notifications, params}, state) do - MediaServer.PushSubscriptions.create(%{ user_id: params["user_id"], push_subscription: params["message"] }) - Logger.info("user_id:#{ params["user_id"] }:denied_push_notifications:media_type_id:#{ params["media_type_id"] }:media_id:#{ params["media_id"] }") + Logger.info( + "user_id:#{params["user_id"]}:denied_push_notifications:media_type_id:#{params["media_type_id"]}:media_id:#{params["media_id"]}" + ) {:noreply, state} end diff --git a/lib/media_server_web/components/follow_component.ex b/lib/media_server_web/components/follow_component.ex index f8f5858b..4051f3c3 100644 --- a/lib/media_server_web/components/follow_component.ex +++ b/lib/media_server_web/components/follow_component.ex @@ -6,16 +6,20 @@ defmodule MediaServerWeb.Components.FollowComponent do @impl true def preload(list_of_assigns) do media_id = Enum.find(list_of_assigns, fn assign -> Map.get(assign, :media_id) end).media_id - media_type = Enum.find(list_of_assigns, fn assign -> Map.get(assign, :media_type) end).media_type + + media_type = + Enum.find(list_of_assigns, fn assign -> Map.get(assign, :media_type) end).media_type + user_id = Enum.find(list_of_assigns, fn assign -> Map.get(assign, :user_id) end).user_id media_type_id = MediaServer.MediaTypes.get_type_id(media_type) query = from media_actions in MediaServer.MediaActions, - where: - media_actions.media_type_id == ^media_type_id and - media_actions.user_id == ^user_id and media_actions.media_id == ^media_id and media_actions.action_id == ^MediaServer.Actions.get_followed_id + where: + media_actions.media_type_id == ^media_type_id and + media_actions.user_id == ^user_id and media_actions.media_id == ^media_id and + media_actions.action_id == ^MediaServer.Actions.get_followed_id() result = MediaServer.Repo.all(query) @@ -30,7 +34,8 @@ defmodule MediaServerWeb.Components.FollowComponent do state: "Following", event: "unfollow" } - end |> Map.merge(%{ + end + |> Map.merge(%{ media_id: assign.media_id, media_type: assign.media_type, user_id: assign.user_id, diff --git a/lib/media_server_web/components/follow_component.html.heex b/lib/media_server_web/components/follow_component.html.heex index 41296f8e..8c7f0500 100644 --- a/lib/media_server_web/components/follow_component.html.heex +++ b/lib/media_server_web/components/follow_component.html.heex @@ -10,4 +10,4 @@ class="inline-flex items-center gap-2 justify-center rounded-lg py-2 px-3 text-sm outline-offset-2 transition active:transition-none bg-zinc-800 font-semibold text-zinc-100 hover:bg-zinc-700 active:bg-zinc-800" > <%= @state %> - \ No newline at end of file + diff --git a/lib/media_server_web/controllers/webhooks_controller.ex b/lib/media_server_web/controllers/webhooks_controller.ex index 480e0f3c..7688b29f 100644 --- a/lib/media_server_web/controllers/webhooks_controller.ex +++ b/lib/media_server_web/controllers/webhooks_controller.ex @@ -1,14 +1,21 @@ defmodule MediaServerWeb.WebhooksController do use MediaServerWeb, :controller - def create(conn, %{"id" => "movie", "eventType" => "Download", "movie" => %{"id" => id, "title" => title}}) do + def create(conn, %{ + "id" => "movie", + "eventType" => "Download", + "movie" => %{"id" => id, "title" => title} + }) do MediaServer.MoviesIndex.reset() followers = MediaServer.MediaActions.movie(id) |> MediaServer.MediaActions.followers() Enum.each(followers, fn media_action -> Enum.each(media_action.user.push_subscriptions, fn push_subscription -> - WebPushElixir.send_notification(push_subscription.push_subscription, "#{ title } is now available") + WebPushElixir.send_notification( + push_subscription.push_subscription, + "#{title} is now available" + ) end) end) diff --git a/lib/media_server_web/live/movies_live/show.html.heex b/lib/media_server_web/live/movies_live/show.html.heex index be376bff..7b21ac04 100644 --- a/lib/media_server_web/live/movies_live/show.html.heex +++ b/lib/media_server_web/live/movies_live/show.html.heex @@ -20,16 +20,16 @@
<%= if @movie["hasFile"] do %> - <.link - id={"play-#{ @movie["id"] }"} - navigate={"/watch?movie=#{@movie["id"]}#{ if !is_nil(@continue) do "×tamp=#{@continue.current_time}" end }"} - class="group relative flex h-16 w-16 flex-shrink-0 items-center justify-center rounded-full bg-zinc-800 hover:bg-zinc-700 focus:outline-none focus:ring focus:ring-slate-700 focus:ring-offset-4" - > - - - - - + <.link + id={"play-#{ @movie["id"] }"} + navigate={"/watch?movie=#{@movie["id"]}#{ if !is_nil(@continue) do "×tamp=#{@continue.current_time}" end }"} + class="group relative flex h-16 w-16 flex-shrink-0 items-center justify-center rounded-full bg-zinc-800 hover:bg-zinc-700 focus:outline-none focus:ring focus:ring-slate-700 focus:ring-offset-4" + > + + + + + <% end %> <.live_component @@ -39,7 +39,7 @@ media_type="movie" user_id={@current_user.id} return_to={~p"/movies/#{@movie["id"]}"} - /> + />
diff --git a/lib/media_server_web/live/series_live/show.html.heex b/lib/media_server_web/live/series_live/show.html.heex index 97acf6f1..05e09650 100644 --- a/lib/media_server_web/live/series_live/show.html.heex +++ b/lib/media_server_web/live/series_live/show.html.heex @@ -26,7 +26,7 @@ media_type="series" user_id={@current_user.id} return_to={~p"/series/#{@serie["id"]}"} - /> + /> diff --git a/lib/media_server_web/templates/layout/live.html.heex b/lib/media_server_web/templates/layout/live.html.heex index 81c5df13..65337f66 100644 --- a/lib/media_server_web/templates/layout/live.html.heex +++ b/lib/media_server_web/templates/layout/live.html.heex @@ -20,7 +20,7 @@
<.link - navigate={"/movies"} + navigate="/movies" class="inline-flex flex-col items-center justify-center p-4 hover:bg-gray-50 dark:hover:bg-gray-800 group" > Movies <.link - navigate={"/series"} + navigate="/series" class="inline-flex flex-col items-center justify-center p-4 hover:bg-gray-50 dark:hover:bg-gray-800 group" > Series <.link - navigate={"/history"} + navigate="/history" class="inline-flex flex-col items-center justify-center p-4 hover:bg-gray-50 dark:hover:bg-gray-800 group" > "http://localhost:8081/some-push-service","keys" => %{"p256dh" => "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM=","auth" => "tBHItJI5svbpez7KI4CCXg=="}} - @subscription_with_error %{"endpoint" => "http://localhost:8081/some-push-service-with-error","keys" => %{"p256dh" => "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM=","auth" => "tBHItJI5svbpez7KI4CCXg=="}} + @subscription %{ + "endpoint" => "http://localhost:8081/some-push-service", + "keys" => %{ + "p256dh" => + "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM=", + "auth" => "tBHItJI5svbpez7KI4CCXg==" + } + } + @subscription_with_error %{ + "endpoint" => "http://localhost:8081/some-push-service-with-error", + "keys" => %{ + "p256dh" => + "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM=", + "auth" => "tBHItJI5svbpez7KI4CCXg==" + } + } setup do %{user: MediaServer.AccountsFixtures.user_fixture()} @@ -16,49 +30,67 @@ defmodule MediaServerWeb.WebhooksControllerTest do end test "movie should fall through", %{conn: conn, user: user} do - conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"someKey" => "someValue"}) + conn = + post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{ + "someKey" => "someValue" + }) assert conn.status === 200 end test "movie should fall through again", %{conn: conn, user: user} do - conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "someValue"}) + conn = + post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{ + "eventType" => "someValue" + }) assert conn.status === 200 end test "it should add movie", %{conn: conn, user: user} do - - {:ok, _struct} = MediaServer.MediaActions.create(%{ - media_id: 3, - user_id: user.id, - action_id: MediaServer.Actions.get_followed_id(), - media_type_id: MediaServer.MediaTypes.get_type_id("movie") - }) - - {:ok, _struct} = MediaServer.PushSubscriptions.create(%{ - user_id: user.id, - push_subscription: Jason.encode!(@subscription) - }) - - {:ok, _struct} = MediaServer.PushSubscriptions.create(%{ - user_id: user.id, - push_subscription: Jason.encode!(@subscription_with_error) - }) - - conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "Download", "movie" => %{"id" => 3, "title" => "Some Movie"}}) + {:ok, _struct} = + MediaServer.MediaActions.create(%{ + media_id: 3, + user_id: user.id, + action_id: MediaServer.Actions.get_followed_id(), + media_type_id: MediaServer.MediaTypes.get_type_id("movie") + }) + + {:ok, _struct} = + MediaServer.PushSubscriptions.create(%{ + user_id: user.id, + push_subscription: Jason.encode!(@subscription) + }) + + {:ok, _struct} = + MediaServer.PushSubscriptions.create(%{ + user_id: user.id, + push_subscription: Jason.encode!(@subscription_with_error) + }) + + conn = + post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{ + "eventType" => "Download", + "movie" => %{"id" => 3, "title" => "Some Movie"} + }) assert conn.status === 201 end test "it should delete movie", %{conn: conn, user: user} do - conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "MovieDelete"}) + conn = + post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{ + "eventType" => "MovieDelete" + }) assert conn.status === 201 end test "it should delete movie file", %{conn: conn, user: user} do - conn = post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{"eventType" => "MovieFileDelete"}) + conn = + post(conn, ~p"/api/webhooks/movie?token=#{user.api_token.token}", %{ + "eventType" => "MovieFileDelete" + }) assert conn.status === 201 end @@ -71,37 +103,49 @@ defmodule MediaServerWeb.WebhooksControllerTest do end test "series should fall through", %{conn: conn, user: user} do - conn = post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{"someKey" => "someValue"}) + conn = + post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{ + "someKey" => "someValue" + }) assert conn.status === 200 end test "series should fall through again", %{conn: conn, user: user} do - conn = post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{"eventType" => "someValue"}) + conn = + post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{ + "eventType" => "someValue" + }) assert conn.status === 200 end test "it should add series", %{conn: conn, user: user} do - - {:ok, _struct} = MediaServer.MediaActions.create(%{ - media_id: 1, - user_id: user.id, - action_id: MediaServer.Actions.get_followed_id(), - media_type_id: MediaServer.MediaTypes.get_type_id("series") - }) - - {:ok, _struct} = MediaServer.PushSubscriptions.create(%{ - user_id: user.id, - push_subscription: Jason.encode!(@subscription) - }) - - {:ok, _struct} = MediaServer.PushSubscriptions.create(%{ - user_id: user.id, - push_subscription: Jason.encode!(@subscription_with_error) - }) - - conn = post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{"eventType" => "Download", "series" => %{"id" => 1, "title" => "Some Series"}}) + {:ok, _struct} = + MediaServer.MediaActions.create(%{ + media_id: 1, + user_id: user.id, + action_id: MediaServer.Actions.get_followed_id(), + media_type_id: MediaServer.MediaTypes.get_type_id("series") + }) + + {:ok, _struct} = + MediaServer.PushSubscriptions.create(%{ + user_id: user.id, + push_subscription: Jason.encode!(@subscription) + }) + + {:ok, _struct} = + MediaServer.PushSubscriptions.create(%{ + user_id: user.id, + push_subscription: Jason.encode!(@subscription_with_error) + }) + + conn = + post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{ + "eventType" => "Download", + "series" => %{"id" => 1, "title" => "Some Series"} + }) assert conn.status === 201 end @@ -109,7 +153,10 @@ defmodule MediaServerWeb.WebhooksControllerTest do test "it should delete series", %{conn: conn, user: user} do Phoenix.PubSub.subscribe(MediaServer.PubSub, "series") - conn = post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{"eventType" => "SeriesDelete"}) + conn = + post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{ + "eventType" => "SeriesDelete" + }) assert_received {:deleted} @@ -119,7 +166,10 @@ defmodule MediaServerWeb.WebhooksControllerTest do test "series should create on episode file delete", %{conn: conn, user: user} do Phoenix.PubSub.subscribe(MediaServer.PubSub, "series") - conn = post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{"eventType" => "EpisodeFileDelete"}) + conn = + post(conn, ~p"/api/webhooks/series?token=#{user.api_token.token}", %{ + "eventType" => "EpisodeFileDelete" + }) assert_received {:deleted_episode_file} diff --git a/test/media_server_web/user_follow_test.exs b/test/media_server_web/user_follow_test.exs index 529b4f71..12dde76b 100644 --- a/test/media_server_web/user_follow_test.exs +++ b/test/media_server_web/user_follow_test.exs @@ -22,7 +22,8 @@ defmodule MediaServerWeb.UserFollowTest do |> element("#follow", "Follow") |> render_hook(:follow, %{media_id: movie["id"], media_type: "movie", user_id: user.id}) - assert_received {:followed, %{"media_id" => 3, "media_type" => "movie", "user_id" => _user_id}} + assert_received {:followed, + %{"media_id" => 3, "media_type" => "movie", "user_id" => _user_id}} # Not ideal but wait for processed message (async) :timer.sleep(1000) @@ -53,7 +54,8 @@ defmodule MediaServerWeb.UserFollowTest do |> element("#follow", "Following") |> render_hook(:unfollow, %{media_id: movie["id"], media_type: "movie", user_id: user.id}) - assert_received {:unfollowed, %{"media_id" => 3, "media_type" => "movie", "user_id" => _user_id}} + assert_received {:unfollowed, + %{"media_id" => 3, "media_type" => "movie", "user_id" => _user_id}} # Not ideal but wait for processed message (async) :timer.sleep(1000) @@ -74,7 +76,8 @@ defmodule MediaServerWeb.UserFollowTest do |> element("#follow", "Follow") |> render_hook(:follow, %{media_id: series["id"], media_type: "series", user_id: user.id}) - assert_received {:followed, %{"media_id" => 1, "media_type" => "series", "user_id" => _user_id}} + assert_received {:followed, + %{"media_id" => 1, "media_type" => "series", "user_id" => _user_id}} # Not ideal but wait for processed message (async) :timer.sleep(1000) @@ -105,7 +108,8 @@ defmodule MediaServerWeb.UserFollowTest do |> element("#follow", "Following") |> render_hook(:unfollow, %{media_id: series["id"], media_type: "series", user_id: user.id}) - assert_received {:unfollowed, %{"media_id" => 1, "media_type" => "series", "user_id" => _user_id}} + assert_received {:unfollowed, + %{"media_id" => 1, "media_type" => "series", "user_id" => _user_id}} # Not ideal but wait for processed message (async) :timer.sleep(1000) @@ -124,9 +128,20 @@ defmodule MediaServerWeb.UserFollowTest do view |> element("#follow", "Follow") - |> render_hook(:grant_push_notifications, %{media_id: movie["id"], media_type: "movie", user_id: user.id, push_subscription: "some subscription"}) + |> render_hook(:grant_push_notifications, %{ + media_id: movie["id"], + media_type: "movie", + user_id: user.id, + push_subscription: "some subscription" + }) - assert_received {:granted_push_notifications, %{"media_id" => 3, "media_type" => "movie", "user_id" => _user_id, "push_subscription" => "some subscription"}} + assert_received {:granted_push_notifications, + %{ + "media_id" => 3, + "media_type" => "movie", + "user_id" => _user_id, + "push_subscription" => "some subscription" + }} # Not ideal but wait for processed message (async) :timer.sleep(1000) @@ -145,9 +160,20 @@ defmodule MediaServerWeb.UserFollowTest do view |> element("#follow", "Follow") - |> render_hook(:deny_push_notifications, %{media_id: movie["id"], media_type: "movie", user_id: user.id, message: "some message"}) + |> render_hook(:deny_push_notifications, %{ + media_id: movie["id"], + media_type: "movie", + user_id: user.id, + message: "some message" + }) - assert_received {:denied_push_notifications, %{"media_id" => 3, "media_type" => "movie", "user_id" => _user_id, "message" => "some message"}} + assert_received {:denied_push_notifications, + %{ + "media_id" => 3, + "media_type" => "movie", + "user_id" => _user_id, + "message" => "some message" + }} # Not ideal but wait for processed message (async) :timer.sleep(1000) From 0180ae71cf7507b5879e56a2c5af60365f586b33 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Sun, 15 Oct 2023 15:53:17 +1300 Subject: [PATCH 17/18] refactor: use task movie actions --- lib/media_server/application.ex | 5 +-- lib/media_server/movie_actions.ex | 34 +++++++++++++++++++ .../controllers/webhooks_controller.ex | 23 +++---------- 3 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 lib/media_server/movie_actions.ex diff --git a/lib/media_server/application.ex b/lib/media_server/application.ex index aeed533b..e5e13a83 100644 --- a/lib/media_server/application.ex +++ b/lib/media_server/application.ex @@ -22,8 +22,9 @@ defmodule MediaServer.Application do {DynamicSupervisor, name: MediaServer.DynamicSupervisor}, MediaServer.MoviesIndex, MediaServer.SeriesIndex, - MediaServer.UserActions, - MediaServer.SeriesActions + MediaServer.MovieActions, + MediaServer.SeriesActions, + MediaServer.UserActions ] # See https://hexdocs.pm/elixir/Supervisor.html diff --git a/lib/media_server/movie_actions.ex b/lib/media_server/movie_actions.ex new file mode 100644 index 00000000..00231b04 --- /dev/null +++ b/lib/media_server/movie_actions.ex @@ -0,0 +1,34 @@ +defmodule MediaServer.MovieActions do + use Task + + def start_link(arg) do + Task.start_link(__MODULE__, :run, [arg]) + end + + def run(_arg) do + Phoenix.PubSub.subscribe(MediaServer.PubSub, "movie") + end + + def handle_info({:added, %{"movie" => %{"id" => id, "title" => title}}}) do + MediaServer.MoviesIndex.reset() + + followers = MediaServer.MediaActions.movie(id) |> MediaServer.MediaActions.followers() + + Enum.each(followers, fn media_action -> + Enum.each(media_action.user.push_subscriptions, fn push_subscription -> + WebPushElixir.send_notification( + push_subscription.push_subscription, + "#{title} is now available" + ) + end) + end) + end + + def handle_info({:deleted}) do + MediaServer.MoviesIndex.reset() + end + + def handle_info({:deleted_file}) do + MediaServer.MoviesIndex.reset() + end +end diff --git a/lib/media_server_web/controllers/webhooks_controller.ex b/lib/media_server_web/controllers/webhooks_controller.ex index 7688b29f..ccae5b53 100644 --- a/lib/media_server_web/controllers/webhooks_controller.ex +++ b/lib/media_server_web/controllers/webhooks_controller.ex @@ -1,37 +1,22 @@ defmodule MediaServerWeb.WebhooksController do use MediaServerWeb, :controller - def create(conn, %{ - "id" => "movie", - "eventType" => "Download", - "movie" => %{"id" => id, "title" => title} - }) do - MediaServer.MoviesIndex.reset() - - followers = MediaServer.MediaActions.movie(id) |> MediaServer.MediaActions.followers() - - Enum.each(followers, fn media_action -> - Enum.each(media_action.user.push_subscriptions, fn push_subscription -> - WebPushElixir.send_notification( - push_subscription.push_subscription, - "#{title} is now available" - ) - end) - end) + def create(conn, %{"id" => "movie", "eventType" => "Download"} = params) do + Phoenix.PubSub.broadcast(MediaServer.PubSub, "movie", {:added, params}) conn |> send_resp(201, "Ok") end def create(conn, %{"id" => "movie", "eventType" => "MovieDelete"}) do - MediaServer.MoviesIndex.reset() + Phoenix.PubSub.broadcast(MediaServer.PubSub, "movie", {:deleted}) conn |> send_resp(201, "Ok") end def create(conn, %{"id" => "movie", "eventType" => "MovieFileDelete"}) do - MediaServer.MoviesIndex.reset() + Phoenix.PubSub.broadcast(MediaServer.PubSub, "movie", {:deleted_file}) conn |> send_resp(201, "Ok") From 56319507b1eb6734dafd9b383d2af22366356e04 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Sun, 15 Oct 2023 15:58:48 +1300 Subject: [PATCH 18/18] docs: update README webhook --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 97af4473..18343b6a 100644 --- a/README.md +++ b/README.md @@ -204,7 +204,7 @@ services: To keep your media in sync, webhook urls are required in your integrations. Midarr accepts a POST request from your integrations with your unique API Token (found on the Midarr Settings page). -Add your webhook urls to `Radarr / Sonarr -> Settings -> Connect`: +Add these webhook urls to Radarr / Sonarr under `Settings -> Connect -> Webhook`: #### Radarr example ```