aboutsummaryrefslogtreecommitdiff
path: root/shard/lib/app/chat.ex
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2018-09-26 15:26:06 +0200
committerAlex Auvolat <alex@adnab.me>2018-09-26 15:26:06 +0200
commit7d0009d83c9b5c747d4adb535817924762429f30 (patch)
tree59b368cf189fc10976337e8caf4e6b623d69a04b /shard/lib/app/chat.ex
parentf16973d3a492ae6d4890c40d77b0a93d3293bf3a (diff)
downloadshard-7d0009d83c9b5c747d4adb535817924762429f30.tar.gz
shard-7d0009d83c9b5c747d4adb535817924762429f30.zip
Make it work, including Web interface
Diffstat (limited to 'shard/lib/app/chat.ex')
-rw-r--r--shard/lib/app/chat.ex53
1 files changed, 28 insertions, 25 deletions
diff --git a/shard/lib/app/chat.ex b/shard/lib/app/chat.ex
index e61d648..c7df62e 100644
--- a/shard/lib/app/chat.ex
+++ b/shard/lib/app/chat.ex
@@ -201,33 +201,37 @@ defmodule SApp.Chat do
end
defp init_merge(state, new_root, source_peer) do
- # TODO: make the merge asynchronous
-
- Logger.info("Starting merge for #{inspect state.manifest}, merging root: #{new_root|>Base.encode16}")
+ if new_root == nil do
+ state
+ else
+ # TODO: make the merge asynchronous
+
+ Logger.info("Starting merge for #{inspect state.manifest}, merging root: #{new_root|>Base.encode16}")
- prev_last = for {x, true} <- MST.last(state.mst, nil, 100), into: MapSet.new, do: x
+ prev_last = for {x, true} <- MST.last(state.mst, nil, 100), into: MapSet.new, do: x
- mgmst = %{state.mst | root: new_root}
- mgmst = put_in(mgmst.store.prefer_ask, [source_peer])
- mst = MST.merge(state.mst, mgmst)
+ mgmst = %{state.mst | root: new_root}
+ mgmst = put_in(mgmst.store.prefer_ask, [source_peer])
+ mst = MST.merge(state.mst, mgmst)
- correct = for {x, true} <- MST.last(mst, nil, 100) do
- if not MapSet.member? prev_last, x do
- msg_callback(state, x)
- {pk, bin, sign} = x
- Shard.Keys.verify(pk, bin, sign)
- else
- true
+ correct = for {x, true} <- MST.last(mst, nil, 100) do
+ if not MapSet.member? prev_last, x do
+ msg_callback(state, x)
+ {pk, bin, sign} = x
+ Shard.Keys.verify(pk, bin, sign)
+ else
+ true
+ end
end
- end
- if Enum.all? correct do
- GenServer.cast(state.page_store, {:set_roots, [mst.root]})
- Shard.Manager.save_state(state.id, mst.root)
- %{state | mst: mst}
- else
- Logger.warn("Incorrect signatures somewhere while merging, dropping merged data")
- state
+ if Enum.all? correct do
+ GenServer.cast(state.page_store, {:set_roots, [mst.root]})
+ Shard.Manager.save_state(state.id, mst.root)
+ %{state | mst: mst}
+ else
+ Logger.warn("Incorrect signatures somewhere while merging, dropping merged data")
+ state
+ end
end
end
@@ -236,11 +240,10 @@ defmodule SApp.Chat do
{:noreply, %{ state | subs: new_subs }}
end
- defp msg_callback(state, {pk, msgbin, _sign}) do
- {ts, msg} = SData.term_unbin msgbin
+ defp msg_callback(state, {pk, msgbin, sign}) do
for pid <- state.subs do
if Process.alive?(pid) do
- send(pid, {:chat_recv, state.channel, {ts, pk, msg}})
+ send(pid, {:chat_recv, state.channel, {pk, msgbin, sign}})
end
end
end