Handle "users/:id" links as well. Fix comments in MR.
[akkoma] / lib / pleroma / web / ostatus / ostatus_controller.ex
1 defmodule Pleroma.Web.OStatus.OStatusController do
2 use Pleroma.Web, :controller
3
4 alias Pleroma.{User, Activity, Object}
5 alias Pleroma.Web.OStatus.{FeedRepresenter, ActivityRepresenter}
6 alias Pleroma.Repo
7 alias Pleroma.Web.{OStatus, Federator}
8 alias Pleroma.Web.XML
9 alias Pleroma.Web.ActivityPub.ObjectView
10 alias Pleroma.Web.ActivityPub.ActivityPubController
11 alias Pleroma.Web.ActivityPub.ActivityPub
12
13 plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
14 action_fallback(:errors)
15
16 def feed_redirect(conn, %{"nickname" => nickname}) do
17 case get_format(conn) do
18 "html" ->
19 with %User{} = user <- User.get_cached_by_nickname_or_id(nickname) do
20 Fallback.RedirectController.redirector_with_meta(conn, %{user: user})
21 else
22 nil -> {:error, :not_found}
23 end
24
25 "activity+json" ->
26 ActivityPubController.call(conn, :user)
27
28 _ ->
29 with %User{} = user <- User.get_cached_by_nickname(nickname) do
30 redirect(conn, external: OStatus.feed_path(user))
31 else
32 nil -> {:error, :not_found}
33 end
34 end
35 end
36
37 def feed(conn, %{"nickname" => nickname} = params) do
38 with %User{} = user <- User.get_cached_by_nickname(nickname) do
39 query_params =
40 Map.take(params, ["max_id"])
41 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
42
43 activities =
44 ActivityPub.fetch_public_activities(query_params)
45 |> Enum.reverse()
46
47 response =
48 user
49 |> FeedRepresenter.to_simple_form(activities, [user])
50 |> :xmerl.export_simple(:xmerl_xml)
51 |> to_string
52
53 conn
54 |> put_resp_content_type("application/atom+xml")
55 |> send_resp(200, response)
56 else
57 nil -> {:error, :not_found}
58 end
59 end
60
61 defp decode_or_retry(body) do
62 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
63 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
64 {:ok, doc}
65 else
66 _e ->
67 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
68 doc <- XML.parse_document(decoded),
69 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
70 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
71 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
72 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
73 {:ok, doc}
74 end
75 end
76 end
77
78 def salmon_incoming(conn, _) do
79 {:ok, body, _conn} = read_body(conn)
80 {:ok, doc} = decode_or_retry(body)
81
82 Federator.enqueue(:incoming_doc, doc)
83
84 conn
85 |> send_resp(200, "")
86 end
87
88 def object(conn, %{"uuid" => uuid}) do
89 if get_format(conn) == "activity+json" do
90 ActivityPubController.call(conn, :object)
91 else
92 with id <- o_status_url(conn, :object, uuid),
93 {_, %Activity{} = activity} <-
94 {:activity, Activity.get_create_activity_by_object_ap_id(id)},
95 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
96 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
97 case get_format(conn) do
98 "html" -> redirect(conn, to: "/notice/#{activity.id}")
99 _ -> represent_activity(conn, nil, activity, user)
100 end
101 else
102 {:public?, false} ->
103 {:error, :not_found}
104
105 {:activity, nil} ->
106 {:error, :not_found}
107
108 e ->
109 e
110 end
111 end
112 end
113
114 def activity(conn, %{"uuid" => uuid}) do
115 with id <- o_status_url(conn, :activity, uuid),
116 {_, %Activity{} = activity} <- {:activity, Activity.normalize(id)},
117 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
118 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
119 case format = get_format(conn) do
120 "html" -> redirect(conn, to: "/notice/#{activity.id}")
121 _ -> represent_activity(conn, format, activity, user)
122 end
123 else
124 {:public?, false} ->
125 {:error, :not_found}
126
127 {:activity, nil} ->
128 {:error, :not_found}
129
130 e ->
131 e
132 end
133 end
134
135 def notice(conn, %{"id" => id}) do
136 with {_, %Activity{} = activity} <- {:activity, Repo.get(Activity, id)},
137 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
138 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
139 case format = get_format(conn) do
140 "html" ->
141 Fallback.RedirectController.redirector_with_meta(conn, %{activity: activity, user: user})
142
143 _ ->
144 represent_activity(conn, format, activity, user)
145 end
146 else
147 {:public?, false} ->
148 {:error, :not_found}
149
150 {:activity, nil} ->
151 {:error, :not_found}
152
153 e ->
154 e
155 end
156 end
157
158 defp represent_activity(
159 conn,
160 "activity+json",
161 %Activity{data: %{"type" => "Create"}} = activity,
162 _user
163 ) do
164 object = Object.normalize(activity.data["object"])
165
166 conn
167 |> put_resp_header("content-type", "application/activity+json")
168 |> json(ObjectView.render("object.json", %{object: object}))
169 end
170
171 defp represent_activity(_conn, "activity+json", _, _) do
172 {:error, :not_found}
173 end
174
175 defp represent_activity(conn, _, activity, user) do
176 response =
177 activity
178 |> ActivityRepresenter.to_simple_form(user, true)
179 |> ActivityRepresenter.wrap_with_entry()
180 |> :xmerl.export_simple(:xmerl_xml)
181 |> to_string
182
183 conn
184 |> put_resp_content_type("application/atom+xml")
185 |> send_resp(200, response)
186 end
187
188 def errors(conn, {:error, :not_found}) do
189 conn
190 |> put_status(404)
191 |> text("Not found")
192 end
193
194 def errors(conn, _) do
195 conn
196 |> put_status(500)
197 |> text("Something went wrong")
198 end
199 end