Merge branch 'docs/admin-api' 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 with id <- o_status_url(conn, :activity, uuid),
116 {_, %Activity{} = activity} <- {:activity, Activity.normalize(id)},
117 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
118 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
119 case format = get_format(conn) do
120 "html" -> redirect(conn, to: "/notice/#{activity.id}")
121 _ -> represent_activity(conn, format, activity, user)
122 end
123 else
124 {:public?, false} ->
125 {:error, :not_found}
126
127 {:activity, nil} ->
128 {:error, :not_found}
129
130 e ->
131 e
132 end
133 end
134
135 def notice(conn, %{"id" => id}) do
136 with {_, %Activity{} = activity} <- {:activity, Repo.get(Activity, id)},
137 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
138 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
139 case format = get_format(conn) do
140 "html" ->
141 conn
142 |> put_resp_content_type("text/html")
143 |> send_file(200, Pleroma.Plugs.InstanceStatic.file_path("index.html"))
144
145 _ ->
146 represent_activity(conn, format, activity, user)
147 end
148 else
149 {:public?, false} ->
150 {:error, :not_found}
151
152 {:activity, nil} ->
153 {:error, :not_found}
154
155 e ->
156 e
157 end
158 end
159
160 defp represent_activity(
161 conn,
162 "activity+json",
163 %Activity{data: %{"type" => "Create"}} = activity,
164 _user
165 ) do
166 object = Object.normalize(activity.data["object"])
167
168 conn
169 |> put_resp_header("content-type", "application/activity+json")
170 |> json(ObjectView.render("object.json", %{object: object}))
171 end
172
173 defp represent_activity(_conn, "activity+json", _, _) do
174 {:error, :not_found}
175 end
176
177 defp represent_activity(conn, _, activity, user) do
178 response =
179 activity
180 |> ActivityRepresenter.to_simple_form(user, true)
181 |> ActivityRepresenter.wrap_with_entry()
182 |> :xmerl.export_simple(:xmerl_xml)
183 |> to_string
184
185 conn
186 |> put_resp_content_type("application/atom+xml")
187 |> send_resp(200, response)
188 end
189
190 def errors(conn, {:error, :not_found}) do
191 conn
192 |> put_status(404)
193 |> text("Not found")
194 end
195
196 def errors(conn, _) do
197 conn
198 |> put_status(500)
199 |> text("Something went wrong")
200 end
201 end