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