]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Subscription.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / classes / Subscription.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 /**
23  * Table Definition for subscription
24  */
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
26
27 class Subscription extends Memcached_DataObject
28 {
29     ###START_AUTOCODE
30     /* the code below is auto generated do not remove the above tag */
31
32     public $__table = 'subscription';                    // table name
33     public $subscriber;                      // int(4)  primary_key not_null
34     public $subscribed;                      // int(4)  primary_key not_null
35     public $jabber;                          // tinyint(1)   default_1
36     public $sms;                             // tinyint(1)   default_1
37     public $token;                           // varchar(255)
38     public $secret;                          // varchar(255)
39     public $created;                         // datetime()   not_null
40     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
41
42     /* Static get */
43     function staticGet($k,$v=null)
44     { return Memcached_DataObject::staticGet('Subscription',$k,$v); }
45
46     /* the code above is auto generated do not remove the tag below */
47     ###END_AUTOCODE
48
49     function pkeyGet($kv)
50     {
51         return Memcached_DataObject::pkeyGet('Subscription', $kv);
52     }
53
54     /**
55      * Make a new subscription
56      *
57      * @param Profile $subscriber party to receive new notices
58      * @param Profile $other      party sending notices; publisher
59      *
60      * @return Subscription new subscription
61      */
62
63     static function start($subscriber, $other)
64     {
65         if (!$subscriber->hasRight(Right::SUBSCRIBE)) {
66             throw new Exception(_('You have been banned from subscribing.'));
67         }
68
69         if (self::exists($subscriber, $other)) {
70             throw new Exception(_('Already subscribed!'));
71         }
72
73         if ($other->hasBlocked($subscriber)) {
74             throw new Exception(_('User has blocked you.'));
75         }
76
77         if (Event::handle('StartSubscribe', array($subscriber, $other))) {
78
79             $sub = new Subscription();
80
81             $sub->subscriber = $subscriber->id;
82             $sub->subscribed = $other->id;
83             $sub->created    = common_sql_now();
84
85             $result = $sub->insert();
86
87             if (!$result) {
88                 common_log_db_error($sub, 'INSERT', __FILE__);
89                 throw new Exception(_('Could not save subscription.'));
90             }
91
92             $sub->notify();
93
94             self::blow('user:notices_with_friends:%d', $subscriber->id);
95
96             $subscriber->blowSubscriptionsCount();
97             $other->blowSubscribersCount();
98
99             $otherUser = User::staticGet('id', $other->id);
100
101             if (!empty($otherUser) &&
102                 $otherUser->autosubscribe &&
103                 !self::exists($other, $subscriber) &&
104                 !$subscriber->hasBlocked($other)) {
105
106                 $auto = new Subscription();
107
108                 $auto->subscriber = $subscriber->id;
109                 $auto->subscribed = $other->id;
110                 $auto->created    = common_sql_now();
111
112                 $result = $auto->insert();
113
114                 if (!$result) {
115                     common_log_db_error($auto, 'INSERT', __FILE__);
116                     throw new Exception(_('Could not save subscription.'));
117                 }
118
119                 $auto->notify();
120             }
121
122             Event::handle('EndSubscribe', array($subscriber, $other));
123         }
124
125         return true;
126     }
127
128     function notify()
129     {
130         # XXX: add other notifications (Jabber, SMS) here
131         # XXX: queue this and handle it offline
132         # XXX: Whatever happens, do it in Twitter-like API, too
133
134         $this->notifyEmail();
135     }
136
137     function notifyEmail()
138     {
139         $subscribedUser = User::staticGet('id', $this->subscribed);
140
141         if (!empty($subscribedUser)) {
142
143             $subscriber = Profile::staticGet('id', $this->subscriber);
144
145             mail_subscribe_notify_profile($subscribedUser, $subscriber);
146         }
147     }
148
149     /**
150      * Cancel a subscription
151      *
152      */
153
154     function cancel($subscriber, $other)
155     {
156         if (!self::exists($subscriber, $other)) {
157             throw new Exception(_('Not subscribed!'));
158         }
159
160         // Don't allow deleting self subs
161
162         if ($subscriber->id == $other->id) {
163             throw new Exception(_('Couldn\'t delete self-subscription.'));
164         }
165
166         if (Event::handle('StartUnsubscribe', array($subscriber, $other))) {
167
168             $sub = Subscription::pkeyGet(array('subscriber' => $subscriber->id,
169                                                'subscribed' => $other->id));
170
171             // note we checked for existence above
172
173             assert(!empty($sub));
174
175             $result = $sub->delete();
176
177             if (!$result) {
178                 common_log_db_error($sub, 'DELETE', __FILE__);
179                 throw new Exception(_('Couldn\'t delete subscription.'));
180             }
181
182             self::blow('user:notices_with_friends:%d', $subscriber->id);
183
184             $subscriber->blowSubscriptionsCount();
185             $other->blowSubscribersCount();
186
187             Event::handle('EndUnsubscribe', array($subscriber, $other));
188         }
189
190         return;
191     }
192
193     function exists($subscriber, $other)
194     {
195         $sub = Subscription::pkeyGet(array('subscriber' => $subscriber->id,
196                                            'subscribed' => $other->id));
197         return (empty($sub)) ? false : true;
198     }
199 }