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