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