]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/OStatus.php
Merge pull request #9590 from MrPetovan/bug/fatal-errors
[friendica.git] / src / Protocol / OStatus.php
index 9c87f367adb7a559a656d3a81d1a447fccc7e804..2cc815c3238ffb075ceb45ed050496a5a2af91a5 100644 (file)
@@ -34,9 +34,9 @@ use Friendica\DI;
 use Friendica\Model\APContact;
 use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
-use Friendica\Model\GContact;
 use Friendica\Model\Item;
 use Friendica\Model\ItemURI;
+use Friendica\Model\Post;
 use Friendica\Model\Tag;
 use Friendica\Model\User;
 use Friendica\Network\Probe;
@@ -216,11 +216,11 @@ class OStatus
 
                        if (!empty($author["author-avatar"]) && ($author["author-avatar"] != $current['avatar'])) {
                                Logger::log("Update profile picture for contact ".$contact["id"], Logger::DEBUG);
-                               Contact::updateAvatar($author["author-avatar"], $importer["uid"], $contact["id"]);
+                               Contact::updateAvatar($contact["id"], $author["author-avatar"]);
                        }
 
                        // Ensure that we are having this contact (with uid=0)
-                       $cid = Contact::getIdForURL($aliaslink, 0, false);
+                       $cid = Contact::getIdForURL($aliaslink);
 
                        if ($cid) {
                                $fields = ['url', 'nurl', 'name', 'nick', 'alias', 'about', 'location'];
@@ -237,18 +237,9 @@ class OStatus
 
                                // Update the avatar
                                if (!empty($author["author-avatar"])) {
-                                       Contact::updateAvatar($author["author-avatar"], 0, $cid);
+                                       Contact::updateAvatar($cid, $author["author-avatar"]);
                                }
                        }
-
-                       $contact["generation"] = 2;
-                       $contact["hide"] = false; // OStatus contacts are never hidden
-                       if (!empty($author["author-avatar"])) {
-                               $contact["photo"] = $author["author-avatar"];
-                       }
-                       $gcid = GContact::update($contact);
-
-                       GContact::link($gcid, $contact["uid"], $contact["id"]);
                } elseif (empty($contact["network"]) || ($contact["network"] != Protocol::DFRN)) {
                        $contact = [];
                }
@@ -491,7 +482,7 @@ class OStatus
                                Logger::log("Favorite ".$orig_uri." ".print_r($item, true));
 
                                $item["verb"] = Activity::LIKE;
-                               $item["parent-uri"] = $orig_uri;
+                               $item["thr-parent"] = $orig_uri;
                                $item["gravity"] = GRAVITY_ACTIVITY;
                                $item["object-type"] = Activity\ObjectType::NOTE;
                        }
@@ -504,7 +495,7 @@ class OStatus
                        self::processPost($xpath, $entry, $item, $importer);
 
                        if ($initialize && (count(self::$itemlist) > 0)) {
-                               if (self::$itemlist[0]['uri'] == self::$itemlist[0]['parent-uri']) {
+                               if (self::$itemlist[0]['uri'] == self::$itemlist[0]['thr-parent']) {
                                        // We will import it everytime, when it is started by our contacts
                                        $valid = Contact::isSharingByURL(self::$itemlist[0]['author-link'], self::$itemlist[0]['uid']);
 
@@ -531,11 +522,7 @@ class OStatus
                                                }
                                        }
                                } else {
-                                       // But we will only import complete threads
-                                       $valid = Item::exists(['uid' => $importer["uid"], 'uri' => self::$itemlist[0]['parent-uri']]);
-                                       if ($valid) {
-                                               Logger::log("Item with uri ".self::$itemlist[0]["uri"]." belongs to parent ".self::$itemlist[0]['parent-uri']." of user ".$importer["uid"].". It will be imported.", Logger::DEBUG);
-                                       }
+                                       $valid = true;
                                }
 
                                if ($valid) {
@@ -554,15 +541,8 @@ class OStatus
                                                } elseif ($item['contact-id'] < 0) {
                                                        Logger::log("Item with uri ".$item["uri"]." is from a blocked contact.", Logger::DEBUG);
                                                } else {
-                                                       // We are having duplicated entries. Hopefully this solves it.
-                                                       if (DI::lock()->acquire('ostatus_process_item_insert')) {
-                                                               $ret = Item::insert($item);
-                                                               DI::lock()->release('ostatus_process_item_insert');
-                                                               Logger::log("Item with uri ".$item["uri"]." for user ".$importer["uid"].' stored. Return value: '.$ret);
-                                                       } else {
-                                                               $ret = Item::insert($item);
-                                                               Logger::log("We couldn't lock - but tried to store the item anyway. Return value is ".$ret);
-                                                       }
+                                                       $ret = Item::insert($item);
+                                                       Logger::log("Item with uri ".$item["uri"]." for user ".$importer["uid"].' stored. Return value: '.$ret);
                                                }
                                        }
                                }
@@ -637,7 +617,7 @@ class OStatus
                if (is_object($inreplyto->item(0))) {
                        foreach ($inreplyto->item(0)->attributes as $attributes) {
                                if ($attributes->name == "ref") {
-                                       $item["parent-uri"] = $attributes->textContent;
+                                       $item["thr-parent"] = $attributes->textContent;
                                }
                                if ($attributes->name == "href") {
                                        $related = $attributes->textContent;
@@ -718,16 +698,16 @@ class OStatus
                        self::fetchConversation($item['conversation-href'], $item['conversation-uri']);
                }
 
-               if (isset($item["parent-uri"])) {
-                       if (!Item::exists(['uid' => $importer["uid"], 'uri' => $item['parent-uri']])) {
+               if (isset($item["thr-parent"])) {
+                       if (!Item::exists(['uid' => $importer["uid"], 'uri' => $item['thr-parent']])) {
                                if ($related != '') {
-                                       self::fetchRelated($related, $item["parent-uri"], $importer);
+                                       self::fetchRelated($related, $item["thr-parent"], $importer);
                                }
                        } else {
                                Logger::log('Reply with URI '.$item["uri"].' already existed for user '.$importer["uid"].'.', Logger::DEBUG);
                        }
                } else {
-                       $item["parent-uri"] = $item["uri"];
+                       $item["thr-parent"] = $item["uri"];
                        $item["gravity"] = GRAVITY_PARENT;
                }
 
@@ -755,7 +735,7 @@ class OStatus
 
                self::$conv_list[$conversation] = true;
 
-               $curlResult = DI::httpRequest()->get($conversation, false, ['accept_content' => 'application/atom+xml, text/html']);
+               $curlResult = DI::httpRequest()->get($conversation, ['accept_content' => 'application/atom+xml, text/html']);
 
                if (!$curlResult->isSuccess()) {
                        return;
@@ -948,7 +928,7 @@ class OStatus
                }
 
                $stored = false;
-               $curlResult = DI::httpRequest()->get($related, false, ['accept_content' => 'application/atom+xml, text/html']);
+               $curlResult = DI::httpRequest()->get($related, ['accept_content' => 'application/atom+xml, text/html']);
 
                if (!$curlResult->isSuccess()) {
                        return;
@@ -1090,7 +1070,7 @@ class OStatus
                if (is_object($inreplyto->item(0))) {
                        foreach ($inreplyto->item(0)->attributes as $attributes) {
                                if ($attributes->name == "ref") {
-                                       $item["parent-uri"] = $attributes->textContent;
+                                       $item["thr-parent"] = $attributes->textContent;
                                }
                        }
                }
@@ -1135,21 +1115,15 @@ class OStatus
                                                if ($filetype == 'image') {
                                                        $link_data['add_body'] .= "\n[img]".$attribute['href'].'[/img]';
                                                } else {
-                                                       if (!empty($item["attach"])) {
-                                                               $item["attach"] .= ',';
-                                                       } else {
-                                                               $item["attach"] = '';
-                                                       }
-                                                       if (!isset($attribute['length'])) {
-                                                               $attribute['length'] = "0";
-                                                       }
-                                                       $item["attach"] .= '[attach]href="'.$attribute['href'].'" length="'.$attribute['length'].'" type="'.$attribute['type'].'" title="'.($attribute['title'] ?? '') .'"[/attach]';
+                                                       Post\Media::insert(['uri-id' => $item['uri-id'], 'type' => Post\Media::DOCUMENT,
+                                                               'url' => $attribute['href'], 'mimetype' => $attribute['type'],
+                                                               'size' => $attribute['length'] ?? null, 'description' => $attribute['title'] ?? null]);
                                                }
                                                break;
                                        case "related":
                                                if ($item["object-type"] != Activity\ObjectType::BOOKMARK) {
-                                                       if (!isset($item["parent-uri"])) {
-                                                               $item["parent-uri"] = $attribute['href'];
+                                                       if (!isset($item["thr-parent"])) {
+                                                               $item["thr-parent"] = $attribute['href'];
                                                        }
                                                        $link_data['related'] = $attribute['href'];
                                                } else {
@@ -1407,25 +1381,19 @@ class OStatus
                        }
                }
 
-               $arr = explode('[/attach],', $item['attach']);
-               if (count($arr)) {
-                       foreach ($arr as $r) {
-                               $matches = false;
-                               $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
-                               if ($cnt) {
-                                       $attributes = ["rel" => "enclosure",
-                                                       "href" => $matches[1],
-                                                       "type" => $matches[3]];
+               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
+                       $attributes = ['rel' => 'enclosure',
+                               'href' => $attachment['url'],
+                               'type' => $attachment['mimetype']];
 
-                                       if (intval($matches[2])) {
-                                               $attributes["length"] = intval($matches[2]);
-                                       }
-                                       if (trim($matches[4]) != "") {
-                                               $attributes["title"] = trim($matches[4]);
-                                       }
-                                       XML::addElement($doc, $root, "link", "", $attributes);
-                               }
+                       if (!empty($attachment['size'])) {
+                               $attributes['length'] = intval($attachment['size']);
+                       }
+                       if (!empty($attachment['description'])) {
+                               $attributes['title'] = $attachment['description'];
                        }
+
+                       XML::addElement($doc, $root, 'link', '', $attributes);
                }
        }
 
@@ -1962,9 +1930,8 @@ class OStatus
 
                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]);
+                       $thrparent = Item::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"];
@@ -1977,7 +1944,7 @@ class OStatus
                        }
 
                        $attributes = [
-                                       "ref" => $parent_item,
+                                       "ref" => $item['thr-parent'],
                                        "href" => $parent_plink];
                        XML::addElement($doc, $entry, "thr:in-reply-to", "", $attributes);
 
@@ -1988,7 +1955,7 @@ class OStatus
                }
 
                if (intval($item['parent']) > 0) {
-                       $conversation_href = $conversation_uri = str_replace('/objects/', '/context/', $item['parent-uri']);
+                       $conversation_href = $conversation_uri = str_replace('/objects/', '/context/', $item['thr-parent']);
 
                        if (isset($parent_item)) {
                                $conversation = DBA::selectFirst('conversation', ['conversation-uri', 'conversation-href'], ['item-uri' => $parent_item]);
@@ -2027,7 +1994,7 @@ class OStatus
                $mentioned = $newmentions;
 
                foreach ($mentioned as $mention) {
-                       $contact = Contact::getByURL($mention, ['contact-type']);
+                       $contact = Contact::getByURL($mention, false, ['contact-type']);
                        if (!empty($contact) && ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) {
                                XML::addElement($doc, $entry, "link", "",
                                        [
@@ -2139,7 +2106,7 @@ class OStatus
                }
 
                $check_date = DateTimeFormat::utc($last_update);
-               $authorid = Contact::getIdForURL($owner["url"], 0, false);
+               $authorid = Contact::getIdForURL($owner["url"]);
 
                $condition = ["`uid` = ? AND `received` > ? AND NOT `deleted`
                        AND `private` != ? AND `visible` AND `wall` AND `parent-network` IN (?, ?)",
@@ -2228,14 +2195,13 @@ class OStatus
         * 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)
+       public static function isSupportedByContactUrl($url)
        {
-               $probe = Probe::uri($url, Protocol::OSTATUS, 0, !$update);
+               $probe = Probe::uri($url, Protocol::OSTATUS);
                return $probe['network'] == Protocol::OSTATUS;
        }
 }