/** * Make a new subscription * * @param Profile $subscriber party to receive new notices * @param Profile $other party sending notices; publisher * @param bool $force pass Subscription::FORCE to override local subscription approval * * @return mixed Subscription or Subscription_queue: new subscription info */ static function start(Profile $subscriber, Profile $other, $force = false) { if (!$subscriber->hasRight(Right::SUBSCRIBE)) { // TRANS: Exception thrown when trying to subscribe while being banned from subscribing. throw new Exception(_('You have been banned from subscribing.')); } if (self::exists($subscriber, $other)) { // TRANS: Exception thrown when trying to subscribe while already subscribed. throw new AlreadyFulfilledException(_('Already subscribed!')); } if ($other->hasBlocked($subscriber)) { // TRANS: Exception thrown when trying to subscribe to a user who has blocked the subscribing user. throw new Exception(_('User has blocked you.')); } if (Event::handle('StartSubscribe', array($subscriber, $other))) { // unless subscription is forced, the user policy for subscription approvals is tested if (!$force && $other->requiresSubscriptionApproval($subscriber)) { try { $sub = Subscription_queue::saveNew($subscriber, $other); $sub->notify(); } catch (AlreadyFulfilledException $e) { $sub = Subscription_queue::getSubQueue($subscriber, $other); } } else { $otherUser = User::getKV('id', $other->id); $sub = self::saveNew($subscriber, $other); $sub->notify(); self::blow('user:notices_with_friends:%d', $subscriber->id); self::blow('subscription:by-subscriber:' . $subscriber->id); self::blow('subscription:by-subscribed:' . $other->id); $subscriber->blowSubscriptionCount(); $other->blowSubscriberCount(); if ($otherUser instanceof User && $otherUser->autosubscribe && !self::exists($other, $subscriber) && !$subscriber->hasBlocked($other)) { try { self::start($other, $subscriber); } catch (AlreadyFulfilledException $e) { // This shouldn't happen due to !self::exists above common_debug('Tried to autosubscribe a user to its new subscriber.'); } catch (Exception $e) { common_log(LOG_ERR, "Exception during autosubscribe of {$other->nickname} to profile {$subscriber->id}: {$e->getMessage()}"); } } } if ($sub instanceof Subscription) { // i.e. not Subscription_queue Event::handle('EndSubscribe', array($subscriber, $other)); } } return $sub; }
/** * Make a new subscription * * @param Profile $subscriber party to receive new notices * @param Profile $other party sending notices; publisher * @param bool $force pass Subscription::FORCE to override local subscription approval * * @return mixed Subscription or Subscription_queue: new subscription info */ static function start($subscriber, $other, $force = false) { // @fixme should we enforce this as profiles in callers instead? if ($subscriber instanceof User) { $subscriber = $subscriber->getProfile(); } if ($other instanceof User) { $other = $other->getProfile(); } if (!$subscriber->hasRight(Right::SUBSCRIBE)) { // TRANS: Exception thrown when trying to subscribe while being banned from subscribing. throw new Exception(_('You have been banned from subscribing.')); } if (self::exists($subscriber, $other)) { // TRANS: Exception thrown when trying to subscribe while already subscribed. throw new Exception(_('Already subscribed!')); } if ($other->hasBlocked($subscriber)) { // TRANS: Exception thrown when trying to subscribe to a user who has blocked the subscribing user. throw new Exception(_('User has blocked you.')); } if (Event::handle('StartSubscribe', array($subscriber, $other))) { $otherUser = User::staticGet('id', $other->id); if ($otherUser && $otherUser->subscribe_policy == User::SUBSCRIBE_POLICY_MODERATE && !$force) { $sub = Subscription_queue::saveNew($subscriber, $other); $sub->notify(); } else { $sub = self::saveNew($subscriber->id, $other->id); $sub->notify(); self::blow('user:notices_with_friends:%d', $subscriber->id); self::blow('subscription:by-subscriber:' . $subscriber->id); self::blow('subscription:by-subscribed:' . $other->id); $subscriber->blowSubscriptionCount(); $other->blowSubscriberCount(); if (!empty($otherUser) && $otherUser->autosubscribe && !self::exists($other, $subscriber) && !$subscriber->hasBlocked($other)) { try { self::start($other, $subscriber); } catch (Exception $e) { common_log(LOG_ERR, "Exception during autosubscribe of {$other->nickname} to profile {$subscriber->id}: {$e->getMessage()}"); } } } Event::handle('EndSubscribe', array($subscriber, $other)); } return $sub; }