]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Merge remote-tracking branch 'upstream/develop' into user-contact
[friendica.git] / src / Protocol / Diaspora.php
index 00da7d3915d2dd68d0f7c6013af0472c5f4b3c8e..b08fa23c2b01c8187bb6e1104919e1a986a8b728 100644 (file)
@@ -1077,7 +1077,7 @@ class Diaspora
 
                Logger::log("Fetch post from ".$source_url, Logger::DEBUG);
 
-               $envelope = DI::httpRequest()->fetch($source_url);
+               $envelope = DI::httpClient()->fetch($source_url);
                if ($envelope) {
                        Logger::log("Envelope was fetched.", Logger::DEBUG);
                        $x = self::verifyMagicEnvelope($envelope);
@@ -1367,7 +1367,7 @@ class Diaspora
                                'notify' => $data['notify'], 'poll' => $data['poll'],
                                'network' => $data['network']];
 
-               DBA::update('contact', $fields, ['addr' => $old_handle]);
+               Contact::update($fields, ['addr' => $old_handle]);
 
                Logger::log('Contacts are updated.');
 
@@ -2129,7 +2129,7 @@ class Diaspora
                        $fields['bd'] = $birthday;
                }
 
-               DBA::update('contact', $fields, ['id' => $contact['id']]);
+               Contact::update($fields, ['id' => $contact['id']]);
 
                Logger::log("Profile of contact ".$contact["id"]." stored for user ".$importer["uid"], Logger::DEBUG);
 
@@ -2211,7 +2211,7 @@ class Diaspora
                                return true;
                        } else {
                                Logger::log("Author ".$author." doesn't want to follow us anymore.", Logger::DEBUG);
-                               Contact::removeFollower($importer, $contact);
+                               Contact::removeFollower($contact);
                                return true;
                        }
                }
@@ -3022,7 +3022,7 @@ class Diaspora
                if (!intval(DI::config()->get("system", "diaspora_test"))) {
                        $content_type = (($public_batch) ? "application/magic-envelope+xml" : "application/json");
 
-                       $postResult = DI::httpRequest()->post($dest_url . "/", $envelope, ["Content-Type: " . $content_type]);
+                       $postResult = DI::httpClient()->post($dest_url . "/", $envelope, ['Content-Type' => $content_type]);
                        $return_code = $postResult->getReturnCode();
                } else {
                        Logger::log("test_mode");
@@ -3068,9 +3068,6 @@ class Diaspora
        {
                $msg = self::buildPostXml($type, $message);
 
-               Logger::log('message: '.$msg, Logger::DATA);
-               Logger::log('send guid '.$guid, Logger::DEBUG);
-
                // Fallback if the private key wasn't transmitted in the expected field
                if (empty($owner['uprvkey'])) {
                        $owner['uprvkey'] = $owner['prvkey'];
@@ -3085,13 +3082,18 @@ class Diaspora
                        if (!empty($fcontact)) {
                                $pubkey = $fcontact['pubkey'];
                        }
+               } else {
+                       // The "addr" field should always be filled.
+                       // If this isn't the case, it will raise a notice some lines later.
+                       // And in the log we will see where it came from and we can handle it there.
+                       Logger::notice('Empty addr', ['contact' => $contact ?? [], 'callstack' => System::callstack(20)]);
                }
 
                $envelope = self::buildMessage($msg, $owner, $contact, $owner['uprvkey'], $pubkey, $public_batch);
 
                $return_code = self::transmit($owner, $contact, $envelope, $public_batch, $guid);
 
-               Logger::log("guid: ".$guid." result ".$return_code, Logger::DEBUG);
+               Logger::info('Transmitted message', ['owner' => $owner['uid'], 'target' => $contact['addr'], 'type' => $type, 'guid' => $guid, 'result' => $return_code]);
 
                return $return_code;
        }
@@ -3119,7 +3121,19 @@ class Diaspora
                        return;
                }
 
-               $owner = User::getOwnerDataById($item['uid']);
+               // Fetch some user id to have a valid handle to transmit the participation.
+               // In fact it doesn't matter which user sends this - but it is needed by the protocol.
+               // If the item belongs to a user, we take this user id.
+               if ($item['uid'] == 0) {
+                       // @todo Possibly use an administrator account?
+                       $condition = ['verified' => true, 'blocked' => false,
+                               'account_removed' => false, 'account_expired' => false, 'account-type' => User::ACCOUNT_TYPE_PERSON];
+                       $first_user = DBA::selectFirst('user', ['uid'], $condition, ['order' => ['uid']]);
+                       $owner = User::getOwnerDataById($first_user['uid']);
+               } else {
+                       $owner = User::getOwnerDataById($item['uid']);
+               }
+
                $author = self::myHandle($owner);
 
                $message = ["author" => $author,
@@ -3919,7 +3933,7 @@ class Diaspora
                        $dob = '';
 
                        if ($profile['dob'] && ($profile['dob'] > '0000-00-00')) {
-                               list($year, $month, $day) = sscanf($profile['dob'], '%4d-%2d-%2d');
+                               [$year, $month, $day] = sscanf($profile['dob'], '%4d-%2d-%2d');
                                if ($year < 1004) {
                                        $year = 1004;
                                }
@@ -3979,13 +3993,7 @@ class Diaspora
                }
 
                if (!$recips) {
-                       $recips = q(
-                               "SELECT `id`,`name`,`network`,`pubkey`,`notify` FROM `contact` WHERE `network` = '%s'
-                               AND `uid` = %d AND `rel` != %d",
-                               DBA::escape(Protocol::DIASPORA),
-                               intval($uid),
-                               intval(Contact::SHARING)
-                       );
+                       $recips = DBA::selectToArray('contact', [], ['network' => Protocol::DIASPORA, 'uid' => $uid, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]]);
                }
 
                if (!$recips) {
@@ -4035,27 +4043,41 @@ class Diaspora
        /**
         * Creates the signature for Comments that are created on our system
         *
-        * @param integer $uid  The user of that comment
         * @param array   $item Item array
         *
         * @return array Signed content
         * @throws \Exception
         */
-       public static function createCommentSignature($uid, array $item)
+       public static function createCommentSignature(array $item)
        {
+               if (!empty($item['author-link'])) {
+                       $url = $item['author-link'];
+               } else {
+                       $contact = Contact::getById($item['author-id'], ['url']);
+                       if (empty($contact['url'])) {
+                               Logger::warning('Author Contact not found', ['author-id' => $item['author-id']]);
+                               return false;
+                       }
+                       $url = $contact['url'];
+               }
+
+               $uid = User::getIdForURL($url);
+               if (empty($uid)) {
+                       Logger::info('No owner post, so not storing signature', ['url' => $contact['url']]);
+                       return false;
+               }
+
                $owner = User::getOwnerDataById($uid);
                if (empty($owner)) {
                        Logger::info('No owner post, so not storing signature');
                        return false;
                }
 
-               $parent = Post::selectFirst(['parent-uri'], ['uri' => $item['thr-parent']]);
-               if (!DBA::isResult($parent)) {
-                       return;
+               // This is only needed for the automated tests
+               if (empty($owner['uprvkey'])) {
+                       return false;
                }
 
-               $item['parent-uri'] = $parent['parent-uri'];
-
                $message = self::constructComment($item, $owner);
                if ($message === false) {
                        return false;