Merge branch 'patch-1' into 'develop'
[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 require Logger
5
6 defp get_href(id) do
7 with %Object{data: %{ "external_url" => external_url } }<- Object.get_cached_by_ap_id(id) do
8 external_url
9 else
10 _e -> id
11 end
12 end
13
14 defp get_in_reply_to(%{"object" => %{"inReplyTo" => in_reply_to}}) do
15 [{:"thr:in-reply-to", [ref: to_charlist(in_reply_to), href: to_charlist(get_href(in_reply_to))], []}]
16 end
17
18 defp get_in_reply_to(_), do: []
19
20 defp get_mentions(to) do
21 Enum.map(to, fn (id) ->
22 cond do
23 # Special handling for the AP/Ostatus public collections
24 "https://www.w3.org/ns/activitystreams#Public" == id ->
25 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/collection", href: "http://activityschema.org/collection/public"], []}
26 # Ostatus doesn't handle follower collections, ignore these.
27 Regex.match?(~r/^#{Pleroma.Web.base_url}.+followers$/, id) ->
28 []
29 true ->
30 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person", href: id], []}
31 end
32 end)
33 end
34
35 defp get_links(%{local: true, data: data}) do
36 h = fn(str) -> [to_charlist(str)] end
37 [
38 {:link, [type: ['application/atom+xml'], href: h.(data["object"]["id"]), rel: 'self'], []},
39 {:link, [type: ['text/html'], href: h.(data["object"]["id"]), rel: 'alternate'], []}
40 ]
41 end
42
43 defp get_links(%{local: false,
44 data: %{
45 "object" => %{
46 "external_url" => external_url
47 }
48 }}) do
49
50 h = fn(str) -> [to_charlist(str)] end
51 [
52 {:link, [type: ['text/html'], href: h.(external_url), rel: 'alternate'], []}
53 ]
54 end
55
56 defp get_links(_activity), do: []
57
58 def to_simple_form(activity, user, with_author \\ false)
59 def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do
60 h = fn(str) -> [to_charlist(str)] end
61
62 updated_at = activity.data["object"]["published"]
63 inserted_at = activity.data["object"]["published"]
64
65 attachments = Enum.map(activity.data["object"]["attachment"] || [], fn(attachment) ->
66 url = hd(attachment["url"])
67 {:link, [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])], []}
68 end)
69
70 in_reply_to = get_in_reply_to(activity.data)
71 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
72 mentions = activity.data["to"] |> get_mentions
73
74 categories = (activity.data["object"]["tag"] || [])
75 |> Enum.map(fn (tag) -> {:category, [term: to_charlist(tag)], []} end)
76
77 [
78 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
79 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
80 {:id, h.(activity.data["object"]["id"])}, # For notes, federate the object id.
81 {:title, ['New note by #{user.nickname}']},
82 {:content, [type: 'html'], h.(activity.data["object"]["content"] |> String.replace(~r/[\n\r]/, ""))},
83 {:published, h.(inserted_at)},
84 {:updated, h.(updated_at)},
85 {:"ostatus:conversation", [], h.(activity.data["context"])},
86 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
87 ] ++ get_links(activity) ++ categories ++ attachments ++ in_reply_to ++ author ++ mentions
88 end
89
90 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
91 h = fn(str) -> [to_charlist(str)] end
92
93 updated_at = activity.data["published"]
94 inserted_at = activity.data["published"]
95
96 in_reply_to = get_in_reply_to(activity.data)
97 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
98 mentions = activity.data["to"] |> get_mentions
99
100 [
101 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
102 {:id, h.(activity.data["id"])},
103 {:title, ['New favorite by #{user.nickname}']},
104 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
105 {:published, h.(inserted_at)},
106 {:updated, h.(updated_at)},
107 {:"activity:object", [
108 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
109 {:id, h.(activity.data["object"])}, # For notes, federate the object id.
110 ]},
111 {:"ostatus:conversation", [], h.(activity.data["context"])},
112 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
113 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
114 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
115 ] ++ author ++ mentions
116 end
117
118 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
119 h = fn(str) -> [to_charlist(str)] end
120
121 updated_at = activity.data["published"]
122 inserted_at = activity.data["published"]
123
124 in_reply_to = get_in_reply_to(activity.data)
125 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
126
127 retweeted_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
128 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
129
130 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
131
132 mentions = activity.data["to"] |> get_mentions
133 [
134 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
135 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
136 {:id, h.(activity.data["id"])},
137 {:title, ['#{user.nickname} repeated a notice']},
138 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
139 {:published, h.(inserted_at)},
140 {:updated, h.(updated_at)},
141 {:"ostatus:conversation", [], h.(activity.data["context"])},
142 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
143 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
144 {:"activity:object", retweeted_xml}
145 ] ++ mentions ++ author
146 end
147
148 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
149 h = fn(str) -> [to_charlist(str)] end
150
151 updated_at = activity.data["published"]
152 inserted_at = activity.data["published"]
153
154 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
155
156 mentions = (activity.data["to"] || []) |> get_mentions
157 [
158 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
159 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
160 {:id, h.(activity.data["id"])},
161 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
162 {:content, [type: 'html'], ['#{user.nickname} started following #{activity.data["object"]}']},
163 {:published, h.(inserted_at)},
164 {:updated, h.(updated_at)},
165 {:"activity:object", [
166 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
167 {:id, h.(activity.data["object"])},
168 {:uri, h.(activity.data["object"])},
169 ]},
170 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
171 ] ++ mentions ++ author
172 end
173
174 # Only undos of follow for now. Will need to get redone once there are more
175 def to_simple_form(%{data: %{"type" => "Undo"}} = activity, user, with_author) do
176 h = fn(str) -> [to_charlist(str)] end
177
178 updated_at = activity.data["published"]
179 inserted_at = activity.data["published"]
180
181 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
182 follow_activity = Activity.get_by_ap_id(activity.data["object"])
183
184 mentions = (activity.data["to"] || []) |> get_mentions
185 [
186 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
187 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
188 {:id, h.(activity.data["id"])},
189 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
190 {:content, [type: 'html'], ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
191 {:published, h.(inserted_at)},
192 {:updated, h.(updated_at)},
193 {:"activity:object", [
194 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
195 {:id, h.(follow_activity.data["object"])},
196 {:uri, h.(follow_activity.data["object"])},
197 ]},
198 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
199 ] ++ mentions ++ author
200 end
201
202 def wrap_with_entry(simple_form) do
203 [{
204 :entry, [
205 xmlns: 'http://www.w3.org/2005/Atom',
206 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
207 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
208 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
209 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'
210 ], simple_form
211 }]
212 end
213
214 def to_simple_form(_, _, _), do: nil
215 end