]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/subs.php
low-level management of blocks
[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         $other = User::staticGet('nickname', $other_nickname);
31
32         if (!$other) {
33                 return _('No such user.');
34         }
35
36         return subs_subscribe_to($user, $other);
37 }
38
39 /* Subscribe user $user to other user $other.
40  * Note: $other must be a local user, not a remote profile.
41  * Because the other way is quite a bit more complicated.
42  */
43
44 function subs_subscribe_to($user, $other) {
45
46         if ($user->isSubscribed($other)) {
47                 return _('Already subscribed!.');
48         }
49
50     if ($other->hasBlocked($user)) {
51                 return _('User has blocked you.');
52     }
53
54         if (!$user->subscribeTo($other)) {
55                 return _('Could not subscribe.');
56                 return;
57         }
58
59     subs_notify($other, $user);
60
61         if (common_config('memcached', 'enabled')) {
62                 $cache = new Memcache();
63                 if ($cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'))) {
64                         $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
65                 }
66         }
67
68         if ($other->autosubscribe && !$other->isSubscribed($user) && !$user->hasBlocked($other)) {
69                 if (!$other->subscribeTo($user)) {
70                         return _('Could not subscribe other to you.');
71                 }
72                 if (common_config('memcached', 'enabled')) {
73                         $cache = new Memcache();
74                         if ($cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'))) {
75                                 $cache->delete(common_cache_key('user:notices_with_friends:' . $other->id));
76                         }
77                 }
78
79                 subs_notify($user, $other);
80         }
81
82         return true;
83 }
84
85 function subs_notify($listenee, $listener) {
86         # XXX: add other notifications (Jabber, SMS) here
87         # XXX: queue this and handle it offline
88         # XXX: Whatever happens, do it in Twitter-like API, too
89         subs_notify_email($listenee, $listener);
90 }
91
92 function subs_notify_email($listenee, $listener) {
93         mail_subscribe_notify($listenee, $listener);
94 }
95
96 /* Unsubscribe $user from nickname $other_nickname
97   Returns true or an error message.
98 */
99
100 function subs_unsubscribe_user($user, $other_nickname) {
101
102         $other = User::staticGet('nickname', $other_nickname);
103
104         if (!$other) {
105                 return _('No such user.');
106         }
107
108         return subs_unsubscribe_to($user, $other->getProfile());
109 }
110
111 /* Unsubscribe user $user from profile $other
112  * NB: other can be a remote user. */
113
114 function subs_unsubscribe_to($user, $other) {
115
116         if (!$user->isSubscribed($other))
117                 return _('Not subscribed!.');
118
119         $sub = DB_DataObject::factory('subscription');
120
121         $sub->subscriber = $user->id;
122         $sub->subscribed = $other->id;
123
124         $sub->find(true);
125
126         // note we checked for existence above
127
128         if (!$sub->delete())
129                 return _('Couldn\'t delete subscription.');
130
131         if (common_config('memcached', 'enabled')) {
132                 $cache = new Memcache();
133                 if ($cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'))) {
134                         $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
135                 }
136         }
137
138         return true;
139 }
140