Restructure code base for API requests/responses

This commit is contained in:
Pim Kunis 2021-09-09 17:26:40 +02:00
parent b60c80b882
commit e55aa4b85c
17 changed files with 103 additions and 127 deletions

View file

@ -0,0 +1,23 @@
defmodule ArchitexWeb.Federation.Request.Profile do
use ArchitexWeb.APIRequest
alias Architex.Types.UserId
@type t :: %__MODULE__{
user_id: UserId.t(),
field: String.t() | nil
}
@primary_key false
embedded_schema do
field :user_id, UserId
field :field, :string
end
def changeset(data, params) do
data
|> cast(params, [:user_id, :field])
|> validate_required([:user_id])
|> validate_inclusion(:field, ["displayname", "avatar_url"])
end
end

View file

@ -0,0 +1,41 @@
defmodule ArchitexWeb.Federation.Response.GetSigningKeys do
use ArchitexWeb.APIRequest
@type t :: %__MODULE__{
server_name: String.t(),
verify_keys: %{optional(String.t()) => %{String.t() => String.t()}},
old_verify_keys: %{optional(String.t()) => map()},
signatures: %{optional(String.t()) => %{optional(String.t()) => String.t()}},
valid_until_ts: integer()
}
@primary_key false
embedded_schema do
field :server_name, :string
field :verify_keys, {:map, {:map, :string}}
field :old_verify_keys, {:map, :map}
field :signatures, {:map, {:map, :string}}
field :valid_until_ts, :integer
end
def changeset(data, params) do
data
|> cast(params, [:server_name, :verify_keys, :old_verify_keys, :signatures, :valid_until_ts])
|> validate_required([:server_name, :verify_keys, :valid_until_ts])
|> Architex.validate_change_truthy(:verify_keys, fn map ->
Enum.all?(map, fn {_, map} ->
is_map_key(map, "key")
end)
end)
|> Architex.validate_change_truthy(:old_verify_keys, fn map ->
Enum.all?(map, fn
{_, %{"key" => key, "expired_ts" => expired_ts}}
when is_binary(key) and is_integer(expired_ts) ->
true
_ ->
false
end)
end)
end
end