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