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 def make_json_ld_header do
11 "https://www.w3.org/ns/activitystreams",
12 "https://w3id.org/security/v1",
14 "manuallyApprovesFollowers" => "as:manuallyApprovesFollowers",
15 "sensitive" => "as:sensitive",
16 "Hashtag" => "as:Hashtag",
17 "ostatus" => "http://ostatus.org#",
18 "atomUri" => "ostatus:atomUri",
19 "inReplyToAtomUri" => "ostatus:inReplyToAtomUri",
20 "conversation" => "ostatus:conversation",
21 "toot" => "http://joinmastodon.org/ns#",
22 "Emoji" => "toot:Emoji"
29 DateTime.utc_now() |> DateTime.to_iso8601()
32 def generate_activity_id do
33 generate_id("activities")
36 def generate_context_id do
37 generate_id("contexts")
40 def generate_object_id do
41 Helpers.o_status_url(Endpoint, :object, UUID.generate())
44 def generate_id(type) do
45 "#{Web.base_url()}/#{type}/#{UUID.generate()}"
48 def create_context(context) do
49 context = context || generate_id("contexts")
50 changeset = Object.context_mapping(context)
52 case Repo.insert(changeset) do
56 # This should be solved by an upsert, but it seems ecto
57 # has problems accessing the constraint inside the jsonb.
59 Object.get_cached_by_ap_id(context)
64 Enqueues an activity for federation if it's local
66 def maybe_federate(%Activity{local: true} = activity) do
68 case activity.data["type"] do
74 Pleroma.Web.Federator.enqueue(:publish, activity, priority)
78 def maybe_federate(_), do: :ok
81 Adds an id and a published data if they aren't there,
82 also adds it to an included object
84 def lazy_put_activity_defaults(map) do
85 %{data: %{"id" => context}, id: context_id} = create_context(map["context"])
89 |> Map.put_new_lazy("id", &generate_activity_id/0)
90 |> Map.put_new_lazy("published", &make_date/0)
91 |> Map.put_new("context", context)
92 |> Map.put_new("context_id", context_id)
94 if is_map(map["object"]) do
95 object = lazy_put_object_defaults(map["object"], map)
96 %{map | "object" => object}
103 Adds an id and published date if they aren't there.
105 def lazy_put_object_defaults(map, activity \\ %{}) do
107 |> Map.put_new_lazy("id", &generate_object_id/0)
108 |> Map.put_new_lazy("published", &make_date/0)
109 |> Map.put_new("context", activity["context"])
110 |> Map.put_new("context_id", activity["context_id"])
114 Inserts a full object if it is contained in an activity.
116 def insert_full_object(%{"object" => %{"type" => type} = object_data})
117 when is_map(object_data) and type in ["Note"] do
118 with {:ok, _} <- Object.create(object_data) do
123 def insert_full_object(_), do: :ok
125 def update_object_in_activities(%{data: %{"id" => id}} = object) do
127 # Update activities that already had this. Could be done in a seperate process.
128 # Alternatively, just don't do this and fetch the current object each time. Most
129 # could probably be taken from cache.
130 relevant_activities = Activity.all_by_object_ap_id(id)
132 Enum.map(relevant_activities, fn activity ->
133 new_activity_data = activity.data |> Map.put("object", object.data)
134 changeset = Changeset.change(activity, data: new_activity_data)
135 Repo.update(changeset)
139 #### Like-related helpers
142 Returns an existing like if a user already liked an object
144 def get_existing_like(actor, %{data: %{"id" => id}}) do
147 activity in Activity,
148 where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
149 # this is to use the index
152 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
157 where: fragment("(?)->>'type' = 'Like'", activity.data)
163 def make_like_data(%User{ap_id: ap_id} = actor, %{data: %{"id" => id}} = object, activity_id) do
168 "to" => [actor.follower_address, object.data["actor"]],
169 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
170 "context" => object.data["context"]
173 if activity_id, do: Map.put(data, "id", activity_id), else: data
176 def update_element_in_object(property, element, object) do
178 object.data |> Map.put("#{property}_count", length(element))
179 |> Map.put("#{property}s", element),
180 changeset <- Changeset.change(object, data: new_data),
181 {:ok, object} <- Repo.update(changeset),
182 _ <- update_object_in_activities(object) do
187 def update_likes_in_object(likes, object) do
188 update_element_in_object("like", likes, object)
191 def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
192 with likes <- [actor | object.data["likes"] || []] |> Enum.uniq() do
193 update_likes_in_object(likes, object)
197 def remove_like_from_object(%Activity{data: %{"actor" => actor}}, object) do
198 with likes <- (object.data["likes"] || []) |> List.delete(actor) do
199 update_likes_in_object(likes, object)
203 #### Follow-related helpers
206 Makes a follow activity data for the given follower and followed
208 def make_follow_data(%User{ap_id: follower_id}, %User{ap_id: followed_id}, activity_id) do
211 "actor" => follower_id,
212 "to" => [followed_id],
213 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
214 "object" => followed_id
217 if activity_id, do: Map.put(data, "id", activity_id), else: data
220 def fetch_latest_follow(%User{ap_id: follower_id}, %User{ap_id: followed_id}) do
223 activity in Activity,
228 ^%{type: "Follow", actor: follower_id, object: followed_id}
230 order_by: [desc: :id],
237 #### Announce-related helpers
240 Retruns an existing announce activity if the notice has already been announced
242 def get_existing_announce(actor, %{data: %{"id" => id}}) do
245 activity in Activity,
246 where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
247 # this is to use the index
250 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
255 where: fragment("(?)->>'type' = 'Announce'", activity.data)
262 Make announce activity data for the given actor and object
264 def make_announce_data(
265 %User{ap_id: ap_id} = user,
266 %Object{data: %{"id" => id}} = object,
270 "type" => "Announce",
273 "to" => [user.follower_address, object.data["actor"]],
274 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
275 "context" => object.data["context"]
278 if activity_id, do: Map.put(data, "id", activity_id), else: data
282 Make unannounce activity data for the given actor and object
284 def make_unannounce_data(
285 %User{ap_id: ap_id} = user,
286 %Object{data: %{"id" => id}} = object
292 "to" => [user.follower_address, object.data["actor"]],
293 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
294 "context" => object.data["context"]
298 def add_announce_to_object(%Activity{data: %{"actor" => actor}}, object) do
299 with announcements <- [actor | object.data["announcements"] || []] |> Enum.uniq() do
300 update_element_in_object("announcement", announcements, object)
304 def remove_announce_from_object(%Activity{data: %{"actor" => actor}}, object) do
305 with announcements <- (object.data["announcements"] || []) |> List.delete(actor) do
306 update_element_in_object("announcement", announcements, object)
310 #### Unfollow-related helpers
312 def make_unfollow_data(follower, followed, follow_activity) do
315 "actor" => follower.ap_id,
316 "to" => [followed.ap_id],
317 "object" => follow_activity.data["id"]
321 #### Create-related helpers
323 def make_create_data(params, additional) do
324 published = params.published || make_date()
328 "to" => params.to |> Enum.uniq(),
329 "actor" => params.actor.ap_id,
330 "object" => params.object,
331 "published" => published,
332 "context" => params.context
334 |> Map.merge(additional)