Clean up unused aliases and imports
[akkoma] / lib / pleroma / web / activity_pub / activity_pub_controller.ex
1 defmodule Pleroma.Web.ActivityPub.ActivityPubController do
2 use Pleroma.Web, :controller
3 alias Pleroma.{User, Object}
4 alias Pleroma.Web.ActivityPub.{ObjectView, UserView, Transmogrifier}
5 alias Pleroma.Web.ActivityPub.ActivityPub
6 alias Pleroma.Web.Federator
7
8 require Logger
9
10 action_fallback(:errors)
11
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
15 conn
16 |> put_resp_header("content-type", "application/activity+json")
17 |> json(UserView.render("user.json", %{user: user}))
18 end
19 end
20
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
24 conn
25 |> put_resp_header("content-type", "application/activity+json")
26 |> json(ObjectView.render("object.json", %{object: object}))
27 end
28 end
29
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)
34
35 conn
36 |> put_resp_header("content-type", "application/activity+json")
37 |> json(UserView.render("following.json", %{user: user, page: page}))
38 end
39 end
40
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
44 conn
45 |> put_resp_header("content-type", "application/activity+json")
46 |> json(UserView.render("following.json", %{user: user}))
47 end
48 end
49
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)
54
55 conn
56 |> put_resp_header("content-type", "application/activity+json")
57 |> json(UserView.render("followers.json", %{user: user, page: page}))
58 end
59 end
60
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
64 conn
65 |> put_resp_header("content-type", "application/activity+json")
66 |> json(UserView.render("followers.json", %{user: user}))
67 end
68 end
69
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
73 conn
74 |> put_resp_header("content-type", "application/activity+json")
75 |> json(UserView.render("outbox.json", %{user: user, max_id: max_id}))
76 end
77 end
78
79 def outbox(conn, %{"nickname" => nickname}) do
80 outbox(conn, %{"nickname" => nickname, "max_id" => nil})
81 end
82
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)
86 json(conn, "ok")
87 end
88
89 def inbox(conn, params) do
90 headers = Enum.into(conn.req_headers, %{})
91
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"])
95 else
96 Logger.info("Signature error")
97 Logger.info("Could not validate #{params["actor"]}")
98 Logger.info(inspect(conn.req_headers))
99 end
100
101 json(conn, "ok")
102 end
103
104 def errors(conn, _e) do
105 conn
106 |> put_status(500)
107 |> json("error")
108 end
109 end