return true;
}
- if (!$oprofile->subscribe()) {
- // TRANS: Exception thrown when setup of remote subscription fails.
- throw new Exception(_m('Could not set up remote subscription.'));
- }
+ $oprofile->subscribe();
}
/**
* @param Profile $profile
*
* @return mixed hook return value
+ * @throws Exception of various kinds, some from $oprofile->subscribe();
*/
function onStartJoinGroup($group, $profile)
{
return true;
}
- if (!$oprofile->subscribe()) {
- // TRANS: Exception thrown when setup of remote group membership fails.
- throw new Exception(_m('Could not set up remote group membership.'));
- }
+ $oprofile->subscribe();
// NOTE: we don't use Group_member::asActivity() since that record
// has not yet been created.
* @param User $user
*
* @return mixed hook return value
+ * @throws Exception of various kinds, some from $oprofile->subscribe();
*/
function onStartSubscribePeopletag($peopletag, $user)
return true;
}
- if (!$oprofile->subscribe()) {
- // TRANS: Exception thrown when setup of remote list subscription fails.
- throw new Exception(_m('Could not set up remote list subscription.'));
- }
+ $oprofile->subscribe();
$sub = $user->getProfile();
$tagger = Profile::getKV($peopletag->tagger);
*
* @param Profile_tag $ptag the people tag that was created
* @return hook return value
+ * @throws Exception of various kinds, some from $oprofile->subscribe();
*/
function onEndTagProfile($ptag)
{
$oprofile->notifyDeferred($act, $tagger);
// initiate a PuSH subscription for the person being tagged
- if (!$oprofile->subscribe()) {
- // TRANS: Exception thrown when subscribing to a remote list fails.
- throw new Exception(sprintf(_m('Could not complete subscription to remote '.
- 'profile\'s feed. List %s could not be saved.'), $ptag->tag));
- return false;
- }
+ $oprofile->subscribe();
return true;
}
* Send a subscription request to the hub for this feed.
* The hub will later send us a confirmation POST to /main/push/callback.
*
- * @return bool true on success, false on failure
+ * @return void
* @throws ServerException if feed state is not valid
*/
public function subscribe()
if (!Event::handle('FeedSubscribe', array($this))) {
// A plugin handled it
- return true;
+ return;
}
if (empty($this->huburi)) {
} else if (common_config('feedsub', 'nohub')) {
// Fake it! We're just testing remote feeds w/o hubs.
// We'll never actually get updates in this mode.
- return true;
+ return;
} else {
// TRANS: Server exception.
throw new ServerException(_m('Attempting to start PuSH subscription for feed with no hub.'));
}
}
- return $this->doSubscribe('subscribe');
+ $this->doSubscribe('subscribe');
}
/**
* the system is using it. Most callers will want garbageCollect() instead,
* which confirms there's no uses left.
*
- * @return bool true on success, false on failure
* @throws ServerException if feed state is not valid
*/
public function unsubscribe() {
if (!Event::handle('FeedUnsubscribe', array($this))) {
// A plugin handled it
- return true;
+ return;
}
if (empty($this->huburi)) {
} else if (common_config('feedsub', 'nohub')) {
// Fake it! We're just testing remote feeds w/o hubs.
// We'll never actually get updates in this mode.
- return true;
+ return;
} else {
// TRANS: Server exception.
throw new ServerException(_m('Attempting to end PuSH subscription for feed with no hub.'));
}
}
- return $this->doSubscribe('unsubscribe');
+ $this->doSubscribe('unsubscribe');
}
/**
* make sure it's inactive, unsubscribing if necessary.
*
* @return boolean true if the subscription is now inactive, false if still active.
+ * @throws Exception if something goes wrong in unsubscribe() method
*/
public function garbageCollect()
{
$count = 0;
Event::handle('FeedSubSubscriberCount', array($this, &$count));
- if ($count) {
+ if ($count > 0) {
common_log(LOG_INFO, __METHOD__ . ': ok, ' . $count . ' user(s) left for ' . $this->getUri());
return false;
} else {
common_log(LOG_INFO, __METHOD__ . ': unsubscribing, no users left for ' . $this->getUri());
- return $this->unsubscribe();
+ // Unsubscribe throws various Exceptions on failure
+ $this->unsubscribe();
+ return true;
}
}
}
* the lookup _while_ we're POSTing data, which means the transaction
* never completes (PushcallbackAction gets an 'inactive' state).
*
- * @return boolean true on successful sub/unsub, false on failure
+ * @return boolean true when everything is ok (throws Exception on fail)
+ * @throws Exception on failure, can be HTTPClient's or our own.
*/
protected function doSubscribe($mode)
{
$client->setAuth($u, $p);
}
} else {
- throw new FeedSubException('WTF?');
+ throw new FeedSubException('Server could not find a usable PuSH hub.');
}
}
$response = $client->post($hub, $headers, $post);
$status = $response->getStatus();
+ // PuSH specificed response status code
if ($status == 202) {
common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback');
- return true;
+ return;
} else if ($status >= 200 && $status < 300) {
common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody());
} else {
common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody());
}
} catch (Exception $e) {
- // wtf!
- common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub $this->huburi subscribing to " . $this->getUri());
+ common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub {$this->huburi} subscribing to {$this->getUri()}");
+ // Reset the subscription state.
$orig = clone($this);
$this->sub_state = 'inactive';
$this->update($orig);
- unset($orig);
+
+ // Throw the Exception again.
+ throw $e;
}
- return false;
+ throw ServerException("{$mode} request failed.");
}
/**
* Send a subscription request to the hub for this feed.
* The hub will later send us a confirmation POST to /main/push/callback.
*
- * @return bool true on success, false on failure
- * @throws ServerException if feed state is not valid
+ * @return void
+ * @throws ServerException if feed state is not valid or subscription fails.
*/
public function subscribe()
{
$feedsub = FeedSub::ensureFeed($this->feeduri);
if ($feedsub->sub_state == 'active') {
// Active subscription, we don't need to do anything.
- return true;
- } else {
- // Inactive or we got left in an inconsistent state.
- // Run a subscription request to make sure we're current!
- return $feedsub->subscribe();
+ return;
}
+
+ // Inactive or we got left in an inconsistent state.
+ // Run a subscription request to make sure we're current!
+ return $feedsub->subscribe();
}
/**
* Check if this remote profile has any active local subscriptions, and
* if not drop the PuSH subscription feed.
*
- * @return bool true on success, false on failure
+ * @return boolean true if subscription is removed, false if there are still subscribers to the feed
+ * @throws Exception of various kinds on failure.
*/
public function unsubscribe() {
- $this->garbageCollect();
+ return $this->garbageCollect();
}
/**
* Check if this remote profile has any active local subscriptions, and
* if not drop the PuSH subscription feed.
*
- * @return boolean
+ * @return boolean true if subscription is removed, false if there are still subscribers to the feed
+ * @throws Exception of various kinds on failure.
*/
public function garbageCollect()
{
$feedsub = new FeedSub();
while ($feedsub->fetch()) {
- print $feedsub->uri . "(" . $feedsub->sub_state . ")";
- $result = $feedsub->garbageCollect();
- if ($result) {
- print " INACTIVE\n";
- } else {
- print " ACTIVE\n";
+ echo "{$feedsub->uri} ({$feedsub->sub_state})";
+ try {
+ if ($feedsub->garbageCollect()) {
+ echo " INACTIVE\n";
+ } else {
+ echo " ACTIVE\n";
+ }
+ } catch (Exception $e) {
+ echo " ERROR: {$e->getMessage()}\n";
}
}
print "Old state:\n";
showSub($sub);
-print "\n";
-
-if (have_option('u') || have_option('--unsub')) {
- print "Pinging hub $sub->huburi with unsubscription for $sub->uri\n";
- $ok = $sub->unsubscribe();
-} else {
- print "Pinging hub $sub->huburi with new subscription for $sub->uri\n";
- $ok = $sub->subscribe();
-}
-
-if ($ok) {
- print "ok\n";
-} else {
- print "Could not confirm.\n";
+try {
+ echo "\n";
+ if (have_option('u') || have_option('--unsub')) {
+ echo "Pinging hub {$sub->huburi} with unsubscription for {$sub->uri}\n";
+ $sub->unsubscribe();
+ } else {
+ echo "Pinging hub {$sub->huburi} with new subscription for {$sub->uri}\n";
+ $sub->subscribe();
+ }
+ echo "ok\n";
+} catch (Exception $e) {
+ echo 'Could not confirm. '.get_class($e).': '.$e->getMessage()."\n";
}
$sub2 = FeedSub::getKV('uri', $feedurl);
print "Feed record ok, not changing.\n\n";
}
-print "\n";
-print "Pinging hub $sub->huburi with new subscription for $sub->uri\n";
-$ok = $sub->subscribe();
-
-if ($ok) {
- print "ok\n";
-} else {
- print "Could not confirm.\n";
+echo "\n";
+echo "Pinging hub {$sub->huburi} with new subscription for {$sub->uri}\n";
+try {
+ $sub->subscribe();
+ echo "ok\n";
+} catch (Exception $e) {
+ echo 'Could not confirm. '.get_class($e).': '.$e->getMessage()."\n";
}
$o2 = Ostatus_profile::getKV('uri', $uri);
protected function saveMirror()
{
- if ($this->oprofile->subscribe()) {
- SubMirror::saveMirror($this->user, $this->profile);
- } else {
- // TRANS: Exception thrown when a subscribing to a feed fails.
- $this->serverError(_m('Could not subscribe to feed.'));
- }
+ $this->oprofile->subscribe();
+ SubMirror::saveMirror($this->user, $this->profile);
}
}