Merge branch '114_user_registration_emails' into 'develop'
[akkoma] / lib / pleroma / web / twitter_api / views / activity_view.ex
1 defmodule Pleroma.Web.TwitterAPI.ActivityView do
2 use Pleroma.Web, :view
3 alias Pleroma.Web.CommonAPI.Utils
4 alias Pleroma.User
5 alias Pleroma.Web.TwitterAPI.UserView
6 alias Pleroma.Web.TwitterAPI.ActivityView
7 alias Pleroma.Web.TwitterAPI.TwitterAPI
8 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
9 alias Pleroma.Activity
10 alias Pleroma.Object
11 alias Pleroma.User
12 alias Pleroma.Repo
13 alias Pleroma.Formatter
14 alias Pleroma.HTML
15
16 import Ecto.Query
17
18 defp query_context_ids([]), do: []
19
20 defp query_context_ids(contexts) do
21 query = from(o in Object, where: fragment("(?)->>'id' = ANY(?)", o.data, ^contexts))
22
23 Repo.all(query)
24 end
25
26 defp query_users([]), do: []
27
28 defp query_users(user_ids) do
29 query = from(user in User, where: user.ap_id in ^user_ids)
30
31 Repo.all(query)
32 end
33
34 defp collect_context_ids(activities) do
35 _contexts =
36 activities
37 |> Enum.reject(& &1.data["context_id"])
38 |> Enum.map(fn %{data: data} ->
39 data["context"]
40 end)
41 |> Enum.filter(& &1)
42 |> query_context_ids()
43 |> Enum.reduce(%{}, fn %{data: %{"id" => ap_id}, id: id}, acc ->
44 Map.put(acc, ap_id, id)
45 end)
46 end
47
48 defp collect_users(activities) do
49 activities
50 |> Enum.map(fn activity ->
51 case activity.data do
52 data = %{"type" => "Follow"} ->
53 [data["actor"], data["object"]]
54
55 data ->
56 [data["actor"]]
57 end ++ activity.recipients
58 end)
59 |> List.flatten()
60 |> Enum.uniq()
61 |> query_users()
62 |> Enum.reduce(%{}, fn user, acc ->
63 Map.put(acc, user.ap_id, user)
64 end)
65 end
66
67 defp get_context_id(%{data: %{"context_id" => context_id}}, _) when not is_nil(context_id),
68 do: context_id
69
70 defp get_context_id(%{data: %{"context" => nil}}, _), do: nil
71
72 defp get_context_id(%{data: %{"context" => context}}, options) do
73 cond do
74 id = options[:context_ids][context] -> id
75 true -> TwitterAPI.context_to_conversation_id(context)
76 end
77 end
78
79 defp get_context_id(_, _), do: nil
80
81 defp get_user(ap_id, opts) do
82 cond do
83 user = opts[:users][ap_id] ->
84 user
85
86 String.ends_with?(ap_id, "/followers") ->
87 nil
88
89 ap_id == "https://www.w3.org/ns/activitystreams#Public" ->
90 nil
91
92 true ->
93 User.get_cached_by_ap_id(ap_id)
94 end
95 end
96
97 def render("index.json", opts) do
98 context_ids = collect_context_ids(opts.activities)
99 users = collect_users(opts.activities)
100
101 opts =
102 opts
103 |> Map.put(:context_ids, context_ids)
104 |> Map.put(:users, users)
105
106 render_many(
107 opts.activities,
108 ActivityView,
109 "activity.json",
110 opts
111 )
112 end
113
114 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
115 user = get_user(activity.data["actor"], opts)
116 created_at = activity.data["published"] |> Utils.date_to_asctime()
117
118 %{
119 "id" => activity.id,
120 "uri" => activity.data["object"],
121 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
122 "attentions" => [],
123 "statusnet_html" => "deleted notice {{tag",
124 "text" => "deleted notice {{tag",
125 "is_local" => activity.local,
126 "is_post_verb" => false,
127 "created_at" => created_at,
128 "in_reply_to_status_id" => nil,
129 "external_url" => activity.data["id"],
130 "activity_type" => "delete"
131 }
132 end
133
134 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
135 user = get_user(activity.data["actor"], opts)
136 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
137 created_at = created_at |> Utils.date_to_asctime()
138
139 followed = get_user(activity.data["object"], opts)
140 text = "#{user.nickname} started following #{followed.nickname}"
141
142 %{
143 "id" => activity.id,
144 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
145 "attentions" => [],
146 "statusnet_html" => text,
147 "text" => text,
148 "is_local" => activity.local,
149 "is_post_verb" => false,
150 "created_at" => created_at,
151 "in_reply_to_status_id" => nil,
152 "external_url" => activity.data["id"],
153 "activity_type" => "follow"
154 }
155 end
156
157 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
158 user = get_user(activity.data["actor"], opts)
159 created_at = activity.data["published"] |> Utils.date_to_asctime()
160 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
161
162 text = "#{user.nickname} retweeted a status."
163
164 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
165
166 %{
167 "id" => activity.id,
168 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
169 "statusnet_html" => text,
170 "text" => text,
171 "is_local" => activity.local,
172 "is_post_verb" => false,
173 "uri" => "tag:#{activity.data["id"]}:objectType=note",
174 "created_at" => created_at,
175 "retweeted_status" => retweeted_status,
176 "statusnet_conversation_id" => get_context_id(announced_activity, opts),
177 "external_url" => activity.data["id"],
178 "activity_type" => "repeat"
179 }
180 end
181
182 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
183 user = get_user(activity.data["actor"], opts)
184 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
185 liked_activity_id = if liked_activity, do: liked_activity.id, else: nil
186
187 created_at =
188 activity.data["published"]
189 |> Utils.date_to_asctime()
190
191 text = "#{user.nickname} favorited a status."
192
193 favorited_status =
194 if liked_activity,
195 do: render("activity.json", Map.merge(opts, %{activity: liked_activity})),
196 else: nil
197
198 %{
199 "id" => activity.id,
200 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
201 "statusnet_html" => text,
202 "text" => text,
203 "is_local" => activity.local,
204 "is_post_verb" => false,
205 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
206 "created_at" => created_at,
207 "favorited_status" => favorited_status,
208 "in_reply_to_status_id" => liked_activity_id,
209 "external_url" => activity.data["id"],
210 "activity_type" => "like"
211 }
212 end
213
214 def render(
215 "activity.json",
216 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
217 ) do
218 user = get_user(activity.data["actor"], opts)
219
220 created_at = object["published"] |> Utils.date_to_asctime()
221 like_count = object["like_count"] || 0
222 announcement_count = object["announcement_count"] || 0
223 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
224 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
225
226 attentions =
227 activity.recipients
228 |> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
229 |> Enum.filter(& &1)
230 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
231
232 conversation_id = get_context_id(activity, opts)
233
234 tags = activity.data["object"]["tag"] || []
235 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
236
237 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
238
239 {summary, content} = render_content(object)
240
241 html =
242 HTML.filter_tags(content, User.html_filter_policy(opts[:for]))
243 |> Formatter.emojify(object["emoji"])
244
245 reply_parent = Activity.get_in_reply_to_activity(activity)
246
247 reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
248
249 %{
250 "id" => activity.id,
251 "uri" => activity.data["object"]["id"],
252 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
253 "statusnet_html" => html,
254 "text" => HTML.strip_tags(content),
255 "is_local" => activity.local,
256 "is_post_verb" => true,
257 "created_at" => created_at,
258 "in_reply_to_status_id" => object["inReplyToStatusId"],
259 "in_reply_to_screen_name" => reply_user && reply_user.nickname,
260 "in_reply_to_profileurl" => User.profile_url(reply_user),
261 "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
262 "in_reply_to_user_id" => reply_user && reply_user.id,
263 "statusnet_conversation_id" => conversation_id,
264 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
265 "attentions" => attentions,
266 "fave_num" => like_count,
267 "repeat_num" => announcement_count,
268 "favorited" => !!favorited,
269 "repeated" => !!repeated,
270 "external_url" => object["external_url"] || object["id"],
271 "tags" => tags,
272 "activity_type" => "post",
273 "possibly_sensitive" => possibly_sensitive,
274 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object),
275 "summary" => summary
276 }
277 end
278
279 def render_content(%{"type" => "Note"} = object) do
280 summary = object["summary"]
281
282 content =
283 if !!summary and summary != "" do
284 "<p>#{summary}</p>#{object["content"]}"
285 else
286 object["content"]
287 end
288
289 {summary, content}
290 end
291
292 def render_content(%{"type" => object_type} = object) when object_type in ["Article", "Page"] do
293 summary = object["name"] || object["summary"]
294
295 content =
296 if !!summary and summary != "" and is_bitstring(object["url"]) do
297 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
298 else
299 object["content"]
300 end
301
302 {summary, content}
303 end
304
305 def render_content(object) do
306 summary = object["summary"] || "Unhandled activity type: #{object["type"]}"
307 content = "<p>#{summary}</p>#{object["content"]}"
308
309 {summary, content}
310 end
311 end