]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/subs.php
Merge branch 'master' of evan@dev.controlyourself.ca:/var/www/trunk
[quix0rs-gnu-social.git] / lib / subs.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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('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
48     if ($user->isSubscribed($other)) {
49         return _('Already subscribed!.');
50     }
51
52     if ($other->hasBlocked($user)) {
53         return _('User has blocked you.');
54     }
55
56     if (!$user->subscribeTo($other)) {
57         return _('Could not subscribe.');
58         return;
59     }
60
61     subs_notify($other, $user);
62
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:' . $user->id));
67         }
68     }
69
70     if ($other->autosubscribe && !$other->isSubscribed($user) && !$user->hasBlocked($other)) {
71         if (!$other->subscribeTo($user)) {
72             return _('Could not subscribe other to you.');
73         }
74         if (common_config('memcached', 'enabled')) {
75             $cache = new Memcache();
76             if ($cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'))) {
77                 $cache->delete(common_cache_key('user:notices_with_friends:' . $other->id));
78             }
79         }
80
81         subs_notify($user, $other);
82     }
83
84     return true;
85 }
86
87 function subs_notify($listenee, $listener)
88 {
89     # XXX: add other notifications (Jabber, SMS) here
90     # XXX: queue this and handle it offline
91     # XXX: Whatever happens, do it in Twitter-like API, too
92     subs_notify_email($listenee, $listener);
93 }
94
95 function subs_notify_email($listenee, $listener)
96 {
97     mail_subscribe_notify($listenee, $listener);
98 }
99
100 /* Unsubscribe $user from nickname $other_nickname
101   Returns true or an error message.
102 */
103
104 function subs_unsubscribe_user($user, $other_nickname)
105 {
106
107     $other = User::staticGet('nickname', $other_nickname);
108
109     if (!$other) {
110         return _('No such user.');
111     }
112
113     return subs_unsubscribe_to($user, $other->getProfile());
114 }
115
116 /* Unsubscribe user $user from profile $other
117  * NB: other can be a remote user. */
118
119 function subs_unsubscribe_to($user, $other)
120 {
121
122     if (!$user->isSubscribed($other))
123         return _('Not subscribed!.');
124
125     $sub = DB_DataObject::factory('subscription');
126
127     $sub->subscriber = $user->id;
128     $sub->subscribed = $other->id;
129
130     $sub->find(true);
131
132     // note we checked for existence above
133
134     if (!$sub->delete())
135         return _('Couldn\'t delete subscription.');
136
137     if (common_config('memcached', 'enabled')) {
138         $cache = new Memcache();
139         if ($cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'))) {
140             $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
141         }
142     }
143
144     return true;
145 }
146