aboutsummaryrefslogblamecommitdiff
path: root/shardweb/lib/controllers/chat_controller.ex
blob: 1ca66d4b6dba02d59f5e891ba1480ccaac8dc0c7 (plain) (tree)
1
2
3
4
5
6
7
8
9


                                    


                                                       

                                                 
 
                             

                   
                         
                

     
                                                        

                                                                                                





















                                                                                                 

                                                                               


                               
                           




                                                 

     
defmodule ShardWeb.ChatController do
  use ShardWeb, :controller

  def chat(conn, %{"chan" => chan}) do
    conn = put_gon(conn, chat_channel: "chat:" <> chan)

    manifest = %SApp.Chat.Manifest{channel: chan}
    shard = manifest |> SData.term_hash

    render conn, "chat.html",
      public: true,
      shard: shard,
      manifest: manifest,
      chan: chan
  end

  def privchat(conn, %{"people_list" => people_list}) do
    known_people = for {_, %SApp.Identity.Manifest{pk: pk}, _} <- Shard.Manager.list_shards() do
      {pk, SApp.Identity.get_nick(pk)}
    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(", ")

      manifest = [conn.assigns.pk | pk_list] |> SApp.Chat.PrivChat.Manifest.new
      shard = manifest |> SData.term_hash

      render conn, "chat.html",
        public: false,
        manifest: manifest,
        shard: shard,
        nicks: name
    else
      render conn, ShardWeb.ErrorView, "404.html"
    end
  end
end