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