chronoscope/lib/chronoscope_web/live/index_live.ex

36 lines
970 B
Elixir

defmodule ChronoscopeWeb.IndexLive do
use ChronoscopeWeb, :live_view
alias Chronoscope.NTS
alias Chronoscope.NTS.KeyEstablishmentResponse
alias ChronoscopeWeb.ClientActivator
@topic Application.compile_env(:chronoscope, :nts_topic)
def mount(_params, _session, socket) do
ChronoscopeWeb.Endpoint.subscribe(@topic)
{:ok, assign(socket, %{servers: server_list(), clients: client_list()})}
end
def handle_info(%{topic: @topic, event: "key-exchange", payload: client}, socket) do
if client.server in socket.assigns.servers do
{:noreply, update(socket, :clients, &update_client(&1, client))}
else
{:noreply, socket}
end
end
defp update_client(client_list, client) do
Enum.map(client_list, &if(client.server == &1.server, do: client, else: &1))
end
defp server_list() do
GenServer.call(ClientActivator, :get_nts_servers)
end
defp client_list() do
server_list()
|> NTS.list_clients()
end
end