diff --git a/lib/live_beats/accounts.ex b/lib/live_beats/accounts.ex index 7ec1f1a..6ec2ac0 100644 --- a/lib/live_beats/accounts.ex +++ b/lib/live_beats/accounts.ex @@ -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 diff --git a/lib/live_beats/presence/phoenix_presence_client.ex b/lib/live_beats/presence/phoenix_presence_client.ex index 0eff36b..e90bb32 100644 --- a/lib/live_beats/presence/phoenix_presence_client.ex +++ b/lib/live_beats/presence/phoenix_presence_client.ex @@ -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 diff --git a/lib/live_beats_web/channels/presence.ex b/lib/live_beats_web/channels/presence.ex index adfd407..7593efc 100644 --- a/lib/live_beats_web/channels/presence.ex +++ b/lib/live_beats_web/channels/presence.ex @@ -12,6 +12,8 @@ defmodule LiveBeatsWeb.Presence do import LiveBeatsWeb.LiveHelpers @pubsub LiveBeats.PubSub + alias LiveBeats.Accounts + def listening_now(assigns) do ~H""" @@ -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 diff --git a/lib/live_beats_web/live/profile_live.ex b/lib/live_beats_web/live/profile_live.ex index e898324..fae41f3 100644 --- a/lib/live_beats_web/live/profile_live.ex +++ b/lib/live_beats_web/live/profile_live.ex @@ -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