From 0d358f6f699a5ce04ffe1bccf4f375b5f321391c Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 12 Oct 2018 15:52:42 +0200 Subject: Private chat interface --- shardweb/lib/controllers/chat_controller.ex | 48 ++++++++++++++++++++++--- shardweb/lib/controllers/identity_controller.ex | 14 +++++--- shardweb/lib/controllers/page_controller.ex | 2 +- 3 files changed, 53 insertions(+), 11 deletions(-) (limited to 'shardweb/lib/controllers') diff --git a/shardweb/lib/controllers/chat_controller.ex b/shardweb/lib/controllers/chat_controller.ex index 45b7d34..31b80ba 100644 --- a/shardweb/lib/controllers/chat_controller.ex +++ b/shardweb/lib/controllers/chat_controller.ex @@ -1,13 +1,51 @@ defmodule ShardWeb.ChatController do use ShardWeb, :controller - def chat(conn, %{"room" => room}) do - conn = put_gon(conn, chat_room: room) + def chat(conn, %{"chan" => chan}) do + conn = put_gon(conn, chat_channel: "chat:" <> chan) + + shard = %SApp.Chat.Manifest{channel: chan} |> SData.term_hash + render conn, "chat.html", - room: room + public: true, + shard: shard, + chan: chan end - def privchat(_conn, %{"pk" => _pk}) do - # TODO + def privchat(conn, %{"people_list" => people_list}) do + known_people = for {_, %SApp.Identity.Manifest{pk: pk}, pid} <- Shard.Manager.list_shards() do + info = GenServer.call(pid, :get_info) + {pk, info.nick} + end + + pk_list = for qname <- String.split(people_list, ",") do + candidates = for {pk, nick} <- known_people, + :binary.longest_common_prefix([qname, nick]) == byte_size(qname) + or :binary.longest_common_prefix([qname, Shard.Keys.pk_display pk]) == byte_size(qname) + or Base.decode16(qname) == {:ok, pk}, + do: {pk, nick} + case candidates do + [] -> :error + [{pk, _}] -> pk + _ -> :error + end + end + + if Enum.all?(pk_list, &(&1 != :error)) do + pk_list_str = pk_list |> Enum.map(&Base.encode16/1) |> Enum.join(",") + conn = put_gon(conn, chat_channel: "privchat:" <> pk_list_str) + name = pk_list + |> Enum.map(&SApp.Identity.get_nick/1) + |> Enum.join(", ") + + shard = [conn.assigns.pk | pk_list] |> SApp.Chat.PrivChat.Manifest.new |> SData.term_hash + + render conn, "chat.html", + public: false, + shard: shard, + nicks: name + else + render conn, ShardWeb.ErrorView, "404.html" + end end end diff --git a/shardweb/lib/controllers/identity_controller.ex b/shardweb/lib/controllers/identity_controller.ex index fdaefd0..3c04fe8 100644 --- a/shardweb/lib/controllers/identity_controller.ex +++ b/shardweb/lib/controllers/identity_controller.ex @@ -9,12 +9,16 @@ defmodule ShardWeb.IdentityController do render conn, "self.html" end + def view(conn, %{"pk" => pk}) do + #TODO + end + def update(conn, params) do pid = SApp.Identity.find_proc(conn.assigns.pk) info = GenServer.call(pid, :get_info) info = %{info | nick: params["nick"]} GenServer.call(pid, {:set_info, info}) - redirect conn, to: identity_path(conn, :view) + redirect conn, to: identity_path(conn, :self) end def switch(conn, params) do @@ -23,22 +27,22 @@ defmodule ShardWeb.IdentityController do if Shard.Keys.have_sk? pk do conn |> put_session(:pk, pk) - |> redirect(to: identity_path(conn, :view)) + |> redirect(to: identity_path(conn, :self)) else conn |> put_flash(:error, "No secret key found") - |> render("view.html") + |> render("self.html") end _ -> conn |> put_flash(:error, "Bad argument") - |> render("view.html") + |> render("self.html") end end def create(conn, _params) do pk = Shard.Keys.new_identity conn = put_session(conn, :pk, pk) - redirect conn, to: identity_path(conn, :view) + redirect conn, to: identity_path(conn, :self) end end diff --git a/shardweb/lib/controllers/page_controller.ex b/shardweb/lib/controllers/page_controller.ex index ebe2099..5c90416 100644 --- a/shardweb/lib/controllers/page_controller.ex +++ b/shardweb/lib/controllers/page_controller.ex @@ -26,6 +26,6 @@ defmodule ShardWeb.PageController do rescue _ -> nil end - redirect conn, to: page_path(conn, :index) + redirect conn, to: page_path(conn, :peer_list) end end -- cgit v1.2.3