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