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