]> git.mxchange.org Git - friendica.git/blob - mod/unfollow.php
New function "Item::storeForUserByUriId"
[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         info(DI::l10n()->t('Contact unfollowed'));
83         DI::baseUrl()->redirect($return_path);
84         // NOTREACHED
85 }
86
87 function unfollow_content(App $a)
88 {
89         $base_return_path = 'contact';
90
91         if (!local_user()) {
92                 notice(DI::l10n()->t('Permission denied.'));
93                 DI::baseUrl()->redirect('login');
94                 // NOTREACHED
95         }
96
97         $uid = local_user();
98         $url = Strings::escapeTags(trim($_REQUEST['url']));
99
100         $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
101                 local_user(), Contact::SHARING, Contact::FRIEND, Strings::normaliseLink($url),
102                 Strings::normaliseLink($url), $url];
103
104         $contact = DBA::selectFirst('contact', ['url', 'network', 'addr', 'name'], $condition);
105
106         if (!DBA::isResult($contact)) {
107                 notice(DI::l10n()->t("You aren't following this contact."));
108                 DI::baseUrl()->redirect($base_return_path);
109                 // NOTREACHED
110         }
111
112         if (!in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
113                 notice(DI::l10n()->t('Unfollowing is currently not supported by your network.'));
114                 DI::baseUrl()->redirect($base_return_path . '/' . $contact['id']);
115                 // NOTREACHED
116         }
117
118         $request = DI::baseUrl() . '/unfollow';
119         $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
120
121         $self = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
122
123         if (!DBA::isResult($self)) {
124                 notice(DI::l10n()->t('Permission denied.'));
125                 DI::baseUrl()->redirect($base_return_path);
126                 // NOTREACHED
127         }
128
129         // Makes the connection request for friendica contacts easier
130         $_SESSION['fastlane'] = $contact['url'];
131
132         $o = Renderer::replaceMacros($tpl, [
133                 '$header'        => DI::l10n()->t('Disconnect/Unfollow'),
134                 '$page_desc'     => '',
135                 '$your_address'  => DI::l10n()->t('Your Identity Address:'),
136                 '$invite_desc'   => '',
137                 '$submit'        => DI::l10n()->t('Submit Request'),
138                 '$cancel'        => DI::l10n()->t('Cancel'),
139                 '$url'           => $contact['url'],
140                 '$zrl'           => Contact::magicLink($contact['url']),
141                 '$url_label'     => DI::l10n()->t('Profile URL'),
142                 '$myaddr'        => $self['url'],
143                 '$request'       => $request,
144                 '$keywords'      => '',
145                 '$keywords_label'=> ''
146         ]);
147
148         DI::page()['aside'] = '';
149         Profile::load($a, '', Contact::getByURL($contact['url'], false));
150
151         $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), ['$title' => DI::l10n()->t('Status Messages and Posts')]);
152
153         // Show last public posts
154         $o .= Contact::getPostsFromUrl($contact['url']);
155
156         return $o;
157 }