From b10f9797cb15d0e8a4e4b5014138dc5bfd875d88 Mon Sep 17 00:00:00 2001 From: href Date: Tue, 7 Sep 2021 08:14:33 +0200 Subject: live fixes/improvements --- lib/lsg_web/live/chat_live.ex | 32 ++++++++++++++++++++++++++------ lib/lsg_web/live/chat_live.html.heex | 7 ++----- 2 files changed, 28 insertions(+), 11 deletions(-) (limited to 'lib/lsg_web/live') 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 %>
  • - - <%= message.sender.nick %> - - -
  • <% end %> -- cgit v1.2.3