1 defmodule Pleroma.Web.ActivityPub.Utils do
2 alias Pleroma.{Repo, Web, Object, Activity, User}
3 alias Pleroma.Web.Router.Helpers
4 alias Pleroma.Web.Endpoint
5 alias Ecto.{Changeset, UUID}
8 # Some implementations send the actor URI as the actor field, others send the entire actor object,
9 # so figure out what the actor's URI is based on what we have.
10 def normalize_actor(actor) do
20 def normalize_params(params) do
21 Map.put(params, "actor", normalize_actor(params["actor"]))
24 def make_json_ld_header do
27 "https://www.w3.org/ns/activitystreams",
28 "https://w3id.org/security/v1",
30 "manuallyApprovesFollowers" => "as:manuallyApprovesFollowers",
31 "sensitive" => "as:sensitive",
32 "Hashtag" => "as:Hashtag",
33 "ostatus" => "http://ostatus.org#",
34 "atomUri" => "ostatus:atomUri",
35 "inReplyToAtomUri" => "ostatus:inReplyToAtomUri",
36 "conversation" => "ostatus:conversation",
37 "toot" => "http://joinmastodon.org/ns#",
38 "Emoji" => "toot:Emoji"
45 DateTime.utc_now() |> DateTime.to_iso8601()
48 def generate_activity_id do
49 generate_id("activities")
52 def generate_context_id do
53 generate_id("contexts")
56 def generate_object_id do
57 Helpers.o_status_url(Endpoint, :object, UUID.generate())
60 def generate_id(type) do
61 "#{Web.base_url()}/#{type}/#{UUID.generate()}"
64 def create_context(context) do
65 context = context || generate_id("contexts")
66 changeset = Object.context_mapping(context)
68 case Repo.insert(changeset) do
72 # This should be solved by an upsert, but it seems ecto
73 # has problems accessing the constraint inside the jsonb.
75 Object.get_cached_by_ap_id(context)
80 Enqueues an activity for federation if it's local
82 def maybe_federate(%Activity{local: true} = activity) do
84 case activity.data["type"] do
90 Pleroma.Web.Federator.enqueue(:publish, activity, priority)
94 def maybe_federate(_), do: :ok
97 Adds an id and a published data if they aren't there,
98 also adds it to an included object
100 def lazy_put_activity_defaults(map) do
101 %{data: %{"id" => context}, id: context_id} = create_context(map["context"])
105 |> Map.put_new_lazy("id", &generate_activity_id/0)
106 |> Map.put_new_lazy("published", &make_date/0)
107 |> Map.put_new("context", context)
108 |> Map.put_new("context_id", context_id)
110 if is_map(map["object"]) do
111 object = lazy_put_object_defaults(map["object"], map)
112 %{map | "object" => object}
119 Adds an id and published date if they aren't there.
121 def lazy_put_object_defaults(map, activity \\ %{}) do
123 |> Map.put_new_lazy("id", &generate_object_id/0)
124 |> Map.put_new_lazy("published", &make_date/0)
125 |> Map.put_new("context", activity["context"])
126 |> Map.put_new("context_id", activity["context_id"])
130 Inserts a full object if it is contained in an activity.
132 def insert_full_object(%{"object" => %{"type" => type} = object_data})
133 when is_map(object_data) and type in ["Note"] do
134 with {:ok, _} <- Object.create(object_data) do
139 def insert_full_object(_), do: :ok
141 def update_object_in_activities(%{data: %{"id" => id}} = object) do
143 # Update activities that already had this. Could be done in a seperate process.
144 # Alternatively, just don't do this and fetch the current object each time. Most
145 # could probably be taken from cache.
146 relevant_activities = Activity.all_by_object_ap_id(id)
148 Enum.map(relevant_activities, fn activity ->
149 new_activity_data = activity.data |> Map.put("object", object.data)
150 changeset = Changeset.change(activity, data: new_activity_data)
151 Repo.update(changeset)
155 #### Like-related helpers
158 Returns an existing like if a user already liked an object
160 def get_existing_like(actor, %{data: %{"id" => id}}) do
163 activity in Activity,
164 where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
165 # this is to use the index
168 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
173 where: fragment("(?)->>'type' = 'Like'", activity.data)
179 def make_like_data(%User{ap_id: ap_id} = actor, %{data: %{"id" => id}} = object, activity_id) do
184 "to" => [actor.follower_address, object.data["actor"]],
185 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
186 "context" => object.data["context"]
189 if activity_id, do: Map.put(data, "id", activity_id), else: data
192 def update_element_in_object(property, element, object) do
195 |> Map.put("#{property}_count", length(element))
196 |> Map.put("#{property}s", element),
197 changeset <- Changeset.change(object, data: new_data),
198 {:ok, object} <- Repo.update(changeset),
199 _ <- update_object_in_activities(object) do
204 def update_likes_in_object(likes, object) do
205 update_element_in_object("like", likes, object)
208 def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
209 with likes <- [actor | object.data["likes"] || []] |> Enum.uniq() do
210 update_likes_in_object(likes, object)
214 def remove_like_from_object(%Activity{data: %{"actor" => actor}}, object) do
215 with likes <- (object.data["likes"] || []) |> List.delete(actor) do
216 update_likes_in_object(likes, object)
220 #### Follow-related helpers
223 Makes a follow activity data for the given follower and followed
225 def make_follow_data(%User{ap_id: follower_id}, %User{ap_id: followed_id}, activity_id) do
228 "actor" => follower_id,
229 "to" => [followed_id],
230 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
231 "object" => followed_id
234 if activity_id, do: Map.put(data, "id", activity_id), else: data
237 def fetch_latest_follow(%User{ap_id: follower_id}, %User{ap_id: followed_id}) do
240 activity in Activity,
245 ^%{type: "Follow", actor: follower_id, object: followed_id}
247 order_by: [desc: :id],
254 #### Announce-related helpers
257 Retruns an existing announce activity if the notice has already been announced
259 def get_existing_announce(actor, %{data: %{"id" => id}}) do
262 activity in Activity,
263 where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
264 # this is to use the index
267 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
272 where: fragment("(?)->>'type' = 'Announce'", activity.data)
279 Make announce activity data for the given actor and object
281 def make_announce_data(
282 %User{ap_id: ap_id} = user,
283 %Object{data: %{"id" => id}} = object,
287 "type" => "Announce",
290 "to" => [user.follower_address, object.data["actor"]],
291 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
292 "context" => object.data["context"]
295 if activity_id, do: Map.put(data, "id", activity_id), else: data
299 Make unannounce activity data for the given actor and object
301 def make_unannounce_data(
302 %User{ap_id: ap_id} = user,
303 %Activity{data: %{"context" => context}} = activity,
309 "object" => activity.data,
310 "to" => [user.follower_address, activity.data["actor"]],
311 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
315 if activity_id, do: Map.put(data, "id", activity_id), else: data
318 def make_unlike_data(
319 %User{ap_id: ap_id} = user,
320 %Activity{data: %{"context" => context}} = activity,
326 "object" => activity.data,
327 "to" => [user.follower_address, activity.data["actor"]],
328 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
332 if activity_id, do: Map.put(data, "id", activity_id), else: data
335 def add_announce_to_object(%Activity{data: %{"actor" => actor}}, object) do
336 with announcements <- [actor | object.data["announcements"] || []] |> Enum.uniq() do
337 update_element_in_object("announcement", announcements, object)
341 def remove_announce_from_object(%Activity{data: %{"actor" => actor}}, object) do
342 with announcements <- (object.data["announcements"] || []) |> List.delete(actor) do
343 update_element_in_object("announcement", announcements, object)
347 #### Unfollow-related helpers
349 def make_unfollow_data(follower, followed, follow_activity) do
352 "actor" => follower.ap_id,
353 "to" => [followed.ap_id],
354 "object" => follow_activity.data["id"]
358 #### Create-related helpers
360 def make_create_data(params, additional) do
361 published = params.published || make_date()
365 "to" => params.to |> Enum.uniq(),
366 "actor" => params.actor.ap_id,
367 "object" => params.object,
368 "published" => published,
369 "context" => params.context
371 |> Map.merge(additional)