Packages
baobab
0.15.2
0.35.1
0.35.0
0.34.0
0.31.0
0.30.0
0.21.2
0.21.1
0.21.0
0.20.0
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.17.1
0.17.0
0.16.8
0.16.7
0.16.6
0.16.5
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.2
0.15.1
0.15.0
0.14.1
0.14.0
0.13.1
0.13.0
0.12.1
0.12.0
0.11.1
0.11.0
0.10.6
0.10.5
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.0
0.5.2
0.5.1
0.5.0
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Bamboo append-only logs
Current section
Files
Jump to
Current section
Files
lib/baobab/clumpmeta.ex
defmodule Baobab.ClumpMeta do
alias Baobab.{Identity, Persistence}
@moduledoc """
Functions for interacting with clump metadata
May be useful between consumers to communicate intent
"""
@doc """
Block the given log author on the supplied clump_id
Authors should be supplied as 32 byte-raw or 43-byte base62-encoded values.
May not be applied to member of local `identities`
"""
@spec block_author(binary, binary) :: :ok | {:error, String.t()}
def block_author(author, clump_id \\ "default") do
case Identity.as_base62(author) do
{:error, _} ->
{:error, "Improper identity supplied"}
id ->
case Enum.any?(Baobab.Identity.list(), fn {_n, k} -> k == id end) do
true ->
{:error, "May not block identities controlled by Baobab"}
false ->
new =
case Persistence.action(:metadata, clump_id, :get, :author_blocks) do
%MapSet{} = curr -> MapSet.put(curr, author)
nil -> MapSet.new([author])
end
Persistence.action(:metadata, clump_id, :put, {:author_blocks, new})
# We're not going to take new entries, let's drop the old.
Baobab.purge(author, log_id: :all, clump_id: clump_id)
:ok
end
end
end
@doc """
Unblock the given log author on the supplied clump_id
Authors should be supplied as 32 byte-raw or 43-byte base6-encoded values.
"""
@spec unblock_author(binary, binary) :: :ok | {:error, String.t()}
def unblock_author(author, clump_id \\ "default") do
case Identity.as_base62(author) do
{:error, _} ->
{:error, "Improper identity supplied"}
id ->
case Persistence.action(:metadata, clump_id, :get, :author_blocks) do
nil ->
:ok
%MapSet{} = curr ->
Persistence.action(
:metadata,
clump_id,
:put,
{:author_blocks, MapSet.delete(curr, id)}
)
:ok
end
end
end
@doc """
Returns a boolean indicating whether a given author is blocked on the supplied clump
"""
@spec blocked_author?(binary) :: boolean | {:error, String.t()}
def blocked_author?(author, clump_id \\ "default") do
case Identity.as_base62(author) do
{:error, _} ->
{:error, "Improper identity supplied"}
id ->
case Persistence.action(:metadata, clump_id, :get, :author_blocks) do
nil -> false
ms -> MapSet.member?(ms, id)
end
end
end
@doc """
Lists currently blocked authors on the supplied clump_id
Results are returned as the base62-encoded identity
"""
@spec list_blocked_authors(binary) :: [binary]
def list_blocked_authors(clump_id \\ "default") do
case Persistence.action(:metadata, clump_id, :get, :author_blocks) do
nil -> []
ms -> MapSet.to_list(ms)
end
end
@doc false
# Consumers shouldn't need to run this independently, I am willing to be
# proven wrong on this point.
def purge_blocked_authors(clump_id \\ "default") do
clump_id
|> list_blocked_authors()
|> Enum.map(fn a -> Baobab.purge(a, log_id: :all, clump_id: clump_id) end)
:ok
end
end