diff options
author | href <href@random.sh> | 2021-09-03 04:04:21 +0200 |
---|---|---|
committer | href <href@random.sh> | 2021-09-03 04:04:21 +0200 |
commit | 5399fc818cf821c75f9f19fd00bf7905ba7fe7e3 (patch) | |
tree | 00408724f9778cc7323b1459782a762626d39b6a /lib/lsg_web/live/chat_live.ex | |
parent | assets.. (diff) |
various fixes, web client wip, pubsub events
Diffstat (limited to '')
-rw-r--r-- | lib/lsg_web/live/chat_live.ex | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/lib/lsg_web/live/chat_live.ex b/lib/lsg_web/live/chat_live.ex new file mode 100644 index 0000000..a2b4c13 --- /dev/null +++ b/lib/lsg_web/live/chat_live.ex @@ -0,0 +1,101 @@ +defmodule LSGWeb.ChatLive do + use Phoenix.LiveView + use Phoenix.HTML + require Logger + + def mount(%{"network" => network, "chan" => chan}, %{"account" => account_id}, socket) do + chan = LSGWeb.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) + + 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, []) + |> 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 + IO.puts("JOIN USER JOIN USER JOIN USER") + + socket = socket + |> assign(:users, Map.put(socket.assigns.users, id, user)) + |> assign(:backlog, socket.assigns.backlog ++ [event]) + + IO.inspect(socket.assigns.users) + + {:noreply, socket} + else + IO.puts("\n\n\n?!\n\n\n?!\n\n\n\n") + {: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)) + |> assign(:backlog, socket.assigns.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]) + {: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]) + {: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 + socket = socket + |> assign(:backlog, socket.assigns.backlog ++ [message]) + {:noreply, socket} + end + + def handle_info(info, socket) do + Logger.debug("Unhandled info: #{inspect info}") + {:noreply, socket} + end + +end |