]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/subs.php
less sleeping, slightly
[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 function subs_subscribe_user($user,$other_nickname) {
28
29         $other = User::staticGet('nickname', $other_nickname);
30
31         if (!$other) {
32                 return _('No such user.');
33         }
34
35         if ($user->isSubscribed($other)) {
36                 return _('Already subscribed!.');
37         }
38
39         if (!$user->subscribeTo($other)) {
40                 return _('Could not subscribe.');
41                 return;
42         }
43
44         subs_notify($other, $user);
45
46         if ($other->autosubscribe && !$other->isSubscribed($user)) {
47                 if (!$other->subscribeTo($user)) {
48                         return _('Could not subscribe other to you.');
49                 }
50                 subs_notify($user, $other);
51         }
52
53         return true;
54
55 }
56
57 function subs_notify($listenee, $listener) {
58         # XXX: add other notifications (Jabber, SMS) here
59         # XXX: queue this and handle it offline
60         # XXX: Whatever happens, do it in Twitter-like API, too
61         subs_notify_email($listenee, $listener);
62 }
63
64 function subs_notify_email($listenee, $listener) {
65         mail_subscribe_notify($listenee, $listener);
66 }
67
68
69 /* Unsubscribe $user from nickname $other_nickname
70   Returns true or an error message.
71 */
72 function subs_unsubscribe_user($user, $other_nickname) {
73
74         $other = User::staticGet('nickname', $other_nickname);
75         if (!$other)
76                 return _('No such user.');
77
78         if (!$user->isSubscribed($other))
79                 return _('Not subscribed!.');
80
81         $sub = DB_DataObject::factory('subscription');
82
83         $sub->subscriber = $user->id;
84         $sub->subscribed = $other->id;
85
86         $sub->find(true);
87
88         // note we checked for existence above
89
90         if (!$sub->delete())
91                 return _('Couldn\'t delete subscription.');
92
93         return true;
94
95 }
96