summaryrefslogtreecommitdiff
path: root/lib/nola_web/live
diff options
context:
space:
mode:
authorJordan Bracco <href@random.sh>2022-12-20 00:21:54 +0000
committerJordan Bracco <href@random.sh>2022-12-20 19:29:41 +0100
commit2d83df8b32bff7f0028923bb5b64dc0b55f20d03 (patch)
tree1207e67b5b15f540963db05e7be89f3ca950e724 /lib/nola_web/live
parentNola rename, the end. pt 6. Refs T77. (diff)
Nola rename: The Big Move, Refs T77
Diffstat (limited to 'lib/nola_web/live')
-rw-r--r--lib/nola_web/live/chat_live.ex120
-rw-r--r--lib/nola_web/live/chat_live.html.heex91
2 files changed, 211 insertions, 0 deletions
diff --git a/lib/nola_web/live/chat_live.ex b/lib/nola_web/live/chat_live.ex
new file mode 100644
index 0000000..276b362
--- /dev/null
+++ b/lib/nola_web/live/chat_live.ex
@@ -0,0 +1,120 @@
+defmodule NolaWeb.ChatLive do
+ use Phoenix.LiveView
+ use Phoenix.HTML
+ require Logger
+
+ def mount(%{"network" => network, "chan" => chan}, %{"account" => account_id}, socket) do
+ chan = NolaWeb.reformat_chan(chan)
+ connection = IRC.Connection.get_network(network, chan)
+ account = IRC.Account.get(account_id)
+ membership = IRC.Membership.of_account(IRC.Account.get("DRgpD4fLf8PDJMLp8Dtb"))
+ if account && connection && Enum.member?(membership, {connection.network, chan}) do
+ {:ok, _} = Registry.register(IRC.PubSub, "#{connection.network}:events", plugin: __MODULE__)
+ for t <- ["messages", "triggers", "outputs", "events"] do
+ {:ok, _} = Registry.register(IRC.PubSub, "#{connection.network}/#{chan}:#{t}", plugin: __MODULE__)
+ end
+
+ IRC.PuppetConnection.start(account, connection)
+
+ users = IRC.UserTrack.channel(connection.network, chan)
+ |> Enum.map(fn(tuple) -> IRC.UserTrack.User.from_tuple(tuple) end)
+ |> Enum.reduce(Map.new, fn(user = %{id: id}, acc) ->
+ Map.put(acc, id, user)
+ end)
+
+ backlog = case Nola.IRC.BufferPlugin.select_buffer(connection.network, chan) do
+ {backlog, _} ->
+ {backlog, _} = Enum.reduce(backlog, {backlog, nil}, &reduce_contextual_event/2)
+ Enum.reverse(backlog)
+ _ -> []
+ end
+
+ socket = socket
+ |> assign(:connection_id, connection.id)
+ |> assign(:network, connection.network)
+ |> assign(:chan, chan)
+ |> assign(:title, "live")
+ |> assign(:channel, chan)
+ |> assign(:account_id, account.id)
+ |> assign(:backlog, backlog)
+ |> assign(:users, users)
+ |> assign(:counter, 0)
+
+ {:ok, socket}
+ else
+ {:ok, redirect(socket, to: "/")}
+ end
+ end
+
+ def handle_event("send", %{"message" => %{"text" => text}}, socket) do
+ account = IRC.Account.get(socket.assigns.account_id)
+ IRC.send_message_as(account, socket.assigns.network, socket.assigns.channel, text, true)
+ {:noreply, assign(socket, :counter, socket.assigns.counter + 1)}
+ end
+
+ def handle_info({:irc, :event, event = %{type: :join, user_id: id}}, socket) do
+ if user = IRC.UserTrack.lookup(id) do
+ socket = socket
+ |> assign(:users, Map.put(socket.assigns.users, id, user))
+ |> append_to_backlog(event)
+ {:noreply, socket}
+ else
+ {:noreply, socket}
+ end
+ end
+
+ def handle_info({:irc, :event, event = %{type: :nick, user_id: id, nick: nick}}, socket) do
+ socket = socket
+ |> assign(:users, update_in(socket.assigns.users, [id, :nick], nick))
+ |> append_to_backlog(event)
+ {:noreply, socket}
+ end
+
+ def handle_info({:irc, :event, event = %{type: :quit, user_id: id}}, socket) do
+ socket = socket
+ |> assign(:users, Map.delete(socket.assigns.users, id))
+ |> append_to_backlog(event)
+ {:noreply, socket}
+ end
+
+ def handle_info({:irc, :event, event = %{type: :part, user_id: id}}, socket) do
+ socket = socket
+ |> assign(:users, Map.delete(socket.assigns.users, id))
+ |> append_to_backlog(event)
+ {:noreply, socket}
+ end
+
+ def handle_info({:irc, :trigger, _, message}, socket) do
+ handle_info({:irc, nil, message}, socket)
+ end
+
+ def handle_info({:irc, :text, message}, socket) do
+ IO.inspect({:live_message, message})
+ socket = socket
+ |> append_to_backlog(message)
+ {:noreply, socket}
+ end
+
+ def handle_info(info, socket) do
+ Logger.debug("Unhandled info: #{inspect info}")
+ {:noreply, socket}
+ end
+
+ defp append_to_backlog(socket, line) do
+ {add, _} = reduce_contextual_event(line, {[], List.last(socket.assigns.backlog)})
+ assign(socket, :backlog, socket.assigns.backlog ++ add)
+ end
+
+ defp reduce_contextual_event(line, {acc, nil}) do
+ {[line | acc], line}
+ end
+ defp reduce_contextual_event(line, {acc, last}) do
+ if NaiveDateTime.to_date(last.at) != NaiveDateTime.to_date(line.at) do
+ {[%{type: :day_changed, date: NaiveDateTime.to_date(line.at), at: nil}, line | acc], line}
+ else
+ {[line | acc], line}
+ end
+
+ end
+
+end
diff --git a/lib/nola_web/live/chat_live.html.heex b/lib/nola_web/live/chat_live.html.heex
new file mode 100644
index 0000000..29cd6a1
--- /dev/null
+++ b/lib/nola_web/live/chat_live.html.heex
@@ -0,0 +1,91 @@
+<div class="chat" data-turbo="false">
+
+ <div class="py-4 px-4 bg-gradient-to-b from-black to-gray-900">
+ <div class="grid grid-cols-2">
+ <h1 class="text-gray-50 tracking-tight font-extrabold text-xl">
+ <%= @network %>
+ <span class="font-bold"><%= @chan %></span>
+ </h1>
+ <div class="text-right">
+ <a href="/" class="text-gray-400"><%= @account_id %></a>
+ </div>
+ </div>
+ </div>
+
+ <div class="body">
+
+ <div class="log">
+ <p class="disconnected text-center text-6xl tracking-tight font-extrabold text-red-800 w-full my-24 mx-auto overflow-y-auto">
+ Disconnected <span class="text-mono">:'(</span>
+ </p>
+ <p class="phx-errored text-center text-6xl tracking-tight font-extrabold text-red-800 w-full my-24 mx-auto overflow-y-auto">
+ Oh no error <span class="text-mono">>:(</span>
+ </p>
+
+ <ul class="pt-4 pl-4">
+ <%= for message <- @backlog do %>
+ <%= if is_map(message) && Map.get(message, :__struct__) == IRC.Message do %>
+ <li class="flex gap-2 place-items-center message"
+ data-account-id={message.account.id}>
+ <NolaWeb.MessageComponent.content
+ message={message}
+ self={message.account.id == @account_id}
+ text={message.text}
+ />
+ </li>
+ <% end %>
+
+ <%= if is_binary(message) do %>
+ <li class="notice"><%= message %></li>
+ <% end %>
+
+ <%= if is_map(message) && Map.get(message, :type) do %>
+ <li class="flex gap-2 place-items-center event">
+ <NolaWeb.Component.naive_date_time_utc datetime={message.at} format="time-24-with-seconds" />
+ <span class="inline-block font-bold flex-none cursor-default text-gray-700">*&nbsp;*&nbsp;*</span>
+ <span class="inline-block flex-grow cursor-default text-gray-700">
+ <NolaWeb.EventComponent.content event={message}
+ self={@users[message.user_id] && @users[message.user_id].account == @account_id}
+ user={@users[message.user_id]}
+ />
+ </span>
+ </li>
+ <% end %>
+ <% end %>
+ </ul>
+ </div>
+
+ <aside>
+ <%= for {_, user} <- @users do %>
+ <details class="user dropdown">
+ <summary><%= user.nick %></summary>
+ <div class="content">
+ <h3 class="text-xl font-bold"><%= user.nick %></h3>
+
+ <ul class="mt-4 space-y-2">
+ <li class="">User: <span class="font-bold"><%= user.username %></span></li>
+ <li class="">Name: <%= user.realname || user.nick %></li>
+ <li class="">Host: <span class="font-mono"><%= user.host %></span></li>
+ </ul>
+
+ <div class="mt-4 font-xs text-gray-300 text-center">
+ UID: <%= user.id %>
+ <br />
+ AID: <%= user.account %>
+ </div>
+ </div>
+ </details>
+ <% end %>
+ </aside>
+
+ </div>
+
+ <.form let={f} id={"form-#{@counter}"} for={:message} phx-submit="send" class="w-full px-4 pt-4">
+ <div>
+ <div class="mt-1 flex rounded-md shadow-sm border border-gray-300">
+ <%= text_input f, :text, class: "focus:ring-indigo-500 focus:border-indigo-500 block w-full border rounded-md pl-4 sm:text-sm border-gray-300", autofocus: true, 'phx-hook': "AutoFocus", autocomplete: "off", placeholder: "Don't be shy, say something…" %>
+ <%= submit content_tag(:span, "Send"), class: "-ml-px relative inline-flex items-center space-x-2 px-4 py-2 border border-gray-300 text-sm font-medium rounded-r-md text-gray-700 bg-gray-50 hover:bg-gray-100 focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500"%>
+ </div>
+ </div>
+ </.form>
+</div>