]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Feed.php
Fix receiving non public posts from AP
[friendica.git] / src / Protocol / Feed.php
index 817134840901b1354bb5a2fc161741d747fd5997..c4cb62a7109e10ead727ab5ce8519090d7806c7f 100644 (file)
@@ -29,7 +29,7 @@ use Friendica\Core\Protocol;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Item;
-use Friendica\Model\Term;
+use Friendica\Model\Tag;
 use Friendica\Util\Network;
 use Friendica\Util\ParseUrl;
 use Friendica\Util\XML;
@@ -37,7 +37,67 @@ use Friendica\Util\XML;
 /**
  * This class contain functions to import feeds (RSS/RDF/Atom)
  */
-class Feed {
+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);
+                               return;
+                       }
+               }
+       }
+
        /**
         * Read a RSS/RDF/Atom feed and create an item entry for it
         *
@@ -385,18 +445,10 @@ class Feed {
                                $item["attach"] .= '[attach]href="' . $href . '" length="' . $length . '" type="' . $type . '"[/attach]';
                        }
 
-                       $tags = '';
                        $taglist = [];
                        $categories = $xpath->query("category", $entry);
                        foreach ($categories AS $category) {
-                               $hashtag = $category->nodeValue;
-                               if ($tags != '') {
-                                       $tags .= ', ';
-                               }
-
-                               $taglink = "#[url=" . DI::baseUrl() . "/search?tag=" . $hashtag . "]" . $hashtag . "[/url]";
-                               $tags .= $taglink;
-                               $taglist[] = $hashtag;
+                               $taglist[] = $category->nodeValue;
                        }
 
                        $body = trim(XML::getFirstNodeValue($xpath, 'atom:content/text()', $entry));
@@ -476,9 +528,8 @@ class Feed {
 
                                // We always strip the title since it will be added in the page information
                                $item["title"] = "";
-                               $item["body"] = $item["body"] . add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
-                               $item["tag"] = add_page_keywords($item["plink"], $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
-                               $taglist = add_page_keywords($item["plink"], $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"], true);
+                               $item["body"] = $item["body"] . add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_denylist"] ?? '');
+                               $taglist = get_page_keywords($item["plink"], $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_denylist"]);
                                $item["object-type"] = Activity\ObjectType::BOOKMARK;
                                unset($item["attach"]);
                        } else {
@@ -487,14 +538,10 @@ class Feed {
                                }
 
                                if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] == 3)) {
-                                       if (!empty($tags)) {
-                                               $item["tag"] = $tags;
-                                       } else {
-                                               // @todo $preview is never set in this case, is it intended? - @MrPetovan 2018-02-13
-                                               $item["tag"] = add_page_keywords($item["plink"], $preview, true, $contact["ffi_keyword_blacklist"]);
-                                               $taglist = add_page_keywords($item["plink"], $preview, true, $contact["ffi_keyword_blacklist"], true);
+                                       if (empty($taglist)) {
+                                               $taglist = get_page_keywords($item["plink"], $preview, true, $contact["ffi_keyword_denylist"]);
                                        }
-                                       $item["body"] .= "\n" . $item['tag'];
+                                       $item["body"] .= "\n" . self::tagToString($taglist);
                                } else {
                                        $taglist = [];
                                }
@@ -524,17 +571,14 @@ class Feed {
                                        $notify = PRIORITY_MEDIUM;
                                }
 
-                               $id = Item::insert($item, false, $notify);
+                               $id = Item::insert($item, $notify);
 
                                Logger::info("Feed for contact " . $contact["url"] . " stored under id " . $id);
 
                                if (!empty($id) && !empty($taglist)) {
                                        $feeditem = Item::selectFirst(['uri-id'], ['id' => $id]);
                                        foreach ($taglist as $tag) {
-                                               $fields = ['uri-id' => $feeditem['uri-id'], 'name' => substr($tag, 0, 64), 'type' => Term::HASHTAG];
-                                               DBA::insert('tag', $fields, true);
-               
-                                               Logger::info('Stored tag', ['uri-id' => $feeditem['uri-id'], 'tag' => $tag, 'fields' => $fields]);
+                                               Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
                                        }                                       
                                }
                        }
@@ -543,6 +587,27 @@ class Feed {
                return ["header" => $author, "items" => $items];
        }
 
+       /**
+        * Convert a tag array to a tag string
+        *
+        * @param array $tags
+        * @return string tag string
+        */
+       private static function tagToString(array $tags)
+       {
+               $tagstr = '';
+
+               foreach ($tags as $tag) {
+                       if ($tagstr != "") {
+                               $tagstr .= ", ";
+                       }
+       
+                       $tagstr .= "#[url=" . DI::baseUrl() . "/search?tag=" . urlencode($tag) . "]" . $tag . "[/url]";
+               }
+
+               return $tagstr;
+       }
+
        private static function titleIsBody($title, $body)
        {
                $title = strip_tags($title);