summaryrefslogtreecommitdiff
path: root/lib/web/live/chat_live.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/web/live/chat_live.ex')
-rw-r--r--lib/web/live/chat_live.ex109
1 files changed, 64 insertions, 45 deletions
diff --git a/lib/web/live/chat_live.ex b/lib/web/live/chat_live.ex
index 6902250..0d45428 100644
--- a/lib/web/live/chat_live.ex
+++ b/lib/web/live/chat_live.ex
@@ -8,37 +8,46 @@ defmodule NolaWeb.ChatLive do
connection = Nola.Irc.Connection.get_network(network, chan)
account = Nola.Account.get(account_id)
membership = Nola.Membership.of_account(Nola.Account.get(account.id))
+
if account && connection && Enum.member?(membership, {connection.network, chan}) do
- {:ok, _} = Registry.register(Nola.PubSub, "#{connection.network}:events", plugin: __MODULE__)
+ {:ok, _} =
+ Registry.register(Nola.PubSub, "#{connection.network}:events", plugin: __MODULE__)
+
for t <- ["messages", "triggers", "outputs", "events"] do
- {:ok, _} = Registry.register(Nola.PubSub, "#{connection.network}/#{chan}:#{t}", plugin: __MODULE__)
+ {:ok, _} =
+ Registry.register(Nola.PubSub, "#{connection.network}/#{chan}:#{t}", plugin: __MODULE__)
end
Nola.Irc.PuppetConnection.start(account, connection)
- users = Nola.UserTrack.channel(connection.network, chan)
- |> Enum.map(fn(tuple) -> Nola.UserTrack.User.from_tuple(tuple) end)
- |> Enum.reduce(Map.new, fn(user = %{id: id}, acc) ->
- Map.put(acc, id, user)
- end)
-
- backlog = case Nola.Plugins.Buffer.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)
+ users =
+ Nola.UserTrack.channel(connection.network, chan)
+ |> Enum.map(fn tuple -> Nola.UserTrack.User.from_tuple(tuple) end)
+ |> Enum.reduce(Map.new(), fn user = %{id: id}, acc ->
+ Map.put(acc, id, user)
+ end)
+
+ backlog =
+ case Nola.Plugins.Buffer.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
@@ -54,9 +63,11 @@ defmodule NolaWeb.ChatLive do
def handle_info({:irc, :event, event = %{type: :join, user_id: id}}, socket) do
if user = Nola.UserTrack.lookup(id) do
- socket = socket
- |> assign(:users, Map.put(socket.assigns.users, id, user))
- |> append_to_backlog(event)
+ socket =
+ socket
+ |> assign(:users, Map.put(socket.assigns.users, id, user))
+ |> append_to_backlog(event)
+
{:noreply, socket}
else
{:noreply, socket}
@@ -64,23 +75,29 @@ defmodule NolaWeb.ChatLive do
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)
+ 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)
+ 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)
+ socket =
+ socket
+ |> assign(:users, Map.delete(socket.assigns.users, id))
+ |> append_to_backlog(event)
+
{:noreply, socket}
end
@@ -91,13 +108,16 @@ defmodule NolaWeb.ChatLive do
# type is text, out, or nil if it's self?
def handle_info({:irc, type, message = %Nola.Message{}}, socket) do
IO.inspect({:live_message, type, message})
- socket = socket
- |> append_to_backlog(message)
+
+ socket =
+ socket
+ |> append_to_backlog(message)
+
{:noreply, socket}
end
def handle_info(info, socket) do
- Logger.debug("Unhandled info: #{inspect info}")
+ Logger.debug("Unhandled info: #{inspect(info)}")
{:noreply, socket}
end
@@ -109,13 +129,12 @@ defmodule NolaWeb.ChatLive do
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
-
+ else
+ {[line | acc], line}
+ end
end
-
end