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
|
@ -5,7 +5,7 @@ defmodule Architex.ServerKeyInfo do
|
|||
|
||||
alias Architex.{Repo, ServerKeyInfo, SigningKey}
|
||||
alias ArchitexWeb.Federation.HTTPClient
|
||||
alias ArchitexWeb.Federation.Request.GetSigningKeys
|
||||
alias ArchitexWeb.Federation.Response.GetSigningKeys
|
||||
alias Ecto.Multi
|
||||
|
||||
@primary_key {:server_name, :string, []}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
defmodule ArchitexWeb.Client.Request.Ban do
|
||||
use ArchitexWeb.Request
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
user_id: String.t(),
|
|
@ -1,9 +1,5 @@
|
|||
defmodule ArchitexWeb.Client.Request.CreateRoom do
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Ecto.Changeset
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
visibility: String.t() | nil,
|
||||
|
@ -29,8 +25,8 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do
|
|||
# is_direct, power_level_content_override
|
||||
end
|
||||
|
||||
def changeset(params) do
|
||||
%__MODULE__{}
|
||||
def changeset(data, params) do
|
||||
data
|
||||
|> cast(params, [
|
||||
:visibility,
|
||||
:room_alias_name,
|
||||
|
@ -42,7 +38,4 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do
|
|||
])
|
||||
|> validate_inclusion(:preset, ["private_chat", "public_chat", "trusted_private_chat"])
|
||||
end
|
||||
|
||||
def get_error(%Changeset{errors: [error | _]}), do: get_error(error)
|
||||
def get_error(_), do: :bad_json
|
||||
end
|
|
@ -1,5 +1,5 @@
|
|||
defmodule ArchitexWeb.Client.Request.Kick do
|
||||
use ArchitexWeb.Request
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
user_id: String.t(),
|
|
@ -1,8 +1,7 @@
|
|||
defmodule ArchitexWeb.Client.Request.Login do
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
# TODO: Identifier
|
||||
@type t :: %__MODULE__{
|
||||
type: String.t(),
|
||||
password: String.t(),
|
||||
|
@ -23,8 +22,8 @@ defmodule ArchitexWeb.Client.Request.Login do
|
|||
end
|
||||
end
|
||||
|
||||
def changeset(params) do
|
||||
%__MODULE__{}
|
||||
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])
|
|
@ -1,5 +1,5 @@
|
|||
defmodule ArchitexWeb.Client.Request.Messages do
|
||||
use ArchitexWeb.Request
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
from: String.t(),
|
|
@ -1,7 +1,5 @@
|
|||
defmodule ArchitexWeb.Client.Request.Register do
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
alias Ecto.Changeset
|
||||
|
||||
|
@ -22,8 +20,8 @@ defmodule ArchitexWeb.Client.Request.Register do
|
|||
field :inhibit_login, :boolean, default: false
|
||||
end
|
||||
|
||||
def changeset(params) do
|
||||
%__MODULE__{}
|
||||
def changeset(data, params) do
|
||||
data
|
||||
|> cast(params, [
|
||||
:device_id,
|
||||
:initial_device_display_name,
|
|
@ -1,5 +1,5 @@
|
|||
defmodule ArchitexWeb.Client.Request.Sync do
|
||||
use ArchitexWeb.Request
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
@type t :: %__MODULE__{}
|
||||
|
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
|
|
@ -1,5 +1,5 @@
|
|||
defmodule ArchitexWeb.Federation.Request.GetSigningKeys do
|
||||
use ArchitexWeb.Request
|
||||
defmodule ArchitexWeb.Federation.Response.GetSigningKeys do
|
||||
use ArchitexWeb.APIRequest
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
server_name: String.t(),
|
|
@ -2,11 +2,9 @@ defmodule ArchitexWeb.Client.LoginController do
|
|||
use ArchitexWeb, :controller
|
||||
|
||||
import ArchitexWeb.Error
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Architex.{Repo, Account, Device}
|
||||
alias ArchitexWeb.Client.Request.Login
|
||||
alias Ecto.Changeset
|
||||
|
||||
@login_type "m.login.password"
|
||||
|
||||
|
@ -33,32 +31,28 @@ defmodule ArchitexWeb.Client.LoginController do
|
|||
conn,
|
||||
%{"type" => @login_type, "identifier" => %{"type" => "m.id.user"}} = params
|
||||
) do
|
||||
case Login.changeset(params) do
|
||||
%Changeset{valid?: true} = cs ->
|
||||
input = apply_changes(cs)
|
||||
with {:ok, request} <- Login.parse(params) do
|
||||
case Account.login(request) |> Repo.transaction() do
|
||||
{:ok,
|
||||
{%Account{localpart: localpart}, %Device{access_token: access_token, id: device_id}}} ->
|
||||
data = %{
|
||||
user_id: Architex.get_mxid(localpart),
|
||||
access_token: access_token,
|
||||
device_id: device_id
|
||||
}
|
||||
|
||||
case Account.login(input) |> Repo.transaction() do
|
||||
{:ok,
|
||||
{%Account{localpart: localpart}, %Device{access_token: access_token, id: device_id}}} ->
|
||||
data = %{
|
||||
user_id: Architex.get_mxid(localpart),
|
||||
access_token: access_token,
|
||||
device_id: device_id
|
||||
}
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
{:error, error} when is_atom(error) ->
|
||||
put_error(conn, error)
|
||||
|
||||
{:error, error} when is_atom(error) ->
|
||||
put_error(conn, error)
|
||||
|
||||
{:error, _} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
|
||||
_ ->
|
||||
put_error(conn, :bad_json)
|
||||
{:error, _} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
else
|
||||
_ -> put_error(conn, :bad_json)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -85,7 +85,8 @@ defmodule ArchitexWeb.Client.ProfileController do
|
|||
put_error(conn, :not_found, "User was not found.")
|
||||
end
|
||||
else
|
||||
case HTTPClient.client(domain) |> HTTPClient.query_profile(user_id, Atom.to_string(property_key)) do
|
||||
case HTTPClient.client(domain)
|
||||
|> HTTPClient.query_profile(user_id, Atom.to_string(property_key)) do
|
||||
{:ok, response} ->
|
||||
conn
|
||||
|> put_status(200)
|
||||
|
|
|
@ -2,11 +2,9 @@ defmodule ArchitexWeb.Client.RegisterController do
|
|||
use ArchitexWeb, :controller
|
||||
|
||||
import ArchitexWeb.Error
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Architex.{Repo, Account, Device}
|
||||
alias ArchitexWeb.Client.Request.Register
|
||||
alias Ecto.Changeset
|
||||
|
||||
@register_type "m.login.dummy"
|
||||
|
||||
|
@ -16,35 +14,32 @@ defmodule ArchitexWeb.Client.RegisterController do
|
|||
Action for POST /_matrix/client/r0/register.
|
||||
"""
|
||||
def register(conn, %{"auth" => %{"type" => @register_type}} = params) do
|
||||
case Register.changeset(params) do
|
||||
%Changeset{valid?: true} = cs ->
|
||||
%Register{inhibit_login: inhibit_login} = input = apply_changes(cs)
|
||||
with {:ok, %Register{inhibit_login: inhibit_login} = request} <- Register.parse(params) do
|
||||
case Account.register(request) |> Repo.transaction() do
|
||||
{:ok,
|
||||
%{
|
||||
account: %Account{localpart: localpart},
|
||||
device: %Device{id: device_id, access_token: access_token}
|
||||
}} ->
|
||||
data = %{user_id: Architex.get_mxid(localpart)}
|
||||
|
||||
case Account.register(input) |> Repo.transaction() do
|
||||
{:ok,
|
||||
%{
|
||||
account: %Account{localpart: localpart},
|
||||
device: %Device{id: device_id, access_token: access_token}
|
||||
}} ->
|
||||
data = %{user_id: Architex.get_mxid(localpart)}
|
||||
data =
|
||||
if not inhibit_login do
|
||||
data
|
||||
|> Map.put(:device_id, device_id)
|
||||
|> Map.put(:access_token, access_token)
|
||||
else
|
||||
data
|
||||
end
|
||||
|
||||
data =
|
||||
if not inhibit_login do
|
||||
data
|
||||
|> Map.put(:device_id, device_id)
|
||||
|> Map.put(:access_token, access_token)
|
||||
else
|
||||
data
|
||||
end
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
|
||||
{:error, _, cs, _} ->
|
||||
put_error(conn, Register.get_error(cs))
|
||||
end
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
|
||||
{:error, _, cs, _} ->
|
||||
put_error(conn, Register.get_error(cs))
|
||||
end
|
||||
else
|
||||
_ ->
|
||||
put_error(conn, :bad_json)
|
||||
end
|
||||
|
|
|
@ -2,12 +2,11 @@ defmodule ArchitexWeb.Client.RoomController do
|
|||
use ArchitexWeb, :controller
|
||||
|
||||
import ArchitexWeb.Error
|
||||
import Ecto.{Changeset, Query}
|
||||
import Ecto.Query
|
||||
|
||||
alias Architex.{Repo, Room, RoomServer, Event}
|
||||
alias Architex.Types.UserId
|
||||
alias ArchitexWeb.Client.Request.{CreateRoom, Kick, Ban, Messages}
|
||||
alias Ecto.Changeset
|
||||
alias Plug.Conn
|
||||
|
||||
@doc """
|
||||
|
@ -16,23 +15,20 @@ defmodule ArchitexWeb.Client.RoomController do
|
|||
Action for POST /_matrix/client/r0/createRoom.
|
||||
"""
|
||||
def create(%Conn{assigns: %{account: account}} = conn, params) do
|
||||
case CreateRoom.changeset(params) do
|
||||
%Changeset{valid?: true} = cs ->
|
||||
input = apply_changes(cs)
|
||||
with {:ok, request} <- CreateRoom.parse(params) do
|
||||
case Room.create(account, request) do
|
||||
{:ok, room_id} ->
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(%{room_id: room_id})
|
||||
|
||||
case Room.create(account, input) do
|
||||
{:ok, room_id} ->
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(%{room_id: room_id})
|
||||
|
||||
{:error, :authorization} ->
|
||||
put_error(conn, :invalid_room_state)
|
||||
|
||||
{:error, :unknown} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
{:error, :authorization} ->
|
||||
put_error(conn, :invalid_room_state)
|
||||
|
||||
{:error, :unknown} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
else
|
||||
_ ->
|
||||
put_error(conn, :bad_json)
|
||||
end
|
||||
|
|
|
@ -7,29 +7,7 @@ defmodule ArchitexWeb.Federation.QueryController do
|
|||
|
||||
alias Architex.{Repo, Account}
|
||||
alias Architex.Types.UserId
|
||||
|
||||
defmodule ProfileRequest do
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :user_id, UserId
|
||||
field :field, :string
|
||||
end
|
||||
|
||||
def validate(params) do
|
||||
%__MODULE__{}
|
||||
|> cast(params, [:user_id, :field])
|
||||
|> validate_required([:user_id])
|
||||
|> validate_inclusion(:field, ["displayname", "avatar_url"])
|
||||
|> then(fn
|
||||
%Ecto.Changeset{valid?: true} = cs -> {:ok, apply_changes(cs)}
|
||||
_ -> :error
|
||||
end)
|
||||
end
|
||||
end
|
||||
alias ArchitexWeb.Federation.Request.Profile
|
||||
|
||||
@doc """
|
||||
Performs a query to get profile information, such as a display name or avatar,
|
||||
|
@ -38,9 +16,8 @@ defmodule ArchitexWeb.Federation.QueryController do
|
|||
Action for GET /_matrix/federation/v1/query/profile.
|
||||
"""
|
||||
def profile(conn, params) do
|
||||
with {:ok,
|
||||
%ProfileRequest{user_id: %UserId{localpart: localpart, domain: domain}, field: field}} <-
|
||||
ProfileRequest.validate(params) do
|
||||
with {:ok, %Profile{user_id: %UserId{localpart: localpart, domain: domain}, field: field}} <-
|
||||
Profile.parse(params) do
|
||||
if domain == Architex.server_name() do
|
||||
case Repo.one(from a in Account, where: a.localpart == ^localpart) do
|
||||
%Account{displayname: displayname, avatar_url: avatar_url} ->
|
||||
|
|
|
@ -7,7 +7,7 @@ defmodule ArchitexWeb.Federation.HTTPClient do
|
|||
use Tesla
|
||||
|
||||
alias ArchitexWeb.Endpoint
|
||||
alias ArchitexWeb.Federation.Request.GetSigningKeys
|
||||
alias ArchitexWeb.Federation.Response.GetSigningKeys
|
||||
alias ArchitexWeb.Federation.Middleware.SignRequest
|
||||
alias ArchitexWeb.Router.Helpers, as: RouteHelpers
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
defmodule ArchitexWeb.Request do
|
||||
defmodule ArchitexWeb.APIRequest do
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Ecto.Changeset
|
||||
|
@ -19,7 +19,7 @@ defmodule ArchitexWeb.Request do
|
|||
|
||||
@spec parse(map()) :: {:ok, struct()} | {:error, Changeset.t()}
|
||||
def parse(params) do
|
||||
ArchitexWeb.Request.parse(__MODULE__, params)
|
||||
ArchitexWeb.APIRequest.parse(__MODULE__, params)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue