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}, %{"id" => object_id}) do
51 h = fn str -> [to_charlist(str)] end
54 {:link, [type: ['application/atom+xml'], href: h.(object_id), rel: 'self'], []},
55 {:link, [type: ['text/html'], href: h.(object_id), rel: 'alternate'], []}
59 defp get_links(%{local: false}, %{"external_url" => external_url}) do
60 h = fn str -> [to_charlist(str)] end
63 {:link, [type: ['text/html'], href: h.(external_url), rel: 'alternate'], []}
67 defp get_links(_activity, _object_data), do: []
69 defp get_emoji_links(emojis) do
70 Enum.map(emojis, fn {emoji, file} ->
71 {:link, [name: to_charlist(emoji), rel: 'emoji', href: to_charlist(file)], []}
75 def to_simple_form(activity, user, with_author \\ false)
77 def to_simple_form(%{data: %{"type" => "Create"}} = activity, user, with_author) do
78 h = fn str -> [to_charlist(str)] end
80 object = Object.normalize(activity.data["object"])
82 updated_at = object.data["published"]
83 inserted_at = object.data["published"]
86 Enum.map(object.data["attachment"] || [], fn attachment ->
87 url = hd(attachment["url"])
90 [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])],
94 in_reply_to = get_in_reply_to(activity.data)
95 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
96 mentions = activity.recipients |> get_mentions
99 (object.data["tag"] || [])
100 |> Enum.map(fn tag ->
102 {:category, [term: to_charlist(tag)], []}
109 emoji_links = get_emoji_links(object.data["emoji"] || %{})
112 if object.data["summary"] do
113 [{:summary, [], h.(object.data["summary"])}]
119 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
120 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
121 # For notes, federate the object id.
122 {:id, h.(object.data["id"])},
123 {:title, ['New note by #{user.nickname}']},
124 {:content, [type: 'html'], h.(object.data["content"] |> String.replace(~r/[\n\r]/, ""))},
125 {:published, h.(inserted_at)},
126 {:updated, h.(updated_at)},
127 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
128 h.(activity.data["context"])},
129 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []}
132 get_links(activity, object.data) ++
133 categories ++ attachments ++ in_reply_to ++ author ++ mentions ++ emoji_links
136 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
137 h = fn str -> [to_charlist(str)] end
139 updated_at = activity.data["published"]
140 inserted_at = activity.data["published"]
142 _in_reply_to = get_in_reply_to(activity.data)
143 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
144 mentions = activity.recipients |> get_mentions
147 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
148 {:id, h.(activity.data["id"])},
149 {:title, ['New favorite by #{user.nickname}']},
150 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
151 {:published, h.(inserted_at)},
152 {:updated, h.(updated_at)},
155 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
156 # For notes, federate the object id.
157 {:id, h.(activity.data["object"])}
159 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
160 h.(activity.data["context"])},
161 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
162 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
163 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
164 ] ++ author ++ mentions
167 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
168 h = fn str -> [to_charlist(str)] end
170 updated_at = activity.data["published"]
171 inserted_at = activity.data["published"]
173 _in_reply_to = get_in_reply_to(activity.data)
174 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
176 retweeted_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
177 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
179 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
182 ([retweeted_user.ap_id] ++ activity.recipients)
187 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
188 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
189 {:id, h.(activity.data["id"])},
190 {:title, ['#{user.nickname} repeated a notice']},
191 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
192 {:published, h.(inserted_at)},
193 {:updated, h.(updated_at)},
194 {:"ostatus:conversation", [ref: h.(activity.data["context"])],
195 h.(activity.data["context"])},
196 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
197 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
198 {:"activity:object", retweeted_xml}
199 ] ++ mentions ++ author
202 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
203 h = fn str -> [to_charlist(str)] end
205 updated_at = activity.data["published"]
206 inserted_at = activity.data["published"]
208 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
210 mentions = (activity.recipients || []) |> get_mentions
213 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
214 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
215 {:id, h.(activity.data["id"])},
216 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
217 {:content, [type: 'html'],
218 ['#{user.nickname} started following #{activity.data["object"]}']},
219 {:published, h.(inserted_at)},
220 {:updated, h.(updated_at)},
223 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
224 {:id, h.(activity.data["object"])},
225 {:uri, h.(activity.data["object"])}
227 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
228 ] ++ mentions ++ author
231 # Only undos of follow for now. Will need to get redone once there are more
233 %{data: %{"type" => "Undo", "object" => %{"type" => "Follow"} = follow_activity}} =
238 h = fn str -> [to_charlist(str)] end
240 updated_at = activity.data["published"]
241 inserted_at = activity.data["published"]
243 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
245 mentions = (activity.recipients || []) |> get_mentions
246 follow_activity = Activity.normalize(follow_activity)
249 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
250 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
251 {:id, h.(activity.data["id"])},
252 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
253 {:content, [type: 'html'],
254 ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
255 {:published, h.(inserted_at)},
256 {:updated, h.(updated_at)},
259 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
260 {:id, h.(follow_activity.data["object"])},
261 {:uri, h.(follow_activity.data["object"])}
263 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
264 ] ++ mentions ++ author
267 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
268 h = fn str -> [to_charlist(str)] end
270 updated_at = activity.data["published"]
271 inserted_at = activity.data["published"]
273 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
276 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
277 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/delete']},
278 {:id, h.(activity.data["object"])},
279 {:title, ['An object was deleted']},
280 {:content, [type: 'html'], ['An object was deleted']},
281 {:published, h.(inserted_at)},
282 {:updated, h.(updated_at)}
286 def to_simple_form(_, _, _), do: nil
288 def wrap_with_entry(simple_form) do
293 xmlns: 'http://www.w3.org/2005/Atom',
294 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
295 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
296 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
297 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'