diff --git a/assets/src/PlanningPokerRoom.elm b/assets/src/PlanningPokerRoom.elm index 54541c6..254bad5 100644 --- a/assets/src/PlanningPokerRoom.elm +++ b/assets/src/PlanningPokerRoom.elm @@ -364,25 +364,11 @@ subscriptions = API.gotPresence GotPresence -type alias Presence = - { metas : List PresenceMeta } - - -type alias PresenceMeta = - { name : String - , online_at : String - , phx_ref : String - } - - playersDecoder : Decode.Decoder (Dict String Player) playersDecoder = let - meta = - Decode.field "name" Decode.string - presence = - Decode.field "metas" (Decode.index 0 meta) + Decode.field "name" Decode.string toPlayer id name = { level = Participant diff --git a/lib/planningpoker/application.ex b/lib/planningpoker/application.ex index a7adcc4..68f31c3 100644 --- a/lib/planningpoker/application.ex +++ b/lib/planningpoker/application.ex @@ -15,7 +15,8 @@ defmodule Planningpoker.Application do PlanningpokerWeb.Endpoint, # Start a worker by calling: Planningpoker.Worker.start_link(arg) # {Planningpoker.Worker, arg} - PlanningpokerWeb.Presence + PlanningpokerWeb.Presence, + Planningpoker.Db ] # See https://hexdocs.pm/elixir/Supervisor.html diff --git a/lib/planningpoker/db.ex b/lib/planningpoker/db.ex new file mode 100644 index 0000000..08e3390 --- /dev/null +++ b/lib/planningpoker/db.ex @@ -0,0 +1,22 @@ +defmodule Planningpoker.Db do + require Logger + use GenServer + + def start_link(_) do + GenServer.start_link(__MODULE__, [], name: __MODULE__) + end + + def init(_) do + :ets.new(:users, [:named_table, :public]) + {:ok, %{}} + end + + def save_name(user, name) do + Logger.debug("Storing user name (#{user} -> #{name})") + :ets.insert(:users, {user, name}) + end + + def get_users(keys) do + :ets.select(:users, (for key <- keys, do: {{key, :_}, [], [:"$_"]})) + end +end diff --git a/lib/planningpoker_web/channels/presence.ex b/lib/planningpoker_web/channels/presence.ex index f90a888..cc502dc 100644 --- a/lib/planningpoker_web/channels/presence.ex +++ b/lib/planningpoker_web/channels/presence.ex @@ -7,4 +7,18 @@ defmodule PlanningpokerWeb.Presence do """ use Phoenix.Presence, otp_app: :planningpoker, pubsub_server: Planningpoker.PubSub + require Logger + alias Planningpoker.Db + + def fetch(_topic, entries) do + users = + entries + |> Map.keys() + |> Db.get_users() + |> Enum.into(%{}) + + for {key, %{metas: metas}} <- entries, into: %{} do + {key, %{metas: metas, name: users[key]}} + end + end end diff --git a/lib/planningpoker_web/channels/room_channel.ex b/lib/planningpoker_web/channels/room_channel.ex index 5c00735..fba1c8b 100644 --- a/lib/planningpoker_web/channels/room_channel.ex +++ b/lib/planningpoker_web/channels/room_channel.ex @@ -1,9 +1,12 @@ defmodule PlanningpokerWeb.RoomChannel do + require Logger use Phoenix.Channel alias PlanningpokerWeb.Presence + alias Planningpoker.Db def join("room:" <> room_id, params, socket) do send(self(), :after_join) + Logger.debug "Proc: #{inspect self()}, Socket: #{inspect socket}" {:ok, %{channel: room_id, topic: "Planning Poker"}, socket |> assign(:room_id, room_id) @@ -14,12 +17,11 @@ defmodule PlanningpokerWeb.RoomChannel do {:noreply, socket} end def handle_in("new_profile", %{"name" => name}, socket) do + Db.save_name(socket.assigns.player_id, name) {:ok, _} = Presence.track( socket, - "player:#{socket.assigns.player_id}", - %{ - name: name - } + socket.assigns.player_id, + %{} ) {:noreply, socket} end