Add specs

This commit is contained in:
Pim Kunis 2021-09-06 16:17:45 +02:00
parent 224201ae2f
commit 5f6476df57
3 changed files with 24 additions and 2 deletions

View file

@ -68,8 +68,8 @@ defmodule Architex.Room do
end
end
@spec get_messages(Room.t(), Messages.t()) :: {[Event.t()], integer() | nil, integer() | nil}
def get_messages(room, %Messages{from: from, to: to, dir: dir, limit: limit}) do
# TODO: Check 'from' and 'to' formats.
limit = limit || 10
events =
@ -84,11 +84,13 @@ defmodule Architex.Room do
{events, get_start(events, dir), get_end(events, limit, dir)}
end
@spec order_by_direction(Ecto.Query.t(), String.t()) :: Ecto.Query.t()
defp order_by_direction(query, "b"), do: order_by(query, desc: :origin_server_ts, desc: :nid)
defp order_by_direction(query, "f"), do: order_by(query, asc: :origin_server_ts, asc: :nid)
# When 'from' is empty, we return events from the start or end
# of the room's history.
@spec events_from(Ecto.Query.t(), String.t(), String.t()) :: Ecto.Query.t()
defp events_from(query, "", _), do: query
defp events_from(query, from, "b") do
@ -101,6 +103,7 @@ defmodule Architex.Room do
where(query, [e], e.nid > ^from)
end
@spec events_to(Ecto.Query.t(), String.t() | nil, String.t()) :: Ecto.Query.t()
defp events_to(query, nil, _), do: query
defp events_to(query, to, "b") do
@ -113,8 +116,8 @@ defmodule Architex.Room do
where(query, [e], e.nid <= ^to)
end
@spec get_start([Event.t()], String.t()) :: integer() | nil
defp get_start([], _), do: nil
defp get_start([%Event{nid: first_nid} | _], "f"), do: first_nid
defp get_start(events, "b") do
@ -122,6 +125,7 @@ defmodule Architex.Room do
last_nid
end
@spec get_end([Event.t()], integer(), String.t()) :: integer() | nil
defp get_end(events, limit, _) when length(events) < limit, do: nil
defp get_end([%Event{nid: first_nid} | _], _, "f"), do: first_nid

View file

@ -204,6 +204,11 @@ defmodule ArchitexWeb.Client.RoomController do
def unban(conn, _), do: put_error(conn, :missing_param)
@doc """
This endpoint is used to send a message event to a room.
Action for PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}.
"""
def send_message(
%Conn{assigns: %{account: account, device: device}, body_params: body_params} = conn,
%{
@ -229,6 +234,11 @@ defmodule ArchitexWeb.Client.RoomController do
end
end
@doc """
This API returns a list of message and state events for a room.
Action for GET /_matrix/client/r0/rooms/{roomId}/messages.
"""
def messages(%Conn{assigns: %{account: account}} = conn, %{"room_id" => room_id} = params) do
with {:ok, request} <- Messages.parse(params) do
room_query =

View file

@ -1,6 +1,14 @@
defmodule ArchitexWeb.Client.Request.Messages do
use ArchitexWeb.Request
@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