3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, 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) {
30 $other = User::staticGet('nickname', $other_nickname);
33 return _('No such user.');
36 return subs_subscribe_to($user, $other);
39 function subs_subscribe_to($user, $other) {
41 if ($user->isSubscribed($other)) {
42 return _('Already subscribed!.');
45 if (!$user->subscribeTo($other)) {
46 return _('Could not subscribe.');
50 subs_notify($other, $user);
52 if (common_config('memcached', 'enabled')) {
53 $cache = new Memcache();
54 if ($cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'))) {
55 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
59 if ($other->autosubscribe && !$other->isSubscribed($user)) {
60 if (!$other->subscribeTo($user)) {
61 return _('Could not subscribe other to you.');
63 if (common_config('memcached', 'enabled')) {
64 $cache = new Memcache();
65 if ($cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'))) {
66 $cache->delete(common_cache_key('user:notices_with_friends:' . $other->id));
70 subs_notify($user, $other);
76 function subs_notify($listenee, $listener) {
77 # XXX: add other notifications (Jabber, SMS) here
78 # XXX: queue this and handle it offline
79 # XXX: Whatever happens, do it in Twitter-like API, too
80 subs_notify_email($listenee, $listener);
83 function subs_notify_email($listenee, $listener) {
84 mail_subscribe_notify($listenee, $listener);
87 /* Unsubscribe $user from nickname $other_nickname
88 Returns true or an error message.
90 function subs_unsubscribe_user($user, $other_nickname) {
92 $other = User::staticGet('nickname', $other_nickname);
95 return _('No such user.');
98 return subs_unsubscribe_to($user, $other);
101 function subs_unsubscribe_to($user, $other) {
103 if (!$user->isSubscribed($other))
104 return _('Not subscribed!.');
106 $sub = DB_DataObject::factory('subscription');
108 $sub->subscriber = $user->id;
109 $sub->subscribed = $other->id;
113 // note we checked for existence above
116 return _('Couldn\'t delete subscription.');
118 if (common_config('memcached', 'enabled')) {
119 $cache = new Memcache();
120 if ($cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'))) {
121 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));