Implement client profile information endpoint

This commit is contained in:
Pim Kunis 2021-09-08 20:21:55 +02:00
parent d214df3605
commit 213980a5be
2 changed files with 34 additions and 1 deletions

View file

@ -10,6 +10,38 @@ defmodule ArchitexWeb.Client.ProfileController do
alias Plug.Conn alias Plug.Conn
alias Ecto.Changeset alias Ecto.Changeset
@doc """
Get the combined profile information for this user.
Action for GET /_matrix/client/r0/profile/{userId}.
"""
def profile(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{displayname: displayname, avatar_url: avatar_url} ->
data = %{}
data = if avatar_url, do: Map.put(data, :avatar_url, avatar_url), else: data
data = if displayname, do: Map.put(data, :displayname, displayname), else: data
conn
|> put_status(200)
|> json(data)
nil ->
put_error(conn, :not_found, "User was not found.")
end
else
# TODO: Use federation to lookup information.
put_error(conn, :not_found, "User was not found.")
end
:error ->
put_error(conn, :not_found, "User ID is invalid.")
end
end
@doc """ @doc """
Get the user's display name. Get the user's display name.
@ -45,7 +77,7 @@ defmodule ArchitexWeb.Client.ProfileController do
put_error(conn, :not_found, "User was not found.") put_error(conn, :not_found, "User was not found.")
end end
else else
# TODO: Use federation to lookup avatar URL. # TODO: Use federation to lookup information.
put_error(conn, :not_found, "User was not found.") put_error(conn, :not_found, "User was not found.")
end end

View file

@ -39,6 +39,7 @@ defmodule ArchitexWeb.Router do
end end
scope "/profile/:user_id" do scope "/profile/:user_id" do
get "/", ProfileController, :profile
get "/avatar_url", ProfileController, :get_avatar_url get "/avatar_url", ProfileController, :get_avatar_url
get "/displayname", ProfileController, :get_displayname get "/displayname", ProfileController, :get_displayname
end end