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