try {
Subscription::start($this->scoped, $this->other);
- } catch (Exception $e) {
- $this->clientError($e->getMessage(), 403);
+ } catch (AlreadyFulfilledException $e) {
+ $this->clientError($e->getMessage(), 409);
}
$this->initDocument($this->format);
$this->clientError(sprintf(_('Unknown profile %s.'), $person->id));
}
- if (Subscription::exists($this->_profile, $profile)) {
+ try {
+ $sub = Subscription::start($this->_profile, $profile);
+ } catch (AlreadyFulfilledException $e) {
// 409 Conflict
- // TRANS: Client error displayed trying to subscribe to an already subscribed profile.
- // TRANS: %s is the profile the user already has a subscription on.
- $this->clientError(sprintf(_('Already subscribed to %s.'),
- $person->id),
- 409);
- }
-
- if (Subscription::start($this->_profile, $profile)) {
- $sub = Subscription::pkeyGet(array('subscriber' => $this->_profile->id,
- 'subscribed' => $profile->id));
+ $this->clientError($e->getMessage(), 409);
}
Event::handle('EndAtomPubNewActivity', array($activity, $sub));
$this->already[] = $other;
} else {
try {
- Subscription::start($profile, $other);
+ Subscription::ensureStart($profile, $other);
$this->subbed[] = $other;
} catch (Exception $e) {
// subscription failed, but keep working
{
// Throws exception on error
- $sub = Subscription::start($this->user->getProfile(),
+ $sub = Subscription::ensureStart($this->user->getProfile(),
$this->other);
if ($this->boolean('ajax')) {
if (Event::handle('StartSubscribe', array($subscriber, $other))) {
$otherUser = User::getKV('id', $other->id);
if ($otherUser instanceof User && $otherUser->subscribe_policy == User::SUBSCRIBE_POLICY_MODERATE && !$force) {
+ // Will throw an AlreadyFulfilledException if this queue item already exists.
$sub = Subscription_queue::saveNew($subscriber, $other);
$sub->notify();
} else {
return $sub;
}
+ static function ensureStart(Profile $subscriber, Profile $other, $force=false)
+ {
+ try {
+ $sub = self::start($subscriber, $other, $force);
+ } catch (AlreadyFulfilledException $e) {
+ // Nothing to see here, move along...
+ return self::getSubscription($subscriber, $other);
+ }
+ return $sub;
+ }
+
/**
* Low-level subscription save.
* Outside callers should use Subscription::start()
public static function saveNew(Profile $subscriber, Profile $subscribed)
{
+ if (self::exists($subscriber, $subscribed)) {
+ throw new AlreadyFulfilledException(_('This subscription request is already in progress.'));
+ }
$rq = new Subscription_queue();
$rq->subscriber = $subscriber->id;
$rq->subscribed = $subscribed->id;
common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick),
__FILE__);
} else {
- Subscription::start($profile, $defuser->getProfile());
+ Subscription::ensureStart($profile, $defuser->getProfile());
}
}
// XXX: don't do this for untrusted input!
- Subscription::start($otherProfile, $profile);
+ Subscription::ensureStart($otherProfile, $profile);
} else if (empty($activity->actor)
|| $activity->actor->id == $author->id) {
throw new ClientException(_('Unknown profile.'));
}
- Subscription::start($profile, $otherProfile);
+ Subscription::ensureStart($profile, $otherProfile);
} else {
// TRANS: Client exception thrown when trying to import an event not related to the importing user.
throw new Exception(_('This activity seems unrelated to our user.'));
"Changing sub to {$act->objects[0]->id}".
"by {$act->actor->id} to {$remote->nickname}.");
$otherProfile = $otherUser->getProfile();
- Subscription::start($otherProfile, $remote);
+ Subscription::ensureStart($otherProfile, $remote);
Subscription::cancel($otherProfile, $user->getProfile());
} else {
$this->log(LOG_NOTICE,