Merge branch 'feature/move-activity' 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
14 require Logger
15 require Pleroma.Constants
16
17 defp touch_changeset(changeset) do
18 updated_at =
19 NaiveDateTime.utc_now()
20 |> NaiveDateTime.truncate(:second)
21
22 Ecto.Changeset.put_change(changeset, :updated_at, updated_at)
23 end
24
25 defp maybe_reinject_internal_fields(data, %{data: %{} = old_data}) do
26 internal_fields = Map.take(old_data, Pleroma.Constants.object_internal_fields())
27
28 Map.merge(data, internal_fields)
29 end
30
31 defp maybe_reinject_internal_fields(data, _), do: data
32
33 @spec reinject_object(struct(), map()) :: {:ok, Object.t()} | {:error, any()}
34 defp reinject_object(struct, data) do
35 Logger.debug("Reinjecting object #{data["id"]}")
36
37 with data <- Transmogrifier.fix_object(data),
38 data <- maybe_reinject_internal_fields(data, struct),
39 changeset <- Object.change(struct, %{data: data}),
40 changeset <- touch_changeset(changeset),
41 {:ok, object} <- Repo.insert_or_update(changeset),
42 {:ok, object} <- Object.set_cache(object) 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, Object.local?(object)},
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} -> {:ok, 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 {:transmogrifier, {:ok, activity}} <-
71 {:transmogrifier, Transmogrifier.handle_incoming(params, options)},
72 {:object, _data, %Object{} = object} <-
73 {:object, data, Object.normalize(activity, false)} do
74 {:ok, object}
75 else
76 {:containment, _} ->
77 {:error, "Object containment failed."}
78
79 {:transmogrifier, {:error, {:reject, nil}}} ->
80 {:reject, nil}
81
82 {:transmogrifier, _} ->
83 {:error, "Transmogrifier failure."}
84
85 {:object, data, nil} ->
86 reinject_object(%Object{}, data)
87
88 {:normalize, object = %Object{}} ->
89 {:ok, object}
90
91 {:fetch_object, %Object{} = object} ->
92 {:ok, object}
93
94 {:fetch, {:error, error}} ->
95 {:error, error}
96
97 e ->
98 e
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 {:error, %Tesla.Mock.Error{}} ->
118 nil
119
120 e ->
121 Logger.error("Error while fetching #{id}: #{inspect(e)}")
122 nil
123 end
124 end
125
126 defp make_signature(id, date) do
127 uri = URI.parse(id)
128
129 signature =
130 InternalFetchActor.get_actor()
131 |> Signature.sign(%{
132 "(request-target)": "get #{uri.path}",
133 host: uri.host,
134 date: date
135 })
136
137 [{:Signature, signature}]
138 end
139
140 defp sign_fetch(headers, id, date) do
141 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
142 headers ++ make_signature(id, date)
143 else
144 headers
145 end
146 end
147
148 defp maybe_date_fetch(headers, date) do
149 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
150 headers ++ [{:Date, date}]
151 else
152 headers
153 end
154 end
155
156 def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
157 Logger.info("Fetching object #{id} via AP")
158
159 date = Pleroma.Signature.signed_date()
160
161 headers =
162 [{:Accept, "application/activity+json"}]
163 |> maybe_date_fetch(date)
164 |> sign_fetch(id, date)
165
166 Logger.debug("Fetch headers: #{inspect(headers)}")
167
168 with {:scheme, true} <- {:scheme, String.starts_with?(id, "http")},
169 {:ok, %{body: body, status: code}} when code in 200..299 <- HTTP.get(id, headers),
170 {:ok, data} <- Jason.decode(body),
171 :ok <- Containment.contain_origin_from_id(id, data) do
172 {:ok, data}
173 else
174 {:ok, %{status: code}} when code in [404, 410] ->
175 {:error, "Object has been deleted"}
176
177 {:scheme, _} ->
178 {:error, "Unsupported URI scheme"}
179
180 {:error, e} ->
181 {:error, e}
182
183 e ->
184 {:error, e}
185 end
186 end
187
188 def fetch_and_contain_remote_object_from_id(%{"id" => id}),
189 do: fetch_and_contain_remote_object_from_id(id)
190
191 def fetch_and_contain_remote_object_from_id(_id), do: {:error, "id must be a string"}
192 end