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