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