Restructure code base for API requests/responses
This commit is contained in:
parent
b60c80b882
commit
e55aa4b85c
17 changed files with 103 additions and 127 deletions
20
lib/architex_web/api_schemas/client/request/ban.ex
Normal file
20
lib/architex_web/api_schemas/client/request/ban.ex
Normal file
|
@ -0,0 +1,20 @@
|
|||
defmodule ArchitexWeb.Client.Request.Ban do
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
user_id: String.t(),
|
||||
reason: String.t() | nil
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :user_id, :string
|
||||
field :reason, :string
|
||||
end
|
||||
|
||||
def changeset(data, params) do
|
||||
data
|
||||
|> cast(params, [:user_id, :reason])
|
||||
|> validate_required([:user_id])
|
||||
end
|
||||
end
|
41
lib/architex_web/api_schemas/client/request/create_room.ex
Normal file
41
lib/architex_web/api_schemas/client/request/create_room.ex
Normal file
|
@ -0,0 +1,41 @@
|
|||
defmodule ArchitexWeb.Client.Request.CreateRoom do
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
visibility: String.t() | nil,
|
||||
room_alias_name: String.t() | nil,
|
||||
name: String.t() | nil,
|
||||
topic: String.t() | nil,
|
||||
invite: list(String.t()) | nil,
|
||||
room_version: String.t() | nil,
|
||||
preset: String.t() | nil
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :visibility, :string
|
||||
field :room_alias_name, :string
|
||||
field :name, :string
|
||||
field :topic, :string
|
||||
field :invite, {:array, :string}
|
||||
field :room_version, :string
|
||||
field :preset, :string
|
||||
# TODO: unimplemented:
|
||||
# creation_content, initial_state, invite_3pid, initial_state,
|
||||
# is_direct, power_level_content_override
|
||||
end
|
||||
|
||||
def changeset(data, params) do
|
||||
data
|
||||
|> cast(params, [
|
||||
:visibility,
|
||||
:room_alias_name,
|
||||
:name,
|
||||
:topic,
|
||||
:invite,
|
||||
:room_version,
|
||||
:preset
|
||||
])
|
||||
|> validate_inclusion(:preset, ["private_chat", "public_chat", "trusted_private_chat"])
|
||||
end
|
||||
end
|
20
lib/architex_web/api_schemas/client/request/kick.ex
Normal file
20
lib/architex_web/api_schemas/client/request/kick.ex
Normal file
|
@ -0,0 +1,20 @@
|
|||
defmodule ArchitexWeb.Client.Request.Kick do
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
user_id: String.t(),
|
||||
reason: String.t() | nil
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :user_id, :string
|
||||
field :reason, :string
|
||||
end
|
||||
|
||||
def changeset(data, params) do
|
||||
data
|
||||
|> cast(params, [:user_id, :reason])
|
||||
|> validate_required([:user_id])
|
||||
end
|
||||
end
|
37
lib/architex_web/api_schemas/client/request/login.ex
Normal file
37
lib/architex_web/api_schemas/client/request/login.ex
Normal file
|
@ -0,0 +1,37 @@
|
|||
defmodule ArchitexWeb.Client.Request.Login do
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
# TODO: Identifier
|
||||
@type t :: %__MODULE__{
|
||||
type: String.t(),
|
||||
password: String.t(),
|
||||
device_id: String.t(),
|
||||
initial_device_display_name: String.t()
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :type, :string
|
||||
field :password, :string
|
||||
field :device_id, :string
|
||||
field :initial_device_display_name, :string
|
||||
|
||||
embeds_one :identifier, Identifier, primary_key: false do
|
||||
field :type, :string
|
||||
field :user, :string
|
||||
end
|
||||
end
|
||||
|
||||
def changeset(data, params) do
|
||||
data
|
||||
|> cast(params, [:type, :password, :device_id, :initial_device_display_name])
|
||||
|> cast_embed(:identifier, with: &identifier_changeset/2, required: true)
|
||||
|> validate_required([:type, :password])
|
||||
end
|
||||
|
||||
def identifier_changeset(identifier, params) do
|
||||
identifier
|
||||
|> cast(params, [:type, :user])
|
||||
|> validate_required([:type, :user])
|
||||
end
|
||||
end
|
31
lib/architex_web/api_schemas/client/request/messages.ex
Normal file
31
lib/architex_web/api_schemas/client/request/messages.ex
Normal file
|
@ -0,0 +1,31 @@
|
|||
defmodule ArchitexWeb.Client.Request.Messages do
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
from: String.t(),
|
||||
to: String.t() | nil,
|
||||
dir: String.t(),
|
||||
limit: integer() | nil,
|
||||
filter: String.t() | nil
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :from, :string
|
||||
field :to, :string
|
||||
field :dir, :string
|
||||
field :limit, :integer, default: 10
|
||||
field :filter, :string
|
||||
end
|
||||
|
||||
def changeset(data, params) do
|
||||
data
|
||||
|> cast(params, [:from, :to, :dir, :limit, :filter], empty_values: [])
|
||||
|> validate_required([:dir])
|
||||
|> Architex.validate_not_nil([:from])
|
||||
|> validate_inclusion(:dir, ["b", "f"])
|
||||
|> validate_number(:limit, greater_than: 0)
|
||||
|> validate_format(:from, ~r/^[0-9]*$/)
|
||||
|> validate_format(:to, ~r/^[0-9]+$/)
|
||||
end
|
||||
end
|
39
lib/architex_web/api_schemas/client/request/register.ex
Normal file
39
lib/architex_web/api_schemas/client/request/register.ex
Normal file
|
@ -0,0 +1,39 @@
|
|||
defmodule ArchitexWeb.Client.Request.Register do
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
alias Ecto.Changeset
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
device_id: String.t(),
|
||||
initial_device_display_name: String.t(),
|
||||
password: String.t(),
|
||||
username: String.t(),
|
||||
inhibit_login: boolean()
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :device_id, :string
|
||||
field :initial_device_display_name, :string
|
||||
field :password, :string
|
||||
field :username, :string
|
||||
field :inhibit_login, :boolean, default: false
|
||||
end
|
||||
|
||||
def changeset(data, params) do
|
||||
data
|
||||
|> cast(params, [
|
||||
:device_id,
|
||||
:initial_device_display_name,
|
||||
:password,
|
||||
:username,
|
||||
:inhibit_login
|
||||
])
|
||||
|> validate_required([:password])
|
||||
end
|
||||
|
||||
def get_error(%Changeset{errors: [error | _]}), do: get_error(error)
|
||||
def get_error({:localpart, {_, [{:constraint, :unique} | _]}}), do: :user_in_use
|
||||
def get_error({:localpart, {_, [{:validation, _} | _]}}), do: :invalid_username
|
||||
def get_error(_), do: :bad_json
|
||||
end
|
14
lib/architex_web/api_schemas/client/request/sync.ex
Normal file
14
lib/architex_web/api_schemas/client/request/sync.ex
Normal file
|
@ -0,0 +1,14 @@
|
|||
defmodule ArchitexWeb.Client.Request.Sync do
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :filter, :string
|
||||
field :since, :string
|
||||
field :full_state, :boolean, default: false
|
||||
field :set_presence, :string, default: "online"
|
||||
field :timeout, :integer, default: 0
|
||||
end
|
||||
end
|
23
lib/architex_web/api_schemas/federation/request/profile.ex
Normal file
23
lib/architex_web/api_schemas/federation/request/profile.ex
Normal 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
|
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue