Merge branch 'feld-varnish' into 'develop'
[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"])], h.(activity.data["context"])},
135 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []}
136 ] ++
137 summary ++
138 get_links(activity) ++
139 categories ++ attachments ++ in_reply_to ++ author ++ mentions ++ emoji_links
140 end
141
142 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
143 h = fn str -> [to_charlist(str)] end
144
145 updated_at = activity.data["published"]
146 inserted_at = activity.data["published"]
147
148 _in_reply_to = get_in_reply_to(activity.data)
149 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
150 mentions = activity.recipients |> get_mentions
151
152 [
153 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
154 {:id, h.(activity.data["id"])},
155 {:title, ['New favorite by #{user.nickname}']},
156 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
157 {:published, h.(inserted_at)},
158 {:updated, h.(updated_at)},
159 {:"activity:object",
160 [
161 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
162 # For notes, federate the object id.
163 {:id, h.(activity.data["object"])}
164 ]},
165 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
166 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
167 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
168 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
169 ] ++ author ++ mentions
170 end
171
172 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
173 h = fn str -> [to_charlist(str)] end
174
175 updated_at = activity.data["published"]
176 inserted_at = activity.data["published"]
177
178 _in_reply_to = get_in_reply_to(activity.data)
179 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
180
181 retweeted_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
182 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
183
184 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
185
186 mentions = activity.recipients |> get_mentions
187
188 [
189 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
190 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
191 {:id, h.(activity.data["id"])},
192 {:title, ['#{user.nickname} repeated a notice']},
193 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
194 {:published, h.(inserted_at)},
195 {:updated, h.(updated_at)},
196 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
197 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
198 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
199 {:"activity:object", retweeted_xml}
200 ] ++ mentions ++ author
201 end
202
203 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
204 h = fn str -> [to_charlist(str)] end
205
206 updated_at = activity.data["published"]
207 inserted_at = activity.data["published"]
208
209 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
210
211 mentions = (activity.recipients || []) |> get_mentions
212
213 [
214 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
215 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
216 {:id, h.(activity.data["id"])},
217 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
218 {:content, [type: 'html'],
219 ['#{user.nickname} started following #{activity.data["object"]}']},
220 {:published, h.(inserted_at)},
221 {:updated, h.(updated_at)},
222 {:"activity:object",
223 [
224 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
225 {:id, h.(activity.data["object"])},
226 {:uri, h.(activity.data["object"])}
227 ]},
228 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
229 ] ++ mentions ++ author
230 end
231
232 # Only undos of follow for now. Will need to get redone once there are more
233 def to_simple_form(%{data: %{"type" => "Undo"}} = activity, user, with_author) do
234 h = fn str -> [to_charlist(str)] end
235
236 updated_at = activity.data["published"]
237 inserted_at = activity.data["published"]
238
239 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
240 follow_activity = Activity.get_by_ap_id(activity.data["object"])
241
242 mentions = (activity.recipients || []) |> get_mentions
243
244 [
245 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
246 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
247 {:id, h.(activity.data["id"])},
248 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
249 {:content, [type: 'html'],
250 ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
251 {:published, h.(inserted_at)},
252 {:updated, h.(updated_at)},
253 {:"activity:object",
254 [
255 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
256 {:id, h.(follow_activity.data["object"])},
257 {:uri, h.(follow_activity.data["object"])}
258 ]},
259 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
260 ] ++ mentions ++ author
261 end
262
263 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
264 h = fn str -> [to_charlist(str)] end
265
266 updated_at = activity.data["published"]
267 inserted_at = activity.data["published"]
268
269 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
270
271 [
272 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
273 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/delete']},
274 {:id, h.(activity.data["object"])},
275 {:title, ['An object was deleted']},
276 {:content, [type: 'html'], ['An object was deleted']},
277 {:published, h.(inserted_at)},
278 {:updated, h.(updated_at)}
279 ] ++ author
280 end
281
282 def to_simple_form(_, _, _), do: nil
283
284 def wrap_with_entry(simple_form) do
285 [
286 {
287 :entry,
288 [
289 xmlns: 'http://www.w3.org/2005/Atom',
290 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
291 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
292 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
293 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'
294 ],
295 simple_form
296 }
297 ]
298 end
299 end