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(content) do
60 Enum.map(Formatter.get_emoji(content), fn({emoji, file}) ->
61 {:link, [name: to_charlist(emoji), rel: 'emoji', href: to_charlist("#{Pleroma.Web.Endpoint.static_url}#{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"]["content"] || "")
87 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
88 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
89 {:id, h.(activity.data["object"]["id"])}, # For notes, federate the object id.
90 {:title, ['New note by #{user.nickname}']},
91 {:content, [type: 'html'], h.(activity.data["object"]["content"] |> String.replace(~r/[\n\r]/, ""))},
92 {:published, h.(inserted_at)},
93 {:updated, h.(updated_at)},
94 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
95 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
96 ] ++ get_links(activity) ++ categories ++ attachments ++ in_reply_to ++ author ++ mentions ++ emoji_links
99 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
100 h = fn(str) -> [to_charlist(str)] end
102 updated_at = activity.data["published"]
103 inserted_at = activity.data["published"]
105 in_reply_to = get_in_reply_to(activity.data)
106 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
107 mentions = activity.data["to"] |> get_mentions
110 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
111 {:id, h.(activity.data["id"])},
112 {:title, ['New favorite by #{user.nickname}']},
113 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
114 {:published, h.(inserted_at)},
115 {:updated, h.(updated_at)},
116 {:"activity:object", [
117 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
118 {:id, h.(activity.data["object"])}, # For notes, federate the object id.
120 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
121 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
122 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
123 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
124 ] ++ author ++ mentions
127 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
128 h = fn(str) -> [to_charlist(str)] end
130 updated_at = activity.data["published"]
131 inserted_at = activity.data["published"]
133 in_reply_to = get_in_reply_to(activity.data)
134 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
136 retweeted_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
137 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
139 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
141 mentions = activity.data["to"] |> get_mentions
143 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
144 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
145 {:id, h.(activity.data["id"])},
146 {:title, ['#{user.nickname} repeated a notice']},
147 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
148 {:published, h.(inserted_at)},
149 {:updated, h.(updated_at)},
150 {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
151 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
152 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
153 {:"activity:object", retweeted_xml}
154 ] ++ mentions ++ author
157 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
158 h = fn(str) -> [to_charlist(str)] end
160 updated_at = activity.data["published"]
161 inserted_at = activity.data["published"]
163 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
165 mentions = (activity.data["to"] || []) |> get_mentions
167 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
168 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
169 {:id, h.(activity.data["id"])},
170 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
171 {:content, [type: 'html'], ['#{user.nickname} started following #{activity.data["object"]}']},
172 {:published, h.(inserted_at)},
173 {:updated, h.(updated_at)},
174 {:"activity:object", [
175 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
176 {:id, h.(activity.data["object"])},
177 {:uri, h.(activity.data["object"])},
179 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
180 ] ++ mentions ++ author
183 # Only undos of follow for now. Will need to get redone once there are more
184 def to_simple_form(%{data: %{"type" => "Undo"}} = activity, user, with_author) do
185 h = fn(str) -> [to_charlist(str)] end
187 updated_at = activity.data["published"]
188 inserted_at = activity.data["published"]
190 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
191 follow_activity = Activity.get_by_ap_id(activity.data["object"])
193 mentions = (activity.data["to"] || []) |> get_mentions
195 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
196 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
197 {:id, h.(activity.data["id"])},
198 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
199 {:content, [type: 'html'], ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
200 {:published, h.(inserted_at)},
201 {:updated, h.(updated_at)},
202 {:"activity:object", [
203 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
204 {:id, h.(follow_activity.data["object"])},
205 {:uri, h.(follow_activity.data["object"])},
207 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
208 ] ++ mentions ++ author
211 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
212 h = fn(str) -> [to_charlist(str)] end
214 updated_at = activity.data["published"]
215 inserted_at = activity.data["published"]
217 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
220 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
221 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/delete']},
222 {:id, h.(activity.data["object"])},
223 {:title, ['An object was deleted']},
224 {:content, [type: 'html'], ['An object was deleted']},
225 {:published, h.(inserted_at)},
226 {:updated, h.(updated_at)}
230 def wrap_with_entry(simple_form) do
233 xmlns: 'http://www.w3.org/2005/Atom',
234 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
235 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
236 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
237 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'
242 def to_simple_form(_, _, _), do: nil