]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/OStatus.php
Merge pull request #8835 from tobiasd/20200701-lng
[friendica.git] / src / Protocol / OStatus.php
index dc30b596fcc2b54098e3dc7e3db1a4dacff18ced..0b9a45fc3b4082c8e0e1034791466ca8555e61c4 100644 (file)
@@ -1,7 +1,24 @@
 <?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;
@@ -18,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;
@@ -230,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 = [];
                }
 
@@ -420,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);
 
@@ -568,7 +588,7 @@ class OStatus
                        return;
                }
 
-               Item::delete($condition);
+               Item::markForDeletion($condition);
 
                Logger::log('Deleted item with uri '.$item['uri'].' for user '.$item['uid']);
        }
@@ -635,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);
                                        }
                                }
                        }
@@ -686,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);
@@ -985,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()) {
@@ -1159,7 +1175,7 @@ class OStatus
         *
         * @return string URL in the format http(s)://....
         */
-       public static function convertHref($href)
+       private static function convertHref($href)
        {
                $elements = explode(":", $href);
 
@@ -1440,7 +1456,7 @@ class OStatus
                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));
+                       XML::addElement($doc, $author, "summary", BBCode::convert($owner["about"], false, BBCode::OSTATUS));
                }
 
                $attributes = ["rel" => "alternate", "type" => "text/html", "href" => $owner["url"]];
@@ -1467,7 +1483,7 @@ class OStatus
                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));
+                       XML::addElement($doc, $author, "poco:note", BBCode::convert($owner["about"], false, BBCode::OSTATUS));
 
                        if (trim($owner["location"]) != "") {
                                $element = $doc->createElement("poco:address");
@@ -1659,13 +1675,13 @@ class OStatus
         */
        private static function reshareEntry(DOMDocument $doc, array $item, array $owner, $repeated_guid, $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);
                }
 
                $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)) {
@@ -1724,7 +1740,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);
                }
 
@@ -1808,16 +1824,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"])];
@@ -1873,13 +1890,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"]);
                        }
@@ -1968,7 +1985,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"]);
 
@@ -2004,8 +2021,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]);
@@ -2031,7 +2048,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,20 +2067,16 @@ class OStatus
 
                        $attributes = [
                                        "href" => $conversation_href,
-                                       "local_id" => $item["parent"],
+                                       "local_id" => $item['parent'],
                                        "ref" => $conversation_uri];
 
                        XML::addElement($doc, $entry, "ostatus:conversation", $conversation_uri, $attributes);
                }
 
-               $tags = item::getFeedTags($item);
-
-               if (count($tags)) {
-                       foreach ($tags as $t) {
-                               if ($t[0] == "@") {
-                                       $mentioned[$t[1]] = $t[1];
-                               }
-                       }
+               // 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'];
                }
 
                // Make sure that mentions are accepted (GNU Social has problems with mixing HTTP and HTTPS)
@@ -2075,10 +2088,8 @@ class OStatus
                $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))) {
+                       $contact = Contact::getByURL($mention, 0, ['contact-type']);
+                       if (!empty($contact) && ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) {
                                XML::addElement($doc, $entry, "link", "",
                                        [
                                                "rel" => "mentioned",
@@ -2103,7 +2114,7 @@ class OStatus
                        ]);
                }
 
-               if (!$item["private"] && !$feed_mode) {
+               if (($item['private'] != 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",
@@ -2112,11 +2123,9 @@ class OStatus
                        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']]);
                        }
                }
 
@@ -2195,8 +2204,8 @@ class OStatus
                $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` = ? ";