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}, %{"id" => object_id}) do
58 h = fn str -> [to_charlist(str)] end
61 {:link, [type: ['application/atom+xml'], href: h.(object_id), rel: 'self'], []},
62 {:link, [type: ['text/html'], href: h.(object_id), rel: 'alternate'], []}
66 defp get_links(%{local: false}, %{"external_url" => external_url}) do
67 h = fn str -> [to_charlist(str)] end
70 {:link, [type: ['text/html'], href: h.(external_url), rel: 'alternate'], []}
74 defp get_links(_activity, _object_data), do: []
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)], []}
82 def to_simple_form(activity, user, with_author \\ false)
84 def to_simple_form(%{data: %{"type" => "Create"}} = activity, user, with_author) do
85 h = fn str -> [to_charlist(str)] end
87 object = Object.normalize(activity)
89 updated_at = object.data["published"]
90 inserted_at = object.data["published"]
93 Enum.map(object.data["attachment"] || [], fn attachment ->
94 url = hd(attachment["url"])
97 [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])],
101 in_reply_to = get_in_reply_to(activity.data)
102 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
103 mentions = activity.recipients |> get_mentions
106 (object.data["tag"] || [])
107 |> Enum.map(fn tag ->
109 {:category, [term: to_charlist(tag)], []}
116 emoji_links = get_emoji_links(object.data["emoji"] || %{})
119 if object.data["summary"] do
120 [{:summary, [], h.(object.data["summary"])}]
126 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
127 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
128 # For notes, federate the object id.
129 {:id, h.(object.data["id"])},
130 {:title, ['New note by #{user.nickname}']},
131 {:content, [type: 'html'], h.(object.data["content"] |> String.replace(~r/[\n\r]/, ""))},
132 {:published, h.(inserted_at)},
133 {:updated, h.(updated_at)},
134 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
135 h.(activity.data["context"])},
136 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []}
139 get_links(activity, object.data) ++
140 categories ++ attachments ++ in_reply_to ++ author ++ mentions ++ emoji_links
143 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
144 h = fn str -> [to_charlist(str)] end
146 updated_at = activity.data["published"]
147 inserted_at = activity.data["published"]
149 _in_reply_to = get_in_reply_to(activity.data)
150 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
151 mentions = activity.recipients |> get_mentions
154 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
155 {:id, h.(activity.data["id"])},
156 {:title, ['New favorite by #{user.nickname}']},
157 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
158 {:published, h.(inserted_at)},
159 {:updated, h.(updated_at)},
162 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
163 # For notes, federate the object id.
164 {:id, h.(activity.data["object"])}
166 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
167 h.(activity.data["context"])},
168 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
169 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
170 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
171 ] ++ author ++ mentions
174 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
175 h = fn str -> [to_charlist(str)] end
177 updated_at = activity.data["published"]
178 inserted_at = activity.data["published"]
180 _in_reply_to = get_in_reply_to(activity.data)
181 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
183 retweeted_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
184 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
186 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
189 ([retweeted_user.ap_id] ++ activity.recipients)
194 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
195 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
196 {:id, h.(activity.data["id"])},
197 {:title, ['#{user.nickname} repeated a notice']},
198 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
199 {:published, h.(inserted_at)},
200 {:updated, h.(updated_at)},
201 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
202 h.(activity.data["context"])},
203 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
204 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
205 {:"activity:object", retweeted_xml}
206 ] ++ mentions ++ author
209 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
210 h = fn str -> [to_charlist(str)] end
212 updated_at = activity.data["published"]
213 inserted_at = activity.data["published"]
215 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
217 mentions = (activity.recipients || []) |> get_mentions
220 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
221 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
222 {:id, h.(activity.data["id"])},
223 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
224 {:content, [type: 'html'],
225 ['#{user.nickname} started following #{activity.data["object"]}']},
226 {:published, h.(inserted_at)},
227 {:updated, h.(updated_at)},
230 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
231 {:id, h.(activity.data["object"])},
232 {:uri, h.(activity.data["object"])}
234 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
235 ] ++ mentions ++ author
238 # Only undos of follow for now. Will need to get redone once there are more
240 %{data: %{"type" => "Undo", "object" => %{"type" => "Follow"} = follow_activity}} =
245 h = fn str -> [to_charlist(str)] end
247 updated_at = activity.data["published"]
248 inserted_at = activity.data["published"]
250 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
252 mentions = (activity.recipients || []) |> get_mentions
253 follow_activity = Activity.normalize(follow_activity)
256 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
257 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
258 {:id, h.(activity.data["id"])},
259 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
260 {:content, [type: 'html'],
261 ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
262 {:published, h.(inserted_at)},
263 {:updated, h.(updated_at)},
266 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
267 {:id, h.(follow_activity.data["object"])},
268 {:uri, h.(follow_activity.data["object"])}
270 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
271 ] ++ mentions ++ author
274 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
275 h = fn str -> [to_charlist(str)] end
277 updated_at = activity.data["published"]
278 inserted_at = activity.data["published"]
280 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
283 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
284 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/delete']},
285 {:id, h.(activity.data["object"])},
286 {:title, ['An object was deleted']},
287 {:content, [type: 'html'], ['An object was deleted']},
288 {:published, h.(inserted_at)},
289 {:updated, h.(updated_at)}
293 def to_simple_form(_, _, _), do: nil
295 def wrap_with_entry(simple_form) do
300 xmlns: 'http://www.w3.org/2005/Atom',
301 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
302 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
303 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
304 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'