]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Revoke.php
Add revoke follow feature
[friendica.git] / src / Module / Contact / Revoke.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\Module\Contact;
23
24 use Friendica\BaseModule;
25 use Friendica\Content\Nav;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Renderer;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model;
31 use Friendica\Module\Contact;
32 use Friendica\Module\Security\Login;
33 use Friendica\Network\HTTPException;
34
35 class Revoke extends BaseModule
36 {
37         /** @var array */
38         private static $contact;
39
40         public static function init(array $parameters = [])
41         {
42                 if (!local_user()) {
43                         return;
44                 }
45
46                 $data = Model\Contact::getPublicAndUserContactID($parameters['id'], local_user());
47                 if (!DBA::isResult($data)) {
48                         throw new HTTPException\NotFoundException(DI::l10n()->t('Unknown contact.'));
49                 }
50
51                 if (empty($data['user'])) {
52                         throw new HTTPException\ForbiddenException();
53                 }
54
55                 self::$contact = Model\Contact::getById($data['user']);
56
57                 if (self::$contact['deleted']) {
58                         throw new HTTPException\NotFoundException(DI::l10n()->t('Contact is deleted.'));
59                 }
60
61                 if (!empty(self::$contact['network']) && self::$contact['network'] == Protocol::PHANTOM) {
62                         throw new HTTPException\NotFoundException(DI::l10n()->t('Contact is being deleted.'));
63                 }
64         }
65
66         public static function post(array $parameters = [])
67         {
68                 if (!local_user()) {
69                         throw new HTTPException\UnauthorizedException();
70                 }
71
72                 self::checkFormSecurityTokenRedirectOnError('contact/' . $parameters['id'], 'contact_revoke');
73
74                 $result = Model\Contact::revokeFollow(self::$contact);
75                 if ($result === true) {
76                         notice(DI::l10n()->t('Follow was successfully revoked.'));
77                 } elseif ($result === null) {
78                         notice(DI::l10n()->t('Follow was successfully revoked, however the remote contact won\'t be aware of this revokation.'));
79                 } else {
80                         notice(DI::l10n()->t('Unable to revoke follow, please try again later or contact the administrator.'));
81                 }
82
83                 DI::baseUrl()->redirect('contact/' . $parameters['id']);
84         }
85
86         public static function content(array $parameters = []): string
87         {
88                 if (!local_user()) {
89                         return Login::form($_SERVER['REQUEST_URI']);
90                 }
91
92                 Nav::setSelected('contact');
93
94                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_drop_confirm.tpl'), [
95                         '$l10n' => [
96                                 'header'  => DI::l10n()->t('Revoke Follow'),
97                                 'message' => DI::l10n()->t('Do you really want to revoke this contact\'s follow? This cannot be undone and they will have to manually follow you back again.'),
98                                 'confirm' => DI::l10n()->t('Yes'),
99                                 'cancel'  => DI::l10n()->t('Cancel'),
100                         ],
101                         '$contact'       => Contact::getContactTemplateVars(self::$contact),
102                         '$method'        => 'post',
103                         '$confirm_url'   => DI::args()->getCommand(),
104                         '$confirm_name'  => 'form_security_token',
105                         '$confirm_value' => BaseModule::getFormSecurityToken('contact_revoke'),
106                 ]);
107         }
108 }