ChatMessages: Add attachments.
[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, opts \\ []) do
27 basic = %{
28 "id" => Utils.generate_object_id(),
29 "actor" => actor.ap_id,
30 "type" => "ChatMessage",
31 "to" => [recipient],
32 "content" => content,
33 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
34 "emoji" => Emoji.Formatter.get_emoji_map(content)
35 }
36
37 case opts[:attachment] do
38 %Object{data: attachment_data} ->
39 {
40 :ok,
41 Map.put(basic, "attachment", attachment_data),
42 []
43 }
44
45 _ ->
46 {:ok, basic, []}
47 end
48 end
49
50 @spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
51 def like(actor, object) do
52 object_actor = User.get_cached_by_ap_id(object.data["actor"])
53
54 # Address the actor of the object, and our actor's follower collection if the post is public.
55 to =
56 if Visibility.is_public?(object) do
57 [actor.follower_address, object.data["actor"]]
58 else
59 [object.data["actor"]]
60 end
61
62 # CC everyone who's been addressed in the object, except ourself and the object actor's
63 # follower collection
64 cc =
65 (object.data["to"] ++ (object.data["cc"] || []))
66 |> List.delete(actor.ap_id)
67 |> List.delete(object_actor.follower_address)
68
69 {:ok,
70 %{
71 "id" => Utils.generate_activity_id(),
72 "actor" => actor.ap_id,
73 "type" => "Like",
74 "object" => object.data["id"],
75 "to" => to,
76 "cc" => cc,
77 "context" => object.data["context"]
78 }, []}
79 end
80 end