mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-11-28 18:51:06 +00:00
Use event structs
This commit is contained in:
parent
ed8ad74141
commit
b3fe0ab666
6 changed files with 33 additions and 18 deletions
|
@ -3,7 +3,7 @@ defmodule LiveBeats.Accounts do
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
alias LiveBeats.Repo
|
alias LiveBeats.Repo
|
||||||
alias LiveBeats.Accounts.{User, Identity}
|
alias LiveBeats.Accounts.{User, Identity, Events}
|
||||||
|
|
||||||
@admin_emails ["chris@chrismccord.com"]
|
@admin_emails ["chris@chrismccord.com"]
|
||||||
@pubsub LiveBeats.PubSub
|
@pubsub LiveBeats.PubSub
|
||||||
|
@ -64,19 +64,19 @@ defmodule LiveBeats.Accounts do
|
||||||
current_user
|
current_user
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_active_profile(%User{} = current_user, profile_user_id) do
|
def update_active_profile(%User{} = current_user, profile_uid) do
|
||||||
{1, _} =
|
{1, _} =
|
||||||
Repo.update_all(from(u in User, where: u.id == ^current_user.id),
|
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!(
|
Phoenix.PubSub.broadcast!(
|
||||||
@pubsub,
|
@pubsub,
|
||||||
topic(current_user.id),
|
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
|
end
|
||||||
|
|
||||||
## User registration
|
## User registration
|
||||||
|
|
5
lib/live_beats/accounts/events.ex
Normal file
5
lib/live_beats/accounts/events.ex
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
defmodule LiveBeats.Accounts.Events do
|
||||||
|
defmodule ActiveProfileChanged do
|
||||||
|
defstruct current_user: nil, new_profile_user_id: nil
|
||||||
|
end
|
||||||
|
end
|
|
@ -6,7 +6,7 @@ defmodule LiveBeats.MediaLibrary do
|
||||||
require Logger
|
require Logger
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query, warn: false
|
||||||
alias LiveBeats.{Repo, MP3Stat, Accounts}
|
alias LiveBeats.{Repo, MP3Stat, Accounts}
|
||||||
alias LiveBeats.MediaLibrary.{Profile, Song, Genre}
|
alias LiveBeats.MediaLibrary.{Profile, Song, Events, Genre}
|
||||||
alias Ecto.{Multi, Changeset}
|
alias Ecto.{Multi, Changeset}
|
||||||
|
|
||||||
@pubsub LiveBeats.PubSub
|
@pubsub LiveBeats.PubSub
|
||||||
|
@ -71,11 +71,10 @@ defmodule LiveBeats.MediaLibrary do
|
||||||
|
|
||||||
elapsed = elapsed_playback(new_song)
|
elapsed = elapsed_playback(new_song)
|
||||||
|
|
||||||
Phoenix.PubSub.broadcast!(
|
Phoenix.PubSub.broadcast!(@pubsub, topic(song.user_id), %Events.Play{
|
||||||
@pubsub,
|
song: song,
|
||||||
topic(song.user_id),
|
elapsed: elapsed
|
||||||
{__MODULE__, :play, song, %{elapsed: elapsed}}
|
})
|
||||||
)
|
|
||||||
|
|
||||||
new_song
|
new_song
|
||||||
end
|
end
|
||||||
|
@ -96,7 +95,7 @@ defmodule LiveBeats.MediaLibrary do
|
||||||
|> Multi.update_all(:now_paused, fn _ -> pause_query end, [])
|
|> Multi.update_all(:now_paused, fn _ -> pause_query end, [])
|
||||||
|> Repo.transaction()
|
|> 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
|
end
|
||||||
|
|
||||||
def play_next_song_auto(%Profile{} = profile) do
|
def play_next_song_auto(%Profile{} = profile) do
|
||||||
|
|
9
lib/live_beats/media_library/events.ex
Normal file
9
lib/live_beats/media_library/events.ex
Normal file
|
@ -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
|
|
@ -215,7 +215,7 @@ defmodule LiveBeatsWeb.PlayerLive do
|
||||||
{:noreply, socket}
|
{:noreply, socket}
|
||||||
end
|
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
|
if user_id do
|
||||||
{:noreply, assign(socket, profile: get_profile(user_id))}
|
{:noreply, assign(socket, profile: get_profile(user_id))}
|
||||||
else
|
else
|
||||||
|
@ -227,11 +227,11 @@ defmodule LiveBeatsWeb.PlayerLive do
|
||||||
{:noreply, play_current_song(socket)}
|
{:noreply, play_current_song(socket)}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_info({MediaLibrary, :pause, _}, socket) do
|
def handle_info(%MediaLibrary.Events.Pause{}, socket) do
|
||||||
{:noreply, push_pause(socket)}
|
{:noreply, push_pause(socket)}
|
||||||
end
|
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)}
|
{:noreply, play_song(socket, song, elapsed)}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -117,21 +117,23 @@ defmodule LiveBeatsWeb.SongLive.Index do
|
||||||
|
|
||||||
def handle_event("delete", %{"id" => id}, socket) do
|
def handle_event("delete", %{"id" => id}, socket) do
|
||||||
song = MediaLibrary.get_song!(id)
|
song = MediaLibrary.get_song!(id)
|
||||||
|
|
||||||
if song.user_id == socket.assigns.current_user.id do
|
if song.user_id == socket.assigns.current_user.id do
|
||||||
{:ok, _} = MediaLibrary.delete_song(song)
|
{:ok, _} = MediaLibrary.delete_song(song)
|
||||||
end
|
end
|
||||||
|
|
||||||
{:noreply, socket}
|
{:noreply, socket}
|
||||||
end
|
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)}
|
{:noreply, assign(socket, active_profile_id: user_id)}
|
||||||
end
|
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)}
|
{:noreply, play_song(socket, song)}
|
||||||
end
|
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)}
|
{:noreply, pause_song(socket, song.id)}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue