]> git.mxchange.org Git - friendica.git/blobdiff - src/Console/Contact.php
Merge remote-tracking branch 'upstream/2021.12-rc' into user-banner
[friendica.git] / src / Console / Contact.php
index 5506881ae8054653a72096a8a6c7112d5c036078..11f7f87ced01f35045b1803d7061d826e34981e8 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -23,9 +23,10 @@ namespace Friendica\Console;
 
 use Console_Table;
 use Friendica\App;
-use Friendica\Content\Pager;
+use Friendica\DI;
 use Friendica\Model\Contact as ContactModel;
 use Friendica\Model\User as UserModel;
+use Friendica\Network\Probe;
 use Friendica\Util\Temporal;
 use RuntimeException;
 use Seld\CliPrompt\CliPrompt;
@@ -154,33 +155,35 @@ HELP;
                        }
                }
 
+               $url = Probe::cleanURI($url);
+
                $contact = ContactModel::getByURLForUser($url, $user['uid']);
                if (!empty($contact)) {
                        throw new RuntimeException('Contact already exists');
                }
 
                $network = $this->getArgument(3);
-               if (empty($network) && $network !== '') {
+               if ($network === null) {
                        $this->out('Enter network, or leave blank: ');
                        $network = CliPrompt::prompt();
                }
 
-               $result = ContactModel::createFromProbe($user, $url, false, $network);
+               $result = ContactModel::createFromProbeForUser($user['uid'], $url, $network);
 
                if ($result['success']) {
                        $this->out('User ' . $user['nickname'] . ' now connected to ' . $url . ', contact ID ' . $result['cid']);
-               }
-               else {
+               } else {
                        throw new RuntimeException($result['message']);
                }
        }
 
        /**
-        * Sends an unfriend message.  Does not remove the contact
+        * Sends an unfriend message.
         *
         * @return bool True, if the command was successful
+        * @throws \Exception
         */
-       private function terminateContact()
+       private function terminateContact(): bool
        {
                $cid = $this->getArgument(1);
                if (empty($cid)) {
@@ -198,13 +201,23 @@ HELP;
 
                $user = UserModel::getById($contact['uid']);
 
-               $result = ContactModel::terminateFriendship($user, $contact);
+               try {
+                       $result = ContactModel::terminateFriendship($user, $contact);
+                       if ($result === false) {
+                               throw new RuntimeException('Unable to unfollow this contact, please retry in a few minutes or check the logs.');
+                       }
+
+                       $this->out('Contact was successfully unfollowed');
+
+                       return true;
+               } catch (\Exception $e) {
+                       DI::logger()->error($e->getMessage(), ['owner' => $user, 'contact' => $contact]);
+                       throw new RuntimeException('Unable to unfollow this contact, please check the log');
+               }
        }
 
        /**
         * Marks a contact for removal
-        *
-        * @return bool True, if the command was successful
         */
        private function removeContact()
        {
@@ -217,7 +230,7 @@ HELP;
                        }
                }
 
-               $result = ContactModel::remove($cid);
+               ContactModel::remove($cid);
        }
 
        /**
@@ -246,7 +259,7 @@ HELP;
                $table = new Console_Table();
                $table->setHeaders(['ID', 'UID', 'Network', 'Name', 'Nick', 'URL', 'E-Mail', 'Created', 'Updated', 'Blocked', 'Deleted']);
 
-               $addRow = function($row) use (&$table) {
+               $addRow = function ($row) use (&$table) {
                        $table->addRow([
                                $row['id'],
                                $row['uid'],
@@ -263,15 +276,15 @@ HELP;
                };
                switch ($subCmd) {
                        case 'id':
-                               $cid = $this->getArgument(2);
+                               $cid     = $this->getArgument(2);
                                $contact = ContactModel::getById($cid, $fields);
                                if (!empty($contact)) {
                                        $addRow($contact);
                                }
                                break;
                        case 'url':
-                               $user = $this->getUserByNick(2);
-                               $url = $this->getArgument(3);
+                               $user    = $this->getUserByNick(2);
+                               $url     = $this->getArgument(3);
                                $contact = ContactModel::getByURLForUser($url, $user['uid'], false, $fields);
                                if (!empty($contact)) {
                                        $addRow($contact);