395436c5c9ee81b90ea0dd5f77366d3c691dc87b
[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.Activity
8 alias Pleroma.Repo
9 alias Pleroma.Web.ActivityPub.ActivityPub
10
11 import Ecto.Query
12
13 @doc """
14 Modifies an incoming AP object (mastodon format) to our internal format.
15 """
16 def fix_object(object) do
17 object
18 |> Map.put("actor", object["attributedTo"])
19 |> fix_attachments
20 |> fix_context
21 end
22
23 def fix_context(object) do
24 object
25 |> Map.put("context", object["conversation"])
26 end
27
28 def fix_attachments(object) do
29 attachments = (object["attachment"] || [])
30 |> Enum.map(fn (data) ->
31 url = [%{"type" => "Link", "mediaType" => data["mediaType"], "href" => data["url"]}]
32 Map.put(data, "url", url)
33 end)
34
35 object
36 |> Map.put("attachment", attachments)
37 end
38
39 # TODO: validate those with a Ecto scheme
40 # - tags
41 # - emoji
42 def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
43 with nil <- Activity.get_create_activity_by_object_ap_id(object["id"]),
44 %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
45 object = fix_object(data["object"])
46 params = %{
47 to: data["to"],
48 object: object,
49 actor: user,
50 context: data["object"]["conversation"],
51 local: false,
52 published: data["published"],
53 additional: Map.take(data, [
54 "cc",
55 "id"
56 ])
57 }
58
59 if object["inReplyTo"] do
60 {:ok, object} = ActivityPub.fetch_object_from_id(object["inReplyTo"])
61 end
62
63 ActivityPub.create(params)
64 else
65 %Activity{} = activity -> {:ok, activity}
66 _e -> :error
67 end
68 end
69
70 def handle_incoming(%{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data) do
71 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
72 %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
73 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
74 ActivityPub.accept(%{to: [follower.ap_id], actor: followed.ap_id, object: data, local: true})
75 User.follow(follower, followed)
76 {:ok, activity}
77 else
78 _e -> :error
79 end
80 end
81
82 def handle_incoming(%{"type" => "Like", "object" => object_id, "actor" => actor, "id" => id} = data) do
83 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
84 %Object{} = object <- Object.get_by_ap_id(object_id),
85 {:ok, activity, object} <- ActivityPub.like(actor, object, id, false) do
86 {:ok, activity}
87 else
88 _e -> :error
89 end
90 end
91
92 def handle_incoming(%{"type" => "Announce", "object" => object_id, "actor" => actor, "id" => id} = data) do
93 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
94 {:ok, object} <- get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
95 {:ok, activity, object} <- ActivityPub.announce(actor, object, id, false) do
96 {:ok, activity}
97 else
98 _e -> :error
99 end
100 end
101
102 # TODO
103 # Accept
104 # Undo
105
106 def handle_incoming(_), do: :error
107
108 def get_obj_helper(id) do
109 if object = Object.get_by_ap_id(id), do: {:ok, object}, else: nil
110 end
111
112 @doc
113 """
114 internal -> Mastodon
115 """
116 def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
117 object = object
118 |> set_sensitive
119 |> add_hashtags
120 |> add_mention_tags
121 |> add_attributed_to
122 |> prepare_attachments
123 |> set_conversation
124
125 data = data
126 |> Map.put("object", object)
127 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
128
129 {:ok, data}
130 end
131
132 def prepare_outgoing(%{"type" => type} = data) do
133 data = data
134 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
135
136 {:ok, data}
137 end
138
139 def add_hashtags(object) do
140 tags = (object["tag"] || [])
141 |> Enum.map fn (tag) -> %{"href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}", "name" => "##{tag}", "type" => "Hashtag"} end
142
143 object
144 |> Map.put("tag", tags)
145 end
146
147 def add_mention_tags(object) do
148 recipients = object["to"] ++ (object["cc"] || [])
149 mentions = recipients
150 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
151 |> Enum.filter(&(&1))
152 |> Enum.map(fn(user) -> %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"} end)
153
154 tags = object["tag"] || []
155
156 object
157 |> Map.put("tag", tags ++ mentions)
158 end
159
160 def set_conversation(object) do
161 Map.put(object, "conversation", object["context"])
162 end
163
164 def set_sensitive(object) do
165 tags = object["tag"] || []
166 Map.put(object, "sensitive", "nsfw" in tags)
167 end
168
169 def add_attributed_to(object) do
170 attributedTo = object["attributedTo"] || object["actor"]
171
172 object
173 |> Map.put("attributedTo", attributedTo)
174 end
175
176 def prepare_attachments(object) do
177 attachments = (object["attachment"] || [])
178 |> Enum.map(fn (data) ->
179 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
180 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
181 end)
182
183 object
184 |> Map.put("attachment", attachments)
185 end
186
187 def upgrade_user_from_ap_id(ap_id) do
188 with %User{} = user <- User.get_by_ap_id(ap_id),
189 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id) do
190 data = data
191 |> Map.put(:info, Map.merge(user.info, data[:info]))
192
193 old_follower_address = user.follower_address
194 {:ok, user} = User.upgrade_changeset(user, data)
195 |> Repo.update()
196
197 # This could potentially take a long time, do it in the background
198 Task.start(fn ->
199 q = from a in Activity,
200 where: ^old_follower_address in a.recipients,
201 update: [set: [recipients: fragment("array_replace(?,?,?)", a.recipients, ^old_follower_address, ^user.follower_address)]]
202 Repo.update_all(q, [])
203
204 q = from u in User,
205 where: ^old_follower_address in u.following,
206 update: [set: [following: fragment("array_replace(?,?,?)", u.following, ^old_follower_address, ^user.follower_address)]]
207 Repo.update_all(q, [])
208 end)
209
210 {:ok, user}
211 else
212 e -> e
213 end
214 end
215 end