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
16 [{:"thr:in-reply-to", [ref: to_charlist(in_reply_to), href: to_charlist(get_href(in_reply_to))], []}]
19 defp get_in_reply_to(_), do: []
21 defp get_mentions(to) do
22 Enum.map(to, fn (id) ->
24 # Special handling for the AP/Ostatus public collections
25 "https://www.w3.org/ns/activitystreams#Public" == id ->
26 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/collection", href: "http://activityschema.org/collection/public"], []}
27 # Ostatus doesn't handle follower collections, ignore these.
28 Regex.match?(~r/^#{Pleroma.Web.base_url}.+followers$/, id) ->
31 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person", href: id], []}
36 defp get_links(%{local: true, data: data}) do
37 h = fn(str) -> [to_charlist(str)] end
39 {:link, [type: ['application/atom+xml'], href: h.(data["object"]["id"]), rel: 'self'], []},
40 {:link, [type: ['text/html'], href: h.(data["object"]["id"]), rel: 'alternate'], []}
44 defp get_links(%{local: false,
47 "external_url" => external_url
51 h = fn(str) -> [to_charlist(str)] end
53 {:link, [type: ['text/html'], href: h.(external_url), rel: 'alternate'], []}
57 defp get_links(_activity), do: []
59 defp get_emoji_links(emojis) do
60 Enum.map(emojis, fn({emoji, file}) ->
61 {:link, [name: to_charlist(emoji), rel: 'emoji', href: to_charlist(file)], []}
65 def to_simple_form(activity, user, with_author \\ false)
66 def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do
67 h = fn(str) -> [to_charlist(str)] end
69 updated_at = activity.data["object"]["published"]
70 inserted_at = activity.data["object"]["published"]
72 attachments = Enum.map(activity.data["object"]["attachment"] || [], fn(attachment) ->
73 url = hd(attachment["url"])
74 {:link, [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])], []}
77 in_reply_to = get_in_reply_to(activity.data)
78 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
79 mentions = activity.recipients |> get_mentions
81 categories = (activity.data["object"]["tag"] || [])
82 |> Enum.map(fn (tag) ->
84 {:category, [term: to_charlist(tag)], []}
91 emoji_links = get_emoji_links(activity.data["object"]["emoji"] || %{})
93 summary = if activity.data["object"]["summary"] do
94 [{:summary, [], h.(activity.data["object"]["summary"])}]
100 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
101 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
102 {:id, h.(activity.data["object"]["id"])}, # For notes, federate the object id.
103 {:title, ['New note by #{user.nickname}']},
104 {:content, [type: 'html'], h.(activity.data["object"]["content"] |> String.replace(~r/[\n\r]/, ""))},
105 {:published, h.(inserted_at)},
106 {:updated, h.(updated_at)},
107 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
108 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
109 ] ++ summary ++ get_links(activity) ++ categories ++ attachments ++ in_reply_to ++ author ++ mentions ++ emoji_links
112 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
113 h = fn(str) -> [to_charlist(str)] end
115 updated_at = activity.data["published"]
116 inserted_at = activity.data["published"]
118 _in_reply_to = get_in_reply_to(activity.data)
119 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
120 mentions = activity.recipients |> get_mentions
123 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
124 {:id, h.(activity.data["id"])},
125 {:title, ['New favorite by #{user.nickname}']},
126 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
127 {:published, h.(inserted_at)},
128 {:updated, h.(updated_at)},
129 {:"activity:object", [
130 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
131 {:id, h.(activity.data["object"])}, # For notes, federate the object id.
133 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
134 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
135 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
136 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
137 ] ++ author ++ mentions
140 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
141 h = fn(str) -> [to_charlist(str)] end
143 updated_at = activity.data["published"]
144 inserted_at = activity.data["published"]
146 _in_reply_to = get_in_reply_to(activity.data)
147 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
149 retweeted_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
150 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
152 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
154 mentions = activity.recipients |> get_mentions
156 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
157 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
158 {:id, h.(activity.data["id"])},
159 {:title, ['#{user.nickname} repeated a notice']},
160 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
161 {:published, h.(inserted_at)},
162 {:updated, h.(updated_at)},
163 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
164 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
165 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
166 {:"activity:object", retweeted_xml}
167 ] ++ mentions ++ author
170 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
171 h = fn(str) -> [to_charlist(str)] end
173 updated_at = activity.data["published"]
174 inserted_at = activity.data["published"]
176 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
178 mentions = (activity.recipients || []) |> get_mentions
180 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
181 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
182 {:id, h.(activity.data["id"])},
183 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
184 {:content, [type: 'html'], ['#{user.nickname} started following #{activity.data["object"]}']},
185 {:published, h.(inserted_at)},
186 {:updated, h.(updated_at)},
187 {:"activity:object", [
188 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
189 {:id, h.(activity.data["object"])},
190 {:uri, h.(activity.data["object"])},
192 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
193 ] ++ mentions ++ author
196 # Only undos of follow for now. Will need to get redone once there are more
197 def to_simple_form(%{data: %{"type" => "Undo"}} = activity, user, with_author) do
198 h = fn(str) -> [to_charlist(str)] end
200 updated_at = activity.data["published"]
201 inserted_at = activity.data["published"]
203 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
204 follow_activity = Activity.get_by_ap_id(activity.data["object"])
206 mentions = (activity.recipients || []) |> get_mentions
208 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
209 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
210 {:id, h.(activity.data["id"])},
211 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
212 {:content, [type: 'html'], ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
213 {:published, h.(inserted_at)},
214 {:updated, h.(updated_at)},
215 {:"activity:object", [
216 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
217 {:id, h.(follow_activity.data["object"])},
218 {:uri, h.(follow_activity.data["object"])},
220 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
221 ] ++ mentions ++ author
224 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
225 h = fn(str) -> [to_charlist(str)] end
227 updated_at = activity.data["published"]
228 inserted_at = activity.data["published"]
230 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
233 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
234 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/delete']},
235 {:id, h.(activity.data["object"])},
236 {:title, ['An object was deleted']},
237 {:content, [type: 'html'], ['An object was deleted']},
238 {:published, h.(inserted_at)},
239 {:updated, h.(updated_at)}
243 def to_simple_form(_, _, _), do: nil
245 def wrap_with_entry(simple_form) do
248 xmlns: 'http://www.w3.org/2005/Atom',
249 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
250 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
251 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
252 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'