federator: move activitypub relaying to the AP publisher module
[akkoma] / lib / pleroma / web / ostatus / activity_representer.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.OStatus.ActivityRepresenter do
6 alias Pleroma.Activity
7 alias Pleroma.Object
8 alias Pleroma.User
9 alias Pleroma.Web.OStatus.UserRepresenter
10
11 require Logger
12
13 defp get_href(id) do
14 with %Object{data: %{"external_url" => external_url}} <- Object.get_cached_by_ap_id(id) do
15 external_url
16 else
17 _e -> id
18 end
19 end
20
21 defp get_in_reply_to(activity) do
22 with %Object{data: %{"inReplyTo" => in_reply_to}} <- Object.normalize(activity) do
23 [
24 {:"thr:in-reply-to",
25 [ref: to_charlist(in_reply_to), href: to_charlist(get_href(in_reply_to))], []}
26 ]
27 else
28 _ ->
29 []
30 end
31 end
32
33 defp get_mentions(to) do
34 Enum.map(to, fn id ->
35 cond do
36 # Special handling for the AP/Ostatus public collections
37 "https://www.w3.org/ns/activitystreams#Public" == id ->
38 {:link,
39 [
40 rel: "mentioned",
41 "ostatus:object-type": "http://activitystrea.ms/schema/1.0/collection",
42 href: "http://activityschema.org/collection/public"
43 ], []}
44
45 # Ostatus doesn't handle follower collections, ignore these.
46 Regex.match?(~r/^#{Pleroma.Web.base_url()}.+followers$/, id) ->
47 []
48
49 true ->
50 {:link,
51 [
52 rel: "mentioned",
53 "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person",
54 href: id
55 ], []}
56 end
57 end)
58 end
59
60 defp get_links(%{local: true}, %{"id" => object_id}) do
61 h = fn str -> [to_charlist(str)] end
62
63 [
64 {:link, [type: ['application/atom+xml'], href: h.(object_id), rel: 'self'], []},
65 {:link, [type: ['text/html'], href: h.(object_id), rel: 'alternate'], []}
66 ]
67 end
68
69 defp get_links(%{local: false}, %{"external_url" => external_url}) do
70 h = fn str -> [to_charlist(str)] end
71
72 [
73 {:link, [type: ['text/html'], href: h.(external_url), rel: 'alternate'], []}
74 ]
75 end
76
77 defp get_links(_activity, _object_data), do: []
78
79 defp get_emoji_links(emojis) do
80 Enum.map(emojis, fn {emoji, file} ->
81 {:link, [name: to_charlist(emoji), rel: 'emoji', href: to_charlist(file)], []}
82 end)
83 end
84
85 def to_simple_form(activity, user, with_author \\ false)
86
87 def to_simple_form(%{data: %{"type" => "Create"}} = activity, user, with_author) do
88 h = fn str -> [to_charlist(str)] end
89
90 object = Object.normalize(activity)
91
92 updated_at = object.data["published"]
93 inserted_at = object.data["published"]
94
95 attachments =
96 Enum.map(object.data["attachment"] || [], fn attachment ->
97 url = hd(attachment["url"])
98
99 {:link,
100 [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])],
101 []}
102 end)
103
104 in_reply_to = get_in_reply_to(activity)
105 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
106 mentions = activity.recipients |> get_mentions
107
108 categories =
109 (object.data["tag"] || [])
110 |> Enum.map(fn tag ->
111 if is_binary(tag) do
112 {:category, [term: to_charlist(tag)], []}
113 else
114 nil
115 end
116 end)
117 |> Enum.filter(& &1)
118
119 emoji_links = get_emoji_links(object.data["emoji"] || %{})
120
121 summary =
122 if object.data["summary"] do
123 [{:summary, [], h.(object.data["summary"])}]
124 else
125 []
126 end
127
128 [
129 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
130 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
131 # For notes, federate the object id.
132 {:id, h.(object.data["id"])},
133 {:title, ['New note by #{user.nickname}']},
134 {:content, [type: 'html'], h.(object.data["content"] |> String.replace(~r/[\n\r]/, ""))},
135 {:published, h.(inserted_at)},
136 {:updated, h.(updated_at)},
137 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
138 h.(activity.data["context"])},
139 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []}
140 ] ++
141 summary ++
142 get_links(activity, object.data) ++
143 categories ++ attachments ++ in_reply_to ++ author ++ mentions ++ emoji_links
144 end
145
146 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
147 h = fn str -> [to_charlist(str)] end
148
149 updated_at = activity.data["published"]
150 inserted_at = activity.data["published"]
151
152 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
153 mentions = activity.recipients |> get_mentions
154
155 [
156 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
157 {:id, h.(activity.data["id"])},
158 {:title, ['New favorite by #{user.nickname}']},
159 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
160 {:published, h.(inserted_at)},
161 {:updated, h.(updated_at)},
162 {:"activity:object",
163 [
164 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
165 # For notes, federate the object id.
166 {:id, h.(activity.data["object"])}
167 ]},
168 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
169 h.(activity.data["context"])},
170 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
171 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
172 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
173 ] ++ author ++ mentions
174 end
175
176 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
177 h = fn str -> [to_charlist(str)] end
178
179 updated_at = activity.data["published"]
180 inserted_at = activity.data["published"]
181
182 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
183
184 retweeted_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
185 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
186
187 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
188
189 mentions =
190 ([retweeted_user.ap_id] ++ activity.recipients)
191 |> Enum.uniq()
192 |> get_mentions()
193
194 [
195 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
196 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
197 {:id, h.(activity.data["id"])},
198 {:title, ['#{user.nickname} repeated a notice']},
199 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
200 {:published, h.(inserted_at)},
201 {:updated, h.(updated_at)},
202 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
203 h.(activity.data["context"])},
204 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
205 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
206 {:"activity:object", retweeted_xml}
207 ] ++ mentions ++ author
208 end
209
210 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
211 h = fn str -> [to_charlist(str)] end
212
213 updated_at = activity.data["published"]
214 inserted_at = activity.data["published"]
215
216 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
217
218 mentions = (activity.recipients || []) |> get_mentions
219
220 [
221 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
222 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
223 {:id, h.(activity.data["id"])},
224 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
225 {:content, [type: 'html'],
226 ['#{user.nickname} started following #{activity.data["object"]}']},
227 {:published, h.(inserted_at)},
228 {:updated, h.(updated_at)},
229 {:"activity:object",
230 [
231 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
232 {:id, h.(activity.data["object"])},
233 {:uri, h.(activity.data["object"])}
234 ]},
235 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
236 ] ++ mentions ++ author
237 end
238
239 # Only undos of follow for now. Will need to get redone once there are more
240 def to_simple_form(
241 %{data: %{"type" => "Undo", "object" => %{"type" => "Follow"} = follow_activity}} =
242 activity,
243 user,
244 with_author
245 ) do
246 h = fn str -> [to_charlist(str)] end
247
248 updated_at = activity.data["published"]
249 inserted_at = activity.data["published"]
250
251 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
252
253 mentions = (activity.recipients || []) |> get_mentions
254 follow_activity = Activity.normalize(follow_activity)
255
256 [
257 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
258 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
259 {:id, h.(activity.data["id"])},
260 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
261 {:content, [type: 'html'],
262 ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
263 {:published, h.(inserted_at)},
264 {:updated, h.(updated_at)},
265 {:"activity:object",
266 [
267 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
268 {:id, h.(follow_activity.data["object"])},
269 {:uri, h.(follow_activity.data["object"])}
270 ]},
271 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
272 ] ++ mentions ++ author
273 end
274
275 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
276 h = fn str -> [to_charlist(str)] end
277
278 updated_at = activity.data["published"]
279 inserted_at = activity.data["published"]
280
281 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
282
283 [
284 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
285 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/delete']},
286 {:id, h.(activity.data["object"])},
287 {:title, ['An object was deleted']},
288 {:content, [type: 'html'], ['An object was deleted']},
289 {:published, h.(inserted_at)},
290 {:updated, h.(updated_at)}
291 ] ++ author
292 end
293
294 def to_simple_form(_, _, _), do: nil
295
296 def wrap_with_entry(simple_form) do
297 [
298 {
299 :entry,
300 [
301 xmlns: 'http://www.w3.org/2005/Atom',
302 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
303 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
304 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
305 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'
306 ],
307 simple_form
308 }
309 ]
310 end
311 end