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