diff options
Diffstat (limited to 'shardweb/lib/channels')
-rw-r--r-- | shardweb/lib/channels/chat_channel.ex | 20 | ||||
-rw-r--r-- | shardweb/lib/channels/user_socket.ex | 3 |
2 files changed, 21 insertions, 2 deletions
diff --git a/shardweb/lib/channels/chat_channel.ex b/shardweb/lib/channels/chat_channel.ex index 3e51c3a..f898602 100644 --- a/shardweb/lib/channels/chat_channel.ex +++ b/shardweb/lib/channels/chat_channel.ex @@ -3,7 +3,7 @@ defmodule ShardWeb.ChatChannel do require Logger - def join("room:" <> room_name, payload, socket) do + def join("chat:" <> room_name, payload, socket) do if authorized?(payload) do pid = Shard.Manager.find_or_start %SApp.Chat.Manifest{channel: room_name} @@ -18,6 +18,24 @@ defmodule ShardWeb.ChatChannel do end end + def join("privchat:" <> room_name, payload, socket) do + if authorized?(payload) do + + pk_list = room_name |> String.split(",") |> Enum.map(&Base.decode16!/1) + pk_list = [socket.assigns.pk | pk_list] |> Enum.sort() |> Enum.uniq() + + pid = Shard.Manager.find_or_start(SApp.Chat.PrivChat.Manifest.new(pk_list)) + socket = assign(socket, :pid, pid) + + GenServer.cast(pid, {:subscribe, self()}) + send(self(), :after_join) + + {:ok, socket} + else + {:error, %{reason: "unauthorized"}} + end + end + def handle_info(:after_join, socket) do GenServer.call(socket.assigns.pid, {:read_history, nil, 100}) |> Enum.each(fn {{pk, msgbin, _sign}, true} -> diff --git a/shardweb/lib/channels/user_socket.ex b/shardweb/lib/channels/user_socket.ex index b4136bd..04cc584 100644 --- a/shardweb/lib/channels/user_socket.ex +++ b/shardweb/lib/channels/user_socket.ex @@ -4,7 +4,8 @@ defmodule ShardWeb.UserSocket do require Logger ## Channels - channel "room:*", ShardWeb.ChatChannel + channel "chat:*", ShardWeb.ChatChannel + channel "privchat:*", ShardWeb.ChatChannel ## Transports transport :websocket, Phoenix.Transports.WebSocket |