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"])],
135 h.(activity.data["context"])},
136 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []}
139 get_links(activity) ++
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_activity_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)
188 mentions = activity.recipients |> get_mentions
191 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
192 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
193 {:id, h.(activity.data["id"])},
194 {:title, ['#{user.nickname} repeated a notice']},
195 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
196 {:published, h.(inserted_at)},
197 {:updated, h.(updated_at)},
198 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
199 h.(activity.data["context"])},
200 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
201 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
202 {:"activity:object", retweeted_xml}
203 ] ++ mentions ++ author
206 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
207 h = fn str -> [to_charlist(str)] end
209 updated_at = activity.data["published"]
210 inserted_at = activity.data["published"]
212 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
214 mentions = (activity.recipients || []) |> get_mentions
217 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
218 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
219 {:id, h.(activity.data["id"])},
220 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
221 {:content, [type: 'html'],
222 ['#{user.nickname} started following #{activity.data["object"]}']},
223 {:published, h.(inserted_at)},
224 {:updated, h.(updated_at)},
227 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
228 {:id, h.(activity.data["object"])},
229 {:uri, h.(activity.data["object"])}
231 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
232 ] ++ mentions ++ author
235 # Only undos of follow for now. Will need to get redone once there are more
236 def to_simple_form(%{data: %{"type" => "Undo"}} = activity, user, with_author) do
237 h = fn str -> [to_charlist(str)] end
239 updated_at = activity.data["published"]
240 inserted_at = activity.data["published"]
242 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
245 if is_map(activity.data["object"]) do
246 Activity.get_by_ap_id(activity.data["object"]["id"])
248 Activity.get_by_ap_id(activity.data["object"])
251 mentions = (activity.recipients || []) |> get_mentions
254 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
255 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
256 {:id, h.(activity.data["id"])},
257 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
258 {:content, [type: 'html'],
259 ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
260 {:published, h.(inserted_at)},
261 {:updated, h.(updated_at)},
264 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
265 {:id, h.(follow_activity.data["object"])},
266 {:uri, h.(follow_activity.data["object"])}
268 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
269 ] ++ mentions ++ author
272 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
273 h = fn str -> [to_charlist(str)] end
275 updated_at = activity.data["published"]
276 inserted_at = activity.data["published"]
278 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
281 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
282 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/delete']},
283 {:id, h.(activity.data["object"])},
284 {:title, ['An object was deleted']},
285 {:content, [type: 'html'], ['An object was deleted']},
286 {:published, h.(inserted_at)},
287 {:updated, h.(updated_at)}
291 def to_simple_form(_, _, _), do: nil
293 def wrap_with_entry(simple_form) do
298 xmlns: 'http://www.w3.org/2005/Atom',
299 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
300 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
301 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
302 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'