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