1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OStatus.ActivityRepresenter do
9 alias Pleroma.Web.OStatus.UserRepresenter
14 with %Object{data: %{"external_url" => external_url}} <- Object.get_cached_by_ap_id(id) do
21 defp get_in_reply_to(%{"object" => %{"inReplyTo" => in_reply_to}}) do
24 [ref: to_charlist(in_reply_to), href: to_charlist(get_href(in_reply_to))], []}
28 defp get_in_reply_to(_), do: []
30 defp get_mentions(to) do
33 # Special handling for the AP/Ostatus public collections
34 "https://www.w3.org/ns/activitystreams#Public" == id ->
38 "ostatus:object-type": "http://activitystrea.ms/schema/1.0/collection",
39 href: "http://activityschema.org/collection/public"
42 # Ostatus doesn't handle follower collections, ignore these.
43 Regex.match?(~r/^#{Pleroma.Web.base_url()}.+followers$/, id) ->
50 "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person",
57 defp get_links(%{local: true, data: data}) do
58 h = fn str -> [to_charlist(str)] end
61 {:link, [type: ['application/atom+xml'], href: h.(data["object"]["id"]), rel: 'self'], []},
62 {:link, [type: ['text/html'], href: h.(data["object"]["id"]), rel: 'alternate'], []}
70 "external_url" => external_url
74 h = fn str -> [to_charlist(str)] end
77 {:link, [type: ['text/html'], href: h.(external_url), rel: 'alternate'], []}
81 defp get_links(_activity), do: []
83 defp get_emoji_links(emojis) do
84 Enum.map(emojis, fn {emoji, file} ->
85 {:link, [name: to_charlist(emoji), rel: 'emoji', href: to_charlist(file)], []}
89 def to_simple_form(activity, user, with_author \\ false)
91 def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do
92 h = fn str -> [to_charlist(str)] end
94 updated_at = activity.data["object"]["published"]
95 inserted_at = activity.data["object"]["published"]
98 Enum.map(activity.data["object"]["attachment"] || [], fn attachment ->
99 url = hd(attachment["url"])
102 [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])],
106 in_reply_to = get_in_reply_to(activity.data)
107 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
108 mentions = activity.recipients |> get_mentions
111 (activity.data["object"]["tag"] || [])
112 |> Enum.map(fn tag ->
114 {:category, [term: to_charlist(tag)], []}
121 emoji_links = get_emoji_links(activity.data["object"]["emoji"] || %{})
124 if activity.data["object"]["summary"] do
125 [{:summary, [], h.(activity.data["object"]["summary"])}]
131 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
132 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
133 # For notes, federate the object id.
134 {:id, h.(activity.data["object"]["id"])},
135 {:title, ['New note by #{user.nickname}']},
136 {:content, [type: 'html'],
137 h.(activity.data["object"]["content"] |> String.replace(~r/[\n\r]/, ""))},
138 {:published, h.(inserted_at)},
139 {:updated, h.(updated_at)},
140 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
141 h.(activity.data["context"])},
142 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []}
145 get_links(activity) ++
146 categories ++ attachments ++ in_reply_to ++ author ++ mentions ++ emoji_links
149 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
150 h = fn str -> [to_charlist(str)] end
152 updated_at = activity.data["published"]
153 inserted_at = activity.data["published"]
155 _in_reply_to = get_in_reply_to(activity.data)
156 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
157 mentions = activity.recipients |> get_mentions
160 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
161 {:id, h.(activity.data["id"])},
162 {:title, ['New favorite by #{user.nickname}']},
163 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
164 {:published, h.(inserted_at)},
165 {:updated, h.(updated_at)},
168 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
169 # For notes, federate the object id.
170 {:id, h.(activity.data["object"])}
172 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
173 h.(activity.data["context"])},
174 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
175 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
176 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
177 ] ++ author ++ mentions
180 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
181 h = fn str -> [to_charlist(str)] end
183 updated_at = activity.data["published"]
184 inserted_at = activity.data["published"]
186 _in_reply_to = get_in_reply_to(activity.data)
187 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
189 retweeted_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
190 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
192 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
195 ([retweeted_user.ap_id] ++ activity.recipients)
200 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
201 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
202 {:id, h.(activity.data["id"])},
203 {:title, ['#{user.nickname} repeated a notice']},
204 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
205 {:published, h.(inserted_at)},
206 {:updated, h.(updated_at)},
207 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
208 h.(activity.data["context"])},
209 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
210 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
211 {:"activity:object", retweeted_xml}
212 ] ++ mentions ++ author
215 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
216 h = fn str -> [to_charlist(str)] end
218 updated_at = activity.data["published"]
219 inserted_at = activity.data["published"]
221 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
223 mentions = (activity.recipients || []) |> get_mentions
226 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
227 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
228 {:id, h.(activity.data["id"])},
229 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
230 {:content, [type: 'html'],
231 ['#{user.nickname} started following #{activity.data["object"]}']},
232 {:published, h.(inserted_at)},
233 {:updated, h.(updated_at)},
236 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
237 {:id, h.(activity.data["object"])},
238 {:uri, h.(activity.data["object"])}
240 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
241 ] ++ mentions ++ author
244 # Only undos of follow for now. Will need to get redone once there are more
246 %{data: %{"type" => "Undo", "object" => %{"type" => "Follow"} = follow_activity}} =
251 h = fn str -> [to_charlist(str)] end
253 updated_at = activity.data["published"]
254 inserted_at = activity.data["published"]
256 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
258 mentions = (activity.recipients || []) |> get_mentions
259 follow_activity = Activity.normalize(follow_activity)
262 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
263 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
264 {:id, h.(activity.data["id"])},
265 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
266 {:content, [type: 'html'],
267 ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
268 {:published, h.(inserted_at)},
269 {:updated, h.(updated_at)},
272 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
273 {:id, h.(follow_activity.data["object"])},
274 {:uri, h.(follow_activity.data["object"])}
276 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
277 ] ++ mentions ++ author
280 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
281 h = fn str -> [to_charlist(str)] end
283 updated_at = activity.data["published"]
284 inserted_at = activity.data["published"]
286 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
289 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
290 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/delete']},
291 {:id, h.(activity.data["object"])},
292 {:title, ['An object was deleted']},
293 {:content, [type: 'html'], ['An object was deleted']},
294 {:published, h.(inserted_at)},
295 {:updated, h.(updated_at)}
299 def to_simple_form(_, _, _), do: nil
301 def wrap_with_entry(simple_form) do
306 xmlns: 'http://www.w3.org/2005/Atom',
307 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
308 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
309 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
310 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'