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