Merge branch '1973-chats-fix-with-restrict-unauthenticated' into 'develop'
[akkoma] / lib / pleroma / object / fetcher.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Object.Fetcher do
6 alias Pleroma.HTTP
7 alias Pleroma.Object
8 alias Pleroma.Object.Containment
9 alias Pleroma.Repo
10 alias Pleroma.Signature
11 alias Pleroma.Web.ActivityPub.InternalFetchActor
12 alias Pleroma.Web.ActivityPub.Transmogrifier
13 alias Pleroma.Web.Federator
14
15 require Logger
16 require Pleroma.Constants
17
18 defp touch_changeset(changeset) do
19 updated_at =
20 NaiveDateTime.utc_now()
21 |> NaiveDateTime.truncate(:second)
22
23 Ecto.Changeset.put_change(changeset, :updated_at, updated_at)
24 end
25
26 defp maybe_reinject_internal_fields(data, %{data: %{} = old_data}) do
27 internal_fields = Map.take(old_data, Pleroma.Constants.object_internal_fields())
28
29 Map.merge(data, internal_fields)
30 end
31
32 defp maybe_reinject_internal_fields(data, _), do: data
33
34 @spec reinject_object(struct(), map()) :: {:ok, Object.t()} | {:error, any()}
35 defp reinject_object(struct, data) do
36 Logger.debug("Reinjecting object #{data["id"]}")
37
38 with data <- Transmogrifier.fix_object(data),
39 data <- maybe_reinject_internal_fields(data, struct),
40 changeset <- Object.change(struct, %{data: data}),
41 changeset <- touch_changeset(changeset),
42 {:ok, object} <- Repo.insert_or_update(changeset),
43 {:ok, object} <- Object.set_cache(object) do
44 {:ok, object}
45 else
46 e ->
47 Logger.error("Error while processing object: #{inspect(e)}")
48 {:error, e}
49 end
50 end
51
52 def refetch_object(%Object{data: %{"id" => id}} = object) do
53 with {:local, false} <- {:local, Object.local?(object)},
54 {:ok, data} <- fetch_and_contain_remote_object_from_id(id),
55 {:ok, object} <- reinject_object(object, data) do
56 {:ok, object}
57 else
58 {:local, true} -> {:ok, object}
59 e -> {:error, e}
60 end
61 end
62
63 # Note: will create a Create activity, which we need internally at the moment.
64 def fetch_object_from_id(id, options \\ []) do
65 with {_, nil} <- {:fetch_object, Object.get_cached_by_ap_id(id)},
66 {_, true} <- {:allowed_depth, Federator.allowed_thread_distance?(options[:depth])},
67 {_, {:ok, data}} <- {:fetch, fetch_and_contain_remote_object_from_id(id)},
68 {_, nil} <- {:normalize, Object.normalize(data, false)},
69 params <- prepare_activity_params(data),
70 {_, :ok} <- {:containment, Containment.contain_origin(id, params)},
71 {_, {:ok, activity}} <-
72 {:transmogrifier, Transmogrifier.handle_incoming(params, options)},
73 {_, _data, %Object{} = object} <-
74 {:object, data, Object.normalize(activity, false)} do
75 {:ok, object}
76 else
77 {:allowed_depth, false} ->
78 {:error, "Max thread distance exceeded."}
79
80 {:containment, _} ->
81 {:error, "Object containment failed."}
82
83 {:transmogrifier, {:error, {:reject, nil}}} ->
84 {:reject, nil}
85
86 {:transmogrifier, _} = e ->
87 {:error, e}
88
89 {:object, data, nil} ->
90 reinject_object(%Object{}, data)
91
92 {:normalize, object = %Object{}} ->
93 {:ok, object}
94
95 {:fetch_object, %Object{} = object} ->
96 {:ok, object}
97
98 {:fetch, {:error, error}} ->
99 {:error, error}
100
101 e ->
102 e
103 end
104 end
105
106 defp prepare_activity_params(data) do
107 %{
108 "type" => "Create",
109 "to" => data["to"],
110 "cc" => data["cc"],
111 # Should we seriously keep this attributedTo thing?
112 "actor" => data["actor"] || data["attributedTo"],
113 "object" => data
114 }
115 end
116
117 def fetch_object_from_id!(id, options \\ []) do
118 with {:ok, object} <- fetch_object_from_id(id, options) do
119 object
120 else
121 {:error, %Tesla.Mock.Error{}} ->
122 nil
123
124 {:error, "Object has been deleted"} ->
125 nil
126
127 {:reject, reason} ->
128 Logger.info("Rejected #{id} while fetching: #{inspect(reason)}")
129 nil
130
131 e ->
132 Logger.error("Error while fetching #{id}: #{inspect(e)}")
133 nil
134 end
135 end
136
137 defp make_signature(id, date) do
138 uri = URI.parse(id)
139
140 signature =
141 InternalFetchActor.get_actor()
142 |> Signature.sign(%{
143 "(request-target)": "get #{uri.path}",
144 host: uri.host,
145 date: date
146 })
147
148 [{"signature", signature}]
149 end
150
151 defp sign_fetch(headers, id, date) do
152 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
153 headers ++ make_signature(id, date)
154 else
155 headers
156 end
157 end
158
159 defp maybe_date_fetch(headers, date) do
160 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
161 headers ++ [{"date", date}]
162 else
163 headers
164 end
165 end
166
167 def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
168 Logger.debug("Fetching object #{id} via AP")
169
170 date = Pleroma.Signature.signed_date()
171
172 headers =
173 [{"accept", "application/activity+json"}]
174 |> maybe_date_fetch(date)
175 |> sign_fetch(id, date)
176
177 Logger.debug("Fetch headers: #{inspect(headers)}")
178
179 with {:scheme, true} <- {:scheme, String.starts_with?(id, "http")},
180 {:ok, %{body: body, status: code}} when code in 200..299 <- HTTP.get(id, headers),
181 {:ok, data} <- Jason.decode(body),
182 :ok <- Containment.contain_origin_from_id(id, data) do
183 {:ok, data}
184 else
185 {:ok, %{status: code}} when code in [404, 410] ->
186 {:error, "Object has been deleted"}
187
188 {:scheme, _} ->
189 {:error, "Unsupported URI scheme"}
190
191 {:error, e} ->
192 {:error, e}
193
194 e ->
195 {:error, e}
196 end
197 end
198
199 def fetch_and_contain_remote_object_from_id(%{"id" => id}),
200 do: fetch_and_contain_remote_object_from_id(id)
201
202 def fetch_and_contain_remote_object_from_id(_id), do: {:error, "id must be a string"}
203 end