Merge pull request #9815 from annando/post-again
[friendica.git/.git] / mod / unfollow.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 use Friendica\App;
23 use Friendica\Core\Protocol;
24 use Friendica\Core\Renderer;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Profile;
29 use Friendica\Model\User;
30 use Friendica\Util\Strings;
31
32 function unfollow_post(App $a)
33 {
34         if (!local_user()) {
35                 notice(DI::l10n()->t('Permission denied.'));
36                 DI::baseUrl()->redirect('login');
37                 // NOTREACHED
38         }
39
40         $url = Strings::escapeTags(trim($_REQUEST['url'] ?? ''));
41
42         unfollow_process($url);
43 }
44
45 function unfollow_content(App $a)
46 {
47         $base_return_path = 'contact';
48
49         if (!local_user()) {
50                 notice(DI::l10n()->t('Permission denied.'));
51                 DI::baseUrl()->redirect('login');
52                 // NOTREACHED
53         }
54
55         $uid = local_user();
56         $url = Strings::escapeTags(trim($_REQUEST['url']));
57
58         $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
59                 local_user(), Contact::SHARING, Contact::FRIEND, Strings::normaliseLink($url),
60                 Strings::normaliseLink($url), $url];
61
62         $contact = DBA::selectFirst('contact', ['url', 'network', 'addr', 'name'], $condition);
63
64         if (!DBA::isResult($contact)) {
65                 notice(DI::l10n()->t("You aren't following this contact."));
66                 DI::baseUrl()->redirect($base_return_path);
67                 // NOTREACHED
68         }
69
70         if (!in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
71                 notice(DI::l10n()->t('Unfollowing is currently not supported by your network.'));
72                 DI::baseUrl()->redirect($base_return_path . '/' . $contact['id']);
73                 // NOTREACHED
74         }
75
76         $request = DI::baseUrl() . '/unfollow';
77         $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
78
79         $self = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
80
81         if (!DBA::isResult($self)) {
82                 notice(DI::l10n()->t('Permission denied.'));
83                 DI::baseUrl()->redirect($base_return_path);
84                 // NOTREACHED
85         }
86
87         // Makes the connection request for friendica contacts easier
88         $_SESSION['fastlane'] = $contact['url'];
89
90         if (!empty($_REQUEST['auto'])) {
91                 unfollow_process($contact['url']);
92         }
93
94         $o = Renderer::replaceMacros($tpl, [
95                 '$header'        => DI::l10n()->t('Disconnect/Unfollow'),
96                 '$page_desc'     => '',
97                 '$your_address'  => DI::l10n()->t('Your Identity Address:'),
98                 '$invite_desc'   => '',
99                 '$submit'        => DI::l10n()->t('Submit Request'),
100                 '$cancel'        => DI::l10n()->t('Cancel'),
101                 '$url'           => $contact['url'],
102                 '$zrl'           => Contact::magicLink($contact['url']),
103                 '$url_label'     => DI::l10n()->t('Profile URL'),
104                 '$myaddr'        => $self['url'],
105                 '$request'       => $request,
106                 '$keywords'      => '',
107                 '$keywords_label'=> ''
108         ]);
109
110         DI::page()['aside'] = '';
111         Profile::load($a, '', Contact::getByURL($contact['url'], false));
112
113         $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), ['$title' => DI::l10n()->t('Status Messages and Posts')]);
114
115         // Show last public posts
116         $o .= Contact::getPostsFromUrl($contact['url']);
117
118         return $o;
119 }
120
121 function unfollow_process(string $url)
122 {
123         $base_return_path = 'contact';
124
125         $uid = local_user();
126
127         $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
128                 $uid, Contact::SHARING, Contact::FRIEND, Strings::normaliseLink($url),
129                 Strings::normaliseLink($url), $url];
130         $contact = DBA::selectFirst('contact', [], $condition);
131
132         if (!DBA::isResult($contact)) {
133                 notice(DI::l10n()->t("You aren't following this contact."));
134                 DI::baseUrl()->redirect($base_return_path);
135                 // NOTREACHED
136         }
137
138         if (!in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
139                 notice(DI::l10n()->t('Unfollowing is currently not supported by your network.'));
140                 DI::baseUrl()->redirect($base_return_path . '/' . $contact['id']);
141                 // NOTREACHED
142         }
143
144         $dissolve = ($contact['rel'] == Contact::SHARING);
145
146         $owner = User::getOwnerDataById($uid);
147         if ($owner) {
148                 Contact::terminateFriendship($owner, $contact, $dissolve);
149         }
150
151         // Sharing-only contacts get deleted as there no relationship any more
152         if ($dissolve) {
153                 Contact::remove($contact['id']);
154                 $return_path = $base_return_path;
155         } else {
156                 DBA::update('contact', ['rel' => Contact::FOLLOWER], ['id' => $contact['id']]);
157                 $return_path = $base_return_path . '/' . $contact['id'];
158         }
159
160         DI::baseUrl()->redirect($return_path);
161 }