]> git.mxchange.org Git - friendica.git/blob - src/Console/GlobalCommunityBlock.php
Merge pull request #11524 from annando/cache-endpoints
[friendica.git] / src / Console / GlobalCommunityBlock.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 use Friendica\App;
25 use Friendica\Core\L10n;
26 use Friendica\Model\Contact;
27
28 /**
29  * tool to block an account from the node
30  *
31  * With this tool, you can block an account in such a way, that no postings
32  * or comments this account writes are accepted to the node.
33  */
34 class GlobalCommunityBlock extends \Asika\SimpleConsole\Console
35 {
36         protected $helpOptions = ['h', 'help', '?'];
37
38         /**
39          * @var App\Mode
40          */
41         private $appMode;
42         /**
43          * @var \Friendica\Core\L10n
44          */
45         private $l10n;
46
47         protected function getHelp()
48         {
49                 $help = <<<HELP
50 console globalcommunityblock - Block remote profile from interacting with this node
51 Usage
52         bin/console globalcommunityblock <profile_url> [<reason>] [-h|--help|-?] [-v]
53
54 Description
55         Blocks an account in such a way that no postings or comments this account writes are accepted to this node.
56         You can provide a optional reason for the block.
57
58 Options
59     -h|--help|-? Show help information
60     -v           Show more debug information.
61 HELP;
62                 return $help;
63         }
64
65         public function __construct(App\Mode $appMode, L10n $l10n, $argv = null)
66         {
67                 parent::__construct($argv);
68
69                 $this->appMode = $appMode;
70                 $this->l10n = $l10n;
71         }
72
73         protected function doExecute()
74         {
75                 if ($this->getOption('v')) {
76                         $this->out('Class: ' . __CLASS__);
77                         $this->out('Arguments: ' . var_export($this->args, true));
78                         $this->out('Options: ' . var_export($this->options, true));
79                 }
80
81                 if (count($this->args) == 0) {
82                         $this->out($this->getHelp());
83                         return 0;
84                 }
85
86                 if (count($this->args) > 2) {
87                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
88                 }
89
90                 if ($this->appMode->isInstall()) {
91                         throw new \RuntimeException('Database isn\'t ready or populated yet');
92                 }
93
94                 $contact_id = Contact::getIdForURL($this->getArgument(0));
95                 if (!$contact_id) {
96                         throw new \RuntimeException($this->l10n->t('Could not find any contact entry for this URL (%s)', $this->getArgument(0)));
97                 }
98
99                 $block_reason = $this->getArgument(1);
100                 if(Contact::block($contact_id, $block_reason)) {
101                         $this->out($this->l10n->t('The contact has been blocked from the node'));
102                 } else {
103                         throw new \RuntimeException('The contact block failed.');
104                 }
105
106                 return 0;
107         }
108 }