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