Use object instead of activity for metadata
[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 if activity.data["type"] == "Create" do
149 %Object{} = object = Object.normalize(activity.data["object"])
150
151 Fallback.RedirectController.redirector_with_meta(conn, %{
152 object: object,
153 user: user
154 })
155 else
156 Fallback.RedirectController.redirector(conn, nil)
157 end
158
159 _ ->
160 represent_activity(conn, format, activity, user)
161 end
162 else
163 {:public?, false} ->
164 {:error, :not_found}
165
166 {:activity, nil} ->
167 {:error, :not_found}
168
169 e ->
170 e
171 end
172 end
173
174 defp represent_activity(
175 conn,
176 "activity+json",
177 %Activity{data: %{"type" => "Create"}} = activity,
178 _user
179 ) do
180 object = Object.normalize(activity.data["object"])
181
182 conn
183 |> put_resp_header("content-type", "application/activity+json")
184 |> json(ObjectView.render("object.json", %{object: object}))
185 end
186
187 defp represent_activity(_conn, "activity+json", _, _) do
188 {:error, :not_found}
189 end
190
191 defp represent_activity(conn, _, activity, user) do
192 response =
193 activity
194 |> ActivityRepresenter.to_simple_form(user, true)
195 |> ActivityRepresenter.wrap_with_entry()
196 |> :xmerl.export_simple(:xmerl_xml)
197 |> to_string
198
199 conn
200 |> put_resp_content_type("application/atom+xml")
201 |> send_resp(200, response)
202 end
203
204 def errors(conn, {:error, :not_found}) do
205 conn
206 |> put_status(404)
207 |> text("Not found")
208 end
209
210 def errors(conn, _) do
211 conn
212 |> put_status(500)
213 |> text("Something went wrong")
214 end
215 end