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