5cc46c3eab66cffbe08fa6de271ddae9ac83dbb9
[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 delete(User.t(), String.t()) :: {:ok, map(), keyword()}
14 def delete(actor, object_id) do
15 object = Object.normalize(object_id)
16
17 to = (object.data["to"] || []) ++ (object.data["cc"] || [])
18
19 {:ok,
20 %{
21 "id" => Utils.generate_activity_id(),
22 "actor" => actor.ap_id,
23 "object" => object_id,
24 "to" => to,
25 "type" => "Delete"
26 }, []}
27 end
28
29 @spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
30 def like(actor, object) do
31 object_actor = User.get_cached_by_ap_id(object.data["actor"])
32
33 # Address the actor of the object, and our actor's follower collection if the post is public.
34 to =
35 if Visibility.is_public?(object) do
36 [actor.follower_address, object.data["actor"]]
37 else
38 [object.data["actor"]]
39 end
40
41 # CC everyone who's been addressed in the object, except ourself and the object actor's
42 # follower collection
43 cc =
44 (object.data["to"] ++ (object.data["cc"] || []))
45 |> List.delete(actor.ap_id)
46 |> List.delete(object_actor.follower_address)
47
48 {:ok,
49 %{
50 "id" => Utils.generate_activity_id(),
51 "actor" => actor.ap_id,
52 "type" => "Like",
53 "object" => object.data["id"],
54 "to" => to,
55 "cc" => cc,
56 "context" => object.data["context"]
57 }, []}
58 end
59 end