AcitvityPub: Outgoing Announces.
[akkoma] / lib / pleroma / web / activity_pub / transmogrifier.ex
1 defmodule Pleroma.Web.ActivityPub.Transmogrifier do
2 @moduledoc """
3 A module to handle coding from internal to wire ActivityPub and back.
4 """
5 alias Pleroma.User
6 alias Pleroma.Object
7 alias Pleroma.Web.ActivityPub.ActivityPub
8
9 @doc """
10 Modifies an incoming AP object (mastodon format) to our internal format.
11 """
12 def fix_object(object) do
13 object
14 |> Map.put("actor", object["attributedTo"])
15 |> fix_attachments
16 end
17
18 def fix_attachments(object) do
19 attachments = object["attachment"] || []
20 |> Enum.map(fn (data) ->
21 url = [%{"type" => "Link", "mediaType" => data["mediaType"], "url" => data["url"]}]
22 Map.put(data, "url", url)
23 end)
24
25 object
26 |> Map.put("attachment", attachments)
27 end
28
29 # TODO: validate those with a Ecto scheme
30 # - tags
31 # - emoji
32 def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
33 with %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
34 object = fix_object(data["object"])
35 params = %{
36 to: data["to"],
37 object: object,
38 actor: user,
39 context: data["object"]["conversation"],
40 local: false,
41 published: data["published"],
42 additional: Map.take(data, [
43 "cc",
44 "id"
45 ])
46 }
47
48 ActivityPub.create(params)
49 else
50 _e -> :error
51 end
52 end
53
54 def handle_incoming(%{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data) do
55 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
56 %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
57 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
58 ActivityPub.accept(%{to: [follower.ap_id], actor: followed.ap_id, object: data, local: true})
59 User.follow(follower, followed)
60 {:ok, activity}
61 else
62 _e -> :error
63 end
64 end
65
66 def handle_incoming(%{"type" => "Like", "object" => object_id, "actor" => actor, "id" => id} = data) do
67 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
68 %Object{} = object <- Object.get_by_ap_id(object_id),
69 {:ok, activity, object} <- ActivityPub.like(actor, object, id, false) do
70 {:ok, activity}
71 else
72 _e -> :error
73 end
74 end
75
76 # TODO
77 # Accept
78 # Undo
79
80 def handle_incoming(_), do: :error
81
82 @doc
83 """
84 internal -> Mastodon
85 """
86 def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
87 object = object
88 |> add_mention_tags
89 |> add_attributed_to
90 |> prepare_attachments
91
92 data = data
93 |> Map.put("object", object)
94 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
95
96 {:ok, data}
97 end
98
99 def prepare_outgoing(%{"type" => type} = data) when type in ["Follow", "Accept", "Like", "Announce"] do
100 data = data
101 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
102
103 {:ok, data}
104 end
105
106 def add_mention_tags(object) do
107 mentions = object["to"]
108 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
109 |> Enum.filter(&(&1))
110 |> Enum.map(fn(user) -> %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"} end)
111
112 tags = object["tag"] || []
113
114 object
115 |> Map.put("tag", tags ++ mentions)
116 end
117
118 def add_attributed_to(object) do
119 attributedTo = object["attributedTo"] || object["actor"]
120
121 object
122 |> Map.put("attributedTo", attributedTo)
123 end
124
125 def prepare_attachments(object) do
126 attachments = (object["attachment"] || [])
127 |> Enum.map(fn (data) ->
128 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
129 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
130 end)
131
132 object
133 |> Map.put("attachment", attachments)
134 end
135 end