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 require Pleroma.Constants
16 @spec emoji_react(User.t(), Object.t(), String.t()) :: {:ok, map(), keyword()}
17 def emoji_react(actor, object, emoji) do
18 with {:ok, data, meta} <- object_action(actor, object) do
21 |> Map.put("content", emoji)
22 |> Map.put("type", "EmojiReact")
28 @spec undo(User.t(), Activity.t()) :: {:ok, map(), keyword()}
29 def undo(actor, object) do
32 "id" => Utils.generate_activity_id(),
33 "actor" => actor.ap_id,
35 "object" => object.data["id"],
36 "to" => object.data["to"] || [],
37 "cc" => object.data["cc"] || []
41 @spec delete(User.t(), String.t()) :: {:ok, map(), keyword()}
42 def delete(actor, object_id) do
43 object = Object.normalize(object_id, false)
45 user = !object && User.get_cached_by_ap_id(object_id)
48 case {object, user} do
50 # We are deleting an object, address everyone who was originally mentioned
51 (object.data["to"] || []) ++ (object.data["cc"] || [])
53 {_, %User{follower_address: follower_address}} ->
54 # We are deleting a user, address the followers of that user
60 "id" => Utils.generate_activity_id(),
61 "actor" => actor.ap_id,
62 "object" => object_id,
68 def create(actor, object, recipients) do
71 "id" => Utils.generate_activity_id(),
72 "actor" => actor.ap_id,
76 "published" => DateTime.utc_now() |> DateTime.to_iso8601()
80 def chat_message(actor, recipient, content, opts \\ []) do
82 "id" => Utils.generate_object_id(),
83 "actor" => actor.ap_id,
84 "type" => "ChatMessage",
87 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
88 "emoji" => Emoji.Formatter.get_emoji_map(content)
91 case opts[:attachment] do
92 %Object{data: attachment_data} ->
95 Map.put(basic, "attachment", attachment_data),
104 @spec tombstone(String.t(), String.t()) :: {:ok, map(), keyword()}
105 def tombstone(actor, id) do
110 "type" => "Tombstone"
114 @spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
115 def like(actor, object) do
116 with {:ok, data, meta} <- object_action(actor, object) do
119 |> Map.put("type", "Like")
125 def announce(actor, object, options \\ []) do
126 public? = Keyword.get(options, :public, false)
127 to = [actor.follower_address, object.data["actor"]]
131 [Pleroma.Constants.as_public() | to]
138 "id" => Utils.generate_activity_id(),
139 "actor" => actor.ap_id,
140 "object" => object.data["id"],
142 "context" => object.data["context"],
143 "type" => "Announce",
144 "published" => Utils.make_date()
148 @spec object_action(User.t(), Object.t()) :: {:ok, map(), keyword()}
149 defp object_action(actor, object) do
150 object_actor = User.get_cached_by_ap_id(object.data["actor"])
152 # Address the actor of the object, and our actor's follower collection if the post is public.
154 if Visibility.is_public?(object) do
155 [actor.follower_address, object.data["actor"]]
157 [object.data["actor"]]
160 # CC everyone who's been addressed in the object, except ourself and the object actor's
161 # follower collection
163 (object.data["to"] ++ (object.data["cc"] || []))
164 |> List.delete(actor.ap_id)
165 |> List.delete(object_actor.follower_address)
169 "id" => Utils.generate_activity_id(),
170 "actor" => actor.ap_id,
171 "object" => object.data["id"],
174 "context" => object.data["context"]