7c9ddcfe750de84bd604d218e7d2b17c31e9d6a3
[akkoma] / lib / pleroma / web / activity_pub / activity_pub.ex
1 defmodule Pleroma.Web.ActivityPub.ActivityPub do
2 alias Pleroma.{Activity, Repo, Object, Upload, User, Notification}
3 alias Pleroma.Web.ActivityPub.Transmogrifier
4 import Ecto.Query
5 import Pleroma.Web.ActivityPub.Utils
6 require Logger
7
8 @httpoison Application.get_env(:pleroma, :httpoison)
9
10 def get_recipients(data) do
11 (data["to"] || []) ++ (data["cc"] || [])
12 end
13
14 def insert(map, local \\ true) when is_map(map) do
15 with nil <- Activity.get_by_ap_id(map["id"]),
16 map <- lazy_put_activity_defaults(map),
17 :ok <- insert_full_object(map) do
18 {:ok, activity} = Repo.insert(%Activity{data: map, local: local, actor: map["actor"], recipients: get_recipients(map)})
19 Notification.create_notifications(activity)
20 stream_out(activity)
21 {:ok, activity}
22 else
23 %Activity{} = activity -> {:ok, activity}
24 error -> {:error, error}
25 end
26 end
27
28 def stream_out(activity) do
29 if activity.data["type"] in ["Create", "Announce"] do
30 Pleroma.Web.Streamer.stream("user", activity)
31 if Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") do
32 Pleroma.Web.Streamer.stream("public", activity)
33 if activity.local do
34 Pleroma.Web.Streamer.stream("public:local", activity)
35 end
36 end
37 end
38 end
39
40 def create(%{to: to, actor: actor, context: context, object: object} = params) do
41 additional = params[:additional] || %{}
42 local = !(params[:local] == false) # only accept false as false value
43 published = params[:published]
44
45 with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional),
46 {:ok, activity} <- insert(create_data, local),
47 :ok <- maybe_federate(activity) do
48 {:ok, activity}
49 end
50 end
51
52 def accept(%{to: to, actor: actor, object: object} = params) do
53 local = !(params[:local] == false) # only accept false as false value
54
55 with data <- %{"to" => to, "type" => "Accept", "actor" => actor, "object" => object},
56 {:ok, activity} <- insert(data, local),
57 :ok <- maybe_federate(activity) do
58 {:ok, activity}
59 end
60 end
61
62 # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
63 def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
64 with nil <- get_existing_like(ap_id, object),
65 like_data <- make_like_data(user, object, activity_id),
66 {:ok, activity} <- insert(like_data, local),
67 {:ok, object} <- add_like_to_object(activity, object),
68 :ok <- maybe_federate(activity) do
69 {:ok, activity, object}
70 else
71 %Activity{} = activity -> {:ok, activity, object}
72 error -> {:error, error}
73 end
74 end
75
76 def unlike(%User{} = actor, %Object{} = object) do
77 with %Activity{} = activity <- get_existing_like(actor.ap_id, object),
78 {:ok, _activity} <- Repo.delete(activity),
79 {:ok, object} <- remove_like_from_object(activity, object) do
80 {:ok, object}
81 else _e -> {:ok, object}
82 end
83 end
84
85 def announce(%User{ap_id: _} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
86 with announce_data <- make_announce_data(user, object, activity_id),
87 {:ok, activity} <- insert(announce_data, local),
88 {:ok, object} <- add_announce_to_object(activity, object),
89 :ok <- maybe_federate(activity) do
90 {:ok, activity, object}
91 else
92 error -> {:error, error}
93 end
94 end
95
96 def follow(follower, followed, activity_id \\ nil, local \\ true) do
97 with data <- make_follow_data(follower, followed, activity_id),
98 {:ok, activity} <- insert(data, local),
99 :ok <- maybe_federate(activity) do
100 {:ok, activity}
101 end
102 end
103
104 def unfollow(follower, followed, local \\ true) do
105 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
106 unfollow_data <- make_unfollow_data(follower, followed, follow_activity),
107 {:ok, activity} <- insert(unfollow_data, local),
108 :ok, maybe_federate(activity) do
109 {:ok, activity}
110 end
111 end
112
113 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
114 user = User.get_cached_by_ap_id(actor)
115 data = %{
116 "type" => "Delete",
117 "actor" => actor,
118 "object" => id,
119 "to" => [user.follower_address, "https://www.w3.org/ns/activitystreams#Public"]
120 }
121 with Repo.delete(object),
122 Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
123 {:ok, activity} <- insert(data, local),
124 :ok <- maybe_federate(activity) do
125 {:ok, activity}
126 end
127 end
128
129 def fetch_activities_for_context(context, opts \\ %{}) do
130 query = from activity in Activity,
131 where: fragment("?->>'type' = ? and ?->>'context' = ?", activity.data, "Create", activity.data, ^context),
132 order_by: [desc: :id]
133 query = restrict_blocked(query, opts)
134 Repo.all(query)
135 end
136
137 def fetch_public_activities(opts \\ %{}) do
138 public = ["https://www.w3.org/ns/activitystreams#Public"]
139 fetch_activities(public, opts)
140 end
141
142 defp restrict_since(query, %{"since_id" => since_id}) do
143 from activity in query, where: activity.id > ^since_id
144 end
145 defp restrict_since(query, _), do: query
146
147 defp restrict_tag(query, %{"tag" => tag}) do
148 from activity in query,
149 where: fragment("? <@ (? #> '{\"object\",\"tag\"}')", ^tag, activity.data)
150 end
151 defp restrict_tag(query, _), do: query
152
153 defp restrict_recipients(query, []), do: query
154 defp restrict_recipients(query, recipients) do
155 from activity in query,
156 where: fragment("? && ?", ^recipients, activity.recipients)
157 end
158
159 defp restrict_local(query, %{"local_only" => true}) do
160 from activity in query, where: activity.local == true
161 end
162 defp restrict_local(query, _), do: query
163
164 defp restrict_max(query, %{"max_id" => max_id}) do
165 from activity in query, where: activity.id < ^max_id
166 end
167 defp restrict_max(query, _), do: query
168
169 defp restrict_actor(query, %{"actor_id" => actor_id}) do
170 from activity in query,
171 where: activity.actor == ^actor_id
172 end
173 defp restrict_actor(query, _), do: query
174
175 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
176 restrict_type(query, %{"type" => [type]})
177 end
178 defp restrict_type(query, %{"type" => type}) do
179 from activity in query,
180 where: fragment("?->>'type' = ANY(?)", activity.data, ^type)
181 end
182 defp restrict_type(query, _), do: query
183
184 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
185 from activity in query,
186 where: fragment("? <@ (? #> '{\"object\",\"likes\"}')", ^ap_id, activity.data)
187 end
188 defp restrict_favorited_by(query, _), do: query
189
190 defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
191 from activity in query,
192 where: fragment("not (? #> '{\"object\",\"attachment\"}' = ?)", activity.data, ^[])
193 end
194 defp restrict_media(query, _), do: query
195
196 # Only search through last 100_000 activities by default
197 defp restrict_recent(query, %{"whole_db" => true}), do: query
198 defp restrict_recent(query, _) do
199 since = (Repo.aggregate(Activity, :max, :id) || 0) - 100_000
200
201 from activity in query,
202 where: activity.id > ^since
203 end
204
205 defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
206 blocks = info["blocks"] || []
207 from activity in query,
208 where: fragment("not (? = ANY(?))", activity.actor, ^blocks)
209 end
210 defp restrict_blocked(query, _), do: query
211
212 def fetch_activities(recipients, opts \\ %{}) do
213 base_query = from activity in Activity,
214 limit: 20,
215 order_by: [fragment("? desc nulls last", activity.id)]
216
217 base_query
218 |> restrict_recipients(recipients)
219 |> restrict_tag(opts)
220 |> restrict_since(opts)
221 |> restrict_local(opts)
222 |> restrict_max(opts)
223 |> restrict_actor(opts)
224 |> restrict_type(opts)
225 |> restrict_favorited_by(opts)
226 |> restrict_recent(opts)
227 |> restrict_blocked(opts)
228 |> restrict_media(opts)
229 |> Repo.all
230 |> Enum.reverse
231 end
232
233 def upload(file) do
234 data = Upload.store(file)
235 Repo.insert(%Object{data: data})
236 end
237
238 def make_user_from_ap_id(ap_id) do
239 with {:ok, %{status_code: 200, body: body}} <- @httpoison.get(ap_id, ["Accept": "application/activity+json"]),
240 {:ok, data} <- Poison.decode(body)
241 do
242 user_data = %{
243 ap_id: data["id"],
244 info: %{
245 "ap_enabled" => true,
246 "source_data" => data
247 },
248 nickname: "#{data["preferredUsername"]}@#{URI.parse(ap_id).host}",
249 name: data["name"]
250 }
251
252 User.insert_or_update_user(user_data)
253 end
254 end
255
256 def publish(actor, activity) do
257 {:ok, followers} = User.get_followers(actor)
258
259 remote_inboxes = Pleroma.Web.Salmon.remote_users(activity) ++ followers
260 |> Enum.filter(fn (user) -> User.ap_enabled?(user) end)
261 |> Enum.map(fn (%{info: %{"source_data" => data}}) ->
262 (data["endpoints"] && data["endpoints"]["sharedInbox"]) ||data["inbox"]
263 end)
264 |> Enum.uniq
265
266 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
267
268 Enum.each remote_inboxes, fn(inbox) ->
269 Logger.info("Federating #{activity.data["id"]} to #{inbox}")
270 host = URI.parse(inbox).host
271 signature = Pleroma.Web.HTTPSignatures.sign(actor, %{host: host})
272 @httpoison.post(inbox, Poison.encode!(data), [{"Content-Type", "application/activity+json"}, {"signature", signature}])
273 end
274 end
275
276 def fetch_object_from_id(id) do
277 if object = Object.get_cached_by_ap_id(id) do
278 {:ok, object}
279 else
280 with {:ok, %{body: body, status_code: code}} when code in 200..299 <- @httpoison.get(id, [Accept: "application/activity+json"], follow_redirect: true, timeout: 10000, recv_timeout: 20000),
281 {:ok, data} <- Poison.decode(body),
282 data <- Transmogrifier.fix_object(data),
283 %User{} <- User.get_or_fetch_by_ap_id(data["attributedTo"]) do
284 Object.create(data)
285 end
286 end
287 end
288 end