43 lines
1.1 KiB
Elixir
43 lines
1.1 KiB
Elixir
defmodule ChronoscopeWeb.IndexLive do
|
|
use ChronoscopeWeb, :live_view
|
|
|
|
alias Chronoscope.NTS
|
|
alias Chronoscope.NTS.KeyEstablishmentResponse
|
|
alias ChronoscopeWeb.ClientActivator
|
|
alias ChronoscopeWeb.Endpoint
|
|
|
|
@topic Application.compile_env(:chronoscope, :nts_topic)
|
|
|
|
@impl true
|
|
def mount(_params, _session, socket) do
|
|
Endpoint.subscribe(@topic)
|
|
{:ok, assign(socket, %{servers: server_list(), clients: client_list()})}
|
|
end
|
|
|
|
@impl true
|
|
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
|
|
|
|
@impl true
|
|
def handle_info(_, socket) do
|
|
{:noreply, socket}
|
|
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
|