send messages to user tracker from presence client

This commit is contained in:
Berenice Medel 2022-01-25 13:39:27 -06:00
parent b440ec24ce
commit f23539c6da
3 changed files with 26 additions and 17 deletions

View file

@ -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}

View file

@ -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

View file

@ -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