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