]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/subs.php
Make it impossible to block (and thus unsubscribe from your
[quix0rs-gnu-social.git] / lib / subs.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once('XMPPHP/XMPP.php');
23
24 /* Subscribe $user to nickname $other_nickname
25   Returns true or an error message.
26 */
27
28 function subs_subscribe_user($user, $other_nickname)
29 {
30
31     $other = User::staticGet('nickname', $other_nickname);
32
33     if (!$other) {
34         return _('No such user.');
35     }
36
37     return subs_subscribe_to($user, $other);
38 }
39
40 /* Subscribe user $user to other user $other.
41  * Note: $other must be a local user, not a remote profile.
42  * Because the other way is quite a bit more complicated.
43  */
44
45 function subs_subscribe_to($user, $other)
46 {
47     if (!$user->hasRight(Right::SUBSCRIBE)) {
48         return _('You have been banned from subscribing.');
49     }
50
51     if ($user->isSubscribed($other)) {
52         return _('Already subscribed!');
53     }
54
55     if ($other->hasBlocked($user)) {
56         return _('User has blocked you.');
57     }
58
59     if (!$user->subscribeTo($other)) {
60         return _('Could not subscribe.');
61         return;
62     }
63
64     subs_notify($other, $user);
65
66     $cache = common_memcache();
67
68     if ($cache) {
69         $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
70         }
71
72     $profile = $user->getProfile();
73
74     $profile->blowSubscriptionsCount();
75     $other->blowSubscribersCount();
76
77     if ($other->autosubscribe && !$other->isSubscribed($user) && !$user->hasBlocked($other)) {
78         if (!$other->subscribeTo($user)) {
79             return _('Could not subscribe other to you.');
80         }
81         $cache = common_memcache();
82
83         if ($cache) {
84             $cache->delete(common_cache_key('user:notices_with_friends:' . $other->id));
85                 }
86
87         subs_notify($user, $other);
88     }
89
90     return true;
91 }
92
93 function subs_notify($listenee, $listener)
94 {
95     # XXX: add other notifications (Jabber, SMS) here
96     # XXX: queue this and handle it offline
97     # XXX: Whatever happens, do it in Twitter-like API, too
98     subs_notify_email($listenee, $listener);
99 }
100
101 function subs_notify_email($listenee, $listener)
102 {
103     mail_subscribe_notify($listenee, $listener);
104 }
105
106 /* Unsubscribe $user from nickname $other_nickname
107   Returns true or an error message.
108 */
109
110 function subs_unsubscribe_user($user, $other_nickname)
111 {
112
113     $other = User::staticGet('nickname', $other_nickname);
114
115     if (!$other) {
116         return _('No such user.');
117     }
118
119     return subs_unsubscribe_to($user, $other->getProfile());
120 }
121
122 /* Unsubscribe user $user from profile $other
123  * NB: other can be a remote user. */
124
125 function subs_unsubscribe_to($user, $other)
126 {
127     if (!$user->isSubscribed($other))
128         return _('Not subscribed!');
129
130     // Don't allow deleting self subs
131
132     if ($user->id == $other->id) {
133         return _('Couldn\'t delete self-subscription.');
134     }
135
136     $sub = DB_DataObject::factory('subscription');
137
138     $sub->subscriber = $user->id;
139     $sub->subscribed = $other->id;
140
141     $sub->find(true);
142
143     // note we checked for existence above
144
145     if (!$sub->delete())
146         return _('Couldn\'t delete subscription.');
147
148     $cache = common_memcache();
149
150     if ($cache) {
151         $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
152         }
153
154     $profile = $user->getProfile();
155
156     $profile->blowSubscriptionsCount();
157     $other->blowSubscribersCount();
158
159     return true;
160 }
161