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))
end
def list_users_by_ids(user_ids) when is_list(user_ids) do
Repo.all(from u in User, where: u.id in ^user_ids)
def get_users_map(user_ids) when is_list(user_ids) do
Repo.all(from u in User, where: u.id in ^user_ids, select: {u.id, u})
end
def lists_users_by_active_profile(id, opts) do

View file

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

View file

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

View file

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