Introduce get_by_id in Activity, replace newlines with spaces
[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} <-
97 {:activity, Activity.get_create_activity_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 Fallback.RedirectController.redirector_with_meta(conn, %{activity: activity, user: user})
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