]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/OStatus.php
Merge pull request #7634 from tobiasd/20190916-de
[friendica.git] / src / Protocol / OStatus.php
index b1cc4f40203d22069cd7f35f37854b895568dcfa..054eaf51b2812939153424fa80058c9485ee18d9 100644 (file)
@@ -10,6 +10,7 @@ use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
+use Friendica\Core\PConfig;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Lock;
@@ -19,6 +20,7 @@ use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
 use Friendica\Model\GContact;
+use Friendica\Model\APContact;
 use Friendica\Model\Item;
 use Friendica\Model\User;
 use Friendica\Network\Probe;
@@ -53,7 +55,7 @@ class OStatus
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function fetchAuthor(DOMXPath $xpath, $context, array $importer, array &$contact, $onlyfetch)
+       private static function fetchAuthor(DOMXPath $xpath, $context, array $importer, array &$contact = null, $onlyfetch)
        {
                $author = [];
                $author["author-link"] = XML::getFirstNodeValue($xpath, 'atom:author/atom:uri/text()', $context);
@@ -72,7 +74,7 @@ class OStatus
                }
                $author["author-id"] = Contact::getIdForURL($author["author-link"]);
 
-               $author["contact-id"] = $contact["id"];
+               $author['contact-id'] = defaults($contact, 'id', $author['author-id']);
 
                $contact = [];
 
@@ -113,6 +115,8 @@ class OStatus
                if (DBA::isResult($contact)) {
                        if ($contact['blocked']) {
                                $contact['id'] = -1;
+                       } elseif (!empty(APContact::getByURL($contact['url'], false))) {
+                               ActivityPub\Receiver::switchContact($contact['id'], $importer['uid'], $contact['url']);
                        }
                        $author["contact-id"] = $contact["id"];
                }
@@ -322,7 +326,7 @@ class OStatus
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function process($xml, array $importer, array &$contact, &$hub, $stored = false, $initialize = true)
+       private static function process($xml, array $importer, array &$contact = null, &$hub, $stored = false, $initialize = true)
        {
                if ($initialize) {
                        self::$itemlist = [];
@@ -417,13 +421,6 @@ class OStatus
                                $author = self::fetchAuthor($xpath, $entry, $importer, $contact, $stored);
                        }
 
-                       $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:preferredUsername/text()', $entry);
-                       if ($value != "") {
-                               $nickname = $value;
-                       } else {
-                               $nickname = $author["author-name"];
-                       }
-
                        $item = array_merge($header, $author);
 
                        $item["uri"] = XML::getFirstNodeValue($xpath, 'atom:id/text()', $entry);
@@ -463,7 +460,7 @@ class OStatus
                        }
 
                        if ($item["verb"] == ACTIVITY_FOLLOW) {
-                               Contact::addRelationship($importer, $contact, $item, $nickname);
+                               Contact::addRelationship($importer, $contact, $item);
                                continue;
                        }
 
@@ -493,10 +490,11 @@ class OStatus
                        if ($initialize && (count(self::$itemlist) > 0)) {
                                if (self::$itemlist[0]['uri'] == self::$itemlist[0]['parent-uri']) {
                                        // We will import it everytime, when it is started by our contacts
-                                       $valid = !empty(self::$itemlist[0]['contact-id']);
+                                       $valid = Contact::isSharingByURL(self::$itemlist[0]['author-link'], self::$itemlist[0]['uid']);
+
                                        if (!$valid) {
                                                // If not, then it depends on this setting
-                                               $valid = !Config::get('system', 'ostatus_full_threads');
+                                               $valid = ((self::$itemlist[0]['uid'] == 0) || !PConfig::get(self::$itemlist[0]['uid'], 'system', 'accept_only_sharer', false));
                                                if ($valid) {
                                                        Logger::log("Item with uri ".self::$itemlist[0]['uri']." will be imported due to the system settings.", Logger::DEBUG);
                                                }
@@ -508,7 +506,7 @@ class OStatus
                                                $valid = false;
                                                $verbs = [ACTIVITY_POST, ACTIVITY_SHARE];
                                                foreach (self::$itemlist as $item) {
-                                                       if (!empty($item['contact-id']) && in_array($item['verb'], $verbs)) {
+                                                       if (in_array($item['verb'], $verbs) && Contact::isSharingByURL($item['author-link'], $item['uid'])) {
                                                                $valid = true;
                                                        }
                                                }
@@ -745,7 +743,7 @@ class OStatus
 
                self::$conv_list[$conversation] = true;
 
-               $curlResult = Network::curl($conversation, false, $redirects, ['accept_content' => 'application/atom+xml, text/html']);
+               $curlResult = Network::curl($conversation, false, ['accept_content' => 'application/atom+xml, text/html']);
 
                if (!$curlResult->isSuccess()) {
                        return;
@@ -938,7 +936,7 @@ class OStatus
                }
 
                $stored = false;
-               $curlResult = Network::curl($related, false, $redirects, ['accept_content' => 'application/atom+xml, text/html']);
+               $curlResult = Network::curl($related, false, ['accept_content' => 'application/atom+xml, text/html']);
 
                if (!$curlResult->isSuccess()) {
                        return;
@@ -1453,7 +1451,7 @@ class OStatus
         * @param array       $owner        Contact data of the poster
         * @param bool        $show_profile Whether to show profile
         *
-        * @return object author element
+        * @return \DOMElement author element
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function addAuthor(DOMDocument $doc, array $owner, $show_profile = true)
@@ -1515,7 +1513,7 @@ class OStatus
                                $author->appendChild($urls);
                        }
 
-                       XML::addElement($doc, $author, "followers", "", ["url" => System::baseUrl()."/viewcontacts/".$owner["nick"]]);
+                       XML::addElement($doc, $author, "followers", "", ["url" => System::baseUrl() . "/profile/" . $owner["nick"] . "/contacts/followers"]);
                        XML::addElement($doc, $author, "statusnet:profile_info", "", ["local_id" => $owner["uid"]]);
 
                        if ($profile["publish"]) {
@@ -1573,7 +1571,7 @@ class OStatus
         * @param bool        $toplevel  optional default false
         * @param bool        $feed_mode Behave like a regular feed for users if true
         *
-        * @return object Entry element
+        * @return \DOMElement Entry element
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
@@ -1605,7 +1603,7 @@ class OStatus
         * @param DOMDocument $doc     XML document
         * @param array       $contact Array of the contact that is added
         *
-        * @return object Source element
+        * @return \DOMElement Source element
         * @throws \Exception
         */
        private static function sourceEntry(DOMDocument $doc, array $contact)
@@ -1748,7 +1746,7 @@ class OStatus
         * @param array       $owner    Contact data of the poster
         * @param bool        $toplevel Is it for en entry element (false) or a feed entry (true)?
         *
-        * @return object Entry element with "like"
+        * @return \DOMElement Entry element with "like"
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
@@ -1763,19 +1761,16 @@ class OStatus
                $verb = NAMESPACE_ACTIVITY_SCHEMA."favorite";
                self::entryContent($doc, $entry, $item, $owner, "Favorite", $verb, false);
 
-               $as_object = $doc->createElement("activity:object");
-
                $parent = Item::selectFirst([], ['uri' => $item["thr-parent"], 'uid' => $item["uid"]]);
+               if (DBA::isResult($parent)) {
+                       $as_object = $doc->createElement("activity:object");
 
-               if (!$parent) {
-                       $parent = [];
-               }
-
-               XML::addElement($doc, $as_object, "activity:object-type", self::constructObjecttype($parent));
+                       XML::addElement($doc, $as_object, "activity:object-type", self::constructObjecttype($parent));
 
-               self::entryContent($doc, $as_object, $parent, $owner, "New entry");
+                       self::entryContent($doc, $as_object, $parent, $owner, "New entry");
 
-               $entry->appendChild($as_object);
+                       $entry->appendChild($as_object);
+               }
 
                self::entryFooter($doc, $entry, $item, $owner);
 
@@ -1835,7 +1830,7 @@ class OStatus
         * @param array       $owner    Contact data of the poster
         * @param bool        $toplevel Is it for en entry element (false) or a feed entry (true)?
         *
-        * @return object Entry element
+        * @return \DOMElement Entry element
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
@@ -1900,7 +1895,7 @@ class OStatus
         * @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 object Entry element
+        * @return \DOMElement Entry element
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
@@ -1976,7 +1971,7 @@ class OStatus
         * @brief Adds elements to the XML document
         *
         * @param DOMDocument $doc       XML document
-        * @param object      $entry     Entry element where the content is added
+        * @param \DOMElement $entry     Entry element where the content is added
         * @param array       $item      Data of the item that is to be posted
         * @param array       $owner     Contact data of the poster
         * @param string      $title     Title for the post
@@ -1986,7 +1981,7 @@ class OStatus
         * @return void
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function entryContent(DOMDocument $doc, $entry, array $item, array $owner, $title, $verb = "", $complete = true, $feed_mode = false)
+       private static function entryContent(DOMDocument $doc, \DOMElement $entry, array $item, array $owner, $title, $verb = "", $complete = true, $feed_mode = false)
        {
                if ($verb == "") {
                        $verb = self::constructVerb($item);
@@ -2229,7 +2224,7 @@ class OStatus
                $check_date = DateTimeFormat::utc($last_update);
                $authorid = Contact::getIdForURL($owner["url"], 0, true);
 
-               $condition = ["`uid` = ? AND `created` > ? AND NOT `deleted`
+               $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];
 
@@ -2244,7 +2239,7 @@ class OStatus
                        $condition[] = $authorid;
                }
 
-               $params = ['order' => ['created' => true], 'limit' => $max_items];
+               $params = ['order' => ['received' => true], 'limit' => $max_items];
 
                if ($filter === 'posts') {
                        $ret = Item::selectThread([], $condition, $params);
@@ -2307,4 +2302,19 @@ class OStatus
 
                return trim($doc->saveXML());
        }
+
+       /**
+        * Checks if the given contact url does support OStatus
+        *
+        * @param string  $url    profile url
+        * @param boolean $update Update the profile
+        * @return boolean
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function isSupportedByContactUrl($url, $update = false)
+       {
+               $probe = Probe::uri($url, Protocol::OSTATUS, 0, !$update);
+               return $probe['network'] == Protocol::OSTATUS;
+       }
 }