Hotfix for tag problems.
[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(emojis) do
60 Enum.map(emojis, fn({emoji, file}) ->
61 {:link, [name: to_charlist(emoji), rel: 'emoji', href: to_charlist(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.recipients |> get_mentions
80
81 categories = (activity.data["object"]["tag"] || [])
82 |> Enum.map(fn (tag) ->
83 if is_binary(tag) do
84 {:category, [term: to_charlist(tag)], []}
85 else
86 nil
87 end
88 end)
89 |> Enum.filter(&(&1))
90
91 emoji_links = get_emoji_links(activity.data["object"]["emoji"] || %{})
92
93 summary = if activity.data["object"]["summary"] do
94 [{:summary, [], h.(activity.data["object"]["summary"])}]
95 else
96 []
97 end
98
99 [
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
110 end
111
112 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
113 h = fn(str) -> [to_charlist(str)] end
114
115 updated_at = activity.data["published"]
116 inserted_at = activity.data["published"]
117
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
121
122 [
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.
132 ]},
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
138 end
139
140 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
141 h = fn(str) -> [to_charlist(str)] end
142
143 updated_at = activity.data["published"]
144 inserted_at = activity.data["published"]
145
146 _in_reply_to = get_in_reply_to(activity.data)
147 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
148
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"])
151
152 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
153
154 mentions = activity.recipients |> get_mentions
155 [
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
168 end
169
170 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
171 h = fn(str) -> [to_charlist(str)] end
172
173 updated_at = activity.data["published"]
174 inserted_at = activity.data["published"]
175
176 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
177
178 mentions = (activity.recipients || []) |> get_mentions
179 [
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"])},
191 ]},
192 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
193 ] ++ mentions ++ author
194 end
195
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
199
200 updated_at = activity.data["published"]
201 inserted_at = activity.data["published"]
202
203 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
204 follow_activity = Activity.get_by_ap_id(activity.data["object"])
205
206 mentions = (activity.recipients || []) |> get_mentions
207 [
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"])},
219 ]},
220 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
221 ] ++ mentions ++ author
222 end
223
224 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
225 h = fn(str) -> [to_charlist(str)] end
226
227 updated_at = activity.data["published"]
228 inserted_at = activity.data["published"]
229
230 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
231
232 [
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)}
240 ] ++ author
241 end
242
243 def to_simple_form(_, _, _), do: nil
244
245 def wrap_with_entry(simple_form) do
246 [{
247 :entry, [
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'
253 ], simple_form
254 }]
255 end
256 end