activity_pub_controller.ex 1.67 KB
Newer Older
lain's avatar
lain committed
1 2
defmodule Pleroma.Web.ActivityPub.ActivityPubController do
  use Pleroma.Web, :controller
lain's avatar
lain committed
3
  alias Pleroma.{User, Repo, Object, Activity}
4
  alias Pleroma.Web.ActivityPub.{ObjectView, UserView, Transmogrifier}
lain's avatar
lain committed
5
  alias Pleroma.Web.ActivityPub.ActivityPub
lain's avatar
lain committed
6
  alias Pleroma.Web.Federator
lain's avatar
lain committed
7

lain's avatar
lain committed
8 9
  require Logger

10 11
  action_fallback :errors

lain's avatar
lain committed
12
  def user(conn, %{"nickname" => nickname}) do
13 14
    with %User{} = user <- User.get_cached_by_nickname(nickname),
         {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
lain's avatar
lain committed
15 16 17
      conn
      |> put_resp_header("content-type", "application/activity+json")
      |> json(UserView.render("user.json", %{user: user}))
lain's avatar
lain committed
18 19 20
    end
  end

21 22 23
  def object(conn, %{"uuid" => uuid}) do
    with ap_id <- o_status_url(conn, :object, uuid),
         %Object{} = object <- Object.get_cached_by_ap_id(ap_id) do
lain's avatar
lain committed
24 25 26
      conn
      |> put_resp_header("content-type", "application/activity+json")
      |> json(ObjectView.render("object.json", %{object: object}))
27 28 29
    end
  end

30
  # TODO: Ensure that this inbox is a recipient of the message
31
  def inbox(%{assigns: %{valid_signature: true}} = conn, params) do
lain's avatar
Typo.  
lain committed
32
    Federator.enqueue(:incoming_ap_doc, params)
lain's avatar
lain committed
33
    json(conn, "ok")
lain's avatar
lain committed
34
  end
35

lain's avatar
lain committed
36
  def inbox(conn, params) do
lain's avatar
Fix.  
lain committed
37 38
    headers = Enum.into(conn.req_headers, %{})
    if !(String.contains(headers["signature"] || "", params["actor"])) do
lain's avatar
lain committed
39 40 41 42 43 44 45
      Logger.info("Signature not from author, relayed message, ignoring.")
    else
      Logger.info("Signature error.")
      Logger.info("Could not validate #{params["actor"]}")
      Logger.info(inspect(conn.req_headers))
    end

lain's avatar
lain committed
46 47
    json(conn, "ok")
  end
lain's avatar
lain committed
48

49 50 51 52 53
  def errors(conn, _e) do
    conn
    |> put_status(500)
    |> json("error")
  end
lain's avatar
lain committed
54
end