From 826cf1d4f900f5787a9208c662f36ef59cba3a4d Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Mon, 31 Jan 2022 08:21:27 -0500 Subject: [PATCH] Add pings --- lib/live_beats/media_library.ex | 5 +++- lib/live_beats_web/channels/presence.ex | 37 +++++++++++++++++++++---- lib/live_beats_web/live/nav.ex | 2 +- lib/live_beats_web/live/profile_live.ex | 30 ++++++++++++-------- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex index 7335ecd..3863b90 100644 --- a/lib/live_beats/media_library.ex +++ b/lib/live_beats/media_library.ex @@ -31,7 +31,10 @@ defmodule LiveBeats.MediaLibrary do end def broadcast_ping(%Accounts.User{} = user, rtt, region) do - broadcast!(user.active_profile_user_id, {:ping, %{rtt: rtt, region: region}}) + broadcast!( + user.active_profile_user_id, + {:ping, %{user: user, rtt: rtt, region: region}} + ) end def unsubscribe_to_profile(%Profile{} = profile) do diff --git a/lib/live_beats_web/channels/presence.ex b/lib/live_beats_web/channels/presence.ex index a82e228..d86e9b2 100644 --- a/lib/live_beats_web/channels/presence.ex +++ b/lib/live_beats_web/channels/presence.ex @@ -29,11 +29,14 @@ defmodule LiveBeatsWeb.Presence do {key, %{metas: metas, user: users[String.to_integer(key)]}} end end +end - def listening_now(assigns) do +defmodule LiveBeatsWeb.Presence.BadgeListComponent do + use LiveBeatsWeb, :live_component + + def render(assigns) do ~H""" - -
+

Listening now

  • <.link navigate={profile_path(presence)} class="flex-1 flex items-center justify-between border-t border-r border-b border-gray-200 bg-white rounded-r-md truncate"> - +
    - <%= render_slot(@title, presence) %> +
    + <%= render_slot(@inner_block, %{user: presence, ping: @pings[presence.id], region: @regions[presence.id]}) %> +
  • @@ -56,4 +61,26 @@ defmodule LiveBeatsWeb.Presence do
""" end + + def mount(socket) do + {:ok, socket, temporary_assigns: [presences: [], pings: %{}, regions: %{}]} + end + + def update(%{action: {:ping, action}}, socket) do + %{user: user, ping: ping, region: region} = action + + {:ok, + socket + |> assign(:presences, [user]) + |> update(:pings, &Map.put(&1, user.id, ping)) + |> update(:regions, &Map.put(&1, user.id, region))} + end + + def update(assigns, socket) do + {:ok, + socket + |> assign(assigns) + |> assign_new(:pings, fn -> %{} end) + |> assign_new(:regions, fn -> %{} end)} + end end diff --git a/lib/live_beats_web/live/nav.ex b/lib/live_beats_web/live/nav.ex index af4085d..5509850 100644 --- a/lib/live_beats_web/live/nav.ex +++ b/lib/live_beats_web/live/nav.ex @@ -8,7 +8,7 @@ defmodule LiveBeatsWeb.Nav do {:cont, socket |> assign(active_users: MediaLibrary.list_active_profiles(limit: 20)) - |> assign(:region, System.get_env("FLY_REGION")) + |> assign(:region, System.get_env("FLY_REGION") || "iad") |> attach_hook(:active_tab, :handle_params, &handle_active_tab_params/3) |> attach_hook(:ping, :handle_event, &handle_event/3)} end diff --git a/lib/live_beats_web/live/profile_live.ex b/lib/live_beats_web/live/profile_live.ex index d224fbf..621b9c6 100644 --- a/lib/live_beats_web/live/profile_live.ex +++ b/lib/live_beats_web/live/profile_live.ex @@ -39,15 +39,17 @@ defmodule LiveBeatsWeb.ProfileLive do - - <:title let={%{presence: presence, ping: ping, region: region}}> - <%= presence.username %> - <%= if ping do %> - (<%= ping %>ms) - <%= if region do %><% end %> - <% end %> - - + <.live_component + let={%{user: user, ping: ping, region: region}} + id={:presence_badges} module={Presence.BadgeListComponent} + presences={@presences} + > + <%= user.username %> + <%= if ping do %> +

ping: <%= ping %>ms

+ <%= if region do %><% end %> + <% end %> +
<%= for song <- if(@owns_profile?, do: @songs, else: []), id = "delete-modal-#{song.id}" do %> @@ -156,6 +158,7 @@ defmodule LiveBeatsWeb.ProfileLive do def handle_info({LiveBeats.PresenceClient, %{user_left: presence}}, socket) do %{user: user} = presence + if presence.metas == [] do {:noreply, push_event(socket, "remove-el", %{id: "presence-#{user.id}"})} else @@ -186,8 +189,13 @@ defmodule LiveBeatsWeb.ProfileLive do {:noreply, update(socket, :songs, &(&1 ++ songs))} end - def handle_info({MediaLibrary, {:ping, %{user_id: id, rtt: rtt, region: region}}}, socket) do - send_update(Presence.Pill, id: id, ping: rtt, region: region) + def handle_info({MediaLibrary, {:ping, ping}}, socket) do + %{user: user, rtt: rtt, region: region} = ping + send_update(Presence.BadgeListComponent, + id: :presence_badges, + action: {:ping, %{user: user, ping: rtt, region: region}} + ) + {:noreply, socket} end