diff options
Diffstat (limited to 'shard')
-rw-r--r-- | shard/lib/app/chat.ex | 39 |
1 files changed, 34 insertions, 5 deletions
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 |