Pipeline: Unify, refactor, DRY.
[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.Emoji
9 alias Pleroma.Object
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Web.ActivityPub.Visibility
13
14 def create(actor, object, recipients) do
15 {:ok,
16 %{
17 "id" => Utils.generate_activity_id(),
18 "actor" => actor.ap_id,
19 "to" => recipients,
20 "object" => object,
21 "type" => "Create",
22 "published" => DateTime.utc_now() |> DateTime.to_iso8601()
23 }, []}
24 end
25
26 def chat_message(actor, recipient, content) do
27 {:ok,
28 %{
29 "id" => Utils.generate_object_id(),
30 "actor" => actor.ap_id,
31 "type" => "ChatMessage",
32 "to" => [recipient],
33 "content" => content,
34 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
35 "emoji" => Emoji.Formatter.get_emoji_map(content)
36 }, []}
37 end
38
39 @spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
40 def like(actor, object) do
41 object_actor = User.get_cached_by_ap_id(object.data["actor"])
42
43 # Address the actor of the object, and our actor's follower collection if the post is public.
44 to =
45 if Visibility.is_public?(object) do
46 [actor.follower_address, object.data["actor"]]
47 else
48 [object.data["actor"]]
49 end
50
51 # CC everyone who's been addressed in the object, except ourself and the object actor's
52 # follower collection
53 cc =
54 (object.data["to"] ++ (object.data["cc"] || []))
55 |> List.delete(actor.ap_id)
56 |> List.delete(object_actor.follower_address)
57
58 {:ok,
59 %{
60 "id" => Utils.generate_activity_id(),
61 "actor" => actor.ap_id,
62 "type" => "Like",
63 "object" => object.data["id"],
64 "to" => to,
65 "cc" => cc,
66 "context" => object.data["context"]
67 }, []}
68 end
69 end