Display html links correctly for remote activies.
[akkoma] / lib / pleroma / web / ostatus / activity_representer.ex
1 defmodule Pleroma.Web.OStatus.ActivityRepresenter do
2 alias Pleroma.{Activity, User}
3 alias Pleroma.Web.OStatus.UserRepresenter
4 require Logger
5
6 defp get_in_reply_to(%{"object" => %{"inReplyTo" => in_reply_to}}) do
7 [{:"thr:in-reply-to", [ref: to_charlist(in_reply_to)], []}]
8 end
9
10 defp get_in_reply_to(_), do: []
11
12 defp get_mentions(to) do
13 Enum.map(to, fn (id) ->
14 cond do
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) ->
20 []
21 true ->
22 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person", href: id], []}
23 end
24 end)
25 end
26
27 defp get_links(%{local: true, data: data}) do
28 h = fn(str) -> [to_charlist(str)] end
29 [
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'], []}
32 ]
33 end
34
35 defp get_links(%{local: false,
36 data: %{
37 "object" => %{
38 "external_url" => external_url
39 }
40 }}) do
41
42 h = fn(str) -> [to_charlist(str)] end
43 [
44 {:link, [type: ['text/html'], href: h.(external_url), rel: 'alternate'], []}
45 ]
46 end
47
48 defp get_links(_activity), do: []
49
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
53
54 updated_at = activity.data["object"]["published"]
55 inserted_at = activity.data["object"]["published"]
56
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"])], []}
60 end)
61
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
65
66 categories = (activity.data["object"]["tag"] || [])
67 |> Enum.map(fn (tag) -> {:category, [term: to_charlist(tag)], []} end)
68
69 [
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
80 end
81
82 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
83 h = fn(str) -> [to_charlist(str)] end
84
85 updated_at = activity.data["published"]
86 inserted_at = activity.data["published"]
87
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
91
92 [
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.
102 ]},
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
108 end
109
110 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
111 h = fn(str) -> [to_charlist(str)] end
112
113 updated_at = activity.data["published"]
114 inserted_at = activity.data["published"]
115
116 in_reply_to = get_in_reply_to(activity.data)
117 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
118
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"])
121
122 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
123
124 mentions = activity.data["to"] |> get_mentions
125 [
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
138 end
139
140 def to_simple_form(%{data: %{"type" => "Follow"}} = 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 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
147
148 mentions = (activity.data["to"] || []) |> get_mentions
149 [
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"])},
161 ]},
162 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
163 ] ++ mentions ++ author
164 end
165
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
169
170 updated_at = activity.data["published"]
171 inserted_at = activity.data["published"]
172
173 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
174 follow_activity = Activity.get_by_ap_id(activity.data["object"])
175
176 mentions = (activity.data["to"] || []) |> get_mentions
177 [
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"])},
189 ]},
190 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
191 ] ++ mentions ++ author
192 end
193
194 def wrap_with_entry(simple_form) do
195 [{
196 :entry, [
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'
202 ], simple_form
203 }]
204 end
205
206 def to_simple_form(_, _, _), do: nil
207 end