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 defp recipient_in_collection(ap_id, coll) when is_binary(coll), do: ap_id == coll
29 defp recipient_in_collection(ap_id, coll) when is_list(coll), do: ap_id in coll
30 defp recipient_in_collection(_, _), do: false
32 def recipient_in_message(ap_id, params) do
34 recipient_in_collection(ap_id, params["to"]) ->
37 recipient_in_collection(ap_id, params["cc"]) ->
40 recipient_in_collection(ap_id, params["bto"]) ->
43 recipient_in_collection(ap_id, params["bcc"]) ->
46 # if the message is unaddressed at all, then assume it is directly addressed
48 !params["to"] && !params["cc"] && !params["bto"] && !params["bcc"] ->
56 defp extract_list(target) when is_binary(target), do: [target]
57 defp extract_list(lst) when is_list(lst), do: lst
58 defp extract_list(_), do: []
60 def maybe_splice_recipient(ap_id, params) do
62 !recipient_in_collection(ap_id, params["to"]) &&
63 !recipient_in_collection(ap_id, params["cc"])
65 cc_list = extract_list(params["cc"])
69 |> Map.put("cc", [ap_id | cc_list])
75 def make_json_ld_header do
78 "https://www.w3.org/ns/activitystreams",
79 "#{Web.base_url()}/schemas/litepub-0.1.jsonld"
85 DateTime.utc_now() |> DateTime.to_iso8601()
88 def generate_activity_id do
89 generate_id("activities")
92 def generate_context_id do
93 generate_id("contexts")
96 def generate_object_id do
97 Helpers.o_status_url(Endpoint, :object, UUID.generate())
100 def generate_id(type) do
101 "#{Web.base_url()}/#{type}/#{UUID.generate()}"
104 def get_notified_from_object(%{"type" => type} = object) when type in @supported_object_types do
105 fake_create_activity = %{
106 "to" => object["to"],
107 "cc" => object["cc"],
112 Notification.get_notified_from_activity(%Activity{data: fake_create_activity}, false)
115 def get_notified_from_object(object) do
116 Notification.get_notified_from_activity(%Activity{data: object}, false)
119 def create_context(context) do
120 context = context || generate_id("contexts")
121 changeset = Object.context_mapping(context)
123 case Repo.insert(changeset) do
127 # This should be solved by an upsert, but it seems ecto
128 # has problems accessing the constraint inside the jsonb.
130 Object.get_cached_by_ap_id(context)
135 Enqueues an activity for federation if it's local
137 def maybe_federate(%Activity{local: true} = activity) do
139 case activity.data["type"] do
145 Pleroma.Web.Federator.enqueue(:publish, activity, priority)
149 def maybe_federate(_), do: :ok
152 Adds an id and a published data if they aren't there,
153 also adds it to an included object
155 def lazy_put_activity_defaults(map) do
156 %{data: %{"id" => context}, id: context_id} = create_context(map["context"])
160 |> Map.put_new_lazy("id", &generate_activity_id/0)
161 |> Map.put_new_lazy("published", &make_date/0)
162 |> Map.put_new("context", context)
163 |> Map.put_new("context_id", context_id)
165 if is_map(map["object"]) do
166 object = lazy_put_object_defaults(map["object"], map)
167 %{map | "object" => object}
174 Adds an id and published date if they aren't there.
176 def lazy_put_object_defaults(map, activity \\ %{}) do
178 |> Map.put_new_lazy("id", &generate_object_id/0)
179 |> Map.put_new_lazy("published", &make_date/0)
180 |> Map.put_new("context", activity["context"])
181 |> Map.put_new("context_id", activity["context_id"])
185 Inserts a full object if it is contained in an activity.
187 def insert_full_object(%{"object" => %{"type" => type} = object_data})
188 when is_map(object_data) and type in @supported_object_types do
189 with {:ok, _} <- Object.create(object_data) do
194 def insert_full_object(_), do: :ok
196 def update_object_in_activities(%{data: %{"id" => id}} = object) do
198 # Update activities that already had this. Could be done in a seperate process.
199 # Alternatively, just don't do this and fetch the current object each time. Most
200 # could probably be taken from cache.
201 relevant_activities = Activity.all_by_object_ap_id(id)
203 Enum.map(relevant_activities, fn activity ->
204 new_activity_data = activity.data |> Map.put("object", object.data)
205 changeset = Changeset.change(activity, data: new_activity_data)
206 Repo.update(changeset)
210 #### Like-related helpers
213 Returns an existing like if a user already liked an object
215 def get_existing_like(actor, %{data: %{"id" => id}}) do
218 activity in Activity,
219 where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
220 # this is to use the index
223 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
228 where: fragment("(?)->>'type' = 'Like'", activity.data)
235 Returns like activities targeting an object
237 def get_object_likes(%{data: %{"id" => id}}) do
240 activity in Activity,
241 # this is to use the index
244 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
249 where: fragment("(?)->>'type' = 'Like'", activity.data)
255 def make_like_data(%User{ap_id: ap_id} = actor, %{data: %{"id" => id}} = object, activity_id) do
260 "to" => [actor.follower_address, object.data["actor"]],
261 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
262 "context" => object.data["context"]
265 if activity_id, do: Map.put(data, "id", activity_id), else: data
268 def update_element_in_object(property, element, object) do
271 |> Map.put("#{property}_count", length(element))
272 |> Map.put("#{property}s", element),
273 changeset <- Changeset.change(object, data: new_data),
274 {:ok, object} <- Repo.update(changeset),
275 _ <- update_object_in_activities(object) do
280 def update_likes_in_object(likes, object) do
281 update_element_in_object("like", likes, object)
284 def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
285 likes = if is_list(object.data["likes"]), do: object.data["likes"], else: []
287 with likes <- [actor | likes] |> Enum.uniq() do
288 update_likes_in_object(likes, object)
292 def remove_like_from_object(%Activity{data: %{"actor" => actor}}, object) do
293 likes = if is_list(object.data["likes"]), do: object.data["likes"], else: []
295 with likes <- likes |> List.delete(actor) do
296 update_likes_in_object(likes, object)
300 #### Follow-related helpers
303 Updates a follow activity's state (for locked accounts).
305 def update_follow_state(%Activity{} = activity, state) do
308 |> Map.put("state", state),
309 changeset <- Changeset.change(activity, data: new_data),
310 {:ok, activity} <- Repo.update(changeset) do
316 Makes a follow activity data for the given follower and followed
318 def make_follow_data(
319 %User{ap_id: follower_id},
320 %User{ap_id: followed_id} = _followed,
325 "actor" => follower_id,
326 "to" => [followed_id],
327 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
328 "object" => followed_id,
332 data = if activity_id, do: Map.put(data, "id", activity_id), else: data
337 def fetch_latest_follow(%User{ap_id: follower_id}, %User{ap_id: followed_id}) do
340 activity in Activity,
343 "? ->> 'type' = 'Follow'",
346 where: activity.actor == ^follower_id,
351 ^%{object: followed_id}
353 order_by: [desc: :id],
360 #### Announce-related helpers
363 Retruns an existing announce activity if the notice has already been announced
365 def get_existing_announce(actor, %{data: %{"id" => id}}) do
368 activity in Activity,
369 where: activity.actor == ^actor,
370 # this is to use the index
373 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
378 where: fragment("(?)->>'type' = 'Announce'", activity.data)
385 Make announce activity data for the given actor and object
387 # for relayed messages, we only want to send to subscribers
388 def make_announce_data(
389 %User{ap_id: ap_id, nickname: nil} = user,
390 %Object{data: %{"id" => id}} = object,
394 "type" => "Announce",
397 "to" => [user.follower_address],
399 "context" => object.data["context"]
402 if activity_id, do: Map.put(data, "id", activity_id), else: data
405 def make_announce_data(
406 %User{ap_id: ap_id} = user,
407 %Object{data: %{"id" => id}} = object,
411 "type" => "Announce",
414 "to" => [user.follower_address, object.data["actor"]],
415 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
416 "context" => object.data["context"]
419 if activity_id, do: Map.put(data, "id", activity_id), else: data
423 Make unannounce activity data for the given actor and object
425 def make_unannounce_data(
426 %User{ap_id: ap_id} = user,
427 %Activity{data: %{"context" => context}} = activity,
433 "object" => activity.data,
434 "to" => [user.follower_address, activity.data["actor"]],
435 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
439 if activity_id, do: Map.put(data, "id", activity_id), else: data
442 def make_unlike_data(
443 %User{ap_id: ap_id} = user,
444 %Activity{data: %{"context" => context}} = activity,
450 "object" => activity.data,
451 "to" => [user.follower_address, activity.data["actor"]],
452 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
456 if activity_id, do: Map.put(data, "id", activity_id), else: data
459 def add_announce_to_object(
461 data: %{"actor" => actor, "cc" => ["https://www.w3.org/ns/activitystreams#Public"]}
466 if is_list(object.data["announcements"]), do: object.data["announcements"], else: []
468 with announcements <- [actor | announcements] |> Enum.uniq() do
469 update_element_in_object("announcement", announcements, object)
473 def add_announce_to_object(_, object), do: {:ok, object}
475 def remove_announce_from_object(%Activity{data: %{"actor" => actor}}, object) do
477 if is_list(object.data["announcements"]), do: object.data["announcements"], else: []
479 with announcements <- announcements |> List.delete(actor) do
480 update_element_in_object("announcement", announcements, object)
484 #### Unfollow-related helpers
486 def make_unfollow_data(follower, followed, follow_activity, activity_id) do
489 "actor" => follower.ap_id,
490 "to" => [followed.ap_id],
491 "object" => follow_activity.data
494 if activity_id, do: Map.put(data, "id", activity_id), else: data
497 #### Block-related helpers
498 def fetch_latest_block(%User{ap_id: blocker_id}, %User{ap_id: blocked_id}) do
501 activity in Activity,
504 "? ->> 'type' = 'Block'",
507 where: activity.actor == ^blocker_id,
512 ^%{object: blocked_id}
514 order_by: [desc: :id],
521 def make_block_data(blocker, blocked, activity_id) do
524 "actor" => blocker.ap_id,
525 "to" => [blocked.ap_id],
526 "object" => blocked.ap_id
529 if activity_id, do: Map.put(data, "id", activity_id), else: data
532 def make_unblock_data(blocker, blocked, block_activity, activity_id) do
535 "actor" => blocker.ap_id,
536 "to" => [blocked.ap_id],
537 "object" => block_activity.data
540 if activity_id, do: Map.put(data, "id", activity_id), else: data
543 #### Create-related helpers
545 def make_create_data(params, additional) do
546 published = params.published || make_date()
550 "to" => params.to |> Enum.uniq(),
551 "actor" => params.actor.ap_id,
552 "object" => params.object,
553 "published" => published,
554 "context" => params.context
556 |> Map.merge(additional)