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