[#534] Refactoring / tweaks per MR review.
[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.Repo
11 alias Pleroma.Web.{OStatus, Federator}
12 alias Pleroma.Web.XML
13 alias Pleroma.Web.ActivityPub.ObjectView
14 alias Pleroma.Web.ActivityPub.ActivityPubController
15 alias Pleroma.Web.ActivityPub.ActivityPub
16
17 plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
18 plug(Pleroma.Web.Plugs.SetRequesterReachablePlug when action in [:salmon_incoming])
19
20 action_fallback(:errors)
21
22 def feed_redirect(conn, %{"nickname" => nickname}) do
23 case get_format(conn) do
24 "html" ->
25 Fallback.RedirectController.redirector(conn, nil)
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, Repo.get(Activity, 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 conn
147 |> put_resp_content_type("text/html")
148 |> send_file(200, Pleroma.Plugs.InstanceStatic.file_path("index.html"))
149
150 _ ->
151 represent_activity(conn, format, activity, user)
152 end
153 else
154 {:public?, false} ->
155 {:error, :not_found}
156
157 {:activity, nil} ->
158 {:error, :not_found}
159
160 e ->
161 e
162 end
163 end
164
165 defp represent_activity(
166 conn,
167 "activity+json",
168 %Activity{data: %{"type" => "Create"}} = activity,
169 _user
170 ) do
171 object = Object.normalize(activity.data["object"])
172
173 conn
174 |> put_resp_header("content-type", "application/activity+json")
175 |> json(ObjectView.render("object.json", %{object: object}))
176 end
177
178 defp represent_activity(_conn, "activity+json", _, _) do
179 {:error, :not_found}
180 end
181
182 defp represent_activity(conn, _, activity, user) do
183 response =
184 activity
185 |> ActivityRepresenter.to_simple_form(user, true)
186 |> ActivityRepresenter.wrap_with_entry()
187 |> :xmerl.export_simple(:xmerl_xml)
188 |> to_string
189
190 conn
191 |> put_resp_content_type("application/atom+xml")
192 |> send_resp(200, response)
193 end
194
195 def errors(conn, {:error, :not_found}) do
196 conn
197 |> put_status(404)
198 |> text("Not found")
199 end
200
201 def errors(conn, _) do
202 conn
203 |> put_status(500)
204 |> text("Something went wrong")
205 end
206 end