Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / object / fetcher.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.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.OStatus
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) do
43 {:ok, object}
44 else
45 e ->
46 Logger.error("Error while processing object: #{inspect(e)}")
47 {:error, e}
48 end
49 end
50
51 def refetch_object(%Object{data: %{"id" => id}} = object) do
52 with {:local, false} <- {:local, String.starts_with?(id, Pleroma.Web.base_url() <> "/")},
53 {:ok, data} <- fetch_and_contain_remote_object_from_id(id),
54 {:ok, object} <- reinject_object(object, data) do
55 {:ok, object}
56 else
57 {:local, true} -> object
58 e -> {:error, e}
59 end
60 end
61
62 # TODO:
63 # This will create a Create activity, which we need internally at the moment.
64 def fetch_object_from_id(id, options \\ []) do
65 with {:fetch_object, nil} <- {:fetch_object, Object.get_cached_by_ap_id(id)},
66 {:fetch, {:ok, data}} <- {:fetch, fetch_and_contain_remote_object_from_id(id)},
67 {:normalize, nil} <- {:normalize, Object.normalize(data, false)},
68 params <- prepare_activity_params(data),
69 {:containment, :ok} <- {:containment, Containment.contain_origin(id, params)},
70 {:ok, activity} <- Transmogrifier.handle_incoming(params, options),
71 {:object, _data, %Object{} = object} <-
72 {:object, data, Object.normalize(activity, false)} do
73 {:ok, object}
74 else
75 {:containment, _} ->
76 {:error, "Object containment failed."}
77
78 {:error, {:reject, nil}} ->
79 {:reject, nil}
80
81 {:object, data, nil} ->
82 reinject_object(%Object{}, data)
83
84 {:normalize, object = %Object{}} ->
85 {:ok, object}
86
87 {:fetch_object, %Object{} = object} ->
88 {:ok, object}
89
90 _e ->
91 # Only fallback when receiving a fetch/normalization error with ActivityPub
92 Logger.info("Couldn't get object via AP, trying out OStatus fetching...")
93
94 # FIXME: OStatus Object Containment?
95 case OStatus.fetch_activity_from_url(id) do
96 {:ok, [activity | _]} -> {:ok, Object.normalize(activity, false)}
97 e -> e
98 end
99 end
100 end
101
102 defp prepare_activity_params(data) do
103 %{
104 "type" => "Create",
105 "to" => data["to"],
106 "cc" => data["cc"],
107 # Should we seriously keep this attributedTo thing?
108 "actor" => data["actor"] || data["attributedTo"],
109 "object" => data
110 }
111 end
112
113 def fetch_object_from_id!(id, options \\ []) do
114 with {:ok, object} <- fetch_object_from_id(id, options) do
115 object
116 else
117 _e ->
118 nil
119 end
120 end
121
122 defp make_signature(id, date) do
123 uri = URI.parse(id)
124
125 signature =
126 InternalFetchActor.get_actor()
127 |> Signature.sign(%{
128 "(request-target)": "get #{uri.path}",
129 host: uri.host,
130 date: date
131 })
132
133 [{:Signature, signature}]
134 end
135
136 defp sign_fetch(headers, id, date) do
137 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
138 headers ++ make_signature(id, date)
139 else
140 headers
141 end
142 end
143
144 defp maybe_date_fetch(headers, date) do
145 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
146 headers ++ [{:Date, date}]
147 else
148 headers
149 end
150 end
151
152 def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
153 Logger.info("Fetching object #{id} via AP")
154
155 date = Pleroma.Signature.signed_date()
156
157 headers =
158 [{:Accept, "application/activity+json"}]
159 |> maybe_date_fetch(date)
160 |> sign_fetch(id, date)
161
162 Logger.debug("Fetch headers: #{inspect(headers)}")
163
164 with true <- String.starts_with?(id, "http"),
165 {:ok, %{body: body, status: code}} when code in 200..299 <- HTTP.get(id, headers),
166 {:ok, data} <- Jason.decode(body),
167 :ok <- Containment.contain_origin_from_id(id, data) do
168 {:ok, data}
169 else
170 {:ok, %{status: code}} when code in [404, 410] ->
171 {:error, "Object has been deleted"}
172
173 e ->
174 {:error, e}
175 end
176 end
177
178 def fetch_and_contain_remote_object_from_id(%{"id" => id}),
179 do: fetch_and_contain_remote_object_from_id(id)
180
181 def fetch_and_contain_remote_object_from_id(_id), do: {:error, "id must be a string"}
182 end