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