]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Feed.php
Merge remote-tracking branch 'upstream/develop' into network-thread-view
[friendica.git] / src / Protocol / Feed.php
index 027efb2d47cc100eb8268be3f2df264dc21c3a92..680e8803bdeae2ab59949bdd7bf63de012f3a6e8 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -29,11 +29,9 @@ use Friendica\Content\Text\HTML;
 use Friendica\Core\Cache\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)
@@ -235,6 +235,7 @@ class Feed
                $header["private"] = Item::PUBLIC;
                $header["verb"] = Activity::POST;
                $header["object-type"] = Activity\ObjectType::NOTE;
+               $header["post-type"] = Item::PT_ARTICLE;
 
                $header["contact-id"] = $contact["id"] ?? 0;
 
@@ -297,7 +298,11 @@ class Feed
 
                        $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);
 
@@ -484,7 +489,7 @@ class Feed
                                        $item["body"] = trim($item["title"]);
                                }
 
-                               $data = ParseUrl::getSiteinfoCached($item['plink'], true);
+                               $data = ParseUrl::getSiteinfoCached($item['plink']);
                                if (!empty($data['text']) && !empty($data['title']) && (mb_strlen($item['body']) < mb_strlen($data['text']))) {
                                        // When the fetched page info text is longer than the body, we do try to enhance the body
                                        if (!empty($item['body']) && (strpos($data['title'], $item['body']) === false) && (strpos($data['text'], $item['body']) === false)) {
@@ -539,6 +544,10 @@ class Feed
                                }
                        }
 
+                       if (empty($item['title'])) {
+                               $item['post-type'] = Item::PT_NOTE;
+                       }
+
                        Logger::info('Stored feed', ['item' => $item]);
 
                        $notify = Item::isRemoteSelf($contact, $item);
@@ -597,7 +606,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);
+                               }
                        }
                }
 
@@ -721,7 +732,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']]);
                }
        }
 
@@ -874,8 +885,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) {
@@ -887,9 +898,9 @@ class Feed
                $params = ['order' => ['received' => true], 'limit' => $max_items];
 
                if ($filter === 'posts') {
-                       $ret = Post::selectThread([], $condition, $params);
+                       $ret = Post::selectThread(Item::DELIVER_FIELDLIST, $condition, $params);
                } else {
-                       $ret = Post::select([], $condition, $params);
+                       $ret = Post::select(Item::DELIVER_FIELDLIST, $condition, $params);
                }
 
                $items = Post::toArray($ret);
@@ -954,7 +965,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::getAvatarUrlForId($owner['uid'], Proxy::SIZE_SMALL));
                XML::addElement($doc, $root, "updated", DateTimeFormat::utcNow(DateTimeFormat::ATOM));
 
                $author = self::addAuthor($doc, $owner);
@@ -1041,7 +1052,7 @@ class Feed
 
                $condition = ['uid' => $owner["uid"], 'guid' => $repeated_guid, 'private' => [Item::PUBLIC, Item::UNLISTED],
                        'network' => Protocol::FEDERATED];
-               $repeated_item = Post::selectFirst([], $condition);
+               $repeated_item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition);
                if (!DBA::isResult($repeated_item)) {
                        return false;
                }
@@ -1103,9 +1114,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"]);
 
@@ -1135,27 +1146,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
@@ -1166,7 +1181,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']]);
                        }
                }
 
@@ -1182,7 +1197,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
@@ -1195,7 +1210,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)) {