architex/lib/architex_web/client/controllers/profile_controller.ex
Pim Kunis af40cebc01 Implement client get/set display name endpoint
Add ability to unset avatar URL/display name
2021-09-08 17:32:10 +02:00

92 lines
2.6 KiB
Elixir

defmodule ArchitexWeb.Client.ProfileController do
# TODO: Changes should be propagated using join events and presence.
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 display name.
Action for GET /_matrix/client/r0/profile/{userId}/displayname.
"""
def get_displayname(conn, %{"user_id" => user_id}) do
get_property(conn, :displayname, user_id)
end
@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
get_property(conn, :avatar_url, user_id)
end
defp get_property(conn, property_key, 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{} = account ->
property_val = Map.get(account, property_key)
data = if property_val, do: %{property_key => property_val}, 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 display name.
Action for PUT /_matrix/client/r0/profile/{userId}/displayname.
"""
def set_displayname(conn, %{"user_id" => user_id} = params) do
displayname = Map.get(params, "displayname")
update_property(conn, :displayname, displayname, user_id)
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, %{"user_id" => user_id} = params) do
avatar_url = Map.get(params, "avatar_url")
update_property(conn, :avatar_url, avatar_url, user_id)
end
defp update_property(%Conn{assigns: %{account: account}} = conn, property_key, property, user_id) do
if Account.get_mxid(account) == user_id do
account
|> Changeset.change([{property_key, property}])
|> Repo.update()
conn
|> send_resp(200, [])
|> halt()
else
put_error(conn, :unauthorized, "User ID does not match access token.")
end
end
end