]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Subscription.php
TinyMCE: only apply HTML parsing if we actually got into JS and poked the editor...
[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         // @fixme should we enforce this as profiles in callers instead?
66         if ($subscriber instanceof User) {
67             $subscriber = $subscriber->getProfile();
68         }
69         if ($other instanceof User) {
70             $other = $other->getProfile();
71         }
72
73         if (!$subscriber->hasRight(Right::SUBSCRIBE)) {
74             // TRANS: Exception thrown when trying to subscribe while being banned from subscribing.
75             throw new Exception(_('You have been banned from subscribing.'));
76         }
77
78         if (self::exists($subscriber, $other)) {
79             // TRANS: Exception thrown when trying to subscribe while already subscribed.
80             throw new Exception(_('Already subscribed!'));
81         }
82
83         if ($other->hasBlocked($subscriber)) {
84             // TRANS: Exception thrown when trying to subscribe to a user who has blocked the subscribing user.
85             throw new Exception(_('User has blocked you.'));
86         }
87
88         if (Event::handle('StartSubscribe', array($subscriber, $other))) {
89             $sub = self::saveNew($subscriber->id, $other->id);
90             $sub->notify();
91
92             self::blow('user:notices_with_friends:%d', $subscriber->id);
93
94             $subscriber->blowSubscriptionCount();
95             $other->blowSubscriberCount();
96
97             $otherUser = User::staticGet('id', $other->id);
98
99             if (!empty($otherUser) &&
100                 $otherUser->autosubscribe &&
101                 !self::exists($other, $subscriber) &&
102                 !$subscriber->hasBlocked($other)) {
103
104                 try {
105                     self::start($other, $subscriber);
106                 } catch (Exception $e) {
107                     common_log(LOG_ERR, "Exception during autosubscribe of {$other->nickname} to profile {$subscriber->id}: {$e->getMessage()}");
108                 }
109             }
110
111             Event::handle('EndSubscribe', array($subscriber, $other));
112         }
113
114         return true;
115     }
116
117     /**
118      * Low-level subscription save.
119      * Outside callers should use Subscription::start()
120      */
121     protected function saveNew($subscriber_id, $other_id)
122     {
123         $sub = new Subscription();
124
125         $sub->subscriber = $subscriber_id;
126         $sub->subscribed = $other_id;
127         $sub->jabber     = 1;
128         $sub->sms        = 1;
129         $sub->created    = common_sql_now();
130
131         $result = $sub->insert();
132
133         if (!$result) {
134             common_log_db_error($sub, 'INSERT', __FILE__);
135             // TRANS: Exception thrown when a subscription could not be stored on the server.
136             throw new Exception(_('Could not save subscription.'));
137         }
138
139         return $sub;
140     }
141
142     function notify()
143     {
144         # XXX: add other notifications (Jabber, SMS) here
145         # XXX: queue this and handle it offline
146         # XXX: Whatever happens, do it in Twitter-like API, too
147
148         $this->notifyEmail();
149     }
150
151     function notifyEmail()
152     {
153         $subscribedUser = User::staticGet('id', $this->subscribed);
154
155         if (!empty($subscribedUser)) {
156
157             $subscriber = Profile::staticGet('id', $this->subscriber);
158
159             mail_subscribe_notify_profile($subscribedUser, $subscriber);
160         }
161     }
162
163     /**
164      * Cancel a subscription
165      *
166      */
167     function cancel($subscriber, $other)
168     {
169         if (!self::exists($subscriber, $other)) {
170             // TRANS: Exception thrown when trying to unsibscribe without a subscription.
171             throw new Exception(_('Not subscribed!'));
172         }
173
174         // Don't allow deleting self subs
175
176         if ($subscriber->id == $other->id) {
177             // TRANS: Exception thrown when trying to unsubscribe a user from themselves.
178             throw new Exception(_('Could not delete self-subscription.'));
179         }
180
181         if (Event::handle('StartUnsubscribe', array($subscriber, $other))) {
182
183             $sub = Subscription::pkeyGet(array('subscriber' => $subscriber->id,
184                                                'subscribed' => $other->id));
185
186             // note we checked for existence above
187
188             assert(!empty($sub));
189
190             // @todo: move this block to EndSubscribe handler for
191             // OMB plugin when it exists.
192
193             if (!empty($sub->token)) {
194
195                 $token = new Token();
196
197                 $token->tok    = $sub->token;
198
199                 if ($token->find(true)) {
200
201                     $result = $token->delete();
202
203                     if (!$result) {
204                         common_log_db_error($token, 'DELETE', __FILE__);
205                         // TRANS: Exception thrown when the OMB token for a subscription could not deleted on the server.
206                         throw new Exception(_('Could not delete subscription OMB token.'));
207                     }
208                 } else {
209                     common_log(LOG_ERR, "Couldn't find credentials with token {$token->tok}");
210                 }
211             }
212
213             $result = $sub->delete();
214
215             if (!$result) {
216                 common_log_db_error($sub, 'DELETE', __FILE__);
217                 // TRANS: Exception thrown when a subscription could not be deleted on the server.
218                 throw new Exception(_('Could not delete subscription.'));
219             }
220
221             self::blow('user:notices_with_friends:%d', $subscriber->id);
222
223             $subscriber->blowSubscriptionCount();
224             $other->blowSubscriberCount();
225
226             Event::handle('EndUnsubscribe', array($subscriber, $other));
227         }
228
229         return;
230     }
231
232     function exists($subscriber, $other)
233     {
234         $sub = Subscription::pkeyGet(array('subscriber' => $subscriber->id,
235                                            'subscribed' => $other->id));
236         return (empty($sub)) ? false : true;
237     }
238 }