Attempt to resolve merge conflict
[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 =
188 ([retweeted_user.ap_id] ++ activity.recipients)
189 |> Enum.uniq()
190 |> get_mentions()
191
192 [
193 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
194 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
195 {:id, h.(activity.data["id"])},
196 {:title, ['#{user.nickname} repeated a notice']},
197 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
198 {:published, h.(inserted_at)},
199 {:updated, h.(updated_at)},
200 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
201 h.(activity.data["context"])},
202 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
203 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
204 {:"activity:object", retweeted_xml}
205 ] ++ mentions ++ author
206 end
207
208 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
209 h = fn str -> [to_charlist(str)] end
210
211 updated_at = activity.data["published"]
212 inserted_at = activity.data["published"]
213
214 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
215
216 mentions = (activity.recipients || []) |> get_mentions
217
218 [
219 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
220 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
221 {:id, h.(activity.data["id"])},
222 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
223 {:content, [type: 'html'],
224 ['#{user.nickname} started following #{activity.data["object"]}']},
225 {:published, h.(inserted_at)},
226 {:updated, h.(updated_at)},
227 {:"activity:object",
228 [
229 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
230 {:id, h.(activity.data["object"])},
231 {:uri, h.(activity.data["object"])}
232 ]},
233 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
234 ] ++ mentions ++ author
235 end
236
237 # Only undos of follow for now. Will need to get redone once there are more
238 def to_simple_form(
239 %{data: %{"type" => "Undo", "object" => %{"type" => "Follow"} = follow_activity}} =
240 activity,
241 user,
242 with_author
243 ) do
244 h = fn str -> [to_charlist(str)] end
245
246 updated_at = activity.data["published"]
247 inserted_at = activity.data["published"]
248
249 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
250
251 mentions = (activity.recipients || []) |> get_mentions
252 follow_activity = Activity.normalize(follow_activity)
253
254 [
255 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
256 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
257 {:id, h.(activity.data["id"])},
258 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
259 {:content, [type: 'html'],
260 ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
261 {:published, h.(inserted_at)},
262 {:updated, h.(updated_at)},
263 {:"activity:object",
264 [
265 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
266 {:id, h.(follow_activity.data["object"])},
267 {:uri, h.(follow_activity.data["object"])}
268 ]},
269 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
270 ] ++ mentions ++ author
271 end
272
273 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
274 h = fn str -> [to_charlist(str)] end
275
276 updated_at = activity.data["published"]
277 inserted_at = activity.data["published"]
278
279 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
280
281 [
282 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
283 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/delete']},
284 {:id, h.(activity.data["object"])},
285 {:title, ['An object was deleted']},
286 {:content, [type: 'html'], ['An object was deleted']},
287 {:published, h.(inserted_at)},
288 {:updated, h.(updated_at)}
289 ] ++ author
290 end
291
292 def to_simple_form(_, _, _), do: nil
293
294 def wrap_with_entry(simple_form) do
295 [
296 {
297 :entry,
298 [
299 xmlns: 'http://www.w3.org/2005/Atom',
300 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
301 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
302 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
303 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'
304 ],
305 simple_form
306 }
307 ]
308 end
309 end