diff options
Diffstat (limited to '')
-rw-r--r-- | lib/lsg_web/live/chat_live.ex | 32 | ||||
-rw-r--r-- | lib/lsg_web/live/chat_live.html.heex | 7 |
2 files changed, 28 insertions, 11 deletions
diff --git a/lib/lsg_web/live/chat_live.ex b/lib/lsg_web/live/chat_live.ex index e7a44db..e84d880 100644 --- a/lib/lsg_web/live/chat_live.ex +++ b/lib/lsg_web/live/chat_live.ex @@ -23,7 +23,9 @@ defmodule LSGWeb.ChatLive do end) backlog = case LSG.IRC.BufferPlugin.select_buffer(connection.network, chan) do - {backlog, _} -> Enum.reverse(backlog) + {backlog, _} -> + {backlog, _} = Enum.reduce(backlog, {backlog, nil}, &reduce_contextual_event/2) + Enum.reverse(backlog) _ -> [] end @@ -54,7 +56,7 @@ defmodule LSGWeb.ChatLive do if user = IRC.UserTrack.lookup(id) do socket = socket |> assign(:users, Map.put(socket.assigns.users, id, user)) - |> assign(:backlog, socket.assigns.backlog ++ [event]) + |> append_to_backlog(event) {:noreply, socket} else {:noreply, socket} @@ -64,21 +66,21 @@ defmodule LSGWeb.ChatLive do 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)) - |> assign(:backlog, socket.assigns.backlog ++ [event]) + |> 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)) - |> assign(:backlog, socket.assigns.backlog ++ [event]) + |> 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)) - |> assign(:backlog, socket.assigns.backlog ++ [event]) + |> append_to_backlog(event) {:noreply, socket} end @@ -87,8 +89,9 @@ defmodule LSGWeb.ChatLive do end def handle_info({:irc, :text, message}, socket) do + IO.inspect({:live_message, message}) socket = socket - |> assign(:backlog, socket.assigns.backlog ++ [message]) + |> append_to_backlog(message) {:noreply, socket} end @@ -97,4 +100,21 @@ defmodule LSGWeb.ChatLive do {: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/lsg_web/live/chat_live.html.heex b/lib/lsg_web/live/chat_live.html.heex index 717be4f..fc1f582 100644 --- a/lib/lsg_web/live/chat_live.html.heex +++ b/lib/lsg_web/live/chat_live.html.heex @@ -27,14 +27,11 @@ <%= 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}> - <LSGWeb.Component.naive_date_time_utc datetime={message.at} format="time-24-with-seconds" /> - <span class="inline-block font-bold flex-none cursor-default"><%= message.sender.nick %></span> - <span class="inline-block flex-grow cursor-default"> - <LSGWeb.MessageComponent.content + <LSGWeb.MessageComponent.content + message={message} self={message.account.id == @account_id} text={message.text} /> - </span> </li> <% end %> |