From 213980a5befe9149fe0405d6467bf1b57758e8af Mon Sep 17 00:00:00 2001 From: Pim Kunis Date: Wed, 8 Sep 2021 20:21:55 +0200 Subject: [PATCH] Implement client profile information endpoint --- .../client/controllers/profile_controller.ex | 34 ++++++++++++++++++- lib/architex_web/router.ex | 1 + 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/architex_web/client/controllers/profile_controller.ex b/lib/architex_web/client/controllers/profile_controller.ex index 7364569..9d9ea34 100644 --- a/lib/architex_web/client/controllers/profile_controller.ex +++ b/lib/architex_web/client/controllers/profile_controller.ex @@ -10,6 +10,38 @@ defmodule ArchitexWeb.Client.ProfileController do alias Plug.Conn 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 """ Get the user's display name. @@ -45,7 +77,7 @@ defmodule ArchitexWeb.Client.ProfileController do put_error(conn, :not_found, "User was not found.") end else - # TODO: Use federation to lookup avatar URL. + # TODO: Use federation to lookup information. put_error(conn, :not_found, "User was not found.") end diff --git a/lib/architex_web/router.ex b/lib/architex_web/router.ex index 47ac7c0..a78247d 100644 --- a/lib/architex_web/router.ex +++ b/lib/architex_web/router.ex @@ -39,6 +39,7 @@ defmodule ArchitexWeb.Router do end scope "/profile/:user_id" do + get "/", ProfileController, :profile get "/avatar_url", ProfileController, :get_avatar_url get "/displayname", ProfileController, :get_displayname end