3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
23 * Table Definition for subscription
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
27 class Subscription extends Memcached_DataObject
30 /* the code below is auto generated do not remove the above tag */
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
43 function staticGet($k,$v=null)
44 { return Memcached_DataObject::staticGet('Subscription',$k,$v); }
46 /* the code above is auto generated do not remove the tag below */
51 return Memcached_DataObject::pkeyGet('Subscription', $kv);
55 * Make a new subscription
57 * @param Profile $subscriber party to receive new notices
58 * @param Profile $other party sending notices; publisher
60 * @return Subscription new subscription
63 static function start($subscriber, $other)
65 // @fixme should we enforce this as profiles in callers instead?
66 if ($subscriber instanceof User) {
67 $subscriber = $subscriber->getProfile();
69 if ($other instanceof User) {
70 $other = $other->getProfile();
73 if (!$subscriber->hasRight(Right::SUBSCRIBE)) {
74 throw new Exception(_('You have been banned from subscribing.'));
77 if (self::exists($subscriber, $other)) {
78 throw new Exception(_('Already subscribed!'));
81 if ($other->hasBlocked($subscriber)) {
82 throw new Exception(_('User has blocked you.'));
85 if (Event::handle('StartSubscribe', array($subscriber, $other))) {
86 $sub = self::saveNew($subscriber->id, $other->id);
89 self::blow('user:notices_with_friends:%d', $subscriber->id);
91 $subscriber->blowSubscriptionsCount();
92 $other->blowSubscribersCount();
94 $otherUser = User::staticGet('id', $other->id);
96 if (!empty($otherUser) &&
97 $otherUser->autosubscribe &&
98 !self::exists($other, $subscriber) &&
99 !$subscriber->hasBlocked($other)) {
102 self::start($other, $subscriber);
103 } catch (Exception $e) {
104 common_log(LOG_ERR, "Exception during autosubscribe of {$other->nickname} to profile {$subscriber->id}: {$e->getMessage()}");
108 Event::handle('EndSubscribe', array($subscriber, $other));
115 * Low-level subscription save.
116 * Outside callers should use Subscription::start()
118 protected function saveNew($subscriber_id, $other_id)
120 $sub = new Subscription();
122 $sub->subscriber = $subscriber_id;
123 $sub->subscribed = $other_id;
126 $sub->created = common_sql_now();
128 $result = $sub->insert();
131 common_log_db_error($sub, 'INSERT', __FILE__);
132 throw new Exception(_('Could not save subscription.'));
140 # XXX: add other notifications (Jabber, SMS) here
141 # XXX: queue this and handle it offline
142 # XXX: Whatever happens, do it in Twitter-like API, too
144 $this->notifyEmail();
147 function notifyEmail()
149 $subscribedUser = User::staticGet('id', $this->subscribed);
151 if (!empty($subscribedUser)) {
153 $subscriber = Profile::staticGet('id', $this->subscriber);
155 mail_subscribe_notify_profile($subscribedUser, $subscriber);
160 * Cancel a subscription
164 function cancel($subscriber, $other)
166 if (!self::exists($subscriber, $other)) {
167 throw new Exception(_('Not subscribed!'));
170 // Don't allow deleting self subs
172 if ($subscriber->id == $other->id) {
173 throw new Exception(_('Couldn\'t delete self-subscription.'));
176 if (Event::handle('StartUnsubscribe', array($subscriber, $other))) {
178 $sub = Subscription::pkeyGet(array('subscriber' => $subscriber->id,
179 'subscribed' => $other->id));
181 // note we checked for existence above
183 assert(!empty($sub));
185 // @todo: move this block to EndSubscribe handler for
186 // OMB plugin when it exists.
188 if (!empty($sub->token)) {
190 $token = new Token();
192 $token->tok = $sub->token;
194 if ($token->find(true)) {
196 $result = $token->delete();
199 common_log_db_error($token, 'DELETE', __FILE__);
200 throw new Exception(_('Couldn\'t delete subscription OMB token.'));
203 common_log(LOG_ERR, "Couldn't find credentials with token {$token->tok}");
207 $result = $sub->delete();
210 common_log_db_error($sub, 'DELETE', __FILE__);
211 throw new Exception(_('Couldn\'t delete subscription.'));
214 self::blow('user:notices_with_friends:%d', $subscriber->id);
216 $subscriber->blowSubscriptionsCount();
217 $other->blowSubscribersCount();
219 Event::handle('EndUnsubscribe', array($subscriber, $other));
225 function exists($subscriber, $other)
227 $sub = Subscription::pkeyGet(array('subscriber' => $subscriber->id,
228 'subscribed' => $other->id));
229 return (empty($sub)) ? false : true;