commit 8b7ed87e4fc9a3ae6b3c0c7fb1d9380fd0bbadab
parent 450152f8d499397c38edfc91ab41c172327f4f91
Author: Alberto Lerda <albertolerda97@gmail.com>
Date: Fri, 6 Jan 2023 11:45:05 +0100
Zenflows.DID: create did in local (WIP send to did controller)
Diffstat:
7 files changed, 65 insertions(+), 19 deletions(-)
diff --git a/conf/.env.templ b/conf/.env.templ
@@ -38,3 +38,8 @@ export ADMIN_KEY=@ADMIN_KEY
export GQL_AUTH_CALLS=true
export GQL_DEF_PAGE_SIZE=50
export GQL_MAX_PAGE_SIZE=100
+
+#export DID_KEYRING
+#export DID_HOST
+#export DID_PORT
+#export DID_SCHEME
diff --git a/conf/runtime.exs b/conf/runtime.exs
@@ -72,6 +72,15 @@ config :zenflows, Zenflows.Restroom,
room_host: get_env("ROOM_HOST", "localhost"),
room_port: get_env_int.("ROOM_PORT", 3000),
room_salt: fetch_env!("ROOM_SALT")
+#
+# did
+#
+did_keyring = Base.decode64!(get_env("DID_KEYRING", ""))
+config :zenflows, Zenflows.DID,
+ did_scheme: if(get_env("DID_SCHEME", "http") == "http", do: :http, else: :https),
+ did_host: get_env("DID_HOST", "did.dyne.org"),
+ did_port: get_env_int.("DID_PORT", 80),
+ did_keyring: if(did_keyring == "", do: nil, else: Jason.decode!(did_keyring))
#
# admin
diff --git a/src/zenflows/application.ex b/src/zenflows/application.ex
@@ -29,6 +29,7 @@ def start(_type, _args) do
Zenflows.DB.Repo,
Zenflows.InstVars.Domain,
Zenflows.Restroom,
+ Zenflows.DID,
{Plug.Cowboy, scheme: :http, plug: Zenflows.Web.Router, options: [port: 8000]},
]
diff --git a/src/zenflows/restroom.ex b/src/zenflows/restroom.ex
@@ -77,28 +77,34 @@ end
# Execute a Zencode specified by `name` with JSON data `data`.
@spec exec(String.t(), map()) :: {:ok, map()} | {:error, term()}
-defp exec(name, post_data) do
- hdrs = [{"content-type", "application/json"}]
+def exec(name, post_data) do
+ request(&Zenflows.HTTPC.request(__MODULE__, &1, &2, &3, &4),
+ name, post_data)
+end
- with {:ok, post_body} <- Jason.encode(%{data: post_data}),
- {:ok, %{status: stat, data: body}} when stat == 200 or stat == 500 <-
- request("POST", "/api/#{name}", hdrs, post_body),
- {:ok, data} <- Jason.decode(body) do
- if stat == 200 do
- {:ok, data}
- else
- {:error, data |> Map.fetch!("zenroom_errors") |> Map.fetch!("logs")}
- end
- else
- {:ok, %{status: stat, data: body}} ->
- {:error, "the http call result in non-200 status code #{stat}: #{inspect(body)}"}
+@doc """
+Given the request function (wrapper of Zenflows.HTTPC.request), the path
+and the data to post, it makes the request and parse the result.
+"""
+@spec request(fun(), String.t(), map()) :: {:ok, map()} | {:error, term()}
+def request(request_fn, path, post_data) do
+ hdrs = [{"content-type", "application/json"}]
- other -> other
- end
-end
+ with {:ok, post_body} <- Jason.encode(%{data: post_data}),
+ {:ok, %{status: stat, data: body}} when stat == 200 or stat == 500 <-
+ request_fn.("POST", "/api/#{path}", hdrs, post_body),
+ {:ok, data} <- Jason.decode(body) do
+ if stat == 200 do
+ {:ok, data}
+ else
+ {:error, data |> Map.fetch!("zenroom_errors") |> Map.fetch!("logs")}
+ end
+ else
+ {:ok, %{status: stat, data: body}} ->
+ {:error, "the http call result in non-200 status code #{stat}: #{inspect(body)}"}
-defp request(method, path, headers, body) do
- Zenflows.HTTPC.request(__MODULE__, method, path, headers, body)
+ other -> other
+ end
end
# Return the salt from the configs.
diff --git a/src/zenflows/vf/person/domain.ex b/src/zenflows/vf/person/domain.ex
@@ -119,6 +119,20 @@ def delete(id) do
end
end
+@spec claim(Schema.id()) ::
+ {:ok, Person.t()} | {:error, String.t() | Changeset.t()}
+def claim(id) do
+ key = multi_key()
+ Multi.new()
+ |> multi_one(id)
+ |> Multi.run(:claim_did, &Zenflows.DID.claim/2)
+ |> Repo.transaction()
+ |> case do
+ {:ok, %{^key => value}} -> {:ok, value}
+ {:error, _, reason, _} -> {:error, reason}
+ end
+end
+
@spec delete!(Schema.id()) :: Person.t()
def delete!(id) do
{:ok, value} = delete(id)
diff --git a/src/zenflows/vf/person/resolv.ex b/src/zenflows/vf/person/resolv.ex
@@ -66,6 +66,12 @@ def delete_person(%{id: id}, _) do
end
end
+def claim_person(%{id: id}, _) do
+ with {:ok, _} <- Domain.claim(id) do
+ {:ok, true}
+ end
+end
+
def images(per, _, _) do
per = Domain.preload(per, :images)
{:ok, per.images}
diff --git a/src/zenflows/vf/person/type.ex b/src/zenflows/vf/person/type.ex
@@ -227,5 +227,10 @@ object :mutation_person do
arg :id, non_null(:id)
resolve &Resolv.delete_person/2
end
+
+ field :claim_person, non_null(:boolean) do
+ arg :id, non_null(:id)
+ resolve &Resolv.claim_person/2
+ end
end
end