1 defmodule Pleroma.Web.ActivityPub.Builder do
3 This module builds the objects. Meant to be used for creating local objects.
5 This module encodes our addressing policies and general shape of our objects.
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Web.ActivityPub.Visibility
14 @spec emoji_react(User.t(), Object.t(), String.t()) :: {:ok, map(), keyword()}
15 def emoji_react(actor, object, emoji) do
16 with {:ok, data, meta} <- object_action(actor, object) do
19 |> Map.put("content", emoji)
20 |> Map.put("type", "EmojiReact")
26 @spec undo(User.t(), Activity.t()) :: {:ok, map(), keyword()}
27 def undo(actor, object) do
30 "id" => Utils.generate_activity_id(),
31 "actor" => actor.ap_id,
33 "object" => object.data["id"],
34 "to" => object.data["to"] || [],
35 "cc" => object.data["cc"] || []
39 @spec delete(User.t(), String.t()) :: {:ok, map(), keyword()}
40 def delete(actor, object_id) do
41 object = Object.normalize(object_id, false)
43 user = !object && User.get_cached_by_ap_id(object_id)
46 case {object, user} do
48 # We are deleting an object, address everyone who was originally mentioned
49 (object.data["to"] || []) ++ (object.data["cc"] || [])
51 {_, %User{follower_address: follower_address}} ->
52 # We are deleting a user, address the followers of that user
58 "id" => Utils.generate_activity_id(),
59 "actor" => actor.ap_id,
60 "object" => object_id,
66 def create(actor, object, recipients) do
69 "id" => Utils.generate_activity_id(),
70 "actor" => actor.ap_id,
74 "published" => DateTime.utc_now() |> DateTime.to_iso8601()
78 def chat_message(actor, recipient, content, opts \\ []) do
80 "id" => Utils.generate_object_id(),
81 "actor" => actor.ap_id,
82 "type" => "ChatMessage",
85 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
86 "emoji" => Emoji.Formatter.get_emoji_map(content)
89 case opts[:attachment] do
90 %Object{data: attachment_data} ->
93 Map.put(basic, "attachment", attachment_data),
102 @spec tombstone(String.t(), String.t()) :: {:ok, map(), keyword()}
103 def tombstone(actor, id) do
108 "type" => "Tombstone"
112 @spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
113 def like(actor, object) do
114 with {:ok, data, meta} <- object_action(actor, object) do
117 |> Map.put("type", "Like")
123 @spec object_action(User.t(), Object.t()) :: {:ok, map(), keyword()}
124 defp object_action(actor, object) do
125 object_actor = User.get_cached_by_ap_id(object.data["actor"])
127 # Address the actor of the object, and our actor's follower collection if the post is public.
129 if Visibility.is_public?(object) do
130 [actor.follower_address, object.data["actor"]]
132 [object.data["actor"]]
135 # CC everyone who's been addressed in the object, except ourself and the object actor's
136 # follower collection
138 (object.data["to"] ++ (object.data["cc"] || []))
139 |> List.delete(actor.ap_id)
140 |> List.delete(object_actor.follower_address)
144 "id" => Utils.generate_activity_id(),
145 "actor" => actor.ap_id,
146 "object" => object.data["id"],
149 "context" => object.data["context"]