implement fetch callback to list users

This commit is contained in:
Berenice Medel Sánchez 2021-12-14 10:23:19 -06:00 committed by Chris McCord
parent defb8ccf92
commit 9b5587d484
4 changed files with 18 additions and 9 deletions

View file

@ -21,8 +21,8 @@ defmodule LiveBeats.Accounts do
Repo.all(from u in User, limit: ^Keyword.fetch!(opts, :limit)) Repo.all(from u in User, limit: ^Keyword.fetch!(opts, :limit))
end end
def list_users_by_ids(user_ids) when is_list(user_ids) do def get_users_map(user_ids) when is_list(user_ids) do
Repo.all(from u in User, where: u.id in ^user_ids) Repo.all(from u in User, where: u.id in ^user_ids, select: {u.id, u})
end end
def lists_users_by_active_profile(id, opts) do def lists_users_by_active_profile(id, opts) do

View file

@ -177,8 +177,6 @@ defmodule Phoenix.Presence.Client do
end end
defp topic_presences_count(state, topic) do defp topic_presences_count(state, topic) do
state.topics[topic] map_size(state.topics[topic])
|> Map.keys()
|> Enum.count()
end end
end end

View file

@ -12,6 +12,8 @@ defmodule LiveBeatsWeb.Presence do
import LiveBeatsWeb.LiveHelpers import LiveBeatsWeb.LiveHelpers
@pubsub LiveBeats.PubSub @pubsub LiveBeats.PubSub
alias LiveBeats.Accounts
def listening_now(assigns) do def listening_now(assigns) do
~H""" ~H"""
<!-- users --> <!-- users -->
@ -33,6 +35,18 @@ defmodule LiveBeatsWeb.Presence do
""" """
end 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 subscribe(user_id) do def subscribe(user_id) do
Phoenix.PubSub.subscribe(@pubsub, topic(user_id)) Phoenix.PubSub.subscribe(@pubsub, topic(user_id))
end end

View file

@ -267,10 +267,7 @@ defmodule LiveBeatsWeb.ProfileLive do
presences = socket.assigns.profile.user_id presences = socket.assigns.profile.user_id
|> topic() |> topic()
|> LiveBeats.PresenceClient.list() |> LiveBeats.PresenceClient.list()
|> Enum.map(fn {user_id, _user_data} -> |> Enum.map(fn {_key, meta} -> meta.user end)
user_id
end)
|> Accounts.list_users_by_ids()
assign(socket, presences: presences) assign(socket, presences: presences)
end end