diff options
author | Alex Auvolat <alex@adnab.me> | 2018-10-12 15:52:42 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2018-10-12 15:52:42 +0200 |
commit | 0d358f6f699a5ce04ffe1bccf4f375b5f321391c (patch) | |
tree | b90a1093b3ac0cd1e7d3643abb34e87078b6072e /shardweb/lib/channels | |
parent | 1e91dc596fd2f7fdd96b7fd2fc50724f93e46529 (diff) | |
download | shard-0d358f6f699a5ce04ffe1bccf4f375b5f321391c.tar.gz shard-0d358f6f699a5ce04ffe1bccf4f375b5f321391c.zip |
Private chat interface
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 |