Merge pull request #13876 from annando/sensitive2

Sensitive previews are now blurred
This commit is contained in:
Hypolite Petovan 2024-02-06 21:19:36 -05:00 committed by GitHub
commit ede41166ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 35 additions and 21 deletions

View file

@ -177,15 +177,20 @@ class PostMedia extends BaseEntity
/** /**
* Get preview path for given media id relative to the base URL * Get preview path for given media id relative to the base URL
* *
* @param string $size One of the Proxy::SIZE_* constants * @param string $size One of the Proxy::SIZE_* constants
* @param bool $vlurred If "true", the preview will be blurred
* @return string preview link * @return string preview link
*/ */
public function getPreviewPath(string $size = ''): string public function getPreviewPath(string $size = '', bool $blurred = false): string
{ {
return '/photo/preview/' . $path = '/photo/preview/' .
(Proxy::getPixelsFromSize($size) ? Proxy::getPixelsFromSize($size) . '/' : '') . (Proxy::getPixelsFromSize($size) ? Proxy::getPixelsFromSize($size) . '/' : '') .
$this->id; $this->id;
if ($blurred) {
$path .= '?' . http_build_query(['blur' => true]);
}
return $path;
} }
/** /**

View file

@ -3345,6 +3345,7 @@ class Item
$item['tags'] = $tags['tags']; $item['tags'] = $tags['tags'];
$item['hashtags'] = $tags['hashtags']; $item['hashtags'] = $tags['hashtags'];
$item['mentions'] = $tags['mentions']; $item['mentions'] = $tags['mentions'];
$sensitive = (bool)$item['sensitive'];
if (!$is_preview) { if (!$is_preview) {
$item['body'] = preg_replace("#\s*\[attachment .*?].*?\[/attachment]\s*#ism", "\n", $item['body']); $item['body'] = preg_replace("#\s*\[attachment .*?].*?\[/attachment]\s*#ism", "\n", $item['body']);
@ -3412,11 +3413,11 @@ class Item
$shared_links = array_merge($shared_links, $sharedSplitAttachments['visual']->column('url')); $shared_links = array_merge($shared_links, $sharedSplitAttachments['visual']->column('url'));
$shared_links = array_merge($shared_links, $sharedSplitAttachments['link']->column('url')); $shared_links = array_merge($shared_links, $sharedSplitAttachments['link']->column('url'));
$shared_links = array_merge($shared_links, $sharedSplitAttachments['additional']->column('url')); $shared_links = array_merge($shared_links, $sharedSplitAttachments['additional']->column('url'));
$item['body'] = self::replaceVisualAttachments($sharedSplitAttachments['visual'], $item['body']); $item['body'] = self::replaceVisualAttachments($sharedSplitAttachments['visual'], $item['body'], $sensitive);
} }
$itemSplitAttachments = DI::postMediaRepository()->splitAttachments($item['uri-id'], $shared_links, $item['has-media'] ?? false); $itemSplitAttachments = DI::postMediaRepository()->splitAttachments($item['uri-id'], $shared_links, $item['has-media'] ?? false);
$item['body'] = self::replaceVisualAttachments($itemSplitAttachments['visual'], $item['body'] ?? ''); $item['body'] = self::replaceVisualAttachments($itemSplitAttachments['visual'], $item['body'] ?? '', $sensitive);
self::putInCache($item); self::putInCache($item);
$item['body'] = $body; $item['body'] = $body;
@ -3471,9 +3472,9 @@ class Item
} }
if (!empty($sharedSplitAttachments)) { if (!empty($sharedSplitAttachments)) {
$s = self::addGallery($s, $sharedSplitAttachments['visual'], (bool)$item['sensitive']); $s = self::addGallery($s, $sharedSplitAttachments['visual'], $sensitive);
$s = self::addVisualAttachments($sharedSplitAttachments['visual'], $shared_item, $s, true); $s = self::addVisualAttachments($sharedSplitAttachments['visual'], $shared_item, $s, true, $sensitive);
$s = self::addLinkAttachment($shared_uri_id ?: $item['uri-id'], $sharedSplitAttachments, $body, $s, true, $quote_shared_links); $s = self::addLinkAttachment($shared_uri_id ?: $item['uri-id'], $sharedSplitAttachments, $body, $s, true, $quote_shared_links, $sensitive);
$s = self::addNonVisualAttachments($sharedSplitAttachments['additional'], $item, $s, true); $s = self::addNonVisualAttachments($sharedSplitAttachments['additional'], $item, $s, true);
$body = BBCode::removeSharedData($body); $body = BBCode::removeSharedData($body);
} }
@ -3484,9 +3485,9 @@ class Item
$s = substr($s, 0, $pos); $s = substr($s, 0, $pos);
} }
$s = self::addGallery($s, $itemSplitAttachments['visual'], (bool)$item['sensitive']); $s = self::addGallery($s, $itemSplitAttachments['visual'], $sensitive);
$s = self::addVisualAttachments($itemSplitAttachments['visual'], $item, $s, false); $s = self::addVisualAttachments($itemSplitAttachments['visual'], $item, $s, false, $sensitive);
$s = self::addLinkAttachment($item['uri-id'], $itemSplitAttachments, $body, $s, false, $shared_links); $s = self::addLinkAttachment($item['uri-id'], $itemSplitAttachments, $body, $s, false, $shared_links, $sensitive);
$s = self::addNonVisualAttachments($itemSplitAttachments['additional'], $item, $s, false); $s = self::addNonVisualAttachments($itemSplitAttachments['additional'], $item, $s, false);
$s = self::addQuestions($item, $s); $s = self::addQuestions($item, $s);
@ -3605,9 +3606,10 @@ class Item
* *
* @param PostMedias $PostMedias * @param PostMedias $PostMedias
* @param string $body * @param string $body
* @param bool $sensitive
* @return string modified body * @return string modified body
*/ */
private static function replaceVisualAttachments(PostMedias $PostMedias, string $body): string private static function replaceVisualAttachments(PostMedias $PostMedias, string $body, bool $sensitive): string
{ {
DI::profiler()->startRecording('rendering'); DI::profiler()->startRecording('rendering');
@ -3616,7 +3618,7 @@ class Item
if (DI::baseUrl()->isLocalUri($PostMedia->preview)) { if (DI::baseUrl()->isLocalUri($PostMedia->preview)) {
continue; continue;
} }
$proxy = DI::baseUrl() . $PostMedia->getPreviewPath(Proxy::SIZE_LARGE); $proxy = DI::baseUrl() . $PostMedia->getPreviewPath(Proxy::SIZE_LARGE, $sensitive);
$search = ['[img=' . $PostMedia->preview . ']', ']' . $PostMedia->preview . '[/img]']; $search = ['[img=' . $PostMedia->preview . ']', ']' . $PostMedia->preview . '[/img]'];
$replace = ['[img=' . $proxy . ']', ']' . $proxy . '[/img]']; $replace = ['[img=' . $proxy . ']', ']' . $proxy . '[/img]'];
@ -3625,7 +3627,7 @@ class Item
if (DI::baseUrl()->isLocalUri($PostMedia->url)) { if (DI::baseUrl()->isLocalUri($PostMedia->url)) {
continue; continue;
} }
$proxy = DI::baseUrl() . $PostMedia->getPreviewPath(Proxy::SIZE_LARGE); $proxy = DI::baseUrl() . $PostMedia->getPreviewPath(Proxy::SIZE_LARGE, $sensitive);
$search = ['[img=' . $PostMedia->url . ']', ']' . $PostMedia->url . '[/img]']; $search = ['[img=' . $PostMedia->url . ']', ']' . $PostMedia->url . '[/img]'];
$replace = ['[img=' . $proxy . ']', ']' . $proxy . '[/img]']; $replace = ['[img=' . $proxy . ']', ']' . $proxy . '[/img]'];
@ -3643,10 +3645,11 @@ class Item
* @param array $item * @param array $item
* @param string $content * @param string $content
* @param bool $shared * @param bool $shared
* @param bool $sensitive
* @return string modified content * @return string modified content
* @throws ServiceUnavailableException * @throws ServiceUnavailableException
*/ */
private static function addVisualAttachments(PostMedias $PostMedias, array $item, string $content, bool $shared): string private static function addVisualAttachments(PostMedias $PostMedias, array $item, string $content, bool $shared, bool $sensitive): string
{ {
DI::profiler()->startRecording('rendering'); DI::profiler()->startRecording('rendering');
$leading = ''; $leading = '';
@ -3661,7 +3664,7 @@ class Item
if ($PostMedia->mimetype->type == 'image' || $PostMedia->preview) { if ($PostMedia->mimetype->type == 'image' || $PostMedia->preview) {
$preview_size = Proxy::SIZE_MEDIUM; $preview_size = Proxy::SIZE_MEDIUM;
$preview_url = DI::baseUrl() . $PostMedia->getPreviewPath($preview_size); $preview_url = DI::baseUrl() . $PostMedia->getPreviewPath($preview_size, $sensitive);
} else { } else {
$preview_size = 0; $preview_size = 0;
$preview_url = ''; $preview_url = '';
@ -3746,11 +3749,12 @@ class Item
* @param string $content * @param string $content
* @param bool $shared * @param bool $shared
* @param array $ignore_links A list of URLs to ignore * @param array $ignore_links A list of URLs to ignore
* @param bool $sensitive
* @return string modified content * @return string modified content
* @throws InternalServerErrorException * @throws InternalServerErrorException
* @throws ServiceUnavailableException * @throws ServiceUnavailableException
*/ */
private static function addLinkAttachment(int $uriid, array $attachments, string $body, string $content, bool $shared, array $ignore_links): string private static function addLinkAttachment(int $uriid, array $attachments, string $body, string $content, bool $shared, array $ignore_links, bool $sensitive): string
{ {
DI::profiler()->startRecording('rendering'); DI::profiler()->startRecording('rendering');
// Don't show a preview when there is a visual attachment (audio or video) // Don't show a preview when there is a visual attachment (audio or video)
@ -3793,9 +3797,9 @@ class Item
if ($preview && $attachment->preview) { if ($preview && $attachment->preview) {
if ($attachment->previewWidth >= 500) { if ($attachment->previewWidth >= 500) {
$data['image'] = DI::baseUrl() . $attachment->getPreviewPath(Proxy::SIZE_MEDIUM); $data['image'] = DI::baseUrl() . $attachment->getPreviewPath(Proxy::SIZE_MEDIUM, $sensitive);
} else { } else {
$data['preview'] = DI::baseUrl() . $attachment->getPreviewPath(Proxy::SIZE_MEDIUM); $data['preview'] = DI::baseUrl() . $attachment->getPreviewPath(Proxy::SIZE_MEDIUM, $sensitive);
} }
} }

View file

@ -55,6 +55,7 @@ class Statuses extends BaseApi
'status' => '', // Text content of the status. If media_ids is provided, this becomes optional. Attaching a poll is optional while status is provided. 'status' => '', // Text content of the status. If media_ids is provided, this becomes optional. Attaching a poll is optional while status is provided.
'media_ids' => [], // Array of Attachment ids to be attached as media. If provided, status becomes optional, and poll cannot be used. 'media_ids' => [], // Array of Attachment ids to be attached as media. If provided, status becomes optional, and poll cannot be used.
'in_reply_to_id' => 0, // ID of the status being replied to, if status is a reply 'in_reply_to_id' => 0, // ID of the status being replied to, if status is a reply
'sensitive' => false, // Mark status and attached media as sensitive? Defaults to false.
'spoiler_text' => '', // Text to be shown as a warning or subject before the actual content. Statuses are generally collapsed behind this field. 'spoiler_text' => '', // Text to be shown as a warning or subject before the actual content. Statuses are generally collapsed behind this field.
'language' => '', // ISO 639 language code for this status. 'language' => '', // ISO 639 language code for this status.
'media_attributes' => [], 'media_attributes' => [],
@ -83,6 +84,7 @@ class Statuses extends BaseApi
$item['gravity'] = $post['gravity']; $item['gravity'] = $post['gravity'];
$item['verb'] = $post['verb']; $item['verb'] = $post['verb'];
$item['app'] = $this->getApp(); $item['app'] = $this->getApp();
$item['sensitive'] = $request['sensitive'];
if (!empty($request['language'])) { if (!empty($request['language'])) {
$item['language'] = json_encode([$request['language'] => 1]); $item['language'] = json_encode([$request['language'] => 1]);
@ -179,7 +181,7 @@ class Statuses extends BaseApi
'poll' => [], // Poll data. If provided, media_ids cannot be used, and poll[expires_in] must be provided. 'poll' => [], // Poll data. If provided, media_ids cannot be used, and poll[expires_in] must be provided.
'in_reply_to_id' => 0, // ID of the status being replied to, if status is a reply 'in_reply_to_id' => 0, // ID of the status being replied to, if status is a reply
'quote_id' => 0, // ID of the message to quote 'quote_id' => 0, // ID of the message to quote
'sensitive' => false, // Mark status and attached media as sensitive? 'sensitive' => false, // Mark status and attached media as sensitive? Defaults to false.
'spoiler_text' => '', // Text to be shown as a warning or subject before the actual content. Statuses are generally collapsed behind this field. 'spoiler_text' => '', // Text to be shown as a warning or subject before the actual content. Statuses are generally collapsed behind this field.
'visibility' => '', // Visibility of the posted status. One of: "public", "unlisted", "private" or "direct". 'visibility' => '', // Visibility of the posted status. One of: "public", "unlisted", "private" or "direct".
'scheduled_at' => '', // ISO 8601 Datetime at which to schedule a status. Providing this parameter will cause ScheduledStatus to be returned instead of Status. Must be at least 5 minutes in the future. 'scheduled_at' => '', // ISO 8601 Datetime at which to schedule a status. Providing this parameter will cause ScheduledStatus to be returned instead of Status. Must be at least 5 minutes in the future.
@ -198,6 +200,7 @@ class Statuses extends BaseApi
$item['title'] = ''; $item['title'] = '';
$item['body'] = $this->formatStatus($request['status'], $uid); $item['body'] = $this->formatStatus($request['status'], $uid);
$item['app'] = $this->getApp(); $item['app'] = $this->getApp();
$item['sensitive'] = $request['sensitive'];
$item['visibility'] = $request['visibility']; $item['visibility'] = $request['visibility'];
switch ($request['visibility']) { switch ($request['visibility']) {

View file

@ -166,7 +166,9 @@ class Photo extends BaseApi
$stamp = microtime(true); $stamp = microtime(true);
$imgdata = MPhoto::getImageDataForPhoto($photo); if (empty($request['blur']) || empty($photo['blurhash'])) {
$imgdata = MPhoto::getImageDataForPhoto($photo);
}
if (empty($imgdata) && empty($photo['blurhash'])) { if (empty($imgdata) && empty($photo['blurhash'])) {
throw new HTTPException\NotFoundException(); throw new HTTPException\NotFoundException();
} elseif (empty($imgdata) && !empty($photo['blurhash'])) { } elseif (empty($imgdata) && !empty($photo['blurhash'])) {