1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.Utils do
6 alias Pleroma.{Repo, Web, Object, Activity, User, Notification}
7 alias Pleroma.Web.Router.Helpers
8 alias Pleroma.Web.Endpoint
9 alias Ecto.{Changeset, UUID}
13 @supported_object_types ["Article", "Note", "Video", "Page"]
15 # Some implementations send the actor URI as the actor field, others send the entire actor object,
16 # so figure out what the actor's URI is based on what we have.
17 def get_ap_id(object) do
24 def normalize_params(params) do
25 Map.put(params, "actor", get_ap_id(params["actor"]))
28 def determine_explicit_mentions(%{"tag" => tag} = _object) when is_list(tag) do
30 |> Enum.filter(fn x -> is_map(x) end)
31 |> Enum.filter(fn x -> x["type"] == "Mention" end)
32 |> Enum.map(fn x -> x["href"] end)
35 def determine_explicit_mentions(%{"tag" => tag} = object) when is_map(tag) do
36 Map.put(object, "tag", [tag])
37 |> determine_explicit_mentions()
40 def determine_explicit_mentions(_), do: []
42 defp recipient_in_collection(ap_id, coll) when is_binary(coll), do: ap_id == coll
43 defp recipient_in_collection(ap_id, coll) when is_list(coll), do: ap_id in coll
44 defp recipient_in_collection(_, _), do: false
46 def recipient_in_message(ap_id, params) do
48 recipient_in_collection(ap_id, params["to"]) ->
51 recipient_in_collection(ap_id, params["cc"]) ->
54 recipient_in_collection(ap_id, params["bto"]) ->
57 recipient_in_collection(ap_id, params["bcc"]) ->
60 # if the message is unaddressed at all, then assume it is directly addressed
62 !params["to"] && !params["cc"] && !params["bto"] && !params["bcc"] ->
70 defp extract_list(target) when is_binary(target), do: [target]
71 defp extract_list(lst) when is_list(lst), do: lst
72 defp extract_list(_), do: []
74 def maybe_splice_recipient(ap_id, params) do
76 !recipient_in_collection(ap_id, params["to"]) &&
77 !recipient_in_collection(ap_id, params["cc"])
79 cc_list = extract_list(params["cc"])
83 |> Map.put("cc", [ap_id | cc_list])
89 def make_json_ld_header do
92 "https://www.w3.org/ns/activitystreams",
93 "#{Web.base_url()}/schemas/litepub-0.1.jsonld"
99 DateTime.utc_now() |> DateTime.to_iso8601()
102 def generate_activity_id do
103 generate_id("activities")
106 def generate_context_id do
107 generate_id("contexts")
110 def generate_object_id do
111 Helpers.o_status_url(Endpoint, :object, UUID.generate())
114 def generate_id(type) do
115 "#{Web.base_url()}/#{type}/#{UUID.generate()}"
118 def get_notified_from_object(%{"type" => type} = object) when type in @supported_object_types do
119 fake_create_activity = %{
120 "to" => object["to"],
121 "cc" => object["cc"],
126 Notification.get_notified_from_activity(%Activity{data: fake_create_activity}, false)
129 def get_notified_from_object(object) do
130 Notification.get_notified_from_activity(%Activity{data: object}, false)
133 def create_context(context) do
134 context = context || generate_id("contexts")
135 changeset = Object.context_mapping(context)
137 case Repo.insert(changeset) do
141 # This should be solved by an upsert, but it seems ecto
142 # has problems accessing the constraint inside the jsonb.
144 Object.get_cached_by_ap_id(context)
149 Enqueues an activity for federation if it's local
151 def maybe_federate(%Activity{local: true} = activity) do
153 case activity.data["type"] do
159 Pleroma.Web.Federator.enqueue(:publish, activity, priority)
163 def maybe_federate(_), do: :ok
166 Adds an id and a published data if they aren't there,
167 also adds it to an included object
169 def lazy_put_activity_defaults(map) do
170 %{data: %{"id" => context}, id: context_id} = create_context(map["context"])
174 |> Map.put_new_lazy("id", &generate_activity_id/0)
175 |> Map.put_new_lazy("published", &make_date/0)
176 |> Map.put_new("context", context)
177 |> Map.put_new("context_id", context_id)
179 if is_map(map["object"]) do
180 object = lazy_put_object_defaults(map["object"], map)
181 %{map | "object" => object}
188 Adds an id and published date if they aren't there.
190 def lazy_put_object_defaults(map, activity \\ %{}) do
192 |> Map.put_new_lazy("id", &generate_object_id/0)
193 |> Map.put_new_lazy("published", &make_date/0)
194 |> Map.put_new("context", activity["context"])
195 |> Map.put_new("context_id", activity["context_id"])
199 Inserts a full object if it is contained in an activity.
201 def insert_full_object(%{"object" => %{"type" => type} = object_data})
202 when is_map(object_data) and type in @supported_object_types do
203 with {:ok, _} <- Object.create(object_data) do
208 def insert_full_object(_), do: :ok
210 def update_object_in_activities(%{data: %{"id" => id}} = object) do
212 # Update activities that already had this. Could be done in a seperate process.
213 # Alternatively, just don't do this and fetch the current object each time. Most
214 # could probably be taken from cache.
215 relevant_activities = Activity.all_by_object_ap_id(id)
217 Enum.map(relevant_activities, fn activity ->
218 new_activity_data = activity.data |> Map.put("object", object.data)
219 changeset = Changeset.change(activity, data: new_activity_data)
220 Repo.update(changeset)
224 #### Like-related helpers
227 Returns an existing like if a user already liked an object
229 def get_existing_like(actor, %{data: %{"id" => id}}) do
232 activity in Activity,
233 where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
234 # this is to use the index
237 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
242 where: fragment("(?)->>'type' = 'Like'", activity.data)
249 Returns like activities targeting an object
251 def get_object_likes(%{data: %{"id" => id}}) do
254 activity in Activity,
255 # this is to use the index
258 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
263 where: fragment("(?)->>'type' = 'Like'", activity.data)
269 def make_like_data(%User{ap_id: ap_id} = actor, %{data: %{"id" => id}} = object, activity_id) do
274 "to" => [actor.follower_address, object.data["actor"]],
275 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
276 "context" => object.data["context"]
279 if activity_id, do: Map.put(data, "id", activity_id), else: data
282 def update_element_in_object(property, element, object) do
285 |> Map.put("#{property}_count", length(element))
286 |> Map.put("#{property}s", element),
287 changeset <- Changeset.change(object, data: new_data),
288 {:ok, object} <- Repo.update(changeset),
289 _ <- update_object_in_activities(object) do
294 def update_likes_in_object(likes, object) do
295 update_element_in_object("like", likes, object)
298 def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
299 likes = if is_list(object.data["likes"]), do: object.data["likes"], else: []
301 with likes <- [actor | likes] |> Enum.uniq() do
302 update_likes_in_object(likes, object)
306 def remove_like_from_object(%Activity{data: %{"actor" => actor}}, object) do
307 likes = if is_list(object.data["likes"]), do: object.data["likes"], else: []
309 with likes <- likes |> List.delete(actor) do
310 update_likes_in_object(likes, object)
314 #### Follow-related helpers
317 Updates a follow activity's state (for locked accounts).
319 def update_follow_state(%Activity{} = activity, state) do
322 |> Map.put("state", state),
323 changeset <- Changeset.change(activity, data: new_data),
324 {:ok, activity} <- Repo.update(changeset) do
330 Makes a follow activity data for the given follower and followed
332 def make_follow_data(
333 %User{ap_id: follower_id},
334 %User{ap_id: followed_id} = _followed,
339 "actor" => follower_id,
340 "to" => [followed_id],
341 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
342 "object" => followed_id,
346 data = if activity_id, do: Map.put(data, "id", activity_id), else: data
351 def fetch_latest_follow(%User{ap_id: follower_id}, %User{ap_id: followed_id}) do
354 activity in Activity,
357 "? ->> 'type' = 'Follow'",
360 where: activity.actor == ^follower_id,
365 ^%{object: followed_id}
367 order_by: [desc: :id],
374 #### Announce-related helpers
377 Retruns an existing announce activity if the notice has already been announced
379 def get_existing_announce(actor, %{data: %{"id" => id}}) do
382 activity in Activity,
383 where: activity.actor == ^actor,
384 # this is to use the index
387 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
392 where: fragment("(?)->>'type' = 'Announce'", activity.data)
399 Make announce activity data for the given actor and object
401 # for relayed messages, we only want to send to subscribers
402 def make_announce_data(
403 %User{ap_id: ap_id} = user,
404 %Object{data: %{"id" => id}} = object,
409 "type" => "Announce",
412 "to" => [user.follower_address],
414 "context" => object.data["context"]
417 if activity_id, do: Map.put(data, "id", activity_id), else: data
420 def make_announce_data(
421 %User{ap_id: ap_id} = user,
422 %Object{data: %{"id" => id}} = object,
427 "type" => "Announce",
430 "to" => [user.follower_address, object.data["actor"]],
431 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
432 "context" => object.data["context"]
435 if activity_id, do: Map.put(data, "id", activity_id), else: data
439 Make unannounce activity data for the given actor and object
441 def make_unannounce_data(
442 %User{ap_id: ap_id} = user,
443 %Activity{data: %{"context" => context}} = activity,
449 "object" => activity.data,
450 "to" => [user.follower_address, activity.data["actor"]],
451 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
455 if activity_id, do: Map.put(data, "id", activity_id), else: data
458 def make_unlike_data(
459 %User{ap_id: ap_id} = user,
460 %Activity{data: %{"context" => context}} = activity,
466 "object" => activity.data,
467 "to" => [user.follower_address, activity.data["actor"]],
468 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
472 if activity_id, do: Map.put(data, "id", activity_id), else: data
475 def add_announce_to_object(
477 data: %{"actor" => actor, "cc" => ["https://www.w3.org/ns/activitystreams#Public"]}
482 if is_list(object.data["announcements"]), do: object.data["announcements"], else: []
484 with announcements <- [actor | announcements] |> Enum.uniq() do
485 update_element_in_object("announcement", announcements, object)
489 def add_announce_to_object(_, object), do: {:ok, object}
491 def remove_announce_from_object(%Activity{data: %{"actor" => actor}}, object) do
493 if is_list(object.data["announcements"]), do: object.data["announcements"], else: []
495 with announcements <- announcements |> List.delete(actor) do
496 update_element_in_object("announcement", announcements, object)
500 #### Unfollow-related helpers
502 def make_unfollow_data(follower, followed, follow_activity, activity_id) do
505 "actor" => follower.ap_id,
506 "to" => [followed.ap_id],
507 "object" => follow_activity.data
510 if activity_id, do: Map.put(data, "id", activity_id), else: data
513 #### Block-related helpers
514 def fetch_latest_block(%User{ap_id: blocker_id}, %User{ap_id: blocked_id}) do
517 activity in Activity,
520 "? ->> 'type' = 'Block'",
523 where: activity.actor == ^blocker_id,
528 ^%{object: blocked_id}
530 order_by: [desc: :id],
537 def make_block_data(blocker, blocked, activity_id) do
540 "actor" => blocker.ap_id,
541 "to" => [blocked.ap_id],
542 "object" => blocked.ap_id
545 if activity_id, do: Map.put(data, "id", activity_id), else: data
548 def make_unblock_data(blocker, blocked, block_activity, activity_id) do
551 "actor" => blocker.ap_id,
552 "to" => [blocked.ap_id],
553 "object" => block_activity.data
556 if activity_id, do: Map.put(data, "id", activity_id), else: data
559 #### Create-related helpers
561 def make_create_data(params, additional) do
562 published = params.published || make_date()
566 "to" => params.to |> Enum.uniq(),
567 "actor" => params.actor.ap_id,
568 "object" => params.object,
569 "published" => published,
570 "context" => params.context
572 |> Map.merge(additional)