From: Michael Date: Thu, 8 Dec 2022 05:49:25 +0000 (+0000) Subject: Issue 12345: No link preview on DFRN posts X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=7d51fa47d56402e8fffde7b9f61b027822e5e054;p=friendica.git Issue 12345: No link preview on DFRN posts --- diff --git a/src/Model/Item.php b/src/Model/Item.php index 625567a3ca..0407af8f8e 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -220,7 +220,7 @@ class Item $content_fields['raw-body'] = Post\Media::insertFromBody($item['uri-id'], $content_fields['raw-body']); $content_fields['raw-body'] = self::setHashtags($content_fields['raw-body']); - Post\Media::insertFromRelevantUrl($item['uri-id'], $content_fields['raw-body']); + Post\Media::insertFromRelevantUrl($item['uri-id'], $content_fields['raw-body'], $fields['body'], $item['author-network']); Post\Content::update($item['uri-id'], $content_fields); } @@ -1188,7 +1188,8 @@ class Item $item['raw-body'] = Post\Media::insertFromBody($item['uri-id'], $item['raw-body']); $item['raw-body'] = self::setHashtags($item['raw-body']); - Post\Media::insertFromRelevantUrl($item['uri-id'], $item['raw-body']); + $author = Contact::getById($item['author-id'], ['network']); + Post\Media::insertFromRelevantUrl($item['uri-id'], $item['raw-body'], $item['body'], $author['network'] ?? ''); // Check for hashtags in the body and repair or add hashtag links $item['body'] = self::setHashtags($item['body']); diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index d3d26a8511..28526b5b04 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -66,6 +66,7 @@ class Media * Insert a post-media record * * @param array $media + * @param bool $force * @return void */ public static function insert(array $media, bool $force = false) @@ -229,20 +230,9 @@ class Media } if ($media['type'] == self::HTML) { - $data = ParseUrl::getSiteinfoCached($media['url'], false); - $media['preview'] = $data['images'][0]['src'] ?? null; - $media['preview-height'] = $data['images'][0]['height'] ?? null; - $media['preview-width'] = $data['images'][0]['width'] ?? null; - $media['blurhash'] = $data['images'][0]['blurhash'] ?? null; - $media['description'] = $data['text'] ?? null; - $media['name'] = $data['title'] ?? null; - $media['author-url'] = $data['author_url'] ?? null; - $media['author-name'] = $data['author_name'] ?? null; - $media['author-image'] = $data['author_img'] ?? null; - $media['publisher-url'] = $data['publisher_url'] ?? null; - $media['publisher-name'] = $data['publisher_name'] ?? null; - $media['publisher-image'] = $data['publisher_img'] ?? null; + $media = self::addPage($media); } + return $media; } @@ -345,6 +335,31 @@ class Media return $media; } + /** + * Add page infos for HTML entries + * + * @param array $media + * @return array + */ + private static function addPage(array $media): array + { + $data = ParseUrl::getSiteinfoCached($media['url'], false); + $media['preview'] = $data['images'][0]['src'] ?? null; + $media['preview-height'] = $data['images'][0]['height'] ?? null; + $media['preview-width'] = $data['images'][0]['width'] ?? null; + $media['blurhash'] = $data['images'][0]['blurhash'] ?? null; + $media['description'] = $data['text'] ?? null; + $media['name'] = $data['title'] ?? null; + $media['author-url'] = $data['author_url'] ?? null; + $media['author-name'] = $data['author_name'] ?? null; + $media['author-image'] = $data['author_img'] ?? null; + $media['publisher-url'] = $data['publisher_url'] ?? null; + $media['publisher-name'] = $data['publisher_name'] ?? null; + $media['publisher-image'] = $data['publisher_img'] ?? null; + + return $media; + } + /** * Fetch media data from local resources * @param array $media @@ -543,7 +558,7 @@ class Media * @param string $body * @return void */ - public static function insertFromRelevantUrl(int $uriid, string $body) + public static function insertFromRelevantUrl(int $uriid, string $body, string $fullbody, string $network) { // Remove all hashtags and mentions $body = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '', $body); @@ -552,7 +567,10 @@ class Media if (preg_match_all("/\[url\](https?:.*?)\[\/url\]/ism", $body, $matches)) { foreach ($matches[1] as $url) { Logger::info('Got page url (link without description)', ['uri-id' => $uriid, 'url' => $url]); - self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url]); + self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network); + if ($network == Protocol::DFRN) { + self::revertHTMLType($uriid, $url, $fullbody); + } } } @@ -560,11 +578,31 @@ class Media if (preg_match_all("/\[url\=(https?:.*?)\].*?\[\/url\]/ism", $body, $matches)) { foreach ($matches[1] as $url) { Logger::info('Got page url (link with description)', ['uri-id' => $uriid, 'url' => $url]); - self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url]); + self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network); + if ($network == Protocol::DFRN) { + self::revertHTMLType($uriid, $url, $fullbody); + } } } } + /** + * Revert the media type of links to UNKNOWN for DFRN posts when they aren't attached + * + * @param integer $uriid + * @param string $url + * @param string $body + * @return void + */ + private static function revertHTMLType(int $uriid, string $url, string $body) + { + $attachment = BBCode::getAttachmentData($body); + if (!empty($attachment['url']) && Network::getUrlMatch($attachment['url'], $url)) { + return; + } + DBA::update('post-media', ['type' => self::UNKNOWN], ['uri-id' => $uriid, 'type' => self::HTML, 'url' => $url]); + } + /** * Add media links from the attachment field * @@ -633,7 +671,7 @@ class Media */ public static function getByURIId(int $uri_id, array $types = []) { - $condition = ['uri-id' => $uri_id]; + $condition = ["`uri-id` = ? AND `type` != ?", $uri_id, self::UNKNOWN]; if (!empty($types)) { $condition = DBA::mergeConditions($condition, ['type' => $types]); @@ -652,7 +690,7 @@ class Media */ public static function existsByURIId(int $uri_id, array $types = []): bool { - $condition = ['uri-id' => $uri_id]; + $condition = ["`uri-id` = ? AND `type` != ?", $uri_id, self::UNKNOWN]; if (!empty($types)) { $condition = DBA::mergeConditions($condition, ['type' => $types]); diff --git a/src/Object/Image.php b/src/Object/Image.php index c798f250ed..06498f963f 100644 --- a/src/Object/Image.php +++ b/src/Object/Image.php @@ -734,6 +734,9 @@ class Image public function getBlurHash(): string { $image = New Image($this->asString()); + if (empty($image)) { + return ''; + } $width = $image->getWidth(); $height = $image->getHeight(); @@ -749,7 +752,11 @@ class Image $row = []; for ($x = 0; $x < $width; ++$x) { if ($image->isImagick()) { - $colors = $image->image->getImagePixelColor($x, $y)->getColor(); + try { + $colors = $image->image->getImagePixelColor($x, $y)->getColor(); + } catch (\Throwable $th) { + return ''; + } $row[] = [$colors['r'], $colors['g'], $colors['b']]; } else { $index = imagecolorat($image->image, $x, $y); diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 92f3e81545..9900708ed0 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -711,7 +711,7 @@ class DFRN */ private static function getAttachment($doc, $root, array $item) { - foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) { + foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) { $attributes = ['rel' => 'enclosure', 'href' => $attachment['url'], 'type' => $attachment['mimetype']]; diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index bb955dca1e..3c60f4041b 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -499,7 +499,7 @@ class Diaspora } if (!($fields = self::validPosting($msg))) { - Logger::warning('Invalid posting'); + Logger::warning('Invalid posting', ['msg' => $msg]); return false; } @@ -534,7 +534,7 @@ class Diaspora if (is_null($fields)) { $private = true; if (!($fields = self::validPosting($msg))) { - Logger::warning('Invalid posting'); + Logger::warning('Invalid posting', ['msg' => $msg]); return false; } } else { @@ -3387,7 +3387,7 @@ class Diaspora $body = '### ' . html_entity_decode($title) . "\n\n" . $body; } - $attachments = Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]); + $attachments = Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT]); if (!empty($attachments)) { $body .= "\n[hr]\n"; foreach ($attachments as $attachment) { diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index 06f849d1fc..ee9c015a9c 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -1186,7 +1186,7 @@ class OStatus } } - foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) { + foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) { $attributes = ['rel' => 'enclosure', 'href' => $attachment['url'], 'type' => $attachment['mimetype']];