]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/OStatus.php
Some more invalid feed elements
[friendica.git] / src / Protocol / OStatus.php
index e1bf8a823fd4b810c147a049d26cd8083a387765..ef2515c1f501da286641052b67644f40346506eb 100644 (file)
@@ -1,19 +1,32 @@
 <?php
 /**
- * @file src/Protocol/OStatus.php
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
  */
+
 namespace Friendica\Protocol;
 
 use DOMDocument;
 use DOMXPath;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
-use Friendica\Core\Cache\Cache;
-use Friendica\Core\Config;
-use Friendica\Core\L10n;
-use Friendica\Core\Lock;
+use Friendica\Core\Cache\Duration;
 use Friendica\Core\Logger;
-use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
 use Friendica\Database\DBA;
 use Friendica\DI;
@@ -22,6 +35,8 @@ use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
 use Friendica\Model\GContact;
 use Friendica\Model\Item;
+use Friendica\Model\ItemURI;
+use Friendica\Model\Tag;
 use Friendica\Model\User;
 use Friendica\Network\Probe;
 use Friendica\Util\DateTimeFormat;
@@ -35,7 +50,7 @@ require_once 'mod/share.php';
 require_once 'include/api.php';
 
 /**
- * @brief This class contain functions for the OStatus protocol
+ * This class contain functions for the OStatus protocol
  */
 class OStatus
 {
@@ -43,7 +58,7 @@ class OStatus
        private static $conv_list = [];
 
        /**
-        * @brief Fetches author data
+        * Fetches author data
         *
         * @param DOMXPath $xpath     The xpath object
         * @param object   $context   The xml context of the author details
@@ -234,7 +249,7 @@ class OStatus
                        $gcid = GContact::update($contact);
 
                        GContact::link($gcid, $contact["uid"], $contact["id"]);
-               } elseif ($contact["network"] != Protocol::DFRN) {
+               } elseif (empty($contact["network"]) || ($contact["network"] != Protocol::DFRN)) {
                        $contact = [];
                }
 
@@ -242,7 +257,7 @@ class OStatus
        }
 
        /**
-        * @brief Fetches author data from a given XML string
+        * Fetches author data from a given XML string
         *
         * @param string $xml      The XML
         * @param array  $importer user record of the importing user
@@ -279,7 +294,7 @@ class OStatus
        }
 
        /**
-        * @brief Read attributes from element
+        * Read attributes from element
         *
         * @param object $element Element object
         *
@@ -297,7 +312,7 @@ class OStatus
        }
 
        /**
-        * @brief Imports an XML string containing OStatus elements
+        * Imports an XML string containing OStatus elements
         *
         * @param string $xml      The XML
         * @param array  $importer user record of the importing user
@@ -313,7 +328,7 @@ class OStatus
        }
 
        /**
-        * @brief Internal feed processing
+        * Internal feed processing
         *
         * @param string  $xml        The XML
         * @param array   $importer   user record of the importing user
@@ -424,6 +439,7 @@ class OStatus
                        $item = array_merge($header, $author);
 
                        $item["uri"] = XML::getFirstNodeValue($xpath, 'atom:id/text()', $entry);
+                       $item['uri-id'] = ItemURI::insert(['uri' => $item['uri']]);
 
                        $item["verb"] = XML::getFirstNodeValue($xpath, 'activity:verb/text()', $entry);
 
@@ -572,13 +588,13 @@ class OStatus
                        return;
                }
 
-               Item::delete($condition);
+               Item::markForDeletion($condition);
 
                Logger::log('Deleted item with uri '.$item['uri'].' for user '.$item['uid']);
        }
 
        /**
-        * @brief Processes the XML for a post
+        * Processes the XML for a post
         *
         * @param DOMXPath $xpath    The xpath object
         * @param object   $entry    The xml entry that is processed
@@ -639,14 +655,8 @@ class OStatus
                        foreach ($categories as $category) {
                                foreach ($category->attributes as $attributes) {
                                        if ($attributes->name == 'term') {
-                                               $term = $attributes->textContent;
-                                               if (!empty($item['tag'])) {
-                                                       $item['tag'] .= ',';
-                                               } else {
-                                                       $item['tag'] = '';
-                                               }
-
-                                               $item['tag'] .= '#[url=' . DI::baseUrl() . '/search?tag=' . $term . ']' . $term . '[/url]';
+                                               // Store the hashtag
+                                               Tag::store($item['uri-id'], Tag::HASHTAG, $attributes->textContent);
                                        }
                                }
                        }
@@ -690,6 +700,8 @@ class OStatus
                        $item["body"] = add_page_info_to_body($item["body"]);
                }
 
+               Tag::storeFromBody($item['uri-id'], $item['body']);
+
                // Mastodon Content Warning
                if (($item["verb"] == Activity::POST) && $xpath->evaluate('boolean(atom:summary)', $entry)) {
                        $clear_text = XML::getFirstNodeValue($xpath, 'atom:summary/text()', $entry);
@@ -727,7 +739,7 @@ class OStatus
        }
 
        /**
-        * @brief Fetch the conversation for posts
+        * Fetch the conversation for posts
         *
         * @param string $conversation     The link to the conversation
         * @param string $conversation_uri The conversation in "uri" format
@@ -789,7 +801,7 @@ class OStatus
        }
 
        /**
-        * @brief Store a feed in several conversation entries
+        * Store a feed in several conversation entries
         *
         * @param string $xml              The feed
         * @param string $conversation     conversation
@@ -870,7 +882,7 @@ class OStatus
        }
 
        /**
-        * @brief Fetch the own post so that it can be stored later
+        * Fetch the own post so that it can be stored later
         *
         * We want to store the original data for later processing.
         * This function is meant for cases where we process a feed with multiple entries.
@@ -909,7 +921,7 @@ class OStatus
        }
 
        /**
-        * @brief Fetch related posts and processes them
+        * Fetch related posts and processes them
         *
         * @param string $related     The link to the related item
         * @param string $related_uri The related item in "uri" format
@@ -989,7 +1001,7 @@ class OStatus
 
                // Even more worse workaround for GNU Social ;-)
                if ($xml == '') {
-                       $related_guess = OStatus::convertHref($related_uri);
+                       $related_guess = self::convertHref($related_uri);
                        $curlResult = Network::curl(str_replace('/notice/', '/api/statuses/show/', $related_guess).'.atom');
 
                        if ($curlResult->isSuccess()) {
@@ -1018,7 +1030,7 @@ class OStatus
        }
 
        /**
-        * @brief Processes the XML for a repeated post
+        * Processes the XML for a repeated post
         *
         * @param DOMXPath $xpath    The xpath object
         * @param object   $entry    The xml entry that is processed
@@ -1087,7 +1099,7 @@ class OStatus
        }
 
        /**
-        * @brief Processes links in the XML
+        * Processes links in the XML
         *
         * @param object $links The xml data that contain links
         * @param array  $item  The item array
@@ -1157,13 +1169,13 @@ class OStatus
        }
 
        /**
-        * @brief Create an url out of an uri
+        * Create an url out of an uri
         *
         * @param string $href URI in the format "parameter1:parameter1:..."
         *
         * @return string URL in the format http(s)://....
         */
-       public static function convertHref($href)
+       private static function convertHref($href)
        {
                $elements = explode(":", $href);
 
@@ -1189,7 +1201,7 @@ class OStatus
        }
 
        /**
-        * @brief Checks if the current post is a reshare
+        * Checks if the current post is a reshare
         *
         * @param array $item The item array of thw post
         *
@@ -1206,7 +1218,7 @@ class OStatus
        }
 
        /**
-        * @brief Cleans the body of a post if it contains picture links
+        * Cleans the body of a post if it contains picture links
         *
         * @param string $body The body
         *
@@ -1244,7 +1256,7 @@ class OStatus
        }
 
        /**
-        * @brief Adds the header elements to the XML document
+        * Adds the header elements to the XML document
         *
         * @param DOMDocument $doc       XML document
         * @param array       $owner     Contact data of the poster
@@ -1259,27 +1271,29 @@ class OStatus
                $root = $doc->createElementNS(ActivityNamespace::ATOM1, 'feed');
                $doc->appendChild($root);
 
-               $root->setAttribute("xmlns:thr", ActivityNamespace::THREAD);
-               $root->setAttribute("xmlns:georss", ActivityNamespace::GEORSS);
-               $root->setAttribute("xmlns:activity", ActivityNamespace::ACTIVITY);
-               $root->setAttribute("xmlns:media", ActivityNamespace::MEDIA);
-               $root->setAttribute("xmlns:poco", ActivityNamespace::POCO);
-               $root->setAttribute("xmlns:ostatus", ActivityNamespace::OSTATUS);
-               $root->setAttribute("xmlns:statusnet", ActivityNamespace::STATUSNET);
-               $root->setAttribute("xmlns:mastodon", ActivityNamespace::MASTODON);
+               if (!$feed_mode) {
+                       $root->setAttribute("xmlns:thr", ActivityNamespace::THREAD);
+                       $root->setAttribute("xmlns:georss", ActivityNamespace::GEORSS);
+                       $root->setAttribute("xmlns:activity", ActivityNamespace::ACTIVITY);
+                       $root->setAttribute("xmlns:media", ActivityNamespace::MEDIA);
+                       $root->setAttribute("xmlns:poco", ActivityNamespace::POCO);
+                       $root->setAttribute("xmlns:ostatus", ActivityNamespace::OSTATUS);
+                       $root->setAttribute("xmlns:statusnet", ActivityNamespace::STATUSNET);
+                       $root->setAttribute("xmlns:mastodon", ActivityNamespace::MASTODON);
+               }
 
                $title = '';
                $selfUri = '/feed/' . $owner["nick"] . '/';
                switch ($filter) {
                        case 'activity':
-                               $title = L10n::t('%s\'s timeline', $owner['name']);
+                               $title = DI::l10n()->t('%s\'s timeline', $owner['name']);
                                $selfUri .= $filter;
                                break;
                        case 'posts':
-                               $title = L10n::t('%s\'s posts', $owner['name']);
+                               $title = DI::l10n()->t('%s\'s posts', $owner['name']);
                                break;
                        case 'comments':
-                               $title = L10n::t('%s\'s comments', $owner['name']);
+                               $title = DI::l10n()->t('%s\'s comments', $owner['name']);
                                $selfUri .= $filter;
                                break;
                }
@@ -1292,11 +1306,11 @@ class OStatus
                XML::addElement($doc, $root, "generator", FRIENDICA_PLATFORM, $attributes);
                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"], Config::get('config', 'sitename')));
+               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, "updated", DateTimeFormat::utcNow(DateTimeFormat::ATOM));
 
-               $author = self::addAuthor($doc, $owner);
+               $author = self::addAuthor($doc, $owner, true, $feed_mode);
                $root->appendChild($author);
 
                $attributes = ["href" => $owner["url"], "rel" => "alternate", "type" => "text/html"];
@@ -1310,14 +1324,16 @@ class OStatus
 
                self::hublinks($doc, $root, $owner["nick"]);
 
-               $attributes = ["href" => DI::baseUrl() . "/salmon/" . $owner["nick"], "rel" => "salmon"];
-               XML::addElement($doc, $root, "link", "", $attributes);
+               if (!$feed_mode) {
+                       $attributes = ["href" => DI::baseUrl() . "/salmon/" . $owner["nick"], "rel" => "salmon"];
+                       XML::addElement($doc, $root, "link", "", $attributes);
 
-               $attributes = ["href" => DI::baseUrl() . "/salmon/" . $owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-replies"];
-               XML::addElement($doc, $root, "link", "", $attributes);
+                       $attributes = ["href" => DI::baseUrl() . "/salmon/" . $owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-replies"];
+                       XML::addElement($doc, $root, "link", "", $attributes);
 
-               $attributes = ["href" => DI::baseUrl() . "/salmon/" . $owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-mention"];
-               XML::addElement($doc, $root, "link", "", $attributes);
+                       $attributes = ["href" => DI::baseUrl() . "/salmon/" . $owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-mention"];
+                       XML::addElement($doc, $root, "link", "", $attributes);
+               }
 
                $attributes = ["href" => DI::baseUrl() . $selfUri, "rel" => "self", "type" => "application/atom+xml"];
                XML::addElement($doc, $root, "link", "", $attributes);
@@ -1333,7 +1349,7 @@ class OStatus
        }
 
        /**
-        * @brief Add the link to the push hubs to the XML document
+        * Add the link to the push hubs to the XML document
         *
         * @param DOMDocument $doc  XML document
         * @param object      $root XML root element where the hub links are added
@@ -1348,7 +1364,7 @@ class OStatus
        }
 
        /**
-        * @brief Adds attachment data to the XML document
+        * Adds attachment data to the XML document
         *
         * @param DOMDocument $doc  XML document
         * @param object      $root XML root element where the hub links are added
@@ -1377,7 +1393,7 @@ class OStatus
                                $attributes = ["rel" => "enclosure",
                                                "href" => $siteinfo["url"],
                                                "type" => "text/html; charset=UTF-8",
-                                               "length" => "",
+                                               "length" => "0",
                                                "title" => ($siteinfo["title"] ?? '') ?: $siteinfo["url"],
                                ];
                                XML::addElement($doc, $root, "link", "", $attributes);
@@ -1386,7 +1402,7 @@ class OStatus
                                break;
                }
 
-               if (!Config::get('system', 'ostatus_not_attach_preview') && ($siteinfo["type"] != "photo") && isset($siteinfo["image"])) {
+               if (!DI::config()->get('system', 'ostatus_not_attach_preview') && ($siteinfo["type"] != "photo") && isset($siteinfo["image"])) {
                        $imgdata = Images::getInfoFromURLCached($siteinfo["image"]);
                        if ($imgdata) {
                                $attributes = ["rel" => "enclosure",
@@ -1421,79 +1437,84 @@ class OStatus
        }
 
        /**
-        * @brief Adds the author element to the XML document
+        * Adds the author element to the XML document
         *
         * @param DOMDocument $doc          XML document
         * @param array       $owner        Contact data of the poster
         * @param bool        $show_profile Whether to show profile
+        * @param bool        $feed_mode Behave like a regular feed for users if true
         *
         * @return \DOMElement author element
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function addAuthor(DOMDocument $doc, array $owner, $show_profile = true)
+       private static function addAuthor(DOMDocument $doc, array $owner, $show_profile = true, $feed_mode = false)
        {
-               $profile = DBA::selectFirst('profile', ['homepage', 'publish'], ['uid' => $owner['uid'], 'is-default' => true]);
+               $profile = DBA::selectFirst('profile', ['homepage', 'publish'], ['uid' => $owner['uid']]);
                $author = $doc->createElement("author");
-               XML::addElement($doc, $author, "id", $owner["url"]);
-               if ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) {
-                       XML::addElement($doc, $author, "activity:object-type", Activity\ObjectType::GROUP);
-               } else {
-                       XML::addElement($doc, $author, "activity:object-type", Activity\ObjectType::PERSON);
+               if (!$feed_mode) {
+                       XML::addElement($doc, $author, "id", $owner["url"]);
+                       if ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) {
+                               XML::addElement($doc, $author, "activity:object-type", Activity\ObjectType::GROUP);
+                       } else {
+                               XML::addElement($doc, $author, "activity:object-type", Activity\ObjectType::PERSON);
+                       }
                }
                XML::addElement($doc, $author, "uri", $owner["url"]);
                XML::addElement($doc, $author, "name", $owner["nick"]);
                XML::addElement($doc, $author, "email", $owner["addr"]);
-               if ($show_profile) {
-                       XML::addElement($doc, $author, "summary", BBCode::convert($owner["about"], false, 7));
+               if ($show_profile && !$feed_mode) {
+                       XML::addElement($doc, $author, "summary", BBCode::convert($owner["about"], false, BBCode::OSTATUS));
                }
 
-               $attributes = ["rel" => "alternate", "type" => "text/html", "href" => $owner["url"]];
-               XML::addElement($doc, $author, "link", "", $attributes);
-
-               $attributes = [
-                               "rel" => "avatar",
-                               "type" => "image/jpeg", // To-Do?
-                               "media:width" => 300,
-                               "media:height" => 300,
-                               "href" => $owner["photo"]];
-               XML::addElement($doc, $author, "link", "", $attributes);
+               if (!$feed_mode) {
+                       $attributes = ["rel" => "alternate", "type" => "text/html", "href" => $owner["url"]];
+                       XML::addElement($doc, $author, "link", "", $attributes);
 
-               if (isset($owner["thumb"])) {
                        $attributes = [
                                        "rel" => "avatar",
                                        "type" => "image/jpeg", // To-Do?
-                                       "media:width" => 80,
-                                       "media:height" => 80,
-                                       "href" => $owner["thumb"]];
+                                       "media:width" => 300,
+                                       "media:height" => 300,
+                                       "href" => $owner["photo"]];
                        XML::addElement($doc, $author, "link", "", $attributes);
-               }
-
-               XML::addElement($doc, $author, "poco:preferredUsername", $owner["nick"]);
-               XML::addElement($doc, $author, "poco:displayName", $owner["name"]);
-               if ($show_profile) {
-                       XML::addElement($doc, $author, "poco:note", BBCode::convert($owner["about"], false, 7));
 
-                       if (trim($owner["location"]) != "") {
-                               $element = $doc->createElement("poco:address");
-                               XML::addElement($doc, $element, "poco:formatted", $owner["location"]);
-                               $author->appendChild($element);
+                       if (isset($owner["thumb"])) {
+                               $attributes = [
+                                               "rel" => "avatar",
+                                               "type" => "image/jpeg", // To-Do?
+                                               "media:width" => 80,
+                                               "media:height" => 80,
+                                               "href" => $owner["thumb"]];
+                               XML::addElement($doc, $author, "link", "", $attributes);
+                       }
+
+                       XML::addElement($doc, $author, "poco:preferredUsername", $owner["nick"]);
+                       XML::addElement($doc, $author, "poco:displayName", $owner["name"]);
+                       if ($show_profile) {
+                               XML::addElement($doc, $author, "poco:note", BBCode::convert($owner["about"], false, BBCode::OSTATUS));
+
+                               if (trim($owner["location"]) != "") {
+                                       $element = $doc->createElement("poco:address");
+                                       XML::addElement($doc, $element, "poco:formatted", $owner["location"]);
+                                       $author->appendChild($element);
+                               }
                        }
-               }
 
-               if (DBA::isResult($profile) && !$show_profile) {
-                       if (trim($profile["homepage"]) != "") {
-                               $urls = $doc->createElement("poco:urls");
-                               XML::addElement($doc, $urls, "poco:type", "homepage");
-                               XML::addElement($doc, $urls, "poco:value", $profile["homepage"]);
-                               XML::addElement($doc, $urls, "poco:primary", "true");
-                               $author->appendChild($urls);
-                       }
+                       if (DBA::isResult($profile) && !$show_profile) {
+                               if (trim($profile["homepage"]) != "") {
+                                       $urls = $doc->createElement("poco:urls");
+                                       XML::addElement($doc, $urls, "poco:type", "homepage");
+                                       XML::addElement($doc, $urls, "poco:value", $profile["homepage"]);
+                                       XML::addElement($doc, $urls, "poco:primary", "true");
+                                       $author->appendChild($urls);
+                               }
 
-                       XML::addElement($doc, $author, "followers", "", ["url" => DI::baseUrl() . "/profile/" . $owner["nick"] . "/contacts/followers"]);
-                       XML::addElement($doc, $author, "statusnet:profile_info", "", ["local_id" => $owner["uid"]]);
+                               XML::addElement($doc, $author, "followers", "", ["url" => DI::baseUrl() . "/profile/" . $owner["nick"] . "/contacts/followers"]);
+                               XML::addElement($doc, $author, "statusnet:profile_info", "", ["local_id" => $owner["uid"]]);
 
-                       if ($profile["publish"]) {
-                               XML::addElement($doc, $author, "mastodon:scope", "public");
+                               if ($profile["publish"]) {
+                                       XML::addElement($doc, $author, "mastodon:scope", "public");
+                               }
                        }
                }
 
@@ -1507,7 +1528,7 @@ class OStatus
         */
 
        /**
-        * @brief Returns the given activity if present - otherwise returns the "post" activity
+        * Returns the given activity if present - otherwise returns the "post" activity
         *
         * @param array $item Data of the item that is to be posted
         *
@@ -1523,7 +1544,7 @@ class OStatus
        }
 
        /**
-        * @brief Returns the given object type if present - otherwise returns the "note" object type
+        * Returns the given object type if present - otherwise returns the "note" object type
         *
         * @param array $item Data of the item that is to be posted
         *
@@ -1539,7 +1560,7 @@ class OStatus
        }
 
        /**
-        * @brief Adds an entry element to the XML document
+        * 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
@@ -1557,7 +1578,7 @@ class OStatus
 
                $repeated_guid = self::getResharedGuid($item);
                if ($repeated_guid != "") {
-                       $xml = self::reshareEntry($doc, $item, $owner, $repeated_guid, $toplevel);
+                       $xml = self::reshareEntry($doc, $item, $owner, $repeated_guid, $toplevel, $feed_mode);
                }
 
                if ($xml) {
@@ -1574,7 +1595,7 @@ class OStatus
        }
 
        /**
-        * @brief Adds a source entry to the XML document
+        * Adds a source entry to the XML document
         *
         * @param DOMDocument $doc     XML document
         * @param array       $contact Array of the contact that is added
@@ -1596,7 +1617,7 @@ class OStatus
        }
 
        /**
-        * @brief Fetches contact data from the contact or the gcontact table
+        * Fetches contact data from the contact or the gcontact table
         *
         * @param string $url   URL of the contact
         * @param array  $owner Contact data of the poster
@@ -1649,27 +1670,28 @@ class OStatus
        }
 
        /**
-        * @brief Adds an entry element with reshared content
+        * 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)?
+        * @param bool        $feed_mode Behave like a regular feed for users if true
         *
         * @return bool Entry element
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function reshareEntry(DOMDocument $doc, array $item, array $owner, $repeated_guid, $toplevel)
+       private static function reshareEntry(DOMDocument $doc, array $item, array $owner, $repeated_guid, $toplevel, $feed_mode = false)
        {
-               if (($item["id"] != $item["parent"]) && (Strings::normaliseLink($item["author-link"]) != Strings::normaliseLink($owner["url"]))) {
+               if (($item['gravity'] != GRAVITY_PARENT) && (Strings::normaliseLink($item["author-link"]) != Strings::normaliseLink($owner["url"]))) {
                        Logger::log("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", Logger::DEBUG);
                }
 
                $entry = self::entryHeader($doc, $owner, $item, $toplevel);
 
-               $condition = ['uid' => $owner["uid"], 'guid' => $repeated_guid, 'private' => false,
+               $condition = ['uid' => $owner["uid"], 'guid' => $repeated_guid, 'private' => [Item::PUBLIC, Item::UNLISTED],
                        'network' => [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]];
                $repeated_item = Item::selectFirst([], $condition);
                if (!DBA::isResult($repeated_item)) {
@@ -1680,42 +1702,44 @@ class OStatus
 
                $title = $owner["nick"]." repeated a notice by ".$contact["nick"];
 
-               self::entryContent($doc, $entry, $item, $owner, $title, Activity::SHARE, false);
+               self::entryContent($doc, $entry, $item, $owner, $title, Activity::SHARE, false, $feed_mode);
 
-               $as_object = $doc->createElement("activity:object");
+               if (!$feed_mode) {
+                       $as_object = $doc->createElement("activity:object");
 
-               XML::addElement($doc, $as_object, "activity:object-type", ActivityNamespace::ACTIVITY_SCHEMA . "activity");
+                       XML::addElement($doc, $as_object, "activity:object-type", ActivityNamespace::ACTIVITY_SCHEMA . "activity");
 
-               self::entryContent($doc, $as_object, $repeated_item, $owner, "", "", false);
+                       self::entryContent($doc, $as_object, $repeated_item, $owner, "", "", false);
 
-               $author = self::addAuthor($doc, $contact, false);
-               $as_object->appendChild($author);
+                       $author = self::addAuthor($doc, $contact, false);
+                       $as_object->appendChild($author);
 
-               $as_object2 = $doc->createElement("activity:object");
+                       $as_object2 = $doc->createElement("activity:object");
 
-               XML::addElement($doc, $as_object2, "activity:object-type", self::constructObjecttype($repeated_item));
+                       XML::addElement($doc, $as_object2, "activity:object-type", self::constructObjecttype($repeated_item));
 
-               $title = sprintf("New comment by %s", $contact["nick"]);
+                       $title = sprintf("New comment by %s", $contact["nick"]);
 
-               self::entryContent($doc, $as_object2, $repeated_item, $owner, $title);
+                       self::entryContent($doc, $as_object2, $repeated_item, $owner, $title);
 
-               $as_object->appendChild($as_object2);
+                       $as_object->appendChild($as_object2);
 
-               self::entryFooter($doc, $as_object, $item, $owner, false);
+                       self::entryFooter($doc, $as_object, $item, $owner, false);
 
-               $source = self::sourceEntry($doc, $contact);
+                       $source = self::sourceEntry($doc, $contact);
 
-               $as_object->appendChild($source);
+                       $as_object->appendChild($source);
 
-               $entry->appendChild($as_object);
+                       $entry->appendChild($as_object);
+               }
 
-               self::entryFooter($doc, $entry, $item, $owner);
+               self::entryFooter($doc, $entry, $item, $owner, true, $feed_mode);
 
                return $entry;
        }
 
        /**
-        * @brief Adds an entry element with a "like"
+        * Adds an entry element with a "like"
         *
         * @param DOMDocument $doc      XML document
         * @param array       $item     Data of the item that is to be posted
@@ -1728,7 +1752,7 @@ class OStatus
         */
        private static function likeEntry(DOMDocument $doc, array $item, array $owner, $toplevel)
        {
-               if (($item["id"] != $item["parent"]) && (Strings::normaliseLink($item["author-link"]) != Strings::normaliseLink($owner["url"]))) {
+               if (($item['gravity'] != GRAVITY_PARENT) && (Strings::normaliseLink($item["author-link"]) != Strings::normaliseLink($owner["url"]))) {
                        Logger::log("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", Logger::DEBUG);
                }
 
@@ -1754,7 +1778,7 @@ class OStatus
        }
 
        /**
-        * @brief Adds the person object element to the XML document
+        * Adds the person object element to the XML document
         *
         * @param DOMDocument $doc     XML document
         * @param array       $owner   Contact data of the poster
@@ -1799,7 +1823,7 @@ class OStatus
        }
 
        /**
-        * @brief Adds a follow/unfollow entry element
+        * Adds a follow/unfollow entry element
         *
         * @param DOMDocument $doc      XML document
         * @param array       $item     Data of the follow/unfollow message
@@ -1812,16 +1836,17 @@ class OStatus
         */
        private static function followEntry(DOMDocument $doc, array $item, array $owner, $toplevel)
        {
-               $item["id"] = $item["parent"] = 0;
+               $item["id"] = $item['parent'] = 0;
                $item["created"] = $item["edited"] = date("c");
-               $item["private"] = true;
+               $item["private"] = Item::PRIVATE;
 
                $contact = Probe::uri($item['follow']);
+               $item['follow'] = $contact['url'];
 
-               if ($contact['alias'] == '') {
-                       $contact['alias'] = $contact["url"];
-               } else {
+               if ($contact['alias']) {
                        $item['follow'] = $contact['alias'];
+               } else {
+                       $contact['alias'] = $contact['url'];
                }
 
                $condition = ['uid' => $owner['uid'], 'nurl' => Strings::normaliseLink($contact["url"])];
@@ -1834,12 +1859,12 @@ class OStatus
                }
 
                if ($item['verb'] == Activity::FOLLOW) {
-                       $message = L10n::t('%s is now following %s.');
-                       $title = L10n::t('following');
+                       $message = DI::l10n()->t('%s is now following %s.');
+                       $title = DI::l10n()->t('following');
                        $action = "subscription";
                } else {
-                       $message = L10n::t('%s stopped following %s.');
-                       $title = L10n::t('stopped following');
+                       $message = DI::l10n()->t('%s stopped following %s.');
+                       $title = DI::l10n()->t('stopped following');
                        $action = "unfollow";
                }
 
@@ -1863,7 +1888,7 @@ class OStatus
        }
 
        /**
-        * @brief Adds a regular entry element
+        * Adds a regular entry element
         *
         * @param DOMDocument $doc       XML document
         * @param array       $item      Data of the item that is to be posted
@@ -1877,13 +1902,13 @@ class OStatus
         */
        private static function noteEntry(DOMDocument $doc, array $item, array $owner, $toplevel, $feed_mode)
        {
-               if (($item["id"] != $item["parent"]) && (Strings::normaliseLink($item["author-link"]) != Strings::normaliseLink($owner["url"]))) {
+               if (($item['gravity'] != GRAVITY_PARENT) && (Strings::normaliseLink($item["author-link"]) != Strings::normaliseLink($owner["url"]))) {
                        Logger::log("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", Logger::DEBUG);
                }
 
                if (!$toplevel) {
                        if (!empty($item['title'])) {
-                               $title = BBCode::convert($item['title'], false, 7);
+                               $title = BBCode::convert($item['title'], false, BBCode::OSTATUS);
                        } else {
                                $title = sprintf("New note by %s", $owner["nick"]);
                        }
@@ -1893,7 +1918,9 @@ class OStatus
 
                $entry = self::entryHeader($doc, $owner, $item, $toplevel);
 
-               XML::addElement($doc, $entry, "activity:object-type", Activity\ObjectType::NOTE);
+               if (!$feed_mode) {
+                       XML::addElement($doc, $entry, "activity:object-type", Activity\ObjectType::NOTE);
+               }
 
                self::entryContent($doc, $entry, $item, $owner, $title, '', true, $feed_mode);
 
@@ -1903,7 +1930,7 @@ class OStatus
        }
 
        /**
-        * @brief Adds a header element to the XML document
+        * Adds a header element to the XML document
         *
         * @param DOMDocument $doc      XML document
         * @param array       $owner    Contact data of the poster
@@ -1944,7 +1971,7 @@ class OStatus
        }
 
        /**
-        * @brief Adds elements to the XML document
+        * Adds elements to the XML document
         *
         * @param DOMDocument $doc       XML document
         * @param \DOMElement $entry     Entry element where the content is added
@@ -1972,7 +1999,7 @@ class OStatus
                        $body = "[b]".$item['title']."[/b]\n\n".$body;
                }
 
-               $body = BBCode::convert($body, false, 7);
+               $body = BBCode::convert($body, false, BBCode::OSTATUS);
 
                XML::addElement($doc, $entry, "content", $body, ["type" => "html"]);
 
@@ -1993,7 +2020,7 @@ class OStatus
        }
 
        /**
-        * @brief Adds the elements at the foot of an entry to the XML document
+        * Adds the elements at the foot of an entry to the XML document
         *
         * @param DOMDocument $doc       XML document
         * @param object      $entry     The entry element where the elements are added
@@ -2008,8 +2035,8 @@ class OStatus
        {
                $mentioned = [];
 
-               if (($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
-                       $parent = Item::selectFirst(['guid', 'author-link', 'owner-link'], ['id' => $item["parent"]]);
+               if ($item['gravity'] != GRAVITY_PARENT) {
+                       $parent = Item::selectFirst(['guid', 'author-link', 'owner-link'], ['id' => $item['parent']]);
                        $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
 
                        $thrparent = Item::selectFirst(['guid', 'author-link', 'owner-link', 'plink'], ['uid' => $owner["uid"], 'uri' => $parent_item]);
@@ -2035,7 +2062,7 @@ class OStatus
                        XML::addElement($doc, $entry, "link", "", $attributes);
                }
 
-               if (!$feed_mode && (intval($item["parent"]) > 0)) {
+               if (!$feed_mode && (intval($item['parent']) > 0)) {
                        $conversation_href = $conversation_uri = str_replace('/objects/', '/context/', $item['parent-uri']);
 
                        if (isset($parent_item)) {
@@ -2050,83 +2077,79 @@ class OStatus
                                }
                        }
 
-                       XML::addElement($doc, $entry, "link", "", ["rel" => "ostatus:conversation", "href" => $conversation_href]);
-
-                       $attributes = [
-                                       "href" => $conversation_href,
-                                       "local_id" => $item["parent"],
-                                       "ref" => $conversation_uri];
-
-                       XML::addElement($doc, $entry, "ostatus:conversation", $conversation_uri, $attributes);
-               }
+                       if (!$feed_mode) {
+                               XML::addElement($doc, $entry, "link", "", ["rel" => "ostatus:conversation", "href" => $conversation_href]);
 
-               $tags = item::getFeedTags($item);
+                               $attributes = [
+                                               "href" => $conversation_href,
+                                               "local_id" => $item['parent'],
+                                               "ref" => $conversation_uri];
 
-               if (count($tags)) {
-                       foreach ($tags as $t) {
-                               if ($t[0] == "@") {
-                                       $mentioned[$t[1]] = $t[1];
-                               }
+                               XML::addElement($doc, $entry, "ostatus:conversation", $conversation_uri, $attributes);
                        }
                }
 
-               // Make sure that mentions are accepted (GNU Social has problems with mixing HTTP and HTTPS)
-               $newmentions = [];
-               foreach ($mentioned as $mention) {
-                       $newmentions[str_replace("http://", "https://", $mention)] = str_replace("http://", "https://", $mention);
-                       $newmentions[str_replace("https://", "http://", $mention)] = str_replace("https://", "http://", $mention);
+               // uri-id isn't present for follow entry pseudo-items
+               $tags = Tag::getByURIId($item['uri-id'] ?? 0);
+               foreach ($tags as $tag) {
+                       $mentioned[$tag['url']] = $tag['url'];
                }
-               $mentioned = $newmentions;
 
-               foreach ($mentioned as $mention) {
-                       $condition = ['uid' => $owner['uid'], 'nurl' => Strings::normaliseLink($mention)];
-                       $contact = DBA::selectFirst('contact', ['forum', 'prv', 'self', 'contact-type'], $condition);
-                       if ($contact["forum"] || $contact["prv"] || ($owner['contact-type'] == Contact::TYPE_COMMUNITY) ||
-                               ($contact['self'] && ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY))) {
-                               XML::addElement($doc, $entry, "link", "",
-                                       [
-                                               "rel" => "mentioned",
-                                               "ostatus:object-type" => Activity\ObjectType::GROUP,
-                                               "href" => $mention]
-                               );
-                       } else {
-                               XML::addElement($doc, $entry, "link", "",
-                                       [
-                                               "rel" => "mentioned",
-                                               "ostatus:object-type" => Activity\ObjectType::PERSON,
-                                               "href" => $mention]
-                               );
+               if (!$feed_mode) {
+                       // Make sure that mentions are accepted (GNU Social has problems with mixing HTTP and HTTPS)
+                       $newmentions = [];
+                       foreach ($mentioned as $mention) {
+                               $newmentions[str_replace("http://", "https://", $mention)] = str_replace("http://", "https://", $mention);
+                               $newmentions[str_replace("https://", "http://", $mention)] = str_replace("https://", "http://", $mention);
+                       }
+                       $mentioned = $newmentions;
+
+                       foreach ($mentioned as $mention) {
+                               $contact = Contact::getByURL($mention, 0, ['contact-type']);
+                               if (!empty($contact) && ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) {
+                                       XML::addElement($doc, $entry, "link", "",
+                                               [
+                                                       "rel" => "mentioned",
+                                                       "ostatus:object-type" => Activity\ObjectType::GROUP,
+                                                       "href" => $mention]
+                                       );
+                               } else {
+                                       XML::addElement($doc, $entry, "link", "",
+                                               [
+                                                       "rel" => "mentioned",
+                                                       "ostatus:object-type" => Activity\ObjectType::PERSON,
+                                                       "href" => $mention]
+                                       );
+                               }
                        }
-               }
 
-               if ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) {
-                       XML::addElement($doc, $entry, "link", "", [
-                               "rel" => "mentioned",
-                               "ostatus:object-type" => "http://activitystrea.ms/schema/1.0/group",
-                               "href" => $owner['url']
-                       ]);
-               }
+                       if ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) {
+                               XML::addElement($doc, $entry, "link", "", [
+                                       "rel" => "mentioned",
+                                       "ostatus:object-type" => "http://activitystrea.ms/schema/1.0/group",
+                                       "href" => $owner['url']
+                               ]);
+                       }
 
-               if (!$item["private"] && !$feed_mode) {
-                       XML::addElement($doc, $entry, "link", "", ["rel" => "ostatus:attention",
-                                                                       "href" => "http://activityschema.org/collection/public"]);
-                       XML::addElement($doc, $entry, "link", "", ["rel" => "mentioned",
-                                                                       "ostatus:object-type" => "http://activitystrea.ms/schema/1.0/collection",
-                                                                       "href" => "http://activityschema.org/collection/public"]);
-                       XML::addElement($doc, $entry, "mastodon:scope", "public");
+                       if ($item['private'] != Item::PRIVATE) {
+                               XML::addElement($doc, $entry, "link", "", ["rel" => "ostatus:attention",
+                                                                               "href" => "http://activityschema.org/collection/public"]);
+                               XML::addElement($doc, $entry, "link", "", ["rel" => "mentioned",
+                                                                               "ostatus:object-type" => "http://activitystrea.ms/schema/1.0/collection",
+                                                                               "href" => "http://activityschema.org/collection/public"]);
+                               XML::addElement($doc, $entry, "mastodon:scope", "public");
+                       }
                }
 
-               if (count($tags)) {
-                       foreach ($tags as $t) {
-                               if ($t[0] != "@") {
-                                       XML::addElement($doc, $entry, "category", "", ["term" => $t[2]]);
-                               }
+               foreach ($tags as $tag) {
+                       if ($tag['type'] == Tag::HASHTAG) {
+                               XML::addElement($doc, $entry, "category", "", ["term" => $tag['name']]);
                        }
                }
 
                self::getAttachment($doc, $entry, $item);
 
-               if ($complete && ($item["id"] > 0)) {
+               if (!$feed_mode && $complete && ($item["id"] > 0)) {
                        $app = $item["app"];
                        if ($app == "") {
                                $app = "web";
@@ -2157,8 +2180,6 @@ class OStatus
         * Updates the provided last_update parameter if the result comes from the
         * cache or it is empty
         *
-        * @brief Creates the XML feed for a given nickname
-        *
         * @param string  $owner_nick  Nickname of the feed owner
         * @param string  $last_update Date of the last update
         * @param integer $max_items   Number of maximum items to fetch
@@ -2197,12 +2218,12 @@ class OStatus
                        $last_update = 'now -30 days';
                }
 
-               $check_date = DateTimeFormat::utc($last_update);
+               $check_date = $feed_mode ? '' : DateTimeFormat::utc($last_update);
                $authorid = Contact::getIdForURL($owner["url"], 0, true);
 
                $condition = ["`uid` = ? AND `received` > ? AND NOT `deleted`
-                       AND NOT `private` AND `visible` AND `wall` AND `parent-network` IN (?, ?)",
-                       $owner["uid"], $check_date, Protocol::OSTATUS, Protocol::DFRN];
+                       AND `private` != ? AND `visible` AND `wall` AND `parent-network` IN (?, ?)",
+                       $owner["uid"], $check_date, Item::PRIVATE, Protocol::OSTATUS, Protocol::DFRN];
 
                if ($filter === 'comments') {
                        $condition[0] .= " AND `object-type` = ? ";
@@ -2231,10 +2252,14 @@ class OStatus
                $root = self::addHeader($doc, $owner, $filter, $feed_mode);
 
                foreach ($items as $item) {
-                       if (Config::get('system', 'ostatus_debug')) {
+                       if (DI::config()->get('system', 'ostatus_debug')) {
                                $item['body'] .= '🍼';
                        }
 
+                       if (in_array($item["verb"], [Activity::FOLLOW, Activity::O_UNFOLLOW, Activity::LIKE])) {
+                               continue;
+                       }
+
                        $entry = self::entry($doc, $item, $owner, false, $feed_mode);
                        $root->appendChild($entry);
 
@@ -2246,7 +2271,7 @@ class OStatus
                $feeddata = trim($doc->saveXML());
 
                $msg = ['feed' => $feeddata, 'last_update' => $last_update];
-               DI::cache()->set($cachekey, $msg, Cache::QUARTER_HOUR);
+               DI::cache()->set($cachekey, $msg, Duration::QUARTER_HOUR);
 
                Logger::log('Feed duration: ' . number_format(microtime(true) - $stamp, 3) . ' - ' . $owner_nick . ' - ' . $filter . ' - ' . $previous_created, Logger::DEBUG);
 
@@ -2254,7 +2279,7 @@ class OStatus
        }
 
        /**
-        * @brief Creates the XML for a salmon message
+        * Creates the XML for a salmon message
         *
         * @param array $item  Data of the item that is to be posted
         * @param array $owner Contact data of the poster
@@ -2268,7 +2293,7 @@ class OStatus
                $doc = new DOMDocument('1.0', 'utf-8');
                $doc->formatOutput = true;
 
-               if (Config::get('system', 'ostatus_debug')) {
+               if (DI::config()->get('system', 'ostatus_debug')) {
                        $item['body'] .= '🐟';
                }