Merge branch 'openapi/admin/invites' into 'develop'
[akkoma] / lib / pleroma / web / activity_pub / builder.ex
1 defmodule Pleroma.Web.ActivityPub.Builder do
2 @moduledoc """
3 This module builds the objects. Meant to be used for creating local objects.
4
5 This module encodes our addressing policies and general shape of our objects.
6 """
7
8 alias Pleroma.Object
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.Relay
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Web.ActivityPub.Visibility
13
14 require Pleroma.Constants
15
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
19 data =
20 data
21 |> Map.put("content", emoji)
22 |> Map.put("type", "EmojiReact")
23
24 {:ok, data, meta}
25 end
26 end
27
28 @spec undo(User.t(), Activity.t()) :: {:ok, map(), keyword()}
29 def undo(actor, object) do
30 {:ok,
31 %{
32 "id" => Utils.generate_activity_id(),
33 "actor" => actor.ap_id,
34 "type" => "Undo",
35 "object" => object.data["id"],
36 "to" => object.data["to"] || [],
37 "cc" => object.data["cc"] || []
38 }, []}
39 end
40
41 @spec delete(User.t(), String.t()) :: {:ok, map(), keyword()}
42 def delete(actor, object_id) do
43 object = Object.normalize(object_id, false)
44
45 user = !object && User.get_cached_by_ap_id(object_id)
46
47 to =
48 case {object, user} do
49 {%Object{}, _} ->
50 # We are deleting an object, address everyone who was originally mentioned
51 (object.data["to"] || []) ++ (object.data["cc"] || [])
52
53 {_, %User{follower_address: follower_address}} ->
54 # We are deleting a user, address the followers of that user
55 [follower_address]
56 end
57
58 {:ok,
59 %{
60 "id" => Utils.generate_activity_id(),
61 "actor" => actor.ap_id,
62 "object" => object_id,
63 "to" => to,
64 "type" => "Delete"
65 }, []}
66 end
67
68 @spec tombstone(String.t(), String.t()) :: {:ok, map(), keyword()}
69 def tombstone(actor, id) do
70 {:ok,
71 %{
72 "id" => id,
73 "actor" => actor,
74 "type" => "Tombstone"
75 }, []}
76 end
77
78 @spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
79 def like(actor, object) do
80 with {:ok, data, meta} <- object_action(actor, object) do
81 data =
82 data
83 |> Map.put("type", "Like")
84
85 {:ok, data, meta}
86 end
87 end
88
89 @spec announce(User.t(), Object.t(), keyword()) :: {:ok, map(), keyword()}
90 def announce(actor, object, options \\ []) do
91 public? = Keyword.get(options, :public, false)
92
93 to =
94 cond do
95 actor.ap_id == Relay.relay_ap_id() ->
96 [actor.follower_address]
97
98 public? ->
99 [actor.follower_address, object.data["actor"], Pleroma.Constants.as_public()]
100
101 true ->
102 [actor.follower_address, object.data["actor"]]
103 end
104
105 {:ok,
106 %{
107 "id" => Utils.generate_activity_id(),
108 "actor" => actor.ap_id,
109 "object" => object.data["id"],
110 "to" => to,
111 "context" => object.data["context"],
112 "type" => "Announce",
113 "published" => Utils.make_date()
114 }, []}
115 end
116
117 @spec object_action(User.t(), Object.t()) :: {:ok, map(), keyword()}
118 defp object_action(actor, object) do
119 object_actor = User.get_cached_by_ap_id(object.data["actor"])
120
121 # Address the actor of the object, and our actor's follower collection if the post is public.
122 to =
123 if Visibility.is_public?(object) do
124 [actor.follower_address, object.data["actor"]]
125 else
126 [object.data["actor"]]
127 end
128
129 # CC everyone who's been addressed in the object, except ourself and the object actor's
130 # follower collection
131 cc =
132 (object.data["to"] ++ (object.data["cc"] || []))
133 |> List.delete(actor.ap_id)
134 |> List.delete(object_actor.follower_address)
135
136 {:ok,
137 %{
138 "id" => Utils.generate_activity_id(),
139 "actor" => actor.ap_id,
140 "object" => object.data["id"],
141 "to" => to,
142 "cc" => cc,
143 "context" => object.data["context"]
144 }, []}
145 end
146 end