1 defmodule Pleroma.Web.OStatus.ActivityRepresenter do
2 alias Pleroma.{Activity, User}
3 alias Pleroma.Web.OStatus.UserRepresenter
6 defp get_in_reply_to(%{"object" => %{"inReplyTo" => in_reply_to}}) do
7 [{:"thr:in-reply-to", [ref: to_charlist(in_reply_to)], []}]
10 defp get_in_reply_to(_), do: []
12 defp get_mentions(to) do
13 Enum.map(to, fn (id) ->
15 # Special handling for the AP/Ostatus public collections
16 "https://www.w3.org/ns/activitystreams#Public" == id ->
17 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/collection", href: "http://activityschema.org/collection/public"], []}
18 # Ostatus doesn't handle follower collections, ignore these.
19 Regex.match?(~r/^#{Pleroma.Web.base_url}.+followers$/, id) ->
22 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person", href: id], []}
27 defp get_links(%{local: true, data: data}) do
28 h = fn(str) -> [to_charlist(str)] end
30 {:link, [type: ['application/atom+xml'], href: h.(data["object"]["id"]), rel: 'self'], []},
31 {:link, [type: ['text/html'], href: h.(data["object"]["id"]), rel: 'alternate'], []}
35 defp get_links(%{local: false,
38 "external_url" => external_url
42 h = fn(str) -> [to_charlist(str)] end
44 {:link, [type: ['text/html'], href: h.(external_url), rel: 'alternate'], []}
48 defp get_links(_activity), do: []
50 def to_simple_form(activity, user, with_author \\ false)
51 def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do
52 h = fn(str) -> [to_charlist(str)] end
54 updated_at = activity.data["object"]["published"]
55 inserted_at = activity.data["object"]["published"]
57 attachments = Enum.map(activity.data["object"]["attachment"] || [], fn(attachment) ->
58 url = hd(attachment["url"])
59 {:link, [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])], []}
62 in_reply_to = get_in_reply_to(activity.data)
63 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
64 mentions = activity.data["to"] |> get_mentions
66 categories = (activity.data["object"]["tag"] || [])
67 |> Enum.map(fn (tag) -> {:category, [term: to_charlist(tag)], []} end)
70 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
71 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
72 {:id, h.(activity.data["object"]["id"])}, # For notes, federate the object id.
73 {:title, ['New note by #{user.nickname}']},
74 {:content, [type: 'html'], h.(activity.data["object"]["content"] |> String.replace(~r/[\n\r]/, ""))},
75 {:published, h.(inserted_at)},
76 {:updated, h.(updated_at)},
77 {:"ostatus:conversation", [], h.(activity.data["context"])},
78 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
79 ] ++ get_links(activity) ++ categories ++ attachments ++ in_reply_to ++ author ++ mentions
82 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
83 h = fn(str) -> [to_charlist(str)] end
85 updated_at = activity.data["published"]
86 inserted_at = activity.data["published"]
88 in_reply_to = get_in_reply_to(activity.data)
89 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
90 mentions = activity.data["to"] |> get_mentions
93 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
94 {:id, h.(activity.data["id"])},
95 {:title, ['New favorite by #{user.nickname}']},
96 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
97 {:published, h.(inserted_at)},
98 {:updated, h.(updated_at)},
99 {:"activity:object", [
100 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
101 {:id, h.(activity.data["object"])}, # For notes, federate the object id.
103 {:"ostatus:conversation", [], h.(activity.data["context"])},
104 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
105 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
106 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
107 ] ++ author ++ mentions
110 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
111 h = fn(str) -> [to_charlist(str)] end
113 updated_at = activity.data["published"]
114 inserted_at = activity.data["published"]
116 in_reply_to = get_in_reply_to(activity.data)
117 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
119 retweeted_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
120 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
122 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
124 mentions = activity.data["to"] |> get_mentions
126 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
127 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
128 {:id, h.(activity.data["id"])},
129 {:title, ['#{user.nickname} repeated a notice']},
130 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
131 {:published, h.(inserted_at)},
132 {:updated, h.(updated_at)},
133 {:"ostatus:conversation", [], 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 {:"activity:object", retweeted_xml}
137 ] ++ mentions ++ author
140 def to_simple_form(%{data: %{"type" => "Follow"}} = 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 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
148 mentions = (activity.data["to"] || []) |> get_mentions
150 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
151 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
152 {:id, h.(activity.data["id"])},
153 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
154 {:content, [type: 'html'], ['#{user.nickname} started following #{activity.data["object"]}']},
155 {:published, h.(inserted_at)},
156 {:updated, h.(updated_at)},
157 {:"activity:object", [
158 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
159 {:id, h.(activity.data["object"])},
160 {:uri, h.(activity.data["object"])},
162 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
163 ] ++ mentions ++ author
166 # Only undos of follow for now. Will need to get redone once there are more
167 def to_simple_form(%{data: %{"type" => "Undo"}} = 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 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
174 follow_activity = Activity.get_by_ap_id(activity.data["object"])
176 mentions = (activity.data["to"] || []) |> get_mentions
178 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
179 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
180 {:id, h.(activity.data["id"])},
181 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
182 {:content, [type: 'html'], ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
183 {:published, h.(inserted_at)},
184 {:updated, h.(updated_at)},
185 {:"activity:object", [
186 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
187 {:id, h.(follow_activity.data["object"])},
188 {:uri, h.(follow_activity.data["object"])},
190 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
191 ] ++ mentions ++ author
194 def wrap_with_entry(simple_form) do
197 xmlns: 'http://www.w3.org/2005/Atom',
198 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
199 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
200 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
201 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'
206 def to_simple_form(_, _, _), do: nil