1 defmodule Pleroma.Web.OStatus.ActivityRepresenter do
2 alias Pleroma.{Activity, User, Object}
3 alias Pleroma.Web.OStatus.UserRepresenter
7 with %Object{data: %{"external_url" => external_url}} <- Object.get_cached_by_ap_id(id) do
14 defp get_in_reply_to(%{"object" => %{"inReplyTo" => in_reply_to}}) do
17 [ref: to_charlist(in_reply_to), href: to_charlist(get_href(in_reply_to))], []}
21 defp get_in_reply_to(_), do: []
23 defp get_mentions(to) do
26 # Special handling for the AP/Ostatus public collections
27 "https://www.w3.org/ns/activitystreams#Public" == id ->
31 "ostatus:object-type": "http://activitystrea.ms/schema/1.0/collection",
32 href: "http://activityschema.org/collection/public"
35 # Ostatus doesn't handle follower collections, ignore these.
36 Regex.match?(~r/^#{Pleroma.Web.base_url()}.+followers$/, id) ->
43 "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person",
50 defp get_links(%{local: true, data: data}) do
51 h = fn str -> [to_charlist(str)] end
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'], []}
63 "external_url" => external_url
67 h = fn str -> [to_charlist(str)] end
70 {:link, [type: ['text/html'], href: h.(external_url), rel: 'alternate'], []}
74 defp get_links(_activity), 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: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do
85 h = fn str -> [to_charlist(str)] end
87 updated_at = activity.data["object"]["published"]
88 inserted_at = activity.data["object"]["published"]
91 Enum.map(activity.data["object"]["attachment"] || [], fn attachment ->
92 url = hd(attachment["url"])
95 [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])],
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
104 (activity.data["object"]["tag"] || [])
105 |> Enum.map(fn tag ->
107 {:category, [term: to_charlist(tag)], []}
114 emoji_links = get_emoji_links(activity.data["object"]["emoji"] || %{})
117 if activity.data["object"]["summary"] do
118 [{:summary, [], h.(activity.data["object"]["summary"])}]
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'], []}
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"])],
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
173 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
174 h = fn str -> [to_charlist(str)] end
176 updated_at = activity.data["published"]
177 inserted_at = activity.data["published"]
179 _in_reply_to = get_in_reply_to(activity.data)
180 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
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"])
185 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
188 ([retweeted_user.ap_id] ++ activity.recipients)
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
208 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
209 h = fn str -> [to_charlist(str)] end
211 updated_at = activity.data["published"]
212 inserted_at = activity.data["published"]
214 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
216 mentions = (activity.recipients || []) |> get_mentions
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)},
229 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
230 {:id, h.(activity.data["object"])},
231 {:uri, h.(activity.data["object"])}
233 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
234 ] ++ mentions ++ author
237 # Only undos of follow for now. Will need to get redone once there are more
239 %{data: %{"type" => "Undo", "object" => %{"type" => "Follow"} = follow_activity}} =
244 h = fn str -> [to_charlist(str)] end
246 updated_at = activity.data["published"]
247 inserted_at = activity.data["published"]
249 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
251 mentions = (activity.recipients || []) |> get_mentions
252 follow_activity = Activity.normalize(follow_activity)
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)},
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"])}
269 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
270 ] ++ mentions ++ author
273 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
274 h = fn str -> [to_charlist(str)] end
276 updated_at = activity.data["published"]
277 inserted_at = activity.data["published"]
279 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
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)}
292 def to_simple_form(_, _, _), do: nil
294 def wrap_with_entry(simple_form) do
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'