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