Merge branch 'feature/undo-validator-reduced' 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.Utils
11 alias Pleroma.Web.ActivityPub.Visibility
12
13 @spec undo(User.t(), Activity.t()) :: {:ok, map(), keyword()}
14 def undo(actor, object) do
15 {:ok,
16 %{
17 "id" => Utils.generate_activity_id(),
18 "actor" => actor.ap_id,
19 "type" => "Undo",
20 "object" => object.data["id"],
21 "to" => object.data["to"] || [],
22 "cc" => object.data["cc"] || []
23 }, []}
24 end
25
26 @spec delete(User.t(), String.t()) :: {:ok, map(), keyword()}
27 def delete(actor, object_id) do
28 object = Object.normalize(object_id, false)
29
30 user = !object && User.get_cached_by_ap_id(object_id)
31
32 to =
33 case {object, user} do
34 {%Object{}, _} ->
35 # We are deleting an object, address everyone who was originally mentioned
36 (object.data["to"] || []) ++ (object.data["cc"] || [])
37
38 {_, %User{follower_address: follower_address}} ->
39 # We are deleting a user, address the followers of that user
40 [follower_address]
41 end
42
43 {:ok,
44 %{
45 "id" => Utils.generate_activity_id(),
46 "actor" => actor.ap_id,
47 "object" => object_id,
48 "to" => to,
49 "type" => "Delete"
50 }, []}
51 end
52
53 @spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
54 def like(actor, object) do
55 object_actor = User.get_cached_by_ap_id(object.data["actor"])
56
57 # Address the actor of the object, and our actor's follower collection if the post is public.
58 to =
59 if Visibility.is_public?(object) do
60 [actor.follower_address, object.data["actor"]]
61 else
62 [object.data["actor"]]
63 end
64
65 # CC everyone who's been addressed in the object, except ourself and the object actor's
66 # follower collection
67 cc =
68 (object.data["to"] ++ (object.data["cc"] || []))
69 |> List.delete(actor.ap_id)
70 |> List.delete(object_actor.follower_address)
71
72 {:ok,
73 %{
74 "id" => Utils.generate_activity_id(),
75 "actor" => actor.ap_id,
76 "type" => "Like",
77 "object" => object.data["id"],
78 "to" => to,
79 "cc" => cc,
80 "context" => object.data["context"]
81 }, []}
82 end
83 end