From f5b4131160520a2540445619188c3c8e0f37da37 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 12 Oct 2018 16:38:11 +0200 Subject: Notifications for chat ; useless user info page --- shard/lib/app/chat.ex | 39 +++++++-- shardweb/assets/css/app.css | 5 ++ shardweb/lib/channels/chat_channel.ex | 3 + shardweb/lib/controllers/identity_controller.ex | 14 +++- shardweb/lib/templates/chat/chat.html.eex | 6 +- shardweb/lib/templates/identity/list.html.eex | 5 +- shardweb/lib/templates/identity/view.html.eex | 25 ++++++ shardweb/lib/templates/layout/app.html.eex | 101 ++++++++++-------------- shardweb/lib/templates/page/peer_list.html.eex | 7 +- shardweb/lib/views/layout_view.ex | 21 +++++ 10 files changed, 157 insertions(+), 69 deletions(-) create mode 100644 shardweb/lib/templates/identity/view.html.eex diff --git a/shard/lib/app/chat.ex b/shard/lib/app/chat.ex index d8ba5da..8d55cda 100644 --- a/shard/lib/app/chat.ex +++ b/shard/lib/app/chat.ex @@ -82,7 +82,10 @@ defmodule SApp.Chat do end Shard.Manager.dispatch_to(id, nil, self()) {:ok, page_store} = SApp.PageStore.start_link(id, :page_store, netgroup) - root = Shard.Manager.load_state id + {root, read} = case Shard.Manager.load_state id do + %{root: root, read: read} -> {root, read} + _ -> {nil, nil} + end root = cond do root == nil -> nil GenServer.call(page_store, {:have_rec, root}) -> root @@ -101,6 +104,7 @@ defmodule SApp.Chat do page_store: page_store, mst: mst, subs: MapSet.new, + read: read, } } :redundant -> @@ -120,6 +124,26 @@ defmodule SApp.Chat do {:reply, ret, state} end + def handle_call(:has_unread, _from, state) do + if state.mst.root != state.read do + case MST.last(state.mst, nil, 1) do + [{{_, msgbin, _}, true}] -> + {ts, _} = SData.term_unbin msgbin + {:reply, ts, state} + [] -> + {:reply, nil, state} + end + else + {:reply, nil, state} + end + end + + def handle_cast(:mark_read, state) do + state = %{state | read: state.mst.root} + save_state(state) + {:noreply, state} + end + @doc """ Implementation of the :chat_send handler. This is the main handler that is used to send a message to the chat room. Puts the message in the store and syncs @@ -133,7 +157,7 @@ defmodule SApp.Chat do prev_root = state.mst.root mst = MST.insert(state.mst, msgitem) state = %{state | mst: mst} - Shard.Manager.save_state(state.id, mst.root) + save_state(state) for pid <- state.subs do if Process.alive?(pid) do @@ -202,7 +226,7 @@ defmodule SApp.Chat do if mst2.root == new_root do state = %{state | mst: mst2} GenServer.cast(state.page_store, {:set_roots, [mst2.root]}) - Shard.Manager.save_state(state.id, mst2.root) + save_state(state) msg_callback(state, msgitem) state else @@ -265,8 +289,9 @@ defmodule SApp.Chat do msg_callback(state, x) end GenServer.cast(state.page_store, {:set_roots, [mst.root]}) - Shard.Manager.save_state(state.id, mst.root) - %{state | mst: mst} + state = %{state | mst: mst} + save_state(state) + state else Logger.warn("Incorrect signatures somewhere while merging, dropping merged data") state @@ -279,6 +304,10 @@ defmodule SApp.Chat do {:noreply, %{ state | subs: new_subs }} end + defp save_state(state) do + Shard.Manager.save_state(state.id, %{root: state.mst.root, read: state.read}) + end + defp msg_callback(state, {pk, msgbin, sign}) do for pid <- state.subs do if Process.alive?(pid) do diff --git a/shardweb/assets/css/app.css b/shardweb/assets/css/app.css index 0676867..e8d40d2 100644 --- a/shardweb/assets/css/app.css +++ b/shardweb/assets/css/app.css @@ -4,3 +4,8 @@ color: #fff; background-color: #080808; } + +.have_unread { + color: #fff; + font-weight: bold; +} diff --git a/shardweb/lib/channels/chat_channel.ex b/shardweb/lib/channels/chat_channel.ex index f898602..a413be1 100644 --- a/shardweb/lib/channels/chat_channel.ex +++ b/shardweb/lib/channels/chat_channel.ex @@ -47,6 +47,7 @@ defmodule ShardWeb.ChatChannel do message: msg, }) end) + GenServer.cast(socket.assigns.pid, :mark_read) {:noreply, socket} end @@ -57,10 +58,12 @@ defmodule ShardWeb.ChatChannel do push socket, "shout", %{"name" => nick, "pk16" => Shard.Keys.pk_display(pk), "message" => msg} + GenServer.cast(socket.assigns.pid, :mark_read) {:noreply, socket} end def handle_info({:chat_send, _, _}, socket) do + GenServer.cast(socket.assigns.pid, :mark_read) {:noreply, socket} end diff --git a/shardweb/lib/controllers/identity_controller.ex b/shardweb/lib/controllers/identity_controller.ex index 3c04fe8..dd254bb 100644 --- a/shardweb/lib/controllers/identity_controller.ex +++ b/shardweb/lib/controllers/identity_controller.ex @@ -10,7 +10,19 @@ defmodule ShardWeb.IdentityController do end def view(conn, %{"pk" => pk}) do - #TODO + {: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 update(conn, params) do diff --git a/shardweb/lib/templates/chat/chat.html.eex b/shardweb/lib/templates/chat/chat.html.eex index 8a43acf..45609d8 100644 --- a/shardweb/lib/templates/chat/chat.html.eex +++ b/shardweb/lib/templates/chat/chat.html.eex @@ -18,14 +18,14 @@ Chat rooms
  • - #<%= @chan %> + #<%= @chan %>
  • <% else %>
  • - Private chat + Private chat
  • - <%= @nicks %> + <%= @nicks %>
  • <% end %> diff --git a/shardweb/lib/templates/identity/list.html.eex b/shardweb/lib/templates/identity/list.html.eex index 007af3d..6437f9c 100644 --- a/shardweb/lib/templates/identity/list.html.eex +++ b/shardweb/lib/templates/identity/list.html.eex @@ -31,7 +31,10 @@ <% end %> - <%= if manifest.pk != @pk do %> + + <%= if manifest.pk == @pk do %> + Edit + <% else %> PM <% end %> diff --git a/shardweb/lib/templates/identity/view.html.eex b/shardweb/lib/templates/identity/view.html.eex new file mode 100644 index 0000000..8bb8ca2 --- /dev/null +++ b/shardweb/lib/templates/identity/view.html.eex @@ -0,0 +1,25 @@ + +
    +
    +

    + <%= @view_nick %> <%= @view_pk |> Base.encode16 %> +

    + +
    +
    + + +<%= render ShardWeb.LayoutView, "flashes.html", assigns %> + +
    +  <%= inspect((GenServer.call(@pid, :get_info)), pretty: true, width: 40) %>
    +
    + + diff --git a/shardweb/lib/templates/layout/app.html.eex b/shardweb/lib/templates/layout/app.html.eex index f49c8e6..067ad57 100644 --- a/shardweb/lib/templates/layout/app.html.eex +++ b/shardweb/lib/templates/layout/app.html.eex @@ -52,79 +52,52 @@