From 924da28b6b0df89ee1a7947a4018aac31a876311 Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Wed, 12 Jan 2022 12:27:30 -0500 Subject: [PATCH] Move presence tracking to player --- .../presence/phoenix_presence_client.ex | 4 +- lib/live_beats/presence/presence_client.ex | 58 ++++++++++--------- lib/live_beats_web/channels/presence.ex | 41 ++++++------- lib/live_beats_web/live/player_live.ex | 10 +++- lib/live_beats_web/live/profile_live.ex | 23 +++----- 5 files changed, 67 insertions(+), 69 deletions(-) diff --git a/lib/live_beats/presence/phoenix_presence_client.ex b/lib/live_beats/presence/phoenix_presence_client.ex index afda887..d416217 100644 --- a/lib/live_beats/presence/phoenix_presence_client.ex +++ b/lib/live_beats/presence/phoenix_presence_client.ex @@ -83,9 +83,9 @@ defmodule Phoenix.Presence.Client do defp untrack_pid(state, pid, topic, key) do if Map.has_key?(state.topics, topic) do state.presence_mod.untrack(pid, topic, key) - else - state end + + state end defp merge_diff(state, topic, %{leaves: leaves, joins: joins}) do diff --git a/lib/live_beats/presence/presence_client.ex b/lib/live_beats/presence/presence_client.ex index 0ddec5e..e72c2a2 100644 --- a/lib/live_beats/presence/presence_client.ex +++ b/lib/live_beats/presence/presence_client.ex @@ -4,6 +4,31 @@ defmodule LiveBeats.PresenceClient do @presence LiveBeatsWeb.Presence @pubsub LiveBeats.PubSub + alias LiveBeats.MediaLibrary + + def track(%MediaLibrary.Profile{} = profile, current_user_id) do + Phoenix.Presence.Client.track( + "proxy:" <> topic(profile), + current_user_id, + %{} + ) + end + + def untrack(%MediaLibrary.Profile{} = profile, current_user_id) do + Phoenix.Presence.Client.untrack( + "proxy:" <> topic(profile), + current_user_id + ) + end + + def subscribe(%MediaLibrary.Profile{} = profile) do + Phoenix.PubSub.subscribe(@pubsub, topic(profile)) + end + + def list(%MediaLibrary.Profile{} = profile) do + list("proxy:" <> topic(profile)) + end + def list(topic) do @presence.list(topic) end @@ -16,42 +41,21 @@ defmodule LiveBeats.PresenceClient do @impl Phoenix.Presence.Client def handle_join(topic, _key, presence, state) do - active_users_topic = - topic - |> profile_identifier() - |> active_users_topic() - - Phoenix.PubSub.local_broadcast( - @pubsub, - active_users_topic, - {__MODULE__, %{user_joined: presence}} - ) - + local_broadcast(topic, {__MODULE__, %{user_joined: presence}}) {:ok, state} end @impl Phoenix.Presence.Client def handle_leave(topic, _key, presence, state) do - active_users_topic = - topic - |> profile_identifier() - |> active_users_topic() - - Phoenix.PubSub.local_broadcast( - @pubsub, - active_users_topic, - {__MODULE__, %{user_left: presence}} - ) - + local_broadcast(topic, {__MODULE__, %{user_left: presence}}) {:ok, state} end - defp active_users_topic(user_id) do - "active_users:#{user_id}" + defp topic(%MediaLibrary.Profile{} = profile) do + "active_profiles:#{profile.user_id}" end - defp profile_identifier(topic) do - "active_profile:" <> identifier = topic - identifier + defp local_broadcast("proxy:" <> topic, payload) do + Phoenix.PubSub.local_broadcast(@pubsub, topic, payload) end end diff --git a/lib/live_beats_web/channels/presence.ex b/lib/live_beats_web/channels/presence.ex index d203309..a82e228 100644 --- a/lib/live_beats_web/channels/presence.ex +++ b/lib/live_beats_web/channels/presence.ex @@ -11,15 +11,30 @@ defmodule LiveBeatsWeb.Presence do import Phoenix.LiveView.Helpers import LiveBeatsWeb.LiveHelpers - @pubsub LiveBeats.PubSub - alias LiveBeats.Accounts + alias LiveBeats.{Accounts, MediaLibrary} + + def subscribe(%MediaLibrary.Profile{} = profile) do + LiveBeats.PresenceClient.subscribe(profile) + end + + def fetch(_topic, presences) do + users = + presences + |> Map.keys() + |> Accounts.get_users_map() + |> Enum.into(%{}) + + for {key, %{metas: metas}} <- presences, into: %{} do + {key, %{metas: metas, user: users[String.to_integer(key)]}} + end + end def listening_now(assigns) do ~H"""
-

Here now

+

Listening now