Federate emoji out.
[akkoma] / lib / pleroma / web / ostatus / activity_representer.ex
1 defmodule Pleroma.Web.OStatus.ActivityRepresenter do
2 alias Pleroma.{Activity, User, Object}
3 alias Pleroma.Web.OStatus.UserRepresenter
4 alias Pleroma.Formatter
5 require Logger
6
7 defp get_href(id) do
8 with %Object{data: %{ "external_url" => external_url } }<- Object.get_cached_by_ap_id(id) do
9 external_url
10 else
11 _e -> id
12 end
13 end
14
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))], []}]
17 end
18
19 defp get_in_reply_to(_), do: []
20
21 defp get_mentions(to) do
22 Enum.map(to, fn (id) ->
23 cond do
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) ->
29 []
30 true ->
31 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person", href: id], []}
32 end
33 end)
34 end
35
36 defp get_links(%{local: true, data: data}) do
37 h = fn(str) -> [to_charlist(str)] end
38 [
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'], []}
41 ]
42 end
43
44 defp get_links(%{local: false,
45 data: %{
46 "object" => %{
47 "external_url" => external_url
48 }
49 }}) do
50
51 h = fn(str) -> [to_charlist(str)] end
52 [
53 {:link, [type: ['text/html'], href: h.(external_url), rel: 'alternate'], []}
54 ]
55 end
56
57 defp get_links(_activity), do: []
58
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}")], []}
62 end)
63 end
64
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
68
69 updated_at = activity.data["object"]["published"]
70 inserted_at = activity.data["object"]["published"]
71
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"])], []}
75 end)
76
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
80
81 categories = (activity.data["object"]["tag"] || [])
82 |> Enum.map(fn (tag) -> {:category, [term: to_charlist(tag)], []} end)
83
84 emoji_links = get_emoji_links(activity.data["object"]["content"] || "")
85
86 [
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
97 end
98
99 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
100 h = fn(str) -> [to_charlist(str)] end
101
102 updated_at = activity.data["published"]
103 inserted_at = activity.data["published"]
104
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
108
109 [
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.
119 ]},
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
125 end
126
127 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
128 h = fn(str) -> [to_charlist(str)] end
129
130 updated_at = activity.data["published"]
131 inserted_at = activity.data["published"]
132
133 in_reply_to = get_in_reply_to(activity.data)
134 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
135
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"])
138
139 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
140
141 mentions = activity.data["to"] |> get_mentions
142 [
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
155 end
156
157 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
158 h = fn(str) -> [to_charlist(str)] end
159
160 updated_at = activity.data["published"]
161 inserted_at = activity.data["published"]
162
163 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
164
165 mentions = (activity.data["to"] || []) |> get_mentions
166 [
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"])},
178 ]},
179 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
180 ] ++ mentions ++ author
181 end
182
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
186
187 updated_at = activity.data["published"]
188 inserted_at = activity.data["published"]
189
190 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
191 follow_activity = Activity.get_by_ap_id(activity.data["object"])
192
193 mentions = (activity.data["to"] || []) |> get_mentions
194 [
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"])},
206 ]},
207 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
208 ] ++ mentions ++ author
209 end
210
211 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
212 h = fn(str) -> [to_charlist(str)] end
213
214 updated_at = activity.data["published"]
215 inserted_at = activity.data["published"]
216
217 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
218
219 [
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)}
227 ] ++ author
228 end
229
230 def wrap_with_entry(simple_form) do
231 [{
232 :entry, [
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'
238 ], simple_form
239 }]
240 end
241
242 def to_simple_form(_, _, _), do: nil
243 end