From f23539c6da3bda7c3d81e49326ec6dc3535f5154 Mon Sep 17 00:00:00 2001 From: Berenice Medel Date: Tue, 25 Jan 2022 13:39:27 -0600 Subject: [PATCH] send messages to user tracker from presence client --- lib/live_beats/application.ex | 4 ++- lib/live_beats/presence/presence_client.ex | 2 ++ lib/live_beats/user_tracker.ex | 37 ++++++++++++---------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/lib/live_beats/application.ex b/lib/live_beats/application.ex index 4d52e9a..d971645 100644 --- a/lib/live_beats/application.ex +++ b/lib/live_beats/application.ex @@ -29,7 +29,9 @@ defmodule LiveBeats.Application do name: PresenceClient}, # Start the Endpoint (http/https) LiveBeatsWeb.Endpoint, - {LiveBeats.SongsCleaner, count: 7, interval: :day} + {LiveBeats.SongsCleaner, count: 7, interval: :day}, + LiveBeats.UserTracker + # Start a worker by calling: LiveBeats.Worker.start_link(arg) # {LiveBeats.Worker, arg} diff --git a/lib/live_beats/presence/presence_client.ex b/lib/live_beats/presence/presence_client.ex index e72c2a2..340fbfd 100644 --- a/lib/live_beats/presence/presence_client.ex +++ b/lib/live_beats/presence/presence_client.ex @@ -41,12 +41,14 @@ defmodule LiveBeats.PresenceClient do @impl Phoenix.Presence.Client def handle_join(topic, _key, presence, state) do + LiveBeats.UserTracker.presence_joined(presence) local_broadcast(topic, {__MODULE__, %{user_joined: presence}}) {:ok, state} end @impl Phoenix.Presence.Client def handle_leave(topic, _key, presence, state) do + LiveBeats.UserTracker.presence_left(presence) local_broadcast(topic, {__MODULE__, %{user_left: presence}}) {:ok, state} end diff --git a/lib/live_beats/user_tracker.ex b/lib/live_beats/user_tracker.ex index e83bf21..e9f062a 100644 --- a/lib/live_beats/user_tracker.ex +++ b/lib/live_beats/user_tracker.ex @@ -13,8 +13,16 @@ defmodule LiveBeats.UserTracker do Phoenix.PubSub.subscribe(@pubsub, topic()) end - def start_link(_opts) do - GenServer.start_link(__MODULE__, name: __MODULE__) + def start_link(opts) do + GenServer.start_link(__MODULE__, opts, name: __MODULE__) + end + + def presence_joined(presence) do + GenServer.call(__MODULE__, {:presence_joined, presence}) + end + + def presence_left(presence) do + GenServer.call(__MODULE__, {:presence_left, presence}) end @impl true @@ -23,13 +31,13 @@ defmodule LiveBeats.UserTracker do end @impl true - def handle_call({:user_joined, user}, _from, state) do - {:reply, :ok, handle_join(state, user) |> IO.inspect} + def handle_call({:presence_joined, presence}, _from, state) do + {:reply, :ok, handle_join(state, presence)} end @impl true - def handle_call({:user_left, user}, _from, state) do - {:reply, :ok, handle_leave(state, user) |> IO.inspect} + def handle_call({:presence_left, presence}, _from, state) do + {:reply, :ok, handle_leave(state, presence)} end @impl true @@ -43,17 +51,17 @@ defmodule LiveBeats.UserTracker do state end - defp handle_join(state, user) do - if Map.has_key?(state, user.id) do + defp handle_join(state, presence) do + if Map.has_key?(state, presence.user.id) do state else - Map.put_new(state, user.id, user) + Map.put_new(state, presence.user.id, presence.user) end end - defp handle_leave(state, user) do - if Map.has_key?(state, user.id) do - Map.delete(state, user.id) + defp handle_leave(state, presence) do + if Map.has_key?(state, presence.user.id) and presence.metas == [] do + Map.delete(state, presence.user.id) else state end @@ -68,10 +76,7 @@ defmodule LiveBeats.UserTracker do state |> Enum.map(fn {_key, value} -> value end) - Logger.info( - "broadcasting updates" - ) - Phoenix.PubSub.local_broadcast(@pubsub, topic(), active_users) + Phoenix.PubSub.local_broadcast(@pubsub, topic(), {LiveBeats.UserTracker, %{active_users: active_users}}) end end