[Pleroma.Web.MastodonAPI.AccountView]: relationship.json: fake endorsed value (false)
[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 %{
126 id: to_string(activity.id),
127 uri: object["id"],
128 url: object["external_url"] || object["id"],
129 account: AccountView.render("account.json", %{user: user}),
130 in_reply_to_id: reply_to && to_string(reply_to.id),
131 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
132 reblog: nil,
133 content: render_content(object),
134 created_at: created_at,
135 reblogs_count: announcement_count,
136 replies_count: 0,
137 favourites_count: like_count,
138 reblogged: !!repeated,
139 favourited: !!favorited,
140 muted: false,
141 sensitive: sensitive,
142 spoiler_text: object["summary"] || "",
143 visibility: get_visibility(object),
144 media_attachments: attachments |> Enum.take(4),
145 mentions: mentions,
146 # fix,
147 tags: [],
148 application: %{
149 name: "Web",
150 website: nil
151 },
152 language: nil,
153 emojis: emojis
154 }
155 end
156
157 def render("attachment.json", %{attachment: attachment}) do
158 [attachment_url | _] = attachment["url"]
159 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
160 href = attachment_url["href"]
161
162 type =
163 cond do
164 String.contains?(media_type, "image") -> "image"
165 String.contains?(media_type, "video") -> "video"
166 String.contains?(media_type, "audio") -> "audio"
167 true -> "unknown"
168 end
169
170 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
171
172 %{
173 id: to_string(attachment["id"] || hash_id),
174 url: MediaProxy.url(href),
175 remote_url: href,
176 preview_url: MediaProxy.url(href),
177 text_url: href,
178 type: type,
179 description: attachment["name"]
180 }
181 end
182
183 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
184 _id = activity.data["object"]["inReplyTo"]
185 replied_to_activities[activity.data["object"]["inReplyTo"]]
186 end
187
188 def get_reply_to(%{data: %{"object" => object}}, _) do
189 if object["inReplyTo"] && object["inReplyTo"] != "" do
190 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
191 else
192 nil
193 end
194 end
195
196 def get_visibility(object) do
197 public = "https://www.w3.org/ns/activitystreams#Public"
198 to = object["to"] || []
199 cc = object["cc"] || []
200
201 cond do
202 public in to ->
203 "public"
204
205 public in cc ->
206 "unlisted"
207
208 # this should use the sql for the object's activity
209 Enum.any?(to, &String.contains?(&1, "/followers")) ->
210 "private"
211
212 true ->
213 "direct"
214 end
215 end
216
217 def render_content(%{"type" => "Video"} = object) do
218 name = object["name"]
219
220 content =
221 if !!name and name != "" do
222 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
223 else
224 object["content"]
225 end
226
227 HTML.filter_tags(content)
228 end
229
230 def render_content(%{"type" => "Article"} = object) do
231 summary = object["name"]
232
233 content =
234 if !!summary and summary != "" do
235 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
236 else
237 object["content"]
238 end
239
240 HTML.filter_tags(content)
241 end
242
243 def render_content(object) do
244 HTML.filter_tags(object["content"])
245 end
246 end