diff options
Diffstat (limited to 'shard/lib/app')
-rw-r--r-- | shard/lib/app/chat.ex | 53 | ||||
-rw-r--r-- | shard/lib/app/identity.ex | 22 |
2 files changed, 45 insertions, 30 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 diff --git a/shard/lib/app/identity.ex b/shard/lib/app/identity.ex index 7e97897..204dfb1 100644 --- a/shard/lib/app/identity.ex +++ b/shard/lib/app/identity.ex @@ -43,10 +43,7 @@ defmodule SApp.Identity do end def default_nick(pk) do - nick_suffix = pk - |> binary_part(0, 4) - |> Base.encode16 - |> String.downcase + nick_suffix = Shard.Keys.pk_display pk "Anon" <> nick_suffix end @@ -56,6 +53,19 @@ defmodule SApp.Identity do Shard.Manager.find_proc id end + def get_nick(pk) do + case find_proc pk do + nil -> + if Shard.Keys.valid_identity_pk? pk do + Shard.Manifest.start %Manifest{pk: pk} + end + default_nick pk + pid -> + info = GenServer.call(pid, :get_info) + info.nick + end + end + def handle_call(:manifest, _from, state) do {:replyl, state.manifest, state} end @@ -85,11 +95,12 @@ defmodule SApp.Identity do def handle_cast({:interested, peer_id}, state) do Shard.Manager.send(peer_id, {state.id, nil, {:update, SData.SignRev.signed(state.state)}}) + {:noreply, state} end def handle_cast({:msg, peer_id, _shard_id, nil, msg}, state) do state = case msg do - {:update, signed} -> + {:update, signed} when signed != nil -> case SData.SignRev.merge(state.state, signed, state.pk) do {true, st2} -> Shard.Manager.save_state(state.id, st2) @@ -99,6 +110,7 @@ defmodule SApp.Identity do {false, _} -> state end + _ -> state end {:noreply, state} end |