]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Feed.php
Merge pull request #9526 from MrPetovan/bug/9525-mastodon-emojis-tag
[friendica.git] / src / Protocol / Feed.php
index 78830c56033dcb2b4a075c07ed37b37e54f89ca7..89e7d75423bf6c5e6ac64edb08b3ad83bae3e73e 100644 (file)
@@ -33,6 +33,7 @@ use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Item;
+use Friendica\Model\Post;
 use Friendica\Model\Tag;
 use Friendica\Model\User;
 use Friendica\Util\DateTimeFormat;
@@ -436,28 +437,22 @@ class Feed
                        $enclosures = $xpath->query("enclosure|atom:link[@rel='enclosure']", $entry);
                        foreach ($enclosures AS $enclosure) {
                                $href = "";
-                               $length = "";
-                               $type = "";
+                               $length = null;
+                               $type = null;
 
                                foreach ($enclosure->attributes AS $attribute) {
                                        if (in_array($attribute->name, ["url", "href"])) {
                                                $href = $attribute->textContent;
                                        } elseif ($attribute->name == "length") {
-                                               $length = $attribute->textContent;
+                                               $length = (int)$attribute->textContent;
                                        } elseif ($attribute->name == "type") {
                                                $type = $attribute->textContent;
                                        }
                                }
 
-                               if (!empty($item["attach"])) {
-                                       $item["attach"] .= ',';
-                               } else {
-                                       $item["attach"] = '';
+                               if (!empty($href)) {
+                                       $attachments[] = ['type' => Post\Media::DOCUMENT, 'url' => $href, 'mimetype' => $type, 'size' => $length];
                                }
-
-                               $attachments[] = ["link" => $href, "type" => $type, "length" => $length];
-
-                               $item["attach"] .= '[attach]href="' . $href . '" length="' . $length . '" type="' . $type . '"[/attach]';
                        }
 
                        $taglist = [];
@@ -494,6 +489,9 @@ class Feed
                        }
                        $item["body"] = HTML::toBBCode($body, $basepath);
 
+                       // Remove tracking pixels
+                       $item["body"] = preg_replace("/\[img=1x1\]([^\[\]]*)\[\/img\]/Usi", '', $item["body"]);
+
                        if (($item["body"] == '') && ($item["title"] != '')) {
                                $item["body"] = $item["title"];
                                $item["title"] = '';
@@ -511,8 +509,8 @@ class Feed
                        if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
                                // Handle enclosures and treat them as preview picture
                                foreach ($attachments AS $attachment) {
-                                       if ($attachment["type"] == "image/jpeg") {
-                                               $preview = $attachment["link"];
+                                       if ($attachment["mimetype"] == "image/jpeg") {
+                                               $preview = $attachment["url"];
                                        }
                                }
 
@@ -533,6 +531,9 @@ class Feed
                                        $replace = true;
                                }
 
+                               $saved_body = $item["body"];
+                               $saved_title = $item["title"];
+
                                if ($replace) {
                                        $item["body"] = trim($item["title"]);
                                }
@@ -549,12 +550,27 @@ class Feed
                                        }
                                }
 
+                               $data = PageInfo::queryUrl($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_denylist"] ?? '');
+
+                               // Take the data that was provided by the feed if the query is empty
+                               if (($data['type'] == 'link') && empty($data['title']) && empty($data['text'])) {
+                                       $data['title'] = $saved_title;
+                                       $item["body"] = $saved_body;
+                               }
+
+                               $data_text = strip_tags(trim($data['text'] ?? ''));
+                               $item_body = strip_tags(trim($item['body'] ?? ''));
+
+                               if (!empty($data_text) && (($data_text == $item_body) || strstr($item_body, $data_text))) {
+                                       $data['text'] = '';
+                               }
+
                                // We always strip the title since it will be added in the page information
                                $item["title"] = "";
-                               $item["body"] = $item["body"] . "\n" . PageInfo::getFooterFromUrl($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_denylist"] ?? '');
+                               $item["body"] = $item["body"] . "\n" . PageInfo::getFooterFromData($data, false);
                                $taglist = $contact["fetch_further_information"] == 2 ? PageInfo::getTagsFromUrl($item["plink"], $preview, $contact["ffi_keyword_denylist"] ?? '') : [];
                                $item["object-type"] = Activity\ObjectType::BOOKMARK;
-                               unset($item["attach"]);
+                               $attachments = [];
                        } else {
                                if (!empty($summary)) {
                                        $item["body"] = '[abstract]' . HTML::toBBCode($summary, $basepath) . "[/abstract]\n" . $item["body"];
@@ -594,15 +610,19 @@ class Feed
 
                        Logger::info("Feed for contact " . $contact["url"] . " stored under id " . $id);
 
-                       if (!empty($id) && !empty($taglist)) {
+                       if (!empty($id) && (!empty($taglist) || !empty($attachments))) {
                                $feeditem = Item::selectFirst(['uri-id'], ['id' => $id]);
                                foreach ($taglist as $tag) {
                                        Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
                                }
+                               foreach ($attachments as $attachment) {
+                                       $attachment['uri-id'] = $feeditem['uri-id']; 
+                                       Post\Media::insert($attachment);
+                               }
                        }
                }
 
-               if (!$dryRun) {
+               if (!$dryRun && DI::config()->get('system', 'adjust_poll_frequency')) {
                        self::adjustPollFrequency($contact, $creation_dates);
                }
 
@@ -618,8 +638,8 @@ class Feed
         */
        private static function adjustPollFrequency(array $contact, array $creation_dates)
        {
-               if (($contact['priority'] > 3) || ($contact['network'] != Protocol::FEED)) {
-                       Logger::info('Contact is no feed or has a low priority, skip.', ['id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url'], 'priority' => $contact['priority'], 'network' => $contact['network']]);
+               if ($contact['network'] != Protocol::FEED) {
+                       Logger::info('Contact is no feed, skip.', ['id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url'], 'network' => $contact['network']]);
                        return;
                }
 
@@ -660,29 +680,29 @@ class Feed
                                }
                        }
 
-                       if (($newest == $oldest) && count($creation_dates) > 1) {
-                               Logger::info('Feed has no different creation dates, quitting', ['date' => $newest_date, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
-                               return;                         
+                       if (count($creation_dates) == 1) {
+                               Logger::info('Feed had posted a single time, switching to daily polling', ['newest' => $newest_date, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
+                               $priority = 8; // Poll once a day
                        }
 
-                       if (empty($frequency)) {
-                               Logger::info('Feed had not posted for at least a week, switching to daily polling', ['newest' => $newest_date, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
-                               $priority = 3; // Poll once a day
+                       if (empty($priority) && (((time() / 86400) - $newest) > 730)) {
+                               Logger::info('Feed had not posted for two years, switching to monthly polling', ['newest' => $newest_date, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
+                               $priority = 10; // Poll every month
                        }
 
-                       if (empty($priority) && (count($creation_dates) == 1)) {
-                               Logger::info('Feed had posted a single time, switching to daily polling', ['newest' => $newest_date, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
-                               $priority = 3; // Poll once a day
+                       if (empty($priority) && (((time() / 86400) - $newest) > 365)) {
+                               Logger::info('Feed had not posted for a year, switching to weekly polling', ['newest' => $newest_date, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
+                               $priority = 9; // Poll every week
                        }
 
-                       if (empty($priority) && (($newest - $oldest) > count($creation_dates))) {
-                               Logger::info('Less than a post per day, switching to daily polling', ['posts' => count($creation_dates), 'oldest' => $oldest_date, 'newest' => $newest_date, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
-                               $priority = 3; // Poll once a day
+                       if (empty($priority) && empty($frequency)) {
+                               Logger::info('Feed had not posted for at least a week, switching to daily polling', ['newest' => $newest_date, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
+                               $priority = 8; // Poll once a day
                        }
 
                        if (empty($priority)) {
                                // Calculate the highest "posts per day" value
-                               $max = 1;
+                               $max = 0;
                                foreach ($frequency as $entry) {
                                        if (($entry['count'] == 1) || ($entry['high'] == $entry['low'])) {
                                                continue;
@@ -698,26 +718,31 @@ class Feed
                                                $max = $ppd;
                                        }
                                }
-
-                               if ($max >= 24) {
-                                       $priority = 0; // Poll with the minimum poll intervall
-                               } elseif ($max >= 12) {
-                                       $priority = 1; // Poll hourly
-                               } elseif ($max > 1) {
-                                       $priority = 2; // Poll twice a day
+                               if ($max > 48) {
+                                       $priority = 1; // Poll every quarter hour
+                               } elseif ($max > 24) {
+                                       $priority = 2; // Poll half an hour
+                               } elseif ($max > 12) {
+                                       $priority = 3; // Poll hourly
+                               } elseif ($max > 8) {
+                                       $priority = 4; // Poll every two hours
+                               } elseif ($max > 4) {
+                                       $priority = 5; // Poll every three hours
+                               } elseif ($max > 2) {
+                                       $priority = 6; // Poll every six hours
                                } else {
-                                       /// @todo In the future we could calculate the days between the posts to set even lower priorities
-                                       $priority = 3; // Poll once a day
+                                       $priority = 7; // Poll twice a day
                                }
                                Logger::info('Calculated priority by the posts per day', ['priority' => $priority, 'max' => round($max, 2), 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
                        }
                } else {
                        Logger::info('No posts, switching to daily polling', ['id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
-                       $priority = 3; // Poll once a day
+                       $priority = 8; // Poll once a day
                }
 
-               if ($contact['priority'] != $priority) {
-                       Logger::notice('Adjusting priority', ['old' => $contact['priority'], 'new' => $priority, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
+               if ($contact['rating'] != $priority) {
+                       Logger::notice('Adjusting priority', ['old' => $contact['rating'], 'new' => $priority, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
+                       DBA::update('contact', ['rating' => $priority], ['id' => $contact['id']]);
                }
        }