]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Feed.php
Log the command, not the module
[friendica.git] / src / Protocol / Feed.php
index 65af597f169962526514a596cf5e5a60c2bb64fb..cde81394d3c0f0acce4354f3c46accd3b26c52ea 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -26,14 +26,12 @@ use DOMXPath;
 use Friendica\Content\PageInfo;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
-use Friendica\Core\Cache\Duration;
+use Friendica\Core\Cache\Enum\Duration;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
-use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
-use Friendica\Model\Conversation;
 use Friendica\Model\Item;
 use Friendica\Model\Post;
 use Friendica\Model\Tag;
@@ -41,8 +39,10 @@ use Friendica\Model\User;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
 use Friendica\Util\ParseUrl;
+use Friendica\Util\Proxy;
 use Friendica\Util\Strings;
 use Friendica\Util\XML;
+use GuzzleHttp\Exception\TransferException;
 
 /**
  * This class contain functions to import feeds (RSS/RDF/Atom)
@@ -69,6 +69,8 @@ class Feed
                        Logger::info("Import Atom/RSS feed '" . $contact["name"] . "' (Contact " . $contact["id"] . ") for user " . $importer["uid"]);
                }
 
+               $xml = trim($xml);
+
                if (empty($xml)) {
                        Logger::info('XML is empty.');
                        return [];
@@ -83,7 +85,7 @@ class Feed
                }
 
                $doc = new DOMDocument();
-               @$doc->loadXML(trim($xml));
+               @$doc->loadXML($xml);
                $xpath = new DOMXPath($doc);
                $xpath->registerNamespace('atom', ActivityNamespace::ATOM1);
                $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
@@ -111,7 +113,7 @@ class Feed
                if ($xpath->query('/atom:feed')->length > 0) {
                        $alternate = XML::getFirstAttributes($xpath, "atom:link[@rel='alternate']");
                        if (is_object($alternate)) {
-                               foreach ($alternate AS $attribute) {
+                               foreach ($alternate as $attribute) {
                                        if ($attribute->name == "href") {
                                                $author["author-link"] = $attribute->textContent;
                                        }
@@ -121,7 +123,7 @@ class Feed
                        if (empty($author["author-link"])) {
                                $self = XML::getFirstAttributes($xpath, "atom:link[@rel='self']");
                                if (is_object($self)) {
-                                       foreach ($self AS $attribute) {
+                                       foreach ($self as $attribute) {
                                                if ($attribute->name == "href") {
                                                        $author["author-link"] = $attribute->textContent;
                                                }
@@ -175,7 +177,7 @@ class Feed
 
                                $avatar = XML::getFirstAttributes($xpath, "atom:author/atom:link[@rel='avatar']");
                                if (is_object($avatar)) {
-                                       foreach ($avatar AS $attribute) {
+                                       foreach ($avatar as $attribute) {
                                                if ($attribute->name == "href") {
                                                        $author["author-avatar"] = $attribute->textContent;
                                                }
@@ -195,7 +197,6 @@ class Feed
                        $author["author-link"] = XML::getFirstNodeValue($xpath, '/rss/channel/link/text()');
 
                        $author["author-name"] = XML::getFirstNodeValue($xpath, '/rss/channel/title/text()');
-                       $author["author-avatar"] = XML::getFirstNodeValue($xpath, '/rss/channel/image/url/text()');
 
                        if (empty($author["author-name"])) {
                                $author["author-name"] = XML::getFirstNodeValue($xpath, '/rss/channel/copyright/text()');
@@ -205,6 +206,25 @@ class Feed
                                $author["author-name"] = XML::getFirstNodeValue($xpath, '/rss/channel/description/text()');
                        }
 
+                       $author["author-avatar"] = XML::getFirstNodeValue($xpath, '/rss/channel/image/url/text()');
+
+                       if (empty($author["author-avatar"])) {
+                               $avatar = XML::getFirstAttributes($xpath, "/rss/channel/itunes:image");
+                               if (is_object($avatar)) {
+                                       foreach ($avatar as $attribute) {
+                                               if ($attribute->name == "href") {
+                                                       $author["author-avatar"] = $attribute->textContent;
+                                               }
+                                       }
+                               }
+                       }
+
+                       $author["author-about"] = HTML::toBBCode(XML::getFirstNodeValue($xpath, '/rss/channel/description/text()'), $basepath);
+
+                       if (empty($author["author-about"])) {
+                               $author["author-about"] = XML::getFirstNodeValue($xpath, '/rss/channel/itunes:summary/text()');
+                       }
+
                        $author["edited"] = $author["created"] = XML::getFirstNodeValue($xpath, '/rss/channel/pubDate/text()');
 
                        $author["app"] = XML::getFirstNodeValue($xpath, '/rss/channel/generator/text()');
@@ -267,7 +287,7 @@ class Feed
                                $alternate = XML::getFirstAttributes($xpath, "atom:link", $entry);
                        }
                        if (is_object($alternate)) {
-                               foreach ($alternate AS $attribute) {
+                               foreach ($alternate as $attribute) {
                                        if ($attribute->name == "href") {
                                                $item["plink"] = $attribute->textContent;
                                        }
@@ -282,33 +302,45 @@ class Feed
                                $item["plink"] = XML::getFirstNodeValue($xpath, 'rss:link/text()', $entry);
                        }
 
+                       // Add the base path if missing
+                       $item["plink"] = Network::addBasePath($item["plink"], $basepath);
+
                        $item["uri"] = XML::getFirstNodeValue($xpath, 'atom:id/text()', $entry);
 
-                       if (empty($item["uri"])) {
-                               $item["uri"] = XML::getFirstNodeValue($xpath, 'guid/text()', $entry);
+                       $guid = XML::getFirstNodeValue($xpath, 'guid/text()', $entry);
+                       if (!empty($guid)) {
+                               $item["uri"] = $guid;
+
+                               // Don't use the GUID value directly but instead use it as a basis for the GUID
+                               $item["guid"] = Item::guidFromUri($guid, parse_url($guid, PHP_URL_HOST) ?? parse_url($item["plink"], PHP_URL_HOST));
                        }
 
                        if (empty($item["uri"])) {
                                $item["uri"] = $item["plink"];
                        }
 
-                       // Add the base path if missing
-                       $item["uri"] = Network::addBasePath($item["uri"], $basepath);
-                       $item["plink"] = Network::addBasePath($item["plink"], $basepath);
-
                        $orig_plink = $item["plink"];
 
-                       $item["plink"] = DI::httpRequest()->finalUrl($item["plink"]);
+                       try {
+                               $item["plink"] = DI::httpClient()->finalUrl($item["plink"]);
+                       } catch (TransferException $exception) {
+                               Logger::notice('Item URL couldn\'t get expanded', ['url' => $item["plink"], 'exception' => $exception]);
+                       }
 
                        $item["title"] = XML::getFirstNodeValue($xpath, 'atom:title/text()', $entry);
 
                        if (empty($item["title"])) {
                                $item["title"] = XML::getFirstNodeValue($xpath, 'title/text()', $entry);
                        }
+
                        if (empty($item["title"])) {
                                $item["title"] = XML::getFirstNodeValue($xpath, 'rss:title/text()', $entry);
                        }
 
+                       if (empty($item["title"])) {
+                               $item["title"] = XML::getFirstNodeValue($xpath, 'itunes:title/text()', $entry);
+                       }
+
                        $item["title"] = html_entity_decode($item["title"], ENT_QUOTES, 'UTF-8');
 
                        $published = XML::getFirstNodeValue($xpath, 'atom:published/text()', $entry);
@@ -379,12 +411,12 @@ class Feed
                        $attachments = [];
 
                        $enclosures = $xpath->query("enclosure|atom:link[@rel='enclosure']", $entry);
-                       foreach ($enclosures AS $enclosure) {
+                       foreach ($enclosures as $enclosure) {
                                $href = "";
                                $length = null;
                                $type = null;
 
-                               foreach ($enclosure->attributes AS $attribute) {
+                               foreach ($enclosure->attributes as $attribute) {
                                        if (in_array($attribute->name, ["url", "href"])) {
                                                $href = $attribute->textContent;
                                        } elseif ($attribute->name == "length") {
@@ -395,13 +427,22 @@ class Feed
                                }
 
                                if (!empty($href)) {
-                                       $attachments[] = ['type' => Post\Media::DOCUMENT, 'url' => $href, 'mimetype' => $type, 'size' => $length];
+                                       $attachment = ['type' => Post\Media::UNKNOWN, 'url' => $href, 'mimetype' => $type, 'size' => $length];
+
+                                       $attachment = Post\Media::fetchAdditionalData($attachment);
+
+                                       // By now we separate the visible media types (audio, video, image) from the rest
+                                       // In the future we should try to avoid the DOCUMENT type and only use the real one - but not in the RC phase.
+                                       if (!in_array($attachment['type'], [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO])) {
+                                               $attachment['type'] = Post\Media::DOCUMENT;
+                                       }
+                                       $attachments[] = $attachment;
                                }
                        }
 
                        $taglist = [];
                        $categories = $xpath->query("category", $entry);
-                       foreach ($categories AS $category) {
+                       foreach ($categories as $category) {
                                $taglist[] = $category->nodeValue;
                        }
 
@@ -442,6 +483,7 @@ class Feed
                        }
 
                        if ($dryRun) {
+                               $item['attachments'] = $attachments;
                                $items[] = $item;
                                break;
                        } elseif (!Item::isValid($item)) {
@@ -455,7 +497,7 @@ class Feed
                        $preview = '';
                        if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
                                // Handle enclosures and treat them as preview picture
-                               foreach ($attachments AS $attachment) {
+                               foreach ($attachments as $attachment) {
                                        if ($attachment["mimetype"] == "image/jpeg") {
                                                $preview = $attachment["url"];
                                        }
@@ -519,6 +561,29 @@ class Feed
                                        $taglist = $contact["fetch_further_information"] == 2 ? PageInfo::getTagsFromUrl($item["plink"], $preview, $contact["ffi_keyword_denylist"] ?? '') : [];
                                        $item["object-type"] = Activity\ObjectType::BOOKMARK;
                                        $attachments = [];
+
+                                       foreach (['audio', 'video'] as $elementname) {
+                                               if (!empty($data[$elementname])) {
+                                                       foreach ($data[$elementname] as $element) {
+                                                               if (!empty($element['src'])) {
+                                                                       $src = $element['src'];
+                                                               } elseif (!empty($element['content'])) {
+                                                                       $src = $element['content'];
+                                                               } else {
+                                                                       continue;
+                                                               }
+
+                                                               $attachments[] = [
+                                                                       'type'        => ($elementname == 'audio') ? Post\Media::AUDIO : Post\Media::VIDEO,
+                                                                       'url'         => $src,
+                                                                       'preview'     => $element['image']       ?? null,
+                                                                       'mimetype'    => $element['contenttype'] ?? null,
+                                                                       'name'        => $element['name']        ?? null,
+                                                                       'description' => $element['description'] ?? null,
+                                                               ];
+                                                       }
+                                               }
+                                       }
                                }
                        } else {
                                if (!empty($summary)) {
@@ -535,7 +600,7 @@ class Feed
                                }
 
                                // Add the link to the original feed entry if not present in feed
-                               if (($item['plink'] != '') && !strstr($item["body"], $item['plink'])) {
+                               if (($item['plink'] != '') && !strstr($item["body"], $item['plink']) && !in_array($item['plink'], array_column($attachments, 'url'))) {
                                        $item["body"] .= "[hr][url]" . $item['plink'] . "[/url]";
                                }
                        }
@@ -602,7 +667,9 @@ class Feed
                                }
                                $publish_at = date(DateTimeFormat::MYSQL, $publish_time);
 
-                               Post\Delayed::add($posting['item']['uri'], $posting['item'], $posting['notify'], false, $publish_at, $posting['taglist'], $posting['attachments']);
+                               if (Post\Delayed::add($posting['item']['uri'], $posting['item'], $posting['notify'], Post\Delayed::PREPARED, $publish_at, $posting['taglist'], $posting['attachments'])) {
+                                       DI::pConfig()->set($item['uid'], 'system', 'last_publish', $publish_time);
+                               }
                        }
                }
 
@@ -726,7 +793,7 @@ class Feed
 
                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']]);
+                       Contact::update(['rating' => $priority], ['id' => $contact['id']]);
                }
        }
 
@@ -858,6 +925,11 @@ class Feed
 
                $cachekey = "feed:feed:" . $owner_nick . ":" . $filter . ":" . $last_update;
 
+               // Display events in the users's timezone
+               if (strlen($owner['timezone'])) {
+                       DI::app()->setTimeZone($owner['timezone']);
+               }
+
                $previous_created = $last_update;
 
                // Don't cache when the last item was posted less then 15 minutes ago (Cache duration)
@@ -879,8 +951,8 @@ class Feed
                        Protocol::OSTATUS, Protocol::DFRN, Protocol::DIASPORA];
 
                if ($filter === 'comments') {
-                       $condition[0] .= " AND `object-type` = ? ";
-                       $condition[] = Activity\ObjectType::COMMENT;
+                       $condition[0] .= " AND `gravity` = ? ";
+                       $condition[] = GRAVITY_COMMENT;
                }
 
                if ($owner['account-type'] != User::ACCOUNT_TYPE_COMMUNITY) {
@@ -905,7 +977,7 @@ class Feed
                $root = self::addHeader($doc, $owner, $filter);
 
                foreach ($items as $item) {
-                       $entry = self::entry($doc, $item, $owner);
+                       $entry = self::noteEntry($doc, $item, $owner);
                        $root->appendChild($entry);
 
                        if ($last_update < $item['created']) {
@@ -959,7 +1031,7 @@ class Feed
                XML::addElement($doc, $root, "id", DI::baseUrl() . "/profile/" . $owner["nick"]);
                XML::addElement($doc, $root, "title", $title);
                XML::addElement($doc, $root, "subtitle", sprintf("Updates from %s on %s", $owner["name"], DI::config()->get('config', 'sitename')));
-               XML::addElement($doc, $root, "logo", $owner["photo"]);
+               XML::addElement($doc, $root, "logo", User::getAvatarUrl($owner, Proxy::SIZE_SMALL));
                XML::addElement($doc, $root, "updated", DateTimeFormat::utcNow(DateTimeFormat::ATOM));
 
                $author = self::addAuthor($doc, $owner);
@@ -995,69 +1067,6 @@ class Feed
                return $author;
        }
 
-       /**
-        * Adds an entry element to the XML document
-        *
-        * @param DOMDocument $doc       XML document
-        * @param array       $item      Data of the item that is to be posted
-        * @param array       $owner     Contact data of the poster
-        * @param bool        $toplevel  optional default false
-        *
-        * @return \DOMElement Entry element
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        * @throws \ImagickException
-        */
-       private static function entry(DOMDocument $doc, array $item, array $owner)
-       {
-               $xml = null;
-
-               $repeated_guid = OStatus::getResharedGuid($item);
-               if ($repeated_guid != "") {
-                       $xml = self::reshareEntry($doc, $item, $owner, $repeated_guid);
-               }
-
-               if ($xml) {
-                       return $xml;
-               }
-
-               return self::noteEntry($doc, $item, $owner);
-       }
-
-               /**
-        * Adds an entry element with reshared content
-        *
-        * @param DOMDocument $doc           XML document
-        * @param array       $item          Data of the item that is to be posted
-        * @param array       $owner         Contact data of the poster
-        * @param string      $repeated_guid guid
-        * @param bool        $toplevel      Is it for en entry element (false) or a feed entry (true)?
-        *
-        * @return bool Entry element
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        * @throws \ImagickException
-        */
-       private static function reshareEntry(DOMDocument $doc, array $item, array $owner, $repeated_guid)
-       {
-               if (($item['gravity'] != GRAVITY_PARENT) && (Strings::normaliseLink($item["author-link"]) != Strings::normaliseLink($owner["url"]))) {
-                       Logger::info('Feed entry author does not match feed owner', ['owner' => $owner["url"], 'author' => $item["author-link"]]);
-               }
-
-               $entry = OStatus::entryHeader($doc, $owner, $item, false);
-
-               $condition = ['uid' => $owner["uid"], 'guid' => $repeated_guid, 'private' => [Item::PUBLIC, Item::UNLISTED],
-                       'network' => Protocol::FEDERATED];
-               $repeated_item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition);
-               if (!DBA::isResult($repeated_item)) {
-                       return false;
-               }
-
-               self::entryContent($doc, $entry, $item, self::getTitle($repeated_item), Activity::SHARE, false);
-
-               self::entryFooter($doc, $entry, $item, $owner);
-
-               return $entry;
-       }
-
        /**
         * Adds a regular entry element
         *
@@ -1108,9 +1117,9 @@ class Feed
                XML::addElement($doc, $entry, "id", $item["uri"]);
                XML::addElement($doc, $entry, "title", html_entity_decode($title, ENT_QUOTES, 'UTF-8'));
 
-               $body = OStatus::formatPicturePost($item['body']);
+               $body = OStatus::formatPicturePost($item['body'], $item['uri-id']);
 
-               $body = BBCode::convert($body, false, BBCode::OSTATUS);
+               $body = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);
 
                XML::addElement($doc, $entry, "content", $body, ["type" => "html"]);
 
@@ -1140,27 +1149,31 @@ class Feed
                if ($item['gravity'] != GRAVITY_PARENT) {
                        $parent = Post::selectFirst(['guid', 'author-link', 'owner-link'], ['id' => $item['parent']]);
 
-                       $thrparent = Post::selectFirst(['guid', 'author-link', 'owner-link', 'plink'], ['uid' => $owner["uid"], 'uri' => $item['thr-parent']]);
+                       $thrparent = Post::selectFirst(['guid', 'author-link', 'owner-link', 'plink'], ['uid' => $owner['uid'], 'uri' => $item['thr-parent']]);
 
                        if (DBA::isResult($thrparent)) {
-                               $mentioned[$thrparent["author-link"]] = $thrparent["author-link"];
-                               $mentioned[$thrparent["owner-link"]] = $thrparent["owner-link"];
-                               $parent_plink = $thrparent["plink"];
+                               $mentioned[$thrparent['author-link']] = $thrparent['author-link'];
+                               $mentioned[$thrparent['owner-link']]  = $thrparent['owner-link'];
+                               $parent_plink                         = $thrparent['plink'];
+                       } elseif (DBA::isResult($parent)) {
+                               $mentioned[$parent['author-link']] = $parent['author-link'];
+                               $mentioned[$parent['owner-link']]  = $parent['owner-link'];
+                               $parent_plink                      = DI::baseUrl() . '/display/' . $parent['guid'];
                        } else {
-                               $mentioned[$parent["author-link"]] = $parent["author-link"];
-                               $mentioned[$parent["owner-link"]] = $parent["owner-link"];
-                               $parent_plink = DI::baseUrl()."/display/".$parent["guid"];
+                               DI::logger()->notice('Missing parent and thr-parent for child item', ['item' => $item]);
                        }
 
-                       $attributes = [
-                                       "ref" => $item['thr-parent'],
-                                       "href" => $parent_plink];
-                       XML::addElement($doc, $entry, "thr:in-reply-to", "", $attributes);
+                       if (isset($parent_plink)) {
+                               $attributes = [
+                                       'ref'  => $item['thr-parent'],
+                                       'href' => $parent_plink];
+                               XML::addElement($doc, $entry, 'thr:in-reply-to', '', $attributes);
 
-                       $attributes = [
-                                       "rel" => "related",
-                                       "href" => $parent_plink];
-                       XML::addElement($doc, $entry, "link", "", $attributes);
+                               $attributes = [
+                                       'rel'  => 'related',
+                                       'href' => $parent_plink];
+                               XML::addElement($doc, $entry, 'link', '', $attributes);
+                       }
                }
 
                // uri-id isn't present for follow entry pseudo-items
@@ -1171,7 +1184,7 @@ class Feed
 
                foreach ($tags as $tag) {
                        if ($tag['type'] == Tag::HASHTAG) {
-                               XML::addElement($doc, $entry, "category", "", ["term" => $tag['name']]);
+                               XML::addElement($doc, $entry, 'category', '', ['term' => $tag['name']]);
                        }
                }
 
@@ -1187,7 +1200,7 @@ class Feed
        private static function getTitle(array $item)
        {
                if ($item['title'] != '') {
-                       return BBCode::convert($item['title'], false, BBCode::OSTATUS);
+                       return BBCode::convertForUriId($item['uri-id'], $item['title'], BBCode::ACTIVITYPUB);
                }
 
                // Fetch information about the post
@@ -1200,7 +1213,7 @@ class Feed
                // Remove the share element before fetching the first line
                $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$item['body']));
 
-               $title = HTML::toPlaintext(BBCode::convert($title, false), 0, true)."\n";
+               $title = BBCode::toPlaintext($title)."\n";
                $pos = strpos($title, "\n");
                $trailer = "";
                if (($pos == 0) || ($pos > 100)) {