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