chronoscope/lib/chronoscope_web/live/index_live.ex

36 lines
970 B
Elixir
Raw Normal View History

2024-05-28 18:27:02 -04:00
defmodule ChronoscopeWeb.IndexLive do
use ChronoscopeWeb, :live_view
2024-05-29 15:25:05 -04:00
alias Chronoscope.NTS
2024-06-01 10:30:35 -04:00
alias Chronoscope.NTS.KeyEstablishmentResponse
2024-06-05 14:21:26 -04:00
alias ChronoscopeWeb.ClientActivator
2024-05-29 15:25:05 -04:00
2024-06-04 20:03:02 -04:00
@topic Application.compile_env(:chronoscope, :nts_topic)
2024-05-28 18:27:02 -04:00
def mount(_params, _session, socket) do
ChronoscopeWeb.Endpoint.subscribe(@topic)
{:ok, assign(socket, %{servers: server_list(), clients: client_list()})}
2024-05-28 18:27:02 -04:00
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
2024-06-04 20:03:02 -04:00
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
2024-06-05 14:21:26 -04:00
GenServer.call(ClientActivator, :get_nts_servers)
end
defp client_list() do
server_list()
2024-06-04 20:03:02 -04:00
|> NTS.list_clients()
2024-05-28 18:27:02 -04:00
end
end