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
12 require Pleroma.Constants
15 with %Object{data: %{"external_url" => external_url}} <- Object.get_cached_by_ap_id(id) do
22 defp get_in_reply_to(activity) do
23 with %Object{data: %{"inReplyTo" => in_reply_to}} <- Object.normalize(activity) do
26 [ref: to_charlist(in_reply_to), href: to_charlist(get_href(in_reply_to))], []}
34 defp get_mentions(to) do
37 # Special handling for the AP/Ostatus public collections
38 Pleroma.Constants.as_public() == id ->
42 "ostatus:object-type": "http://activitystrea.ms/schema/1.0/collection",
43 href: "http://activityschema.org/collection/public"
46 # Ostatus doesn't handle follower collections, ignore these.
47 Regex.match?(~r/^#{Pleroma.Web.base_url()}.+followers$/, id) ->
54 "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person",
61 defp get_links(%{local: true}, %{"id" => object_id}) do
62 h = fn str -> [to_charlist(str)] end
65 {:link, [type: ['application/atom+xml'], href: h.(object_id), rel: 'self'], []},
66 {:link, [type: ['text/html'], href: h.(object_id), rel: 'alternate'], []}
70 defp get_links(%{local: false}, %{"external_url" => external_url}) do
71 h = fn str -> [to_charlist(str)] end
74 {:link, [type: ['text/html'], href: h.(external_url), rel: 'alternate'], []}
78 defp get_links(_activity, _object_data), do: []
80 defp get_emoji_links(emojis) do
81 Enum.map(emojis, fn {emoji, file} ->
82 {:link, [name: to_charlist(emoji), rel: 'emoji', href: to_charlist(file)], []}
86 def to_simple_form(activity, user, with_author \\ false)
88 def to_simple_form(%{data: %{"type" => "Create"}} = activity, user, with_author) do
89 h = fn str -> [to_charlist(str)] end
91 object = Object.normalize(activity)
93 updated_at = object.data["published"]
94 inserted_at = object.data["published"]
97 Enum.map(object.data["attachment"] || [], fn attachment ->
98 url = hd(attachment["url"])
101 [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])],
105 in_reply_to = get_in_reply_to(activity)
106 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
107 mentions = activity.recipients |> get_mentions
110 (object.data["tag"] || [])
111 |> Enum.map(fn tag ->
113 {:category, [term: to_charlist(tag)], []}
120 emoji_links = get_emoji_links(object.data["emoji"] || %{})
123 if object.data["summary"] do
124 [{:summary, [], h.(object.data["summary"])}]
130 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
131 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
132 # For notes, federate the object id.
133 {:id, h.(object.data["id"])},
134 {:title, ['New note by #{user.nickname}']},
135 {:content, [type: 'html'], h.(object.data["content"] |> String.replace(~r/[\n\r]/, ""))},
136 {:published, h.(inserted_at)},
137 {:updated, h.(updated_at)},
138 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
139 h.(activity.data["context"])},
140 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []}
143 get_links(activity, object.data) ++
144 categories ++ attachments ++ in_reply_to ++ author ++ mentions ++ emoji_links
147 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
148 h = fn str -> [to_charlist(str)] end
150 updated_at = activity.data["published"]
151 inserted_at = activity.data["published"]
153 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
154 mentions = activity.recipients |> get_mentions
157 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
158 {:id, h.(activity.data["id"])},
159 {:title, ['New favorite by #{user.nickname}']},
160 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
161 {:published, h.(inserted_at)},
162 {:updated, h.(updated_at)},
165 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
166 # For notes, federate the object id.
167 {:id, h.(activity.data["object"])}
169 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
170 h.(activity.data["context"])},
171 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
172 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
173 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
174 ] ++ author ++ mentions
177 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
178 h = fn str -> [to_charlist(str)] end
180 updated_at = activity.data["published"]
181 inserted_at = activity.data["published"]
183 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
185 retweeted_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
186 retweeted_object = Object.normalize(retweeted_activity)
187 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
189 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
192 ([retweeted_user.ap_id] ++ activity.recipients)
197 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
198 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
199 {:id, h.(activity.data["id"])},
200 {:title, ['#{user.nickname} repeated a notice']},
201 {:content, [type: 'html'], ['RT #{retweeted_object.data["content"]}']},
202 {:published, h.(inserted_at)},
203 {:updated, h.(updated_at)},
204 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
205 h.(activity.data["context"])},
206 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
207 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
208 {:"activity:object", retweeted_xml}
209 ] ++ mentions ++ author
212 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
213 h = fn str -> [to_charlist(str)] end
215 updated_at = activity.data["published"]
216 inserted_at = activity.data["published"]
218 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
220 mentions = (activity.recipients || []) |> get_mentions
223 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
224 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
225 {:id, h.(activity.data["id"])},
226 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
227 {:content, [type: 'html'],
228 ['#{user.nickname} started following #{activity.data["object"]}']},
229 {:published, h.(inserted_at)},
230 {:updated, h.(updated_at)},
233 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
234 {:id, h.(activity.data["object"])},
235 {:uri, h.(activity.data["object"])}
237 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
238 ] ++ mentions ++ author
241 # Only undos of follow for now. Will need to get redone once there are more
243 %{data: %{"type" => "Undo", "object" => %{"type" => "Follow"} = follow_activity}} =
248 h = fn str -> [to_charlist(str)] end
250 updated_at = activity.data["published"]
251 inserted_at = activity.data["published"]
253 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
255 mentions = (activity.recipients || []) |> get_mentions
256 follow_activity = Activity.normalize(follow_activity)
259 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
260 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
261 {:id, h.(activity.data["id"])},
262 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
263 {:content, [type: 'html'],
264 ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
265 {:published, h.(inserted_at)},
266 {:updated, h.(updated_at)},
269 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
270 {:id, h.(follow_activity.data["object"])},
271 {:uri, h.(follow_activity.data["object"])}
273 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
274 ] ++ mentions ++ author
277 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
278 h = fn str -> [to_charlist(str)] end
280 updated_at = activity.data["published"]
281 inserted_at = activity.data["published"]
283 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
286 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
287 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/delete']},
288 {:id, h.(activity.data["object"])},
289 {:title, ['An object was deleted']},
290 {:content, [type: 'html'], ['An object was deleted']},
291 {:published, h.(inserted_at)},
292 {:updated, h.(updated_at)}
296 def to_simple_form(_, _, _), do: nil
298 def wrap_with_entry(simple_form) do
303 xmlns: 'http://www.w3.org/2005/Atom',
304 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
305 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
306 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
307 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'