]> git.mxchange.org Git - friendica.git/blob - src/Console/Contact.php
Merge pull request #10575 from MrPetovan/bug/10019-peertube-embed
[friendica.git] / src / Console / Contact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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 Console_Table;
25 use Friendica\App;
26 use Friendica\Model\Contact as ContactModel;
27 use Friendica\Model\User as UserModel;
28 use Friendica\Util\Temporal;
29 use RuntimeException;
30 use Seld\CliPrompt\CliPrompt;
31
32 /**
33  * tool to manage contacts of users of the current node
34  */
35 class Contact extends \Asika\SimpleConsole\Console
36 {
37         protected $helpOptions = ['h', 'help', '?'];
38
39         /**
40          * @var App\Mode
41          */
42         private $appMode;
43         /**
44          * @var IPConfig
45          */
46         private $pConfig;
47
48         protected function getHelp()
49         {
50                 $help = <<<HELP
51 console contact - Modify contact settings per console commands.
52 Usage
53         bin/console contact add <user nick> <URL> [<network>] [-h|--help|-?] [-v]
54         bin/console contact remove <CID> [-h|--help|-?] [-v]
55         bin/console contact search id <CID> [-h|--help|-?] [-v]
56         bin/console contact search url <user nick> <URL> [-h|--help|-?] [-v]
57         bin/console contact terminate <CID> [-h|--help|-?] [-v]
58
59 Description
60         Modify contact settings per console commands.
61
62 Options
63     -h|--help|-? Show help information
64     -v           Show more debug information
65     -y           Non-interactive mode, assume "yes" as answer to the user deletion prompt
66 HELP;
67                 return $help;
68         }
69
70         public function __construct(App\Mode $appMode, array $argv = null)
71         {
72                 parent::__construct($argv);
73
74                 $this->appMode = $appMode;
75         }
76
77         protected function doExecute()
78         {
79                 if ($this->getOption('v')) {
80                         $this->out('Class: ' . __CLASS__);
81                         $this->out('Arguments: ' . var_export($this->args, true));
82                         $this->out('Options: ' . var_export($this->options, true));
83                 }
84
85                 if (count($this->args) == 0) {
86                         $this->out($this->getHelp());
87                         return 0;
88                 }
89
90                 if ($this->appMode->isInstall()) {
91                         throw new RuntimeException('Database isn\'t ready or populated yet');
92                 }
93
94                 $command = $this->getArgument(0);
95
96                 switch ($command) {
97                         case 'add':
98                                 return $this->addContact();
99                         case 'remove':
100                                 return $this->removeContact();
101                         case 'search':
102                                 return $this->searchContact();
103                         case 'terminate':
104                                 return $this->terminateContact();
105                         default:
106                                 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
107                 }
108         }
109
110         /**
111          * Retrieves the user from a nick supplied as an argument or from a prompt
112          *
113          * @param int $arg_index Index of the nick argument in the arguments list
114          *
115          * @return array|boolean User record with uid field, or false if user is not found
116          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
117          */
118         private function getUserByNick($arg_index)
119         {
120                 $nick = $this->getArgument($arg_index);
121
122                 if (empty($nick)) {
123                         $this->out('Enter user nickname: ');
124                         $nick = CliPrompt::prompt();
125                         if (empty($nick)) {
126                                 throw new RuntimeException('A user nickname must be specified.');
127                         }
128                 }
129
130                 $user = UserModel::getByNickname($nick, ['uid', 'nickname']);
131                 if (empty($user)) {
132                         throw new RuntimeException('User not found');
133                 }
134
135                 return $user;
136         }
137
138         /**
139          * Adds a contact to a user from a URL
140          *
141          * @return bool True, if the command was successful
142          */
143         private function addContact()
144         {
145                 $user = $this->getUserByNick(1);
146
147                 $url = $this->getArgument(2);
148                 if (empty($url)) {
149                         $this->out('Enter contact URL: ');
150                         $url = CliPrompt::prompt();
151                         if (empty($url)) {
152                                 throw new RuntimeException('A contact URL must be specified.');
153                         }
154                 }
155
156                 $contact = ContactModel::getByURLForUser($url, $user['uid']);
157                 if (!empty($contact)) {
158                         throw new RuntimeException('Contact already exists');
159                 }
160
161                 $network = $this->getArgument(3);
162                 if ($network === null) {
163                         $this->out('Enter network, or leave blank: ');
164                         $network = CliPrompt::prompt();
165                 }
166
167                 $result = ContactModel::createFromProbe($user, $url, false, $network);
168
169                 if ($result['success']) {
170                         $this->out('User ' . $user['nickname'] . ' now connected to ' . $url . ', contact ID ' . $result['cid']);
171                 } else {
172                         throw new RuntimeException($result['message']);
173                 }
174         }
175
176         /**
177          * Sends an unfriend message.  Does not remove the contact
178          *
179          * @return bool True, if the command was successful
180          */
181         private function terminateContact()
182         {
183                 $cid = $this->getArgument(1);
184                 if (empty($cid)) {
185                         $this->out('Enter contact ID: ');
186                         $cid = CliPrompt::prompt();
187                         if (empty($cid)) {
188                                 throw new RuntimeException('A contact ID must be specified.');
189                         }
190                 }
191
192                 $contact = ContactModel::getById($cid);
193                 if (empty($contact)) {
194                         throw new RuntimeException('Contact not found');
195                 }
196
197                 $user = UserModel::getById($contact['uid']);
198
199                 $result = ContactModel::terminateFriendship($user, $contact);
200         }
201
202         /**
203          * Marks a contact for removal
204          *
205          * @return bool True, if the command was successful
206          */
207         private function removeContact()
208         {
209                 $cid = $this->getArgument(1);
210                 if (empty($cid)) {
211                         $this->out('Enter contact ID: ');
212                         $cid = CliPrompt::prompt();
213                         if (empty($cid)) {
214                                 throw new RuntimeException('A contact ID must be specified.');
215                         }
216                 }
217
218                 $result = ContactModel::remove($cid);
219         }
220
221         /**
222          * Returns a contact based on search parameter
223          *
224          * @return bool True, if the command was successful
225          */
226         private function searchContact()
227         {
228                 $fields = [
229                         'id',
230                         'uid',
231                         'network',
232                         'name',
233                         'nick',
234                         'url',
235                         'addr',
236                         'created',
237                         'updated',
238                         'blocked',
239                         'deleted',
240                 ];
241
242                 $subCmd = $this->getArgument(1);
243
244                 $table = new Console_Table();
245                 $table->setHeaders(['ID', 'UID', 'Network', 'Name', 'Nick', 'URL', 'E-Mail', 'Created', 'Updated', 'Blocked', 'Deleted']);
246
247                 $addRow = function ($row) use (&$table) {
248                         $table->addRow([
249                                 $row['id'],
250                                 $row['uid'],
251                                 $row['network'],
252                                 $row['name'],
253                                 $row['nick'],
254                                 $row['url'],
255                                 $row['addr'],
256                                 Temporal::getRelativeDate($row['created']),
257                                 Temporal::getRelativeDate($row['updated']),
258                                 $row['blocked'],
259                                 $row['deleted'],
260                         ]);
261                 };
262                 switch ($subCmd) {
263                         case 'id':
264                                 $cid     = $this->getArgument(2);
265                                 $contact = ContactModel::getById($cid, $fields);
266                                 if (!empty($contact)) {
267                                         $addRow($contact);
268                                 }
269                                 break;
270                         case 'url':
271                                 $user    = $this->getUserByNick(2);
272                                 $url     = $this->getArgument(3);
273                                 $contact = ContactModel::getByURLForUser($url, $user['uid'], false, $fields);
274                                 if (!empty($contact)) {
275                                         $addRow($contact);
276                                 }
277                                 break;
278                         default:
279                                 $this->out($this->getHelp());
280                                 return false;
281                 }
282
283                 $this->out($table->getTable());
284
285                 return true;
286         }
287 }