Implement client get/set avatar URL endpoint
This commit is contained in:
parent
b12872fe2c
commit
3e1a377226
7 changed files with 80 additions and 7 deletions
|
@ -1,7 +1,6 @@
|
|||
defmodule ArchitexWeb.Client.AccountController do
|
||||
use ArchitexWeb, :controller
|
||||
|
||||
import Architex
|
||||
import ArchitexWeb.Error
|
||||
|
||||
alias Architex.{Account, Repo}
|
||||
|
@ -32,7 +31,7 @@ defmodule ArchitexWeb.Client.AccountController do
|
|||
Action for GET /_matrix/client/r0/account/whoami.
|
||||
"""
|
||||
def whoami(%Conn{assigns: %{account: %Account{localpart: localpart}}} = conn, _params) do
|
||||
data = %{user_id: get_mxid(localpart)}
|
||||
data = %{user_id: Architex.get_mxid(localpart)}
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|
|
64
lib/architex_web/client/controllers/profile_controller.ex
Normal file
64
lib/architex_web/client/controllers/profile_controller.ex
Normal file
|
@ -0,0 +1,64 @@
|
|||
defmodule ArchitexWeb.Client.ProfileController do
|
||||
use ArchitexWeb, :controller
|
||||
|
||||
import ArchitexWeb.Error
|
||||
import Ecto.Query
|
||||
|
||||
alias Architex.{Repo, Account}
|
||||
alias Architex.Types.UserId
|
||||
alias Plug.Conn
|
||||
alias Ecto.Changeset
|
||||
|
||||
@doc """
|
||||
Get the user's avatar URL.
|
||||
|
||||
Action for GET /_matrix/client/r0/profile/{userId}/avatar_url.
|
||||
"""
|
||||
def get_avatar_url(conn, %{"user_id" => user_id}) do
|
||||
case UserId.cast(user_id) do
|
||||
{:ok, %UserId{localpart: localpart, domain: domain}} ->
|
||||
if domain == Architex.server_name() do
|
||||
case Repo.one(from a in Account, where: a.localpart == ^localpart) do
|
||||
%Account{avatar_url: avatar_url} ->
|
||||
data = if avatar_url, do: %{avatar_url: avatar_url}, else: %{}
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
|
||||
nil ->
|
||||
put_error(conn, :not_found, "User was not found.")
|
||||
end
|
||||
else
|
||||
# TODO: Use federation to lookup avatar URL.
|
||||
put_error(conn, :not_found, "User was not found.")
|
||||
end
|
||||
|
||||
:error ->
|
||||
put_error(conn, :not_found, "User ID is invalid.")
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
This API sets the given user's avatar URL.
|
||||
|
||||
Action for PUT /_matrix/client/r0/profile/{userId}/avatar_url.
|
||||
"""
|
||||
def set_avatar_url(%Conn{assigns: %{account: account}} = conn, %{"user_id" => user_id} = params) do
|
||||
if Account.get_mxid(account) == user_id do
|
||||
avatar_url = Map.get(params, "avatar_url")
|
||||
|
||||
if not is_nil(avatar_url) do
|
||||
account
|
||||
|> Changeset.change(avatar_url: avatar_url)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
conn
|
||||
|> send_resp(200, [])
|
||||
|> halt()
|
||||
else
|
||||
put_error(conn, :unauthorized, "User ID does not match access token.")
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue