defmodule ShardWeb.IdentityController do
use ShardWeb, :controller
def list(conn, _params) do
render conn, "list.html"
end
def self(conn, _params) do
render conn, "self.html"
end
def view(conn, %{"pk" => pk}) do
{:ok, pk} = Base.decode16(pk)
shard = %SApp.Identity.Manifest{pk: pk} |> SData.term_hash
pid = Shard.Manager.find_proc shard
if pid == nil do
render conn, ShardWeb.ErrorView, "404.html"
else
render conn, "view.html",
view_pk: pk,
view_nick: SApp.Identity.get_nick(pk),
shard: shard,
pid: pid
end
end
def view_post(conn, opts) do
IO.puts(inspect opts)
{:ok, pk} = Base.decode16(opts["pk"])
manifest = %SApp.Identity.Manifest{pk: pk}
nick = SApp.Identity.get_nick pk
shard = manifest |> SData.term_hash
pid = Shard.Manager.find_proc shard
if opts["add_friend"] == "true" do
friend_list = SApp.Directory.friends_dir(conn.assigns.pk)
name = nick <> " " <> Shard.Keys.pk_display(pk)
SApp.Directory.add_file(friend_list, name, manifest)
end
if opts["rm_friend"] == "true" do
friend_list = SApp.Directory.friends_dir(conn.assigns.pk)
name = SApp.Directory.find(friend_list, manifest)
if name != nil do
SApp.Directory.rm_file(friend_list, name)
end
end
if pid == nil do
render conn, ShardWeb.ErrorView, "404.html"
else
render conn, "view.html",
view_pk: pk,
view_nick: SApp.Identity.get_nick(pk),
shard: shard,
pid: pid
end
#TODO
end
def update(conn, params) do
pid = SApp.Identity.find_proc(conn.assigns.pk)
info = SApp.Identity.get_info(pid)
info = %{info | nick: params["nick"]}
SApp.Identity.set_info(pid, info)
redirect conn, to: identity_path(conn, :self)
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, :self))
else
conn
|> put_flash(:error, "No secret key found")
|> render("self.html")
end
_ ->
conn
|> put_flash(:error, "Bad argument")
|> 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, :self)
end
end