Gitlab: Run benchmark in CI.
[akkoma] / lib / pleroma / web / federator / federator.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.Web.Federator do
6 alias Pleroma.Activity
7 alias Pleroma.Object.Containment
8 alias Pleroma.User
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.ActivityPub.Transmogrifier
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Web.Federator.Publisher
13 alias Pleroma.Web.Federator.RetryQueue
14 alias Pleroma.Web.OStatus
15 alias Pleroma.Web.Websub
16
17 require Logger
18
19 def init do
20 # 1 minute
21 Process.sleep(1000 * 60)
22 refresh_subscriptions()
23 end
24
25 @doc "Addresses [memory leaks on recursive replies fetching](https://git.pleroma.social/pleroma/pleroma/issues/161)"
26 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
27 def allowed_incoming_reply_depth?(depth) do
28 max_replies_depth = Pleroma.Config.get([:instance, :federation_incoming_replies_max_depth])
29
30 if max_replies_depth do
31 (depth || 1) <= max_replies_depth
32 else
33 true
34 end
35 end
36
37 # Client API
38
39 def incoming_doc(doc) do
40 PleromaJobQueue.enqueue(:federator_incoming, __MODULE__, [:incoming_doc, doc])
41 end
42
43 def incoming_ap_doc(params) do
44 PleromaJobQueue.enqueue(:federator_incoming, __MODULE__, [:incoming_ap_doc, params])
45 end
46
47 def publish(activity, priority \\ 1) do
48 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:publish, activity], priority)
49 end
50
51 def verify_websub(websub) do
52 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:verify_websub, websub])
53 end
54
55 def request_subscription(sub) do
56 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:request_subscription, sub])
57 end
58
59 def refresh_subscriptions do
60 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:refresh_subscriptions])
61 end
62
63 # Job Worker Callbacks
64
65 def perform(:refresh_subscriptions) do
66 Logger.debug("Federator running refresh subscriptions")
67 Websub.refresh_subscriptions()
68
69 spawn(fn ->
70 # 6 hours
71 Process.sleep(1000 * 60 * 60 * 6)
72 refresh_subscriptions()
73 end)
74 end
75
76 def perform(:request_subscription, websub) do
77 Logger.debug("Refreshing #{websub.topic}")
78
79 with {:ok, websub} <- Websub.request_subscription(websub) do
80 Logger.debug("Successfully refreshed #{websub.topic}")
81 else
82 _e -> Logger.debug("Couldn't refresh #{websub.topic}")
83 end
84 end
85
86 def perform(:publish, activity) do
87 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
88
89 with %User{} = actor <- User.get_cached_by_ap_id(activity.data["actor"]),
90 {:ok, actor} <- User.ensure_keys_present(actor) do
91 Publisher.publish(actor, activity)
92 end
93 end
94
95 def perform(:verify_websub, websub) do
96 Logger.debug(fn ->
97 "Running WebSub verification for #{websub.id} (#{websub.topic}, #{websub.callback})"
98 end)
99
100 Websub.verify(websub)
101 end
102
103 def perform(:incoming_doc, doc) do
104 Logger.info("Got document, trying to parse")
105 OStatus.handle_incoming(doc)
106 end
107
108 def perform(:incoming_ap_doc, params) do
109 Logger.info("Handling incoming AP activity")
110
111 params = Utils.normalize_params(params)
112
113 # NOTE: we use the actor ID to do the containment, this is fine because an
114 # actor shouldn't be acting on objects outside their own AP server.
115 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
116 nil <- Activity.normalize(params["id"]),
117 :ok <- Containment.contain_origin_from_id(params["actor"], params),
118 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
119 {:ok, activity}
120 else
121 %Activity{} ->
122 Logger.info("Already had #{params["id"]}")
123 :error
124
125 _e ->
126 # Just drop those for now
127 Logger.info("Unhandled activity")
128 Logger.info(Jason.encode!(params, pretty: true))
129 :error
130 end
131 end
132
133 def perform(
134 :publish_single_websub,
135 %{xml: _xml, topic: _topic, callback: _callback, secret: _secret} = params
136 ) do
137 case Websub.publish_one(params) do
138 {:ok, _} ->
139 :ok
140
141 {:error, _} ->
142 RetryQueue.enqueue(params, Websub)
143 end
144 end
145
146 def perform(type, _) do
147 Logger.debug(fn -> "Unknown task: #{type}" end)
148 {:error, "Don't know what to do with this"}
149 end
150
151 def ap_enabled_actor(id) do
152 user = User.get_cached_by_ap_id(id)
153
154 if User.ap_enabled?(user) do
155 {:ok, user}
156 else
157 ActivityPub.make_user_from_ap_id(id)
158 end
159 end
160 end