mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-11-24 17:01:00 +00:00
implement fetch callback to list users
This commit is contained in:
parent
defb8ccf92
commit
9b5587d484
4 changed files with 18 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue