]> git.mxchange.org Git - friendica.git/commitdiff
Issue 12345: No link preview on DFRN posts
authorMichael <heluecht@pirati.ca>
Thu, 8 Dec 2022 05:49:25 +0000 (05:49 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 8 Dec 2022 05:49:25 +0000 (05:49 +0000)
src/Model/Item.php
src/Model/Post/Media.php
src/Object/Image.php
src/Protocol/DFRN.php
src/Protocol/Diaspora.php
src/Protocol/OStatus.php

index 625567a3ca33ad14d024110a4c3cf92c8fcc22e0..0407af8f8e4e20537fe825289e28cb989128bd8f 100644 (file)
@@ -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']);
index d3d26a85114adf8dfce3a88110a4d64a44006257..28526b5b04bd340b87904995546bbd4df6a2b58e 100644 (file)
@@ -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]);
index c798f250ed8748de607c80dad71f708840eff48b..06498f963fcb859b78d4d96c5381824ddd6b0794 100644 (file)
@@ -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);
index 92f3e8154548aa891a8f9d3797ce9cece8a11f2c..9900708ed0d29d9a5a3766bdf0c054d265e45641 100644 (file)
@@ -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']];
index bb955dca1ecb728a3fd51d9f4e612fb3003a7d33..3c60f4041ba98ae374dbcce1f5021bf6c5064e7b 100644 (file)
@@ -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) {
index 06f849d1fccf1722ce6b0ed48a67cead90bfde3d..ee9c015a9c44260aa92bbb7c5f8237615b4272f2 100644 (file)
@@ -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']];