3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once('XMPPHP/XMPP.php');
24 /* Subscribe $user to nickname $other_nickname
25 Returns true or an error message.
28 function subs_subscribe_user($user, $other_nickname)
31 $other = User::staticGet('nickname', $other_nickname);
34 return _('No such user.');
37 return subs_subscribe_to($user, $other);
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.
45 function subs_subscribe_to($user, $other)
47 if ($user->isSubscribed($other)) {
48 return _('Already subscribed!.');
51 if ($other->hasBlocked($user)) {
52 return _('User has blocked you.');
55 if (!$user->subscribeTo($other)) {
56 return _('Could not subscribe.');
60 subs_notify($other, $user);
62 $cache = common_memcache();
65 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
68 $profile = $user->getProfile();
70 $profile->blowSubscriptionsCount();
71 $other->blowSubscribersCount();
73 if ($other->autosubscribe && !$other->isSubscribed($user) && !$user->hasBlocked($other)) {
74 if (!$other->subscribeTo($user)) {
75 return _('Could not subscribe other to you.');
77 $cache = common_memcache();
80 $cache->delete(common_cache_key('user:notices_with_friends:' . $other->id));
83 subs_notify($user, $other);
89 function subs_notify($listenee, $listener)
91 # XXX: add other notifications (Jabber, SMS) here
92 # XXX: queue this and handle it offline
93 # XXX: Whatever happens, do it in Twitter-like API, too
94 subs_notify_email($listenee, $listener);
97 function subs_notify_email($listenee, $listener)
99 mail_subscribe_notify($listenee, $listener);
102 /* Unsubscribe $user from nickname $other_nickname
103 Returns true or an error message.
106 function subs_unsubscribe_user($user, $other_nickname)
109 $other = User::staticGet('nickname', $other_nickname);
112 return _('No such user.');
115 return subs_unsubscribe_to($user, $other->getProfile());
118 /* Unsubscribe user $user from profile $other
119 * NB: other can be a remote user. */
121 function subs_unsubscribe_to($user, $other)
123 if (!$user->isSubscribed($other))
124 return _('Not subscribed!.');
126 $sub = DB_DataObject::factory('subscription');
128 $sub->subscriber = $user->id;
129 $sub->subscribed = $other->id;
133 // note we checked for existence above
136 return _('Couldn\'t delete subscription.');
138 $cache = common_memcache();
141 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
144 $profile = $user->getProfile();
146 $profile->blowSubscriptionsCount();
147 $other->blowSubscribersCount();