]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Feed.php
Merge pull request #10203 from nupplaphil/feat/auto_release
[friendica.git] / src / Protocol / Feed.php
index ed3598e641655510586bc890e5e633fd8dc02baa..ebd6f5ed3008ddf1f569d55b78ec08501092cbac 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
  *
@@ -32,7 +32,6 @@ use Friendica\Core\Protocol;
 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;
@@ -48,65 +47,6 @@ use Friendica\Util\XML;
  */
 class Feed
 {
-       /**
-        * consume - process atom feed and update anything/everything we might need to update
-        *
-        * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
-        *
-        * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
-        *             It is this person's stuff that is going to be updated.
-        * $contact =  the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
-        *             from an external network and MAY create an appropriate contact record. Otherwise, we MUST
-        *             have a contact record.
-        * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
-        *        might not) try and subscribe to it.
-        * $datedir sorts in reverse order
-        * $pass - by default ($pass = 0) we cannot guarantee that a parent item has been
-        *      imported prior to its children being seen in the stream unless we are certain
-        *      of how the feed is arranged/ordered.
-        * With $pass = 1, we only pull parent items out of the stream.
-        * With $pass = 2, we only pull children (comments/likes).
-        *
-        * So running this twice, first with pass 1 and then with pass 2 will do the right
-        * thing regardless of feed ordering. This won't be adequate in a fully-threaded
-        * model where comments can have sub-threads. That would require some massive sorting
-        * to get all the feed items into a mostly linear ordering, and might still require
-        * recursion.
-        *
-        * @param       $xml
-        * @param array $importer
-        * @param array $contact
-        * @param       $hub
-        * @throws ImagickException
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        */
-       public static function consume($xml, array $importer, array $contact, &$hub)
-       {
-               if ($contact['network'] === Protocol::OSTATUS) {
-                       Logger::info('Consume OStatus messages');
-                       OStatus::import($xml, $importer, $contact, $hub);
-
-                       return;
-               }
-
-               if ($contact['network'] === Protocol::FEED) {
-                       Logger::info('Consume feeds');
-                       self::import($xml, $importer, $contact);
-
-                       return;
-               }
-
-               if ($contact['network'] === Protocol::DFRN) {
-                       Logger::info('Consume DFRN messages');
-                       $dfrn_importer = DFRN::getImporter($contact['id'], $importer['uid']);
-                       if (!empty($dfrn_importer)) {
-                               Logger::info('Now import the DFRN feed');
-                               DFRN::import($xml, $dfrn_importer, true, Conversation::PARCEL_LEGACY_DFRN);
-                               return;
-                       }
-               }
-       }
-
        /**
         * Read a RSS/RDF/Atom feed and create an item entry for it
         *
@@ -293,6 +233,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;
 
@@ -399,7 +340,7 @@ class Feed
                        if (!$dryRun) {
                                $condition = ["`uid` = ? AND `uri` = ? AND `network` IN (?, ?)",
                                        $importer["uid"], $item["uri"], Protocol::FEED, Protocol::DFRN];
-                               $previous = Item::selectFirst(['id', 'created'], $condition);
+                               $previous = Post::selectFirst(['id', 'created'], $condition);
                                if (DBA::isResult($previous)) {
                                        // Use the creation date when the post had been stored. It can happen this date changes in the feed.
                                        $creation_dates[] = $previous['created'];
@@ -502,7 +443,10 @@ class Feed
                                $items[] = $item;
                                break;
                        } elseif (!Item::isValid($item)) {
-                               Logger::info('Feed is invalid', ['created' => $item['created'], 'uid' => $item['uid'], 'uri' => $item['uri']]);
+                               Logger::info('Feed item is invalid', ['created' => $item['created'], 'uid' => $item['uid'], 'uri' => $item['uri']]);
+                               continue;
+                       } elseif (Item::isTooOld($item)) {
+                               Logger::info('Feed is too old', ['created' => $item['created'], 'uid' => $item['uid'], 'uri' => $item['uri']]);
                                continue;
                        }
 
@@ -539,7 +483,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)) {
@@ -594,6 +538,10 @@ class Feed
                                }
                        }
 
+                       if (empty($item['title'])) {
+                               $item['post-type'] = Item::PT_NOTE;
+                       }
+
                        Logger::info('Stored feed', ['item' => $item]);
 
                        $notify = Item::isRemoteSelf($contact, $item);
@@ -602,7 +550,7 @@ class Feed
                        // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
                        if ($notify) {
                                $item['guid'] = Item::guidFromUri($orig_plink, DI::baseUrl()->getHostname());
-                               unset($item['uri']);
+                               $item['uri'] = Item::newURI($item['uid'], $item['guid']);
                                unset($item['thr-parent']);
                                unset($item['parent-uri']);
 
@@ -610,17 +558,27 @@ class Feed
                                $notify = PRIORITY_MEDIUM;
                        }
 
-                       $postings[] = ['item' => $item, 'notify' => $notify,
-                               'taglist' => $taglist, 'attachments' => $attachments];
+                       $condition = ['uid' => $item['uid'], 'uri' => $item['uri']];
+                       if (!Post::exists($condition) && !Post\Delayed::exists($item["uri"], $item['uid'])) {
+                               if (!$notify) {
+                                       Post\Delayed::publish($item, $notify, $taglist, $attachments);
+                               } else {
+                                       $postings[] = ['item' => $item, 'notify' => $notify,
+                                               'taglist' => $taglist, 'attachments' => $attachments];
+                               }
+                       } else {
+                               Logger::info('Post already created or exists in the delayed posts queue', ['uid' => $item['uid'], 'uri' => $item["uri"]]);
+                       }
                }
 
                if (!empty($postings)) {
+                       $min_posting = DI::config()->get('system', 'minimum_posting_interval', 0);
                        $total = count($postings);
                        if ($total > 1) {
                                // Posts shouldn't be delayed more than a day
-                               $interval = max(1440, self::getPollInterval($contact));
-                               $delay = round(($interval * 60) / $total);
-                               Logger::notice('Got posting delay', ['delay' => $delay, 'interval' => $interval, 'items' => $total, 'cid' => $contact['id'], 'url' => $contact['url']]);
+                               $interval = min(1440, self::getPollInterval($contact));
+                               $delay = max(round(($interval * 60) / $total), 60 * $min_posting);
+                               Logger::info('Got posting delay', ['delay' => $delay, 'interval' => $interval, 'items' => $total, 'cid' => $contact['id'], 'url' => $contact['url']]);
                        } else {
                                $delay = 0;
                        }
@@ -629,25 +587,20 @@ class Feed
 
                        foreach ($postings as $posting) {
                                if ($delay > 0) {
-                                       $publish_at = DateTimeFormat::utc('now + ' . $post_delay . ' minute');
-                                       Logger::notice('Got publishing date', ['delay' => $delay, 'publish_at' => $publish_at, 'cid' => $contact['id'], 'url' => $contact['url']]);
+                                       $publish_time = time() + $post_delay;
                                        $post_delay += $delay;
+                               } else {
+                                       $publish_time = time();
                                }
 
-                               $id = Item::insert($posting['item'], $posting['notify']);
-
-                               Logger::notice("Feed for contact " . $contact["url"] . " stored under id " . $id);
-
-                               if (!empty($id) && (!empty($posting['taglist']) || !empty($posting['attachments']))) {
-                                       $feeditem = Item::selectFirst(['uri-id'], ['id' => $id]);
-                                       foreach ($posting['taglist'] as $tag) {
-                                               Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
-                                       }
-                                       foreach ($posting['attachments'] as $attachment) {
-                                               $attachment['uri-id'] = $feeditem['uri-id'];
-                                               Post\Media::insert($attachment);
-                                       }
+                               $last_publish = DI::pConfig()->get($posting['item']['uid'], 'system', 'last_publish', 0, true);
+                               $next_publish = max($last_publish + (60 * $min_posting), time());
+                               if ($publish_time < $next_publish) {
+                                       $publish_time = $next_publish;
                                }
+                               $publish_at = date(DateTimeFormat::MYSQL, $publish_time);
+
+                               Post\Delayed::add($posting['item']['uri'], $posting['item'], $posting['notify'], false, $publish_at, $posting['taglist'], $posting['attachments']);
                        }
                }
 
@@ -937,12 +890,12 @@ class Feed
                $params = ['order' => ['received' => true], 'limit' => $max_items];
 
                if ($filter === 'posts') {
-                       $ret = Item::selectThread([], $condition, $params);
+                       $ret = Post::selectThread(Item::DELIVER_FIELDLIST, $condition, $params);
                } else {
-                       $ret = Item::select([], $condition, $params);
+                       $ret = Post::select(Item::DELIVER_FIELDLIST, $condition, $params);
                }
 
-               $items = Item::inArray($ret);
+               $items = Post::toArray($ret);
 
                $doc = new DOMDocument('1.0', 'utf-8');
                $doc->formatOutput = true;
@@ -1091,7 +1044,7 @@ class Feed
 
                $condition = ['uid' => $owner["uid"], 'guid' => $repeated_guid, 'private' => [Item::PUBLIC, Item::UNLISTED],
                        'network' => Protocol::FEDERATED];
-               $repeated_item = Item::selectFirst([], $condition);
+               $repeated_item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition);
                if (!DBA::isResult($repeated_item)) {
                        return false;
                }
@@ -1183,9 +1136,9 @@ class Feed
                $mentioned = [];
 
                if ($item['gravity'] != GRAVITY_PARENT) {
-                       $parent = Item::selectFirst(['guid', 'author-link', 'owner-link'], ['id' => $item['parent']]);
+                       $parent = Post::selectFirst(['guid', 'author-link', 'owner-link'], ['id' => $item['parent']]);
 
-                       $thrparent = Item::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"];