]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/subs.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into 0.9.x
[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     try {
60         if (Event::handle('StartSubscribe', array($user, $other))) {
61
62             if (!$user->subscribeTo($other)) {
63                 return _('Could not subscribe.');
64                 return;
65             }
66
67             subs_notify($other, $user);
68
69             $cache = common_memcache();
70
71             if ($cache) {
72                 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
73             }
74
75             $profile = $user->getProfile();
76
77             $profile->blowSubscriptionsCount();
78             $other->blowSubscribersCount();
79
80             if ($other->autosubscribe && !$other->isSubscribed($user) && !$user->hasBlocked($other)) {
81                 if (!$other->subscribeTo($user)) {
82                     return _('Could not subscribe other to you.');
83                 }
84                 $cache = common_memcache();
85
86                 if ($cache) {
87                     $cache->delete(common_cache_key('user:notices_with_friends:' . $other->id));
88                 }
89
90                 subs_notify($user, $other);
91             }
92
93             Event::handle('EndSubscribe', array($user, $other));
94         }
95     } catch (Exception $e) {
96         return $e->getMessage();
97     }
98
99     return true;
100 }
101
102 function subs_notify($listenee, $listener)
103 {
104     # XXX: add other notifications (Jabber, SMS) here
105     # XXX: queue this and handle it offline
106     # XXX: Whatever happens, do it in Twitter-like API, too
107     subs_notify_email($listenee, $listener);
108 }
109
110 function subs_notify_email($listenee, $listener)
111 {
112     mail_subscribe_notify($listenee, $listener);
113 }
114
115 /* Unsubscribe $user from nickname $other_nickname
116   Returns true or an error message.
117 */
118
119 function subs_unsubscribe_user($user, $other_nickname)
120 {
121
122     $other = User::staticGet('nickname', $other_nickname);
123
124     if (!$other) {
125         return _('No such user.');
126     }
127
128     return subs_unsubscribe_to($user, $other->getProfile());
129 }
130
131 /* Unsubscribe user $user from profile $other
132  * NB: other can be a remote user. */
133
134 function subs_unsubscribe_to($user, $other)
135 {
136     if (!$user->isSubscribed($other))
137         return _('Not subscribed!');
138
139     // Don't allow deleting self subs
140
141     if ($user->id == $other->id) {
142         return _('Couldn\'t delete self-subscription.');
143     }
144
145     try {
146         if (Event::handle('StartUnsubscribe', array($user, $other))) {
147
148             $sub = DB_DataObject::factory('subscription');
149
150             $sub->subscriber = $user->id;
151             $sub->subscribed = $other->id;
152
153             $sub->find(true);
154
155             // note we checked for existence above
156
157             if (!$sub->delete())
158               return _('Couldn\'t delete subscription.');
159
160             $cache = common_memcache();
161
162             if ($cache) {
163                 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
164             }
165
166             $profile = $user->getProfile();
167
168             $profile->blowSubscriptionsCount();
169             $other->blowSubscribersCount();
170
171             Event::handle('EndUnsubscribe', array($user, $other));
172         }
173     } catch (Exception $e) {
174         return $e->getMessage();
175     }
176
177     return true;
178 }
179