Merge branch 'develop' into feature/activitypub
[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) -> {:category, [term: to_charlist(tag)], []} end)
83
84 emoji_links = get_emoji_links(activity.data["object"]["emoji"] || %{})
85
86 summary = if activity.data["object"]["summary"] do
87 [{:summary, [], h.(activity.data["object"]["summary"])}]
88 else
89 []
90 end
91
92 [
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
103 end
104
105 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
106 h = fn(str) -> [to_charlist(str)] end
107
108 updated_at = activity.data["published"]
109 inserted_at = activity.data["published"]
110
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.recipients |> get_mentions
114
115 [
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.
125 ]},
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
131 end
132
133 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
134 h = fn(str) -> [to_charlist(str)] end
135
136 updated_at = activity.data["published"]
137 inserted_at = activity.data["published"]
138
139 _in_reply_to = get_in_reply_to(activity.data)
140 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
141
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"])
144
145 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
146
147 mentions = activity.recipients |> get_mentions
148 [
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
161 end
162
163 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
164 h = fn(str) -> [to_charlist(str)] end
165
166 updated_at = activity.data["published"]
167 inserted_at = activity.data["published"]
168
169 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
170
171 mentions = (activity.recipients || []) |> get_mentions
172 [
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"])},
184 ]},
185 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
186 ] ++ mentions ++ author
187 end
188
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
192
193 updated_at = activity.data["published"]
194 inserted_at = activity.data["published"]
195
196 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
197 follow_activity = Activity.get_by_ap_id(activity.data["object"])
198
199 mentions = (activity.recipients || []) |> get_mentions
200 [
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"])},
212 ]},
213 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
214 ] ++ mentions ++ author
215 end
216
217 def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do
218 h = fn(str) -> [to_charlist(str)] end
219
220 updated_at = activity.data["published"]
221 inserted_at = activity.data["published"]
222
223 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
224
225 [
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)}
233 ] ++ author
234 end
235
236 def to_simple_form(_, _, _), do: nil
237
238 def wrap_with_entry(simple_form) do
239 [{
240 :entry, [
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'
246 ], simple_form
247 }]
248 end
249 end