Implement client room messages endpoint
This commit is contained in:
parent
40f3eeff7c
commit
0871c3cdd9
9 changed files with 161 additions and 17 deletions
lib/architex_web/client
|
@ -39,8 +39,7 @@ defmodule ArchitexWeb.Client.LoginController do
|
|||
|
||||
case Account.login(input) |> Repo.transaction() do
|
||||
{:ok,
|
||||
{%Account{localpart: localpart},
|
||||
%Device{access_token: access_token, id: device_id}}} ->
|
||||
{%Account{localpart: localpart}, %Device{access_token: access_token, id: device_id}}} ->
|
||||
data = %{
|
||||
user_id: Architex.get_mxid(localpart),
|
||||
access_token: access_token,
|
||||
|
|
|
@ -4,9 +4,9 @@ defmodule ArchitexWeb.Client.RoomController do
|
|||
import ArchitexWeb.Error
|
||||
import Ecto.{Changeset, Query}
|
||||
|
||||
alias Architex.{Repo, Room, RoomServer}
|
||||
alias Architex.{Repo, Room, RoomServer, Event}
|
||||
alias Architex.Types.UserId
|
||||
alias ArchitexWeb.Client.Request.{CreateRoom, Kick, Ban}
|
||||
alias ArchitexWeb.Client.Request.{CreateRoom, Kick, Ban, Messages}
|
||||
alias Ecto.Changeset
|
||||
alias Plug.Conn
|
||||
|
||||
|
@ -50,13 +50,9 @@ defmodule ArchitexWeb.Client.RoomController do
|
|||
|> select([jr], jr.id)
|
||||
|> Repo.all()
|
||||
|
||||
data = %{
|
||||
joined_rooms: joined_room_ids
|
||||
}
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
|> json(%{joined_rooms: joined_room_ids})
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
@ -235,10 +231,32 @@ defmodule ArchitexWeb.Client.RoomController do
|
|||
|
||||
# GET /_matrix/client/r0/rooms/!atYDsyowueiToUvuqY:localhost:4000/messages
|
||||
# Parameters: %{"dir" => "b", "from" => "", "limit" => "727", "path" => ["_matrix", "client", "r0", "rooms", "!atYDsyowueiToUvuqY:localhost:4000", "messages"]}
|
||||
def message(conn, params) do
|
||||
def messages(%Conn{assigns: %{account: account}} = conn, %{"room_id" => room_id} = params) do
|
||||
# IO.inspect(Messages.changeset(%Messages{}, params))
|
||||
|
||||
conn
|
||||
|> send_resp(400, [])
|
||||
|> halt()
|
||||
with {:ok, request} <- Messages.parse(params) do
|
||||
room_query =
|
||||
account
|
||||
|> Ecto.assoc(:joined_rooms)
|
||||
|> where([r], r.id == ^room_id)
|
||||
|
||||
case Repo.one(room_query) do
|
||||
%Room{} = room ->
|
||||
{events, start, end_} = Room.get_messages(room, request)
|
||||
events = Enum.map(events, &Event.Formatters.for_client/1)
|
||||
data = %{chunk: events}
|
||||
data = if start, do: Map.put(data, :start, start), else: data
|
||||
data = if end_, do: Map.put(data, :end, end_), else: data
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
|
||||
nil ->
|
||||
put_error(conn, :forbidden, "You are not participating in this room.")
|
||||
end
|
||||
else
|
||||
{:error, _} -> put_error(conn, :bad_json)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
21
lib/architex_web/client/request/messages.ex
Normal file
21
lib/architex_web/client/request/messages.ex
Normal file
|
@ -0,0 +1,21 @@
|
|||
defmodule ArchitexWeb.Client.Request.Messages do
|
||||
use ArchitexWeb.Request
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :from, :string
|
||||
field :to, :string
|
||||
field :dir, :string
|
||||
field :limit, :integer
|
||||
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)
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue