Merge branch 'develop' into match-file-name
[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.Web.ActivityPub.Transmogrifier
10 alias Pleroma.Web.OStatus
11
12 require Logger
13
14 defp reinject_object(data) do
15 Logger.debug("Reinjecting object #{data["id"]}")
16
17 with data <- Transmogrifier.fix_object(data),
18 {:ok, object} <- Object.create(data) do
19 {:ok, object}
20 else
21 e ->
22 Logger.error("Error while processing object: #{inspect(e)}")
23 {:error, e}
24 end
25 end
26
27 # TODO:
28 # This will create a Create activity, which we need internally at the moment.
29 def fetch_object_from_id(id, options \\ []) do
30 if object = Object.get_cached_by_ap_id(id) do
31 {:ok, object}
32 else
33 Logger.info("Fetching #{id} via AP")
34
35 with {:fetch, {:ok, data}} <- {:fetch, fetch_and_contain_remote_object_from_id(id)},
36 {:normalize, nil} <- {:normalize, Object.normalize(data, false)},
37 params <- %{
38 "type" => "Create",
39 "to" => data["to"],
40 "cc" => data["cc"],
41 # Should we seriously keep this attributedTo thing?
42 "actor" => data["actor"] || data["attributedTo"],
43 "object" => data
44 },
45 {:containment, :ok} <- {:containment, Containment.contain_origin(id, params)},
46 {:ok, activity} <- Transmogrifier.handle_incoming(params, options),
47 {:object, _data, %Object{} = object} <-
48 {:object, data, Object.normalize(activity, false)} do
49 {:ok, object}
50 else
51 {:containment, _} ->
52 {:error, "Object containment failed."}
53
54 {:error, {:reject, nil}} ->
55 {:reject, nil}
56
57 {:object, data, nil} ->
58 reinject_object(data)
59
60 {:normalize, object = %Object{}} ->
61 {:ok, object}
62
63 _e ->
64 # Only fallback when receiving a fetch/normalization error with ActivityPub
65 Logger.info("Couldn't get object via AP, trying out OStatus fetching...")
66
67 # FIXME: OStatus Object Containment?
68 case OStatus.fetch_activity_from_url(id) do
69 {:ok, [activity | _]} -> {:ok, Object.normalize(activity, false)}
70 e -> e
71 end
72 end
73 end
74 end
75
76 def fetch_object_from_id!(id, options \\ []) do
77 with {:ok, object} <- fetch_object_from_id(id, options) do
78 object
79 else
80 _e ->
81 nil
82 end
83 end
84
85 def fetch_and_contain_remote_object_from_id(id) do
86 Logger.info("Fetching object #{id} via AP")
87
88 with true <- String.starts_with?(id, "http"),
89 {:ok, %{body: body, status: code}} when code in 200..299 <-
90 HTTP.get(
91 id,
92 [{:Accept, "application/activity+json"}]
93 ),
94 {:ok, data} <- Jason.decode(body),
95 :ok <- Containment.contain_origin_from_id(id, data) do
96 {:ok, data}
97 else
98 {:ok, %{status: code}} when code in [404, 410] ->
99 {:error, "Object has been deleted"}
100
101 e ->
102 {:error, e}
103 end
104 end
105 end