Skip to content

Commit 513e2a8

Browse files
committed
Update lobby_list for admins.
We broadcast to admins every time user connects to our app via the lobby_list event. Our frontend will then be able to listen to this event and handle updates accordingly. This event will get triggered every time a user joins the admin:active_users topic so it will be up to the frontend to check whether a user is already on the list.
1 parent 76af21c commit 513e2a8

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

web/channels/admin_channel.ex

+21-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ defmodule PhoenixChat.AdminChannel do
88

99
alias PhoenixChat.{Presence, Repo, AnonymousUser}
1010

11+
intercept ~w(lobby_list)
12+
1113
@doc """
1214
The `admin:active_users` topic is how we identify all users currently using the app.
1315
"""
@@ -35,7 +37,11 @@ defmodule PhoenixChat.AdminChannel do
3537
# in our DB.
3638
def handle_info(:after_join, %{assigns: %{uuid: uuid}} = socket) do
3739
# We save anonymous user to DB when it hasn't been saved before
38-
ensure_user_saved!(uuid)
40+
user = ensure_user_saved!(uuid)
41+
42+
# Used by the frontend to update their lobby_list (chatrooms displayed
43+
# on the sidebar)
44+
broadcast! socket, "lobby_list", user
3945

4046
push socket, "presence_state", Presence.list(socket)
4147
Logger.debug "Presence for socket: #{inspect socket}"
@@ -45,9 +51,22 @@ defmodule PhoenixChat.AdminChannel do
4551
{:noreply, socket}
4652
end
4753

54+
@doc """
55+
Sends the lobby_list only to admins
56+
"""
57+
def handle_out("lobby_list", payload, socket) do
58+
assigns = socket.assigns
59+
if assigns[:user_id] do
60+
push socket, "lobby_list", payload
61+
end
62+
{:noreply, socket}
63+
end
64+
4865
defp ensure_user_saved!(uuid) do
4966
user_exists = Repo.get(AnonymousUser, uuid)
50-
unless user_exists do
67+
if user_exists do
68+
user_exists
69+
else
5170
changeset = AnonymousUser.changeset(%AnonymousUser{}, %{id: uuid})
5271
Repo.insert!(changeset)
5372
end

0 commit comments

Comments
 (0)