diff --git a/lib/live_beats/accounts.ex b/lib/live_beats/accounts.ex index 22ad860..ab84bf2 100644 --- a/lib/live_beats/accounts.ex +++ b/lib/live_beats/accounts.ex @@ -3,7 +3,7 @@ defmodule LiveBeats.Accounts do import Ecto.Changeset alias LiveBeats.Repo - alias LiveBeats.Accounts.{User, Identity} + alias LiveBeats.Accounts.{User, Identity, Events} @admin_emails ["chris@chrismccord.com"] @pubsub LiveBeats.PubSub @@ -64,19 +64,19 @@ defmodule LiveBeats.Accounts do current_user end - def update_active_profile(%User{} = current_user, profile_user_id) do + def update_active_profile(%User{} = current_user, profile_uid) do {1, _} = Repo.update_all(from(u in User, where: u.id == ^current_user.id), - set: [active_profile_user_id: profile_user_id] + set: [active_profile_user_id: profile_uid] ) Phoenix.PubSub.broadcast!( @pubsub, topic(current_user.id), - {__MODULE__, :active_profile_changed, current_user, %{user_id: profile_user_id}} + %Events.ActiveProfileChanged{current_user: current_user, new_profile_user_id: profile_uid} ) - %User{current_user | active_profile_user_id: profile_user_id} + %User{current_user | active_profile_user_id: profile_uid} end ## User registration diff --git a/lib/live_beats/accounts/events.ex b/lib/live_beats/accounts/events.ex new file mode 100644 index 0000000..1f6d152 --- /dev/null +++ b/lib/live_beats/accounts/events.ex @@ -0,0 +1,5 @@ +defmodule LiveBeats.Accounts.Events do + defmodule ActiveProfileChanged do + defstruct current_user: nil, new_profile_user_id: nil + end +end diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex index d12fa18..ab11e58 100644 --- a/lib/live_beats/media_library.ex +++ b/lib/live_beats/media_library.ex @@ -6,7 +6,7 @@ defmodule LiveBeats.MediaLibrary do require Logger import Ecto.Query, warn: false alias LiveBeats.{Repo, MP3Stat, Accounts} - alias LiveBeats.MediaLibrary.{Profile, Song, Genre} + alias LiveBeats.MediaLibrary.{Profile, Song, Events, Genre} alias Ecto.{Multi, Changeset} @pubsub LiveBeats.PubSub @@ -71,11 +71,10 @@ defmodule LiveBeats.MediaLibrary do elapsed = elapsed_playback(new_song) - Phoenix.PubSub.broadcast!( - @pubsub, - topic(song.user_id), - {__MODULE__, :play, song, %{elapsed: elapsed}} - ) + Phoenix.PubSub.broadcast!(@pubsub, topic(song.user_id), %Events.Play{ + song: song, + elapsed: elapsed + }) new_song end @@ -96,7 +95,7 @@ defmodule LiveBeats.MediaLibrary do |> Multi.update_all(:now_paused, fn _ -> pause_query end, []) |> Repo.transaction() - Phoenix.PubSub.broadcast!(@pubsub, topic(song.user_id), {__MODULE__, :pause, song}) + Phoenix.PubSub.broadcast!(@pubsub, topic(song.user_id), %Events.Pause{song: song}) end def play_next_song_auto(%Profile{} = profile) do diff --git a/lib/live_beats/media_library/events.ex b/lib/live_beats/media_library/events.ex new file mode 100644 index 0000000..78f7d71 --- /dev/null +++ b/lib/live_beats/media_library/events.ex @@ -0,0 +1,9 @@ +defmodule LiveBeats.MediaLibrary.Events do + defmodule Play do + defstruct song: nil, elapsed: nil + end + + defmodule Pause do + defstruct song: nil + end +end diff --git a/lib/live_beats_web/live/player_live.ex b/lib/live_beats_web/live/player_live.ex index 75428fb..e63e972 100644 --- a/lib/live_beats_web/live/player_live.ex +++ b/lib/live_beats_web/live/player_live.ex @@ -215,7 +215,7 @@ defmodule LiveBeatsWeb.PlayerLive do {:noreply, socket} end - def handle_info({Accounts, :active_profile_changed, _cur_user, %{user_id: user_id}}, socket) do + def handle_info(%Accounts.Events.ActiveProfileChanged{new_profile_user_id: user_id}, socket) do if user_id do {:noreply, assign(socket, profile: get_profile(user_id))} else @@ -227,11 +227,11 @@ defmodule LiveBeatsWeb.PlayerLive do {:noreply, play_current_song(socket)} end - def handle_info({MediaLibrary, :pause, _}, socket) do + def handle_info(%MediaLibrary.Events.Pause{}, socket) do {:noreply, push_pause(socket)} end - def handle_info({MediaLibrary, :play, %Song{} = song, %{elapsed: elapsed}}, socket) do + def handle_info(%MediaLibrary.Events.Play{song: song, elapsed: elapsed}, socket) do {:noreply, play_song(socket, song, elapsed)} end diff --git a/lib/live_beats_web/live/song_live/index.ex b/lib/live_beats_web/live/song_live/index.ex index ec30376..14c64d7 100644 --- a/lib/live_beats_web/live/song_live/index.ex +++ b/lib/live_beats_web/live/song_live/index.ex @@ -117,21 +117,23 @@ defmodule LiveBeatsWeb.SongLive.Index do def handle_event("delete", %{"id" => id}, socket) do song = MediaLibrary.get_song!(id) + if song.user_id == socket.assigns.current_user.id do {:ok, _} = MediaLibrary.delete_song(song) end + {:noreply, socket} end - def handle_info({Accounts, :active_profile_changed, _cur_user, %{user_id: user_id}}, socket) do + def handle_info(%Accounts.Events.ActiveProfileChanged{new_profile_user_id: user_id}, socket) do {:noreply, assign(socket, active_profile_id: user_id)} end - def handle_info({MediaLibrary, :play, %MediaLibrary.Song{} = song, _meta}, socket) do + def handle_info(%MediaLibrary.Events.Play{song: song}, socket) do {:noreply, play_song(socket, song)} end - def handle_info({MediaLibrary, :pause, %MediaLibrary.Song{} = song}, socket) do + def handle_info(%MediaLibrary.Events.Pause{song: song}, socket) do {:noreply, pause_song(socket, song.id)} end