diff options
Diffstat (limited to 'shardweb/lib/shard_web/controllers')
4 files changed, 61 insertions, 44 deletions
diff --git a/shardweb/lib/shard_web/controllers/identity_controller.ex b/shardweb/lib/shard_web/controllers/identity_controller.ex new file mode 100644 index 0000000..a4f54e7 --- /dev/null +++ b/shardweb/lib/shard_web/controllers/identity_controller.ex @@ -0,0 +1,40 @@ +defmodule ShardWeb.IdentityController do + use ShardWeb, :controller + + def view(conn, _params) do + render conn, "view.html" + 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) + end + + def switch(conn, params) do + case Base.decode16(params["pk"]) do + {:ok, pk} -> + if Shard.Keys.have_sk? pk do + conn + |> put_session(:pk, pk) + |> redirect(to: identity_path(conn, :view)) + else + conn + |> put_flash(:error, "No secret key found") + |> render("view.html") + end + _ -> + conn + |> put_flash(:error, "Bad argument") + |> render("view.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) + end +end diff --git a/shardweb/lib/shard_web/controllers/page_controller.ex b/shardweb/lib/shard_web/controllers/page_controller.ex index a590630..305e751 100644 --- a/shardweb/lib/shard_web/controllers/page_controller.ex +++ b/shardweb/lib/shard_web/controllers/page_controller.ex @@ -4,4 +4,24 @@ defmodule ShardWeb.PageController do def index(conn, _params) do render conn, "index.html" end + + def add_peer(conn, _params) do + try do + ip = conn.params["ip"] + port = conn.params["port"] + {:ok, ip_tuple} = case :inet.parse_address(to_charlist(ip)) do + {:ok, tup} -> {:ok, tup} + _ -> + case :inet.gethostbyname(to_charlist(ip)) do + {:ok, {:hostent, _, _, :inet, 4, [ip_tup | _]}} -> {:ok, ip_tup} + _ -> :error + end + end + {port_num, _} = Integer.parse port + Shard.Manager.add_peer(ip_tuple, port_num) + rescue + _ -> nil + end + redirect conn, to: page_path(conn, :index) + end end diff --git a/shardweb/lib/shard_web/controllers/peer_controller.ex b/shardweb/lib/shard_web/controllers/peer_controller.ex deleted file mode 100644 index 0bf6ded..0000000 --- a/shardweb/lib/shard_web/controllers/peer_controller.ex +++ /dev/null @@ -1,25 +0,0 @@ -defmodule ShardWeb.PeerController do - use ShardWeb, :controller - - require Logger - - def add(conn, _params) do - try do - ip = conn.params["ip"] - port = conn.params["port"] - {:ok, ip_tuple} = case :inet.parse_address(to_charlist(ip)) do - {:ok, tup} -> {:ok, tup} - _ -> - case :inet.gethostbyname(to_charlist(ip)) do - {:ok, {:hostent, _, _, :inet, 4, [ip_tup | _]}} -> {:ok, ip_tup} - _ -> :error - end - end - {port_num, _} = Integer.parse port - Shard.Manager.add_peer(ip_tuple, port_num) - rescue - _ -> nil - end - redirect conn, to: page_path(conn, :index) - end -end diff --git a/shardweb/lib/shard_web/controllers/room_controller.ex b/shardweb/lib/shard_web/controllers/room_controller.ex index 8c98aa6..d24649b 100644 --- a/shardweb/lib/shard_web/controllers/room_controller.ex +++ b/shardweb/lib/shard_web/controllers/room_controller.ex @@ -1,27 +1,9 @@ defmodule ShardWeb.RoomController do use ShardWeb, :controller - require Logger - import PhoenixGon.Controller - def show(conn, %{"room" => room}) do - pk = get_session(conn, :pk) - {pk, conn} = cond do - pk == nil || not Shard.Keys.have_sk? pk -> - pk = Shard.Keys.get_any_identity - conn = put_session(conn, :pk, pk) - {pk, conn} - true -> - {pk, conn} - end - - name = SApp.Identity.get_nick pk - conn = put_gon(conn, chat_room: room) - conn = put_gon(conn, pk: (pk|>Base.encode16)) render conn, "show.html", - room: room, - pk: pk, - name: name + room: room end end |