diff options
Diffstat (limited to '')
-rw-r--r-- | lib/lsg_web/live/chat_live.ex | 32 |
1 files changed, 26 insertions, 6 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 |