formatting
[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 %{
194 "id" => activity.id,
195 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
196 "statusnet_html" => text,
197 "text" => text,
198 "is_local" => activity.local,
199 "is_post_verb" => false,
200 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
201 "created_at" => created_at,
202 "in_reply_to_status_id" => liked_activity_id,
203 "external_url" => activity.data["id"],
204 "activity_type" => "like"
205 }
206 end
207
208 def render(
209 "activity.json",
210 %{activity: %{data: %{"type" => "Create", "object" => object_id}} = activity} = opts
211 ) do
212 user = get_user(activity.data["actor"], opts)
213
214 object = Object.normalize(object_id)
215
216 created_at = object.data["published"] |> Utils.date_to_asctime()
217 like_count = object.data["like_count"] || 0
218 announcement_count = object.data["announcement_count"] || 0
219 favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || [])
220 repeated = opts[:for] && opts[:for].ap_id in (object.data["announcements"] || [])
221
222 attentions =
223 activity.recipients
224 |> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
225 |> Enum.filter(& &1)
226 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
227
228 conversation_id = get_context_id(activity, opts)
229
230 tags = object.data["tag"] || []
231 possibly_sensitive = object.data["sensitive"] || Enum.member?(tags, "nsfw")
232
233 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
234
235 {summary, content} = render_content(object.data)
236
237 html =
238 HTML.filter_tags(content, User.html_filter_policy(opts[:for]))
239 |> Formatter.emojify(object.data["emoji"])
240
241 reply_parent = Activity.get_in_reply_to_activity(activity)
242
243 reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
244
245 %{
246 "id" => activity.id,
247 "uri" => object.data["id"],
248 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
249 "statusnet_html" => html,
250 "text" => HTML.strip_tags(content),
251 "is_local" => activity.local,
252 "is_post_verb" => true,
253 "created_at" => created_at,
254 "in_reply_to_status_id" => object.data["inReplyToStatusId"],
255 "in_reply_to_screen_name" => reply_user && reply_user.nickname,
256 "in_reply_to_profileurl" => User.profile_url(reply_user),
257 "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
258 "in_reply_to_user_id" => reply_user && reply_user.id,
259 "statusnet_conversation_id" => conversation_id,
260 "attachments" => (object.data["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
261 "attentions" => attentions,
262 "fave_num" => like_count,
263 "repeat_num" => announcement_count,
264 "favorited" => !!favorited,
265 "repeated" => !!repeated,
266 "external_url" => object.data["external_url"] || object.data["id"],
267 "tags" => tags,
268 "activity_type" => "post",
269 "possibly_sensitive" => possibly_sensitive,
270 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object.data),
271 "summary" => summary
272 }
273 end
274
275 def render_content(%{"type" => "Note"} = object) do
276 summary = object["summary"]
277
278 content =
279 if !!summary and summary != "" do
280 "<p>#{summary}</p>#{object["content"]}"
281 else
282 object["content"]
283 end
284
285 {summary, content}
286 end
287
288 def render_content(%{"type" => object_type} = object) when object_type in ["Article", "Page"] do
289 summary = object["name"] || object["summary"]
290
291 content =
292 if !!summary and summary != "" and is_bitstring(object["url"]) do
293 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
294 else
295 object["content"]
296 end
297
298 {summary, content}
299 end
300
301 def render_content(object) do
302 summary = object["summary"] || "Unhandled activity type: #{object["type"]}"
303 content = "<p>#{summary}</p>#{object["content"]}"
304
305 {summary, content}
306 end
307 end