everywhere: use Pleroma.HTML module instead of HtmlSanitizeEx directly
[akkoma] / lib / pleroma / web / mastodon_api / views / status_view.ex
1 defmodule Pleroma.Web.MastodonAPI.StatusView do
2 use Pleroma.Web, :view
3 alias Pleroma.Web.MastodonAPI.{AccountView, StatusView}
4 alias Pleroma.{User, Activity}
5 alias Pleroma.Web.CommonAPI.Utils
6 alias Pleroma.Web.MediaProxy
7 alias Pleroma.Repo
8 alias Pleroma.HTML
9
10 # TODO: Add cached version.
11 defp get_replied_to_activities(activities) do
12 activities
13 |> Enum.map(fn
14 %{data: %{"type" => "Create", "object" => %{"inReplyTo" => inReplyTo}}} ->
15 inReplyTo != "" && inReplyTo
16
17 _ ->
18 nil
19 end)
20 |> Enum.filter(& &1)
21 |> Activity.create_activity_by_object_id_query()
22 |> Repo.all()
23 |> Enum.reduce(%{}, fn activity, acc ->
24 Map.put(acc, activity.data["object"]["id"], activity)
25 end)
26 end
27
28 def render("index.json", opts) do
29 replied_to_activities = get_replied_to_activities(opts.activities)
30
31 render_many(
32 opts.activities,
33 StatusView,
34 "status.json",
35 Map.put(opts, :replied_to_activities, replied_to_activities)
36 )
37 end
38
39 def render(
40 "status.json",
41 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
42 ) do
43 user = User.get_cached_by_ap_id(activity.data["actor"])
44 created_at = Utils.to_masto_date(activity.data["published"])
45
46 reblogged = Activity.get_create_activity_by_object_ap_id(object)
47 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
48
49 mentions =
50 activity.recipients
51 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
52 |> Enum.filter(& &1)
53 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
54
55 %{
56 id: to_string(activity.id),
57 uri: object,
58 url: object,
59 account: AccountView.render("account.json", %{user: user}),
60 in_reply_to_id: nil,
61 in_reply_to_account_id: nil,
62 reblog: reblogged,
63 content: reblogged[:content],
64 created_at: created_at,
65 reblogs_count: 0,
66 favourites_count: 0,
67 reblogged: false,
68 favourited: false,
69 muted: false,
70 sensitive: false,
71 spoiler_text: "",
72 visibility: "public",
73 media_attachments: [],
74 mentions: mentions,
75 tags: [],
76 application: %{
77 name: "Web",
78 website: nil
79 },
80 language: nil,
81 emojis: []
82 }
83 end
84
85 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
86 user = User.get_cached_by_ap_id(activity.data["actor"])
87
88 like_count = object["like_count"] || 0
89 announcement_count = object["announcement_count"] || 0
90
91 tags = object["tag"] || []
92 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
93
94 mentions =
95 activity.recipients
96 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
97 |> Enum.filter(& &1)
98 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
99
100 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
101 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
102
103 attachment_data = object["attachment"] || []
104 attachment_data = attachment_data ++ if object["type"] == "Video", do: [object], else: []
105 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
106
107 created_at = Utils.to_masto_date(object["published"])
108
109 reply_to = get_reply_to(activity, opts)
110 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
111
112 emojis =
113 (activity.data["object"]["emoji"] || [])
114 |> Enum.map(fn {name, url} ->
115 name = HTML.strip_tags(name)
116
117 url =
118 HTML.strip_tags(url)
119 |> MediaProxy.url()
120
121 %{shortcode: name, url: url, static_url: url}
122 end)
123
124 %{
125 id: to_string(activity.id),
126 uri: object["id"],
127 url: object["external_url"] || object["id"],
128 account: AccountView.render("account.json", %{user: user}),
129 in_reply_to_id: reply_to && to_string(reply_to.id),
130 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
131 reblog: nil,
132 content: render_content(object),
133 created_at: created_at,
134 reblogs_count: announcement_count,
135 favourites_count: like_count,
136 reblogged: !!repeated,
137 favourited: !!favorited,
138 muted: false,
139 sensitive: sensitive,
140 spoiler_text: object["summary"] || "",
141 visibility: get_visibility(object),
142 media_attachments: attachments |> Enum.take(4),
143 mentions: mentions,
144 # fix,
145 tags: [],
146 application: %{
147 name: "Web",
148 website: nil
149 },
150 language: nil,
151 emojis: emojis
152 }
153 end
154
155 def render("attachment.json", %{attachment: attachment}) do
156 [attachment_url | _] = attachment["url"]
157 media_type = attachment_url["mediaType"] || attachment_url["mimeType"]
158 href = attachment_url["href"]
159
160 type =
161 cond do
162 String.contains?(media_type, "image") -> "image"
163 String.contains?(media_type, "video") -> "video"
164 String.contains?(media_type, "audio") -> "audio"
165 true -> "unknown"
166 end
167
168 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
169
170 %{
171 id: to_string(attachment["id"] || hash_id),
172 url: MediaProxy.url(href),
173 remote_url: href,
174 preview_url: MediaProxy.url(href),
175 text_url: href,
176 type: type,
177 description: attachment["name"]
178 }
179 end
180
181 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
182 _id = activity.data["object"]["inReplyTo"]
183 replied_to_activities[activity.data["object"]["inReplyTo"]]
184 end
185
186 def get_reply_to(%{data: %{"object" => object}}, _) do
187 if object["inReplyTo"] && object["inReplyTo"] != "" do
188 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
189 else
190 nil
191 end
192 end
193
194 def get_visibility(object) do
195 public = "https://www.w3.org/ns/activitystreams#Public"
196 to = object["to"] || []
197 cc = object["cc"] || []
198
199 cond do
200 public in to ->
201 "public"
202
203 public in cc ->
204 "unlisted"
205
206 # this should use the sql for the object's activity
207 Enum.any?(to, &String.contains?(&1, "/followers")) ->
208 "private"
209
210 true ->
211 "direct"
212 end
213 end
214
215 def render_content(%{"type" => "Video"} = object) do
216 name = object["name"]
217
218 content =
219 if !!name and name != "" do
220 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
221 else
222 object["content"]
223 end
224
225 HTML.filter_tags(content)
226 end
227
228 def render_content(%{"type" => "Article"} = object) do
229 summary = object["name"]
230
231 content =
232 if !!summary and summary != "" do
233 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
234 else
235 object["content"]
236 end
237
238 HTML.filter_tags(content)
239 end
240
241 def render_content(object) do
242 HTML.filter_tags(object["content"])
243 end
244 end