]> git.mxchange.org Git - friendica.git/blob - mod/unfollow.php
Fix unfollow for sharing-only contacts
[friendica.git] / mod / unfollow.php
1 <?php
2 /**
3  * @file mod/unfollow.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\L10n;
8 use Friendica\Core\Protocol;
9 use Friendica\Core\System;
10 use Friendica\Database\DBA;
11 use Friendica\Model\Contact;
12 use Friendica\Model\Profile;
13
14 function unfollow_post(App $a)
15 {
16         if (!local_user()) {
17                 notice(L10n::t('Permission denied.') . EOL);
18                 goaway($_SESSION['return_url']);
19                 // NOTREACHED
20         }
21
22         if ($_REQUEST['cancel']) {
23                 goaway($_SESSION['return_url']);
24         }
25
26         $uid = local_user();
27         $url = notags(trim($_REQUEST['url']));
28         $return_url = $_SESSION['return_url'];
29
30         $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?) AND `network` != ?",
31                         $uid, Contact::SHARING, Contact::FRIEND, normalise_link($url),
32                         normalise_link($url), $url, Protocol::STATUSNET];
33         $contact = DBA::selectFirst('contact', [], $condition);
34
35         if (!DBA::isResult($contact)) {
36                 notice(L10n::t("Contact wasn't found or can't be unfollowed."));
37                 goaway($return_url);
38         }
39
40         if (in_array($contact['network'], [Protocol::OSTATUS, Protocol::DIASPORA, Protocol::DFRN])) {
41                 $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
42                         WHERE `user`.`uid` = %d AND `contact`.`self` LIMIT 1",
43                         intval($uid)
44                 );
45                 if (DBA::isResult($r)) {
46                         Contact::terminateFriendship($r[0], $contact);
47                 }
48         }
49
50         // Sharing-only contacts get deleted as there no relationship any more
51         if ($contact['rel'] == Contact::SHARING) {
52                 Contact::remove($contact['id']);
53                 $return_path = 'contacts';
54         } else {
55                 DBA::update('contact', ['rel' => Contact::FOLLOWER], ['id' => $contact['id']]);
56                 $return_path = 'contacts/' . $contact['id'];
57         }
58
59         info(L10n::t('Contact unfollowed'));
60         goaway($return_path);
61         // NOTREACHED
62 }
63
64 function unfollow_content(App $a)
65 {
66         if (! local_user()) {
67                 notice(L10n::t('Permission denied.') . EOL);
68                 goaway($_SESSION['return_url']);
69                 // NOTREACHED
70         }
71
72         $uid = local_user();
73         $url = notags(trim($_REQUEST['url']));
74
75         $submit = L10n::t('Submit Request');
76
77         $condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?) AND `network` != ?",
78                         local_user(), Contact::SHARING, Contact::FRIEND, normalise_link($url),
79                         normalise_link($url), $url, Protocol::STATUSNET];
80
81         $contact = DBA::selectFirst('contact', ['url', 'network', 'addr', 'name'], $condition);
82
83         if (!DBA::isResult($contact)) {
84                 notice(L10n::t("You aren't a friend of this contact.").EOL);
85                 $submit = "";
86                 // NOTREACHED
87         }
88
89         if (!in_array($contact['network'], [Protocol::DIASPORA, Protocol::OSTATUS, Protocol::DFRN])) {
90                 notice(L10n::t("Unfollowing is currently not supported by your network.").EOL);
91                 $submit = "";
92                 // NOTREACHED
93         }
94
95         $request = System::baseUrl()."/unfollow";
96         $tpl = get_markup_template('auto_request.tpl');
97
98         $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1", intval($uid));
99
100         if (!$r) {
101                 notice(L10n::t('Permission denied.') . EOL);
102                 goaway($_SESSION['return_url']);
103                 // NOTREACHED
104         }
105
106         $myaddr = $r[0]["url"];
107
108         // Makes the connection request for friendica contacts easier
109         $_SESSION["fastlane"] = $contact["url"];
110
111         $header = L10n::t("Disconnect/Unfollow");
112
113         $o  = replace_macros($tpl, [
114                         '$header' => htmlentities($header),
115                         '$desc' => "",
116                         '$pls_answer' => "",
117                         '$does_know_you' => "",
118                         '$add_note' => "",
119                         '$page_desc' => "",
120                         '$friendica' => "",
121                         '$statusnet' => "",
122                         '$diaspora' => "",
123                         '$diasnote' => "",
124                         '$your_address' => L10n::t('Your Identity Address:'),
125                         '$invite_desc' => "",
126                         '$emailnet' => "",
127                         '$submit' => $submit,
128                         '$cancel' => L10n::t('Cancel'),
129                         '$nickname' => "",
130                         '$name' => $contact["name"],
131                         '$url' => $contact["url"],
132                         '$zrl' => Contact::magicLink($contact["url"]),
133                         '$url_label' => L10n::t("Profile URL"),
134                         '$myaddr' => $myaddr,
135                         '$request' => $request,
136                         '$keywords' => "",
137                         '$keywords_label' => ""
138         ]);
139
140         $a->page['aside'] = "";
141         Profile::load($a, "", 0, Contact::getDetailsByURL($contact["url"]));
142
143         $o .= replace_macros(get_markup_template('section_title.tpl'), ['$title' => L10n::t('Status Messages and Posts')]);
144
145         // Show last public posts
146         $o .= Contact::getPostsFromUrl($contact["url"]);
147
148         return $o;
149 }