1 defmodule Pleroma.Web.ActivityPub.Utils do
2 alias Pleroma.{Repo, Web, Object, Activity, User, Notification}
3 alias Pleroma.Web.Router.Helpers
4 alias Pleroma.Web.Endpoint
5 alias Ecto.{Changeset, UUID}
9 # Some implementations send the actor URI as the actor field, others send the entire actor object,
10 # so figure out what the actor's URI is based on what we have.
11 def get_ap_id(object) do
18 def normalize_params(params) do
19 Map.put(params, "actor", get_ap_id(params["actor"]))
22 defp recipient_in_collection(ap_id, coll) when is_binary(coll), do: ap_id == coll
23 defp recipient_in_collection(ap_id, coll) when is_list(coll), do: ap_id in coll
24 defp recipient_in_collection(_, _), do: false
26 def recipient_in_message(ap_id, params) do
28 recipient_in_collection(ap_id, params["to"]) ->
31 recipient_in_collection(ap_id, params["cc"]) ->
34 recipient_in_collection(ap_id, params["bto"]) ->
37 recipient_in_collection(ap_id, params["bcc"]) ->
40 # if the message is unaddressed at all, then assume it is directly addressed
42 !params["to"] && !params["cc"] && !params["bto"] && !params["bcc"] ->
50 defp extract_list(target) when is_binary(target), do: [target]
51 defp extract_list(lst) when is_list(lst), do: lst
52 defp extract_list(_), do: []
54 def maybe_splice_recipient(ap_id, params) do
56 !recipient_in_collection(ap_id, params["to"]) &&
57 !recipient_in_collection(ap_id, params["cc"])
59 cc_list = extract_list(params["cc"])
63 |> Map.put("cc", [ap_id | cc_list])
69 def make_json_ld_header do
72 "https://www.w3.org/ns/activitystreams",
73 "https://litepub.github.io/litepub/context.jsonld"
79 DateTime.utc_now() |> DateTime.to_iso8601()
82 def generate_activity_id do
83 generate_id("activities")
86 def generate_context_id do
87 generate_id("contexts")
90 def generate_object_id do
91 Helpers.o_status_url(Endpoint, :object, UUID.generate())
94 def generate_id(type) do
95 "#{Web.base_url()}/#{type}/#{UUID.generate()}"
98 def get_notified_from_object(%{"type" => type} = object) when type == "Note" do
99 fake_create_activity = %{
100 "to" => object["to"],
101 "cc" => object["cc"],
106 Notification.get_notified_from_activity(%Activity{data: fake_create_activity}, false)
109 def get_notified_from_object(object) do
110 Notification.get_notified_from_activity(%Activity{data: object}, false)
113 def create_context(context) do
114 context = context || generate_id("contexts")
115 changeset = Object.context_mapping(context)
117 case Repo.insert(changeset) do
121 # This should be solved by an upsert, but it seems ecto
122 # has problems accessing the constraint inside the jsonb.
124 Object.get_cached_by_ap_id(context)
129 Enqueues an activity for federation if it's local
131 def maybe_federate(%Activity{local: true} = activity) do
133 case activity.data["type"] do
139 Pleroma.Web.Federator.enqueue(:publish, activity, priority)
143 def maybe_federate(_), do: :ok
146 Adds an id and a published data if they aren't there,
147 also adds it to an included object
149 def lazy_put_activity_defaults(map) do
150 %{data: %{"id" => context}, id: context_id} = create_context(map["context"])
154 |> Map.put_new_lazy("id", &generate_activity_id/0)
155 |> Map.put_new_lazy("published", &make_date/0)
156 |> Map.put_new("context", context)
157 |> Map.put_new("context_id", context_id)
159 if is_map(map["object"]) do
160 object = lazy_put_object_defaults(map["object"], map)
161 %{map | "object" => object}
168 Adds an id and published date if they aren't there.
170 def lazy_put_object_defaults(map, activity \\ %{}) do
172 |> Map.put_new_lazy("id", &generate_object_id/0)
173 |> Map.put_new_lazy("published", &make_date/0)
174 |> Map.put_new("context", activity["context"])
175 |> Map.put_new("context_id", activity["context_id"])
179 Inserts a full object if it is contained in an activity.
181 def insert_full_object(%{"object" => %{"type" => type} = object_data})
182 when is_map(object_data) and type in ["Article", "Note", "Video", "Page"] do
183 with {:ok, _} <- Object.create(object_data) do
188 def insert_full_object(_), do: :ok
190 def update_object_in_activities(%{data: %{"id" => id}} = object) do
192 # Update activities that already had this. Could be done in a seperate process.
193 # Alternatively, just don't do this and fetch the current object each time. Most
194 # could probably be taken from cache.
195 relevant_activities = Activity.all_by_object_ap_id(id)
197 Enum.map(relevant_activities, fn activity ->
198 new_activity_data = activity.data |> Map.put("object", object.data)
199 changeset = Changeset.change(activity, data: new_activity_data)
200 Repo.update(changeset)
204 #### Like-related helpers
207 Returns an existing like if a user already liked an object
209 def get_existing_like(actor, %{data: %{"id" => id}}) do
212 activity in Activity,
213 where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
214 # this is to use the index
217 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
222 where: fragment("(?)->>'type' = 'Like'", activity.data)
228 def make_like_data(%User{ap_id: ap_id} = actor, %{data: %{"id" => id}} = object, activity_id) do
233 "to" => [actor.follower_address, object.data["actor"]],
234 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
235 "context" => object.data["context"]
238 if activity_id, do: Map.put(data, "id", activity_id), else: data
241 def update_element_in_object(property, element, object) do
244 |> Map.put("#{property}_count", length(element))
245 |> Map.put("#{property}s", element),
246 changeset <- Changeset.change(object, data: new_data),
247 {:ok, object} <- Repo.update(changeset),
248 _ <- update_object_in_activities(object) do
253 def update_likes_in_object(likes, object) do
254 update_element_in_object("like", likes, object)
257 def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
258 likes = if is_list(object.data["likes"]), do: object.data["likes"], else: []
260 with likes <- [actor | likes] |> Enum.uniq() do
261 update_likes_in_object(likes, object)
265 def remove_like_from_object(%Activity{data: %{"actor" => actor}}, object) do
266 likes = if is_list(object.data["likes"]), do: object.data["likes"], else: []
268 with likes <- likes |> List.delete(actor) do
269 update_likes_in_object(likes, object)
273 #### Follow-related helpers
276 Updates a follow activity's state (for locked accounts).
278 def update_follow_state(%Activity{} = activity, state) do
281 |> Map.put("state", state),
282 changeset <- Changeset.change(activity, data: new_data),
283 {:ok, activity} <- Repo.update(changeset) do
289 Makes a follow activity data for the given follower and followed
291 def make_follow_data(
292 %User{ap_id: follower_id},
293 %User{ap_id: followed_id} = followed,
298 "actor" => follower_id,
299 "to" => [followed_id],
300 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
301 "object" => followed_id,
305 data = if activity_id, do: Map.put(data, "id", activity_id), else: data
310 def fetch_latest_follow(%User{ap_id: follower_id}, %User{ap_id: followed_id}) do
313 activity in Activity,
316 "? ->> 'type' = 'Follow'",
319 where: activity.actor == ^follower_id,
324 ^%{object: followed_id}
326 order_by: [desc: :id],
333 #### Announce-related helpers
336 Retruns an existing announce activity if the notice has already been announced
338 def get_existing_announce(actor, %{data: %{"id" => id}}) do
341 activity in Activity,
342 where: activity.actor == ^actor,
343 # this is to use the index
346 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
351 where: fragment("(?)->>'type' = 'Announce'", activity.data)
358 Make announce activity data for the given actor and object
360 # for relayed messages, we only want to send to subscribers
361 def make_announce_data(
362 %User{ap_id: ap_id, nickname: nil} = user,
363 %Object{data: %{"id" => id}} = object,
367 "type" => "Announce",
370 "to" => [user.follower_address],
372 "context" => object.data["context"]
375 if activity_id, do: Map.put(data, "id", activity_id), else: data
378 def make_announce_data(
379 %User{ap_id: ap_id} = user,
380 %Object{data: %{"id" => id}} = object,
384 "type" => "Announce",
387 "to" => [user.follower_address, object.data["actor"]],
388 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
389 "context" => object.data["context"]
392 if activity_id, do: Map.put(data, "id", activity_id), else: data
396 Make unannounce activity data for the given actor and object
398 def make_unannounce_data(
399 %User{ap_id: ap_id} = user,
400 %Activity{data: %{"context" => context}} = activity,
406 "object" => activity.data,
407 "to" => [user.follower_address, activity.data["actor"]],
408 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
412 if activity_id, do: Map.put(data, "id", activity_id), else: data
415 def make_unlike_data(
416 %User{ap_id: ap_id} = user,
417 %Activity{data: %{"context" => context}} = activity,
423 "object" => activity.data,
424 "to" => [user.follower_address, activity.data["actor"]],
425 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
429 if activity_id, do: Map.put(data, "id", activity_id), else: data
432 def add_announce_to_object(
434 data: %{"actor" => actor, "cc" => ["https://www.w3.org/ns/activitystreams#Public"]}
439 if is_list(object.data["announcements"]), do: object.data["announcements"], else: []
441 with announcements <- [actor | announcements] |> Enum.uniq() do
442 update_element_in_object("announcement", announcements, object)
446 def add_announce_to_object(_, object), do: {:ok, object}
448 def remove_announce_from_object(%Activity{data: %{"actor" => actor}}, object) do
450 if is_list(object.data["announcements"]), do: object.data["announcements"], else: []
452 with announcements <- announcements |> List.delete(actor) do
453 update_element_in_object("announcement", announcements, object)
457 #### Unfollow-related helpers
459 def make_unfollow_data(follower, followed, follow_activity, activity_id) do
462 "actor" => follower.ap_id,
463 "to" => [followed.ap_id],
464 "object" => follow_activity.data
467 if activity_id, do: Map.put(data, "id", activity_id), else: data
470 #### Block-related helpers
471 def fetch_latest_block(%User{ap_id: blocker_id}, %User{ap_id: blocked_id}) do
474 activity in Activity,
477 "? ->> 'type' = 'Block'",
480 where: activity.actor == ^blocker_id,
485 ^%{object: blocked_id}
487 order_by: [desc: :id],
494 def make_block_data(blocker, blocked, activity_id) do
497 "actor" => blocker.ap_id,
498 "to" => [blocked.ap_id],
499 "object" => blocked.ap_id
502 if activity_id, do: Map.put(data, "id", activity_id), else: data
505 def make_unblock_data(blocker, blocked, block_activity, activity_id) do
508 "actor" => blocker.ap_id,
509 "to" => [blocked.ap_id],
510 "object" => block_activity.data
513 if activity_id, do: Map.put(data, "id", activity_id), else: data
516 #### Create-related helpers
518 def make_create_data(params, additional) do
519 published = params.published || make_date()
523 "to" => params.to |> Enum.uniq(),
524 "actor" => params.actor.ap_id,
525 "object" => params.object,
526 "published" => published,
527 "context" => params.context
529 |> Map.merge(additional)