architex/lib/architex_web/federation/controllers/query_controller.ex

51 lines
1.5 KiB
Elixir
Raw Normal View History

2021-09-01 12:43:55 +00:00
defmodule ArchitexWeb.Federation.QueryController do
use ArchitexWeb, :controller
use ArchitexWeb.Federation.AuthenticateServer
2021-08-10 16:02:53 +00:00
2021-09-01 12:43:55 +00:00
import ArchitexWeb.Error
2021-08-10 16:02:53 +00:00
import Ecto.Query
2021-09-01 12:43:55 +00:00
alias Architex.{Repo, Account}
alias Architex.Types.UserId
alias ArchitexWeb.Federation.Request.Profile
2021-08-10 16:02:53 +00:00
@doc """
Performs a query to get profile information, such as a display name or avatar,
for a given user.
Action for GET /_matrix/federation/v1/query/profile.
"""
2021-08-10 16:02:53 +00:00
def profile(conn, params) do
with {:ok, %Profile{user_id: %UserId{localpart: localpart, domain: domain}, field: field}} <-
Profile.parse(params) do
2021-09-01 12:43:55 +00:00
if domain == Architex.server_name() do
2021-08-10 16:02:53 +00:00
case Repo.one(from a in Account, where: a.localpart == ^localpart) do
2021-09-09 14:00:58 +00:00
%Account{displayname: displayname, avatar_url: avatar_url} ->
data = %{}
data =
if field == "displayname" or (field == nil and displayname != nil),
do: Map.put(data, :displayname, displayname),
else: data
data =
if field == "avatar_url" or (field == nil and avatar_url != nil),
do: Map.put(data, :avatar_url, avatar_url),
else: data
2021-08-10 16:02:53 +00:00
conn
|> put_status(200)
2021-09-09 14:00:58 +00:00
|> json(data)
2021-08-10 16:02:53 +00:00
nil ->
put_error(conn, :not_found, "User does not exist.")
2021-08-10 16:02:53 +00:00
end
else
put_error(conn, :not_found, "Wrong server name.")
2021-08-10 16:02:53 +00:00
end
else
_ -> put_error(conn, :bad_json)
end
end
end