1 defmodule Pleroma.Web.OStatus.ActivityRepresenter do
2 alias Pleroma.{Activity, User, Object}
3 alias Pleroma.Web.OStatus.UserRepresenter
4 alias Pleroma.Formatter
8 with %Object{data: %{"external_url" => external_url}} <- Object.get_cached_by_ap_id(id) do
15 defp get_in_reply_to(%{"object" => %{"inReplyTo" => in_reply_to}}) do
18 [ref: to_charlist(in_reply_to), href: to_charlist(get_href(in_reply_to))], []}
22 defp get_in_reply_to(_), do: []
24 defp get_mentions(to) do
27 # Special handling for the AP/Ostatus public collections
28 "https://www.w3.org/ns/activitystreams#Public" == id ->
32 "ostatus:object-type": "http://activitystrea.ms/schema/1.0/collection",
33 href: "http://activityschema.org/collection/public"
36 # Ostatus doesn't handle follower collections, ignore these.
37 Regex.match?(~r/^#{Pleroma.Web.base_url()}.+followers$/, id) ->
44 "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person",
51 defp get_links(%{local: true, data: data}) do
52 h = fn str -> [to_charlist(str)] end
55 {:link, [type: ['application/atom+xml'], href: h.(data["object"]["id"]), rel: 'self'], []},
56 {:link, [type: ['text/html'], href: h.(data["object"]["id"]), rel: 'alternate'], []}
64 "external_url" => external_url
68 h = fn str -> [to_charlist(str)] end
71 {:link, [type: ['text/html'], href: h.(external_url), rel: 'alternate'], []}
75 defp get_links(_activity), do: []
77 defp get_emoji_links(emojis) do
78 Enum.map(emojis, fn {emoji, file} ->
79 {:link, [name: to_charlist(emoji), rel: 'emoji', href: to_charlist(file)], []}
83 def to_simple_form(activity, user, with_author \\ false)
85 def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do
86 h = fn str -> [to_charlist(str)] end
88 updated_at = activity.data["object"]["published"]
89 inserted_at = activity.data["object"]["published"]
92 Enum.map(activity.data["object"]["attachment"] || [], fn attachment ->
93 url = hd(attachment["url"])
96 [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])],
100 in_reply_to = get_in_reply_to(activity.data)
101 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
102 mentions = activity.recipients |> get_mentions
105 (activity.data["object"]["tag"] || [])
106 |> Enum.map(fn tag ->
108 {:category, [term: to_charlist(tag)], []}
115 emoji_links = get_emoji_links(activity.data["object"]["emoji"] || %{})
118 if activity.data["object"]["summary"] do
119 [{:summary, [], h.(activity.data["object"]["summary"])}]
125 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
126 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
127 # For notes, federate the object id.
128 {:id, h.(activity.data["object"]["id"])},
129 {:title, ['New note by #{user.nickname}']},
130 {:content, [type: 'html'],
131 h.(activity.data["object"]["content"] |> String.replace(~r/[\n\r]/, ""))},
132 {:published, h.(inserted_at)},
133 {:updated, h.(updated_at)},
134 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
135 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []}
138 get_links(activity) ++
139 categories ++ attachments ++ in_reply_to ++ author ++ mentions ++ emoji_links
142 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
143 h = fn str -> [to_charlist(str)] end
145 updated_at = activity.data["published"]
146 inserted_at = activity.data["published"]
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
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)},
161 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
162 # For notes, federate the object id.
163 {:id, h.(activity.data["object"])}
165 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
166 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
167 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
168 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
169 ] ++ author ++ mentions
172 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
173 h = fn str -> [to_charlist(str)] end
175 updated_at = activity.data["published"]
176 inserted_at = activity.data["published"]
178 _in_reply_to = get_in_reply_to(activity.data)
179 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
181 retweeted_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
182 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
184 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
186 mentions = activity.recipients |> get_mentions
189 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
190 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
191 {:id, h.(activity.data["id"])},
192 {:title, ['#{user.nickname} repeated a notice']},
193 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
194 {:published, h.(inserted_at)},
195 {:updated, h.(updated_at)},
196 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
197 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
198 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
199 {:"activity:object", retweeted_xml}
200 ] ++ mentions ++ author
203 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
204 h = fn str -> [to_charlist(str)] end
206 updated_at = activity.data["published"]
207 inserted_at = activity.data["published"]
209 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
211 mentions = (activity.recipients || []) |> get_mentions
214 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
215 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
216 {:id, h.(activity.data["id"])},
217 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
218 {:content, [type: 'html'],
219 ['#{user.nickname} started following #{activity.data["object"]}']},
220 {:published, h.(inserted_at)},
221 {:updated, h.(updated_at)},
224 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
225 {:id, h.(activity.data["object"])},
226 {:uri, h.(activity.data["object"])}
228 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
229 ] ++ mentions ++ author
232 # Only undos of follow for now. Will need to get redone once there are more
233 def to_simple_form(%{data: %{"type" => "Undo"}} = activity, user, with_author) do
234 h = fn str -> [to_charlist(str)] end
236 updated_at = activity.data["published"]
237 inserted_at = activity.data["published"]
239 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
240 follow_activity = Activity.get_by_ap_id(activity.data["object"])
242 mentions = (activity.recipients || []) |> get_mentions
245 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
246 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
247 {:id, h.(activity.data["id"])},
248 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
249 {:content, [type: 'html'],
250 ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
251 {:published, h.(inserted_at)},
252 {:updated, h.(updated_at)},
255 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
256 {:id, h.(follow_activity.data["object"])},
257 {:uri, h.(follow_activity.data["object"])}
259 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
260 ] ++ mentions ++ author
263 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
264 h = fn str -> [to_charlist(str)] end
266 updated_at = activity.data["published"]
267 inserted_at = activity.data["published"]
269 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
272 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
273 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/delete']},
274 {:id, h.(activity.data["object"])},
275 {:title, ['An object was deleted']},
276 {:content, [type: 'html'], ['An object was deleted']},
277 {:published, h.(inserted_at)},
278 {:updated, h.(updated_at)}
282 def to_simple_form(_, _, _), do: nil
284 def wrap_with_entry(simple_form) do
289 xmlns: 'http://www.w3.org/2005/Atom',
290 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
291 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
292 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
293 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'