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