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