diff --git a/lib/live_beats/application.ex b/lib/live_beats/application.ex index 3002daf..bd62df6 100644 --- a/lib/live_beats/application.ex +++ b/lib/live_beats/application.ex @@ -21,10 +21,11 @@ defmodule LiveBeats.Application do def start(_type, _args) do parent = FLAME.Parent.get() LiveBeats.MediaLibrary.attach() + whisper_serving? = parent || FLAME.Backend.impl() != FLAME.FlyBackend children = [ - {Nx.Serving, name: LiveBeats.WhisperServing, serving: load_serving()}, + whisper_serving? && {Nx.Serving, name: LiveBeats.WhisperServing, serving: load_serving()}, !parent && {DNSCluster, query: Application.get_env(:wps, :dns_cluster_query) || :ignore}, {Task.Supervisor, name: LiveBeats.TaskSupervisor}, # Start the Ecto repository diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex index 39e6670..acb94ef 100644 --- a/lib/live_beats/media_library.ex +++ b/lib/live_beats/media_library.ex @@ -23,7 +23,28 @@ defmodule LiveBeats.MediaLibrary do def handle_execute({Accounts, %Accounts.Events.PublicSettingsChanged{user: user}}) do profile = get_profile!(user) - broadcast!(user.id, %Events.PublicProfileUpdated{profile: profile}) + + active? = + Repo.exists?( + from(s in Song, + inner_join: u in LiveBeats.Accounts.User, + on: s.user_id == ^user.id, + where: s.status in [:playing], + limit: 1 + ) + ) + + if active? do + broadcast_active_profile!(profile) + else + broadcast_inactive_profile!(profile) + end + + broadcast!(user.id, %Events.PublicProfileUpdated{profile: profile, active?: active?}) + end + + def subscribe_to_active_profiles do + Phoenix.PubSub.subscribe(@pubsub, "active_profiles") end def subscribe_to_profile(%Profile{} = profile) do @@ -53,6 +74,8 @@ defmodule LiveBeats.MediaLibrary do end def play_song(%Song{} = song) do + user = Accounts.get_user!(song.user_id) + played_at = cond do playing?(song) -> @@ -86,6 +109,7 @@ defmodule LiveBeats.MediaLibrary do elapsed = elapsed_playback(new_song) + broadcast_active_profile!(get_profile!(user)) broadcast!(song.user_id, %Events.Play{song: song, elapsed: elapsed}) new_song @@ -98,6 +122,8 @@ defmodule LiveBeats.MediaLibrary do end def pause_song(%Song{} = song) do + user = Accounts.get_user!(song.user_id) + now = DateTime.truncate(DateTime.utc_now(), :second) set = [status: :paused, paused_at: now] pause_query = from(s in Song, where: s.id == ^song.id, update: [set: ^set]) @@ -114,6 +140,7 @@ defmodule LiveBeats.MediaLibrary do |> Multi.update_all(:now_paused, fn _ -> pause_query end, []) |> Repo.transaction() + broadcast_inactive_profile!(get_profile!(user)) broadcast!(song.user_id, %Events.Pause{song: song}) end @@ -554,6 +581,16 @@ defmodule LiveBeats.MediaLibrary do Phoenix.PubSub.broadcast_from!(@pubsub, pid, topic(user_id), {__MODULE__, msg}) end + defp broadcast_active_profile!(%Profile{} = profile) do + msg = %Events.PublicProfileActive{profile: profile} + Phoenix.PubSub.broadcast!(@pubsub, "active_profiles", {__MODULE__, msg}) + end + + defp broadcast_inactive_profile!(%Profile{} = profile) do + msg = %Events.PublicProfileInActive{profile: profile} + Phoenix.PubSub.broadcast!(@pubsub, "active_profiles", {__MODULE__, msg}) + end + defp topic(user_id) when is_integer(user_id), do: "profile:#{user_id}" defp validate_songs_limit(user_songs, new_songs_count) do diff --git a/lib/live_beats/media_library/events.ex b/lib/live_beats/media_library/events.ex index 724c3ee..ed0e3db 100644 --- a/lib/live_beats/media_library/events.ex +++ b/lib/live_beats/media_library/events.ex @@ -8,6 +8,14 @@ defmodule LiveBeats.MediaLibrary.Events do end defmodule PublicProfileUpdated do + defstruct profile: nil, active?: false + end + + defmodule PublicProfileActive do + defstruct profile: nil + end + + defmodule PublicProfileInActive do defstruct profile: nil end diff --git a/lib/live_beats_web/components/core_components.ex b/lib/live_beats_web/components/core_components.ex index 0c8bcd7..bcbc7e9 100644 --- a/lib/live_beats_web/components/core_components.ex +++ b/lib/live_beats_web/components/core_components.ex @@ -574,8 +574,8 @@ defmodule LiveBeatsWeb.CoreComponents do slot :col, required: true do attr :label, :string - attr :class, :string - attr :class!, :string + attr :class, :any + attr :class!, :any end def table(assigns) do diff --git a/lib/live_beats_web/components/layouts.ex b/lib/live_beats_web/components/layouts.ex index 5c318db..80060d7 100644 --- a/lib/live_beats_web/components/layouts.ex +++ b/lib/live_beats_web/components/layouts.ex @@ -12,18 +12,18 @@ defmodule LiveBeatsWeb.Layouts do