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