1 defmodule Pleroma.Web.ActivityPub.ActivityPubController do
2 use Pleroma.Web, :controller
3 alias Pleroma.{User, Repo, Object, Activity}
4 alias Pleroma.Web.ActivityPub.{ObjectView, UserView, Transmogrifier}
5 alias Pleroma.Web.ActivityPub.ActivityPub
6 alias Pleroma.Web.Federator
10 action_fallback(:errors)
12 def user(conn, %{"nickname" => nickname}) do
13 with %User{} = user <- User.get_cached_by_nickname(nickname),
14 {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
16 |> put_resp_header("content-type", "application/activity+json")
17 |> json(UserView.render("user.json", %{user: user}))
21 def object(conn, %{"uuid" => uuid}) do
22 with ap_id <- o_status_url(conn, :object, uuid),
23 %Object{} = object <- Object.get_cached_by_ap_id(ap_id) do
25 |> put_resp_header("content-type", "application/activity+json")
26 |> json(ObjectView.render("object.json", %{object: object}))
30 def following(conn, %{"nickname" => nickname, "page" => page}) do
31 with %User{} = user <- User.get_cached_by_nickname(nickname),
32 {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
33 {page, _} = Integer.parse(page)
36 |> put_resp_header("content-type", "application/activity+json")
37 |> json(UserView.render("following.json", %{user: user, page: page}))
41 def following(conn, %{"nickname" => nickname}) do
42 with %User{} = user <- User.get_cached_by_nickname(nickname),
43 {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
45 |> put_resp_header("content-type", "application/activity+json")
46 |> json(UserView.render("following.json", %{user: user}))
50 def followers(conn, %{"nickname" => nickname, "page" => page}) do
51 with %User{} = user <- User.get_cached_by_nickname(nickname),
52 {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
53 {page, _} = Integer.parse(page)
56 |> put_resp_header("content-type", "application/activity+json")
57 |> json(UserView.render("followers.json", %{user: user, page: page}))
61 def followers(conn, %{"nickname" => nickname}) do
62 with %User{} = user <- User.get_cached_by_nickname(nickname),
63 {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
65 |> put_resp_header("content-type", "application/activity+json")
66 |> json(UserView.render("followers.json", %{user: user}))
70 def outbox(conn, %{"nickname" => nickname, "max_id" => max_id}) do
71 with %User{} = user <- User.get_cached_by_nickname(nickname),
72 {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
74 |> put_resp_header("content-type", "application/activity+json")
75 |> json(UserView.render("outbox.json", %{user: user, max_id: max_id}))
79 def outbox(conn, %{"nickname" => nickname}) do
80 outbox(conn, %{"nickname" => nickname, "max_id" => nil})
83 # TODO: Ensure that this inbox is a recipient of the message
84 def inbox(%{assigns: %{valid_signature: true}} = conn, params) do
85 Federator.enqueue(:incoming_ap_doc, params)
89 def inbox(conn, params) do
90 headers = Enum.into(conn.req_headers, %{})
92 if !String.contains?(headers["signature"] || "", params["actor"]) do
93 Logger.info("Signature not from author, relayed message, fetching from source")
94 ActivityPub.fetch_object_from_id(params["object"]["id"])
96 Logger.info("Signature error")
97 Logger.info("Could not validate #{params["actor"]}")
98 Logger.info(inspect(conn.req_headers))
104 def errors(conn, _e) do