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.data["to"] |> get_mentions
81 categories = (activity.data["object"]["tag"] || [])
82 |> Enum.map(fn (tag) -> {:category, [term: to_charlist(tag)], []} end)
84 emoji_links = get_emoji_links(activity.data["object"]["emoji"] || %{})
86 summary = if activity.data["object"]["summary"] do
87 [{:summary, [], h.(activity.data["object"]["summary"])}]
93 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
94 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
95 {:id, h.(activity.data["object"]["id"])}, # For notes, federate the object id.
96 {:title, ['New note by #{user.nickname}']},
97 {:content, [type: 'html'], h.(activity.data["object"]["content"] |> String.replace(~r/[\n\r]/, ""))},
98 {:published, h.(inserted_at)},
99 {:updated, h.(updated_at)},
100 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
101 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
102 ] ++ summary ++ get_links(activity) ++ categories ++ attachments ++ in_reply_to ++ author ++ mentions ++ emoji_links
105 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
106 h = fn(str) -> [to_charlist(str)] end
108 updated_at = activity.data["published"]
109 inserted_at = activity.data["published"]
111 _in_reply_to = get_in_reply_to(activity.data)
112 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
113 mentions = activity.data["to"] |> get_mentions
116 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
117 {:id, h.(activity.data["id"])},
118 {:title, ['New favorite by #{user.nickname}']},
119 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
120 {:published, h.(inserted_at)},
121 {:updated, h.(updated_at)},
122 {:"activity:object", [
123 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
124 {:id, h.(activity.data["object"])}, # For notes, federate the object id.
126 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
127 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
128 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
129 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
130 ] ++ author ++ mentions
133 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
134 h = fn(str) -> [to_charlist(str)] end
136 updated_at = activity.data["published"]
137 inserted_at = activity.data["published"]
139 _in_reply_to = get_in_reply_to(activity.data)
140 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
142 retweeted_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
143 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
145 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
147 mentions = activity.data["to"] |> get_mentions
149 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
150 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
151 {:id, h.(activity.data["id"])},
152 {:title, ['#{user.nickname} repeated a notice']},
153 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
154 {:published, h.(inserted_at)},
155 {:updated, h.(updated_at)},
156 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
157 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
158 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
159 {:"activity:object", retweeted_xml}
160 ] ++ mentions ++ author
163 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
164 h = fn(str) -> [to_charlist(str)] end
166 updated_at = activity.data["published"]
167 inserted_at = activity.data["published"]
169 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
171 mentions = (activity.data["to"] || []) |> get_mentions
173 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
174 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
175 {:id, h.(activity.data["id"])},
176 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
177 {:content, [type: 'html'], ['#{user.nickname} started following #{activity.data["object"]}']},
178 {:published, h.(inserted_at)},
179 {:updated, h.(updated_at)},
180 {:"activity:object", [
181 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
182 {:id, h.(activity.data["object"])},
183 {:uri, h.(activity.data["object"])},
185 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
186 ] ++ mentions ++ author
189 # Only undos of follow for now. Will need to get redone once there are more
190 def to_simple_form(%{data: %{"type" => "Undo"}} = activity, user, with_author) do
191 h = fn(str) -> [to_charlist(str)] end
193 updated_at = activity.data["published"]
194 inserted_at = activity.data["published"]
196 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
197 follow_activity = Activity.get_by_ap_id(activity.data["object"])
199 mentions = (activity.data["to"] || []) |> get_mentions
201 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
202 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
203 {:id, h.(activity.data["id"])},
204 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
205 {:content, [type: 'html'], ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
206 {:published, h.(inserted_at)},
207 {:updated, h.(updated_at)},
208 {:"activity:object", [
209 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
210 {:id, h.(follow_activity.data["object"])},
211 {:uri, h.(follow_activity.data["object"])},
213 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
214 ] ++ mentions ++ author
217 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
218 h = fn(str) -> [to_charlist(str)] end
220 updated_at = activity.data["published"]
221 inserted_at = activity.data["published"]
223 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
226 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
227 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/delete']},
228 {:id, h.(activity.data["object"])},
229 {:title, ['An object was deleted']},
230 {:content, [type: 'html'], ['An object was deleted']},
231 {:published, h.(inserted_at)},
232 {:updated, h.(updated_at)}
236 def to_simple_form(_, _, _), do: nil
238 def wrap_with_entry(simple_form) do
241 xmlns: 'http://www.w3.org/2005/Atom',
242 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
243 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
244 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
245 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'