]> git.mxchange.org Git - friendica.git/blobdiff - src/Console/Relay.php
spelling: unable
[friendica.git] / src / Console / Relay.php
index e1da56dba7431e6cef5e278e7be741621532cd2b..52682241e69c63941ec84610a70445fad4287b7b 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 namespace Friendica\Console;
 
 use Asika\SimpleConsole\CommandArgsException;
-use Friendica\App;
-use Friendica\Database\DBA;
 use Friendica\Model\APContact;
 use Friendica\Model\Contact;
 use Friendica\Protocol\ActivityPub\Transmitter;
 
 /**
- * tool to access the system config from the CLI
+ * tool to control the list of ActivityPub relay servers from the CLI
  *
- * With this script you can access the system configuration of your node from
- * the CLI. You can do both, reading current values stored in the database and
- * set new values to config variables.
- *
- * Usage:
- *   If you specify no parameters at the CLI, the script will list all config
- *   variables defined.
- *
- *   If you specify one parameter, the script will list all config variables
- *   defined in this section of the configuration (e.g. "system").
- *
- *   If you specify two parameters, the script will show you the current value
- *   of the named configuration setting. (e.g. "system loglevel")
- *
- *   If you specify three parameters, the named configuration setting will be
- *   set to the value of the last parameter. (e.g. "system loglevel 0" will
- *   disable logging)
+ * With this script you can access the relay servers of your node from
+ * the CLI.
  */
 class Relay extends \Asika\SimpleConsole\Console
 {
        protected $helpOptions = ['h', 'help', '?'];
 
        /**
-        * @var App\Mode
+        * @var $dba Friendica\Database\Database
         */
-       private $appMode;
+       private $dba;
+
 
        protected function getHelp()
        {
                $help = <<<HELP
 console relay - Manage ActivityPub relay configuration
 Synopsis
-       bin/console relay [-h|--help|-?] [-v]
+       bin/console relay list [-h|--help|-?] [-v]
        bin/console relay add <actor> [-h|--help|-?] [-v]
-       bin/console relay remove <actoor> [-h|--help|-?] [-v]
+       bin/console relay remove <actor> [-f|--force] [-h|--help|-?] [-v]
 
 Description
-       bin/console relay
-               Lists all active relais
+       bin/console relay list
+               Lists all active relay servers
 
        bin/console relay add <actor>
-               Add a relay actor
+               Add a relay actor in the format https://relayserver.tld/actor
 
-       bin/console relay remove <actoor>
-               Remove a relay actor
+       bin/console relay remove <actor>
+               Remove a relay actor in the format https://relayserver.tld/actor
 
 Options
+    -f|--force   Change the relay status in the system even if the unsubscribe message failed
     -h|--help|-? Show help information
     -v           Show more debug information.
 HELP;
                return $help;
        }
 
-       public function __construct(App\Mode $appMode, array $argv = null)
+       public function __construct(\Friendica\Database\Database $dba, array $argv = null)
        {
                parent::__construct($argv);
 
-               $this->appMode = $appMode;
+               $this->dba = $dba;
        }
 
-       protected function doExecute()
+       protected function doExecute(): int
        {
                if ($this->getOption('v')) {
                        $this->out('Executable: ' . $this->executable);
@@ -104,18 +89,18 @@ HELP;
                        throw new CommandArgsException('Too many arguments');
                }
 
-               if (count($this->args) == 1) {
-                       throw new CommandArgsException('Too few arguments');
-               }
-
-               if (count($this->args) == 0) {
-                       $contacts = DBA::select('apcontact', ['url'],
-                       ["`type` = ? AND `url` IN (SELECT `url` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))",
-                               'Application', 0, Contact::FOLLOWER, Contact::FRIEND]);
-                       while ($contact = DBA::fetch($contacts)) {
+               if ((count($this->args) == 1) && ($this->getArgument(0) == 'list')) {
+                       $contacts = $this->dba->select('apcontact', ['url'],
+                       ["`type` IN (?, ?) AND `url` IN (SELECT `url` FROM `contact` WHERE `uid` = ? AND `rel` = ?)",
+                               'Application', 'Service', 0, Contact::FRIEND]);
+                       while ($contact = $this->dba->fetch($contacts)) {
                                $this->out($contact['url']);
                        }
-                       DBA::close($contacts);
+                       $this->dba->close($contacts);
+               } elseif (count($this->args) == 0) {
+                       throw new CommandArgsException('too few arguments');
+               } elseif (count($this->args) == 1) {
+                       throw new CommandArgsException($this->getArgument(0) . ' is no valid command');
                }
 
                if (count($this->args) == 2) {
@@ -123,7 +108,7 @@ HELP;
                        $actor = $this->getArgument(1);
 
                        $apcontact = APContact::getByURL($actor);
-                       if (empty($apcontact) || ($apcontact['type'] != 'Application')) {
+                       if (empty($apcontact) || !in_array($apcontact['type'], ['Application', 'Service'])) {
                                $this->out($actor . ' is no relay actor');
                                return 1;
                        }
@@ -135,10 +120,14 @@ HELP;
                                        $this->out($actor . " couldn't be added");
                                }
                        } elseif ($mode == 'remove') {
-                               if (Transmitter::sendRelayUndoFollow($actor)) {
+                               $force = $this->getOption(['f', 'force'], false);
+
+                               if (Transmitter::sendRelayUndoFollow($actor, $force)) {
                                        $this->out('Successfully removed ' . $actor);
-                               } else {
+                               } elseif (!$force) {
                                        $this->out($actor . " couldn't be removed");
+                               } else {
+                                       $this->out($actor . " is forcefully removed");
                                }
                        } else {
                                throw new CommandArgsException($mode . ' is no valid command');