Separate ensureHub into function in FeedSub
This commit is contained in:
parent
598b51eb7a
commit
853b016a42
@ -167,35 +167,81 @@ class FeedSub extends Managed_DataObject
|
|||||||
*/
|
*/
|
||||||
public static function ensureFeed($feeduri)
|
public static function ensureFeed($feeduri)
|
||||||
{
|
{
|
||||||
$current = self::getKV('uri', $feeduri);
|
$feedsub = self::getKV('uri', $feeduri);
|
||||||
if ($current instanceof FeedSub) {
|
if ($feedsub instanceof FeedSub) {
|
||||||
return $current;
|
if (!empty($feedsub->huburi)) {
|
||||||
|
// If there is already a huburi we don't
|
||||||
|
// rediscover it on ensureFeed, call
|
||||||
|
// ensureHub to do that (compare ->modified
|
||||||
|
// to see if it might be time to do it).
|
||||||
|
return $feedsub;
|
||||||
}
|
}
|
||||||
|
if ($feedsub->sub_state !== 'inactive') {
|
||||||
$discover = new FeedDiscovery();
|
throw new ServerException('Can only ensure WebSub hub for inactive (unsubscribed) feeds.');
|
||||||
$discover->discoverFromFeedURL($feeduri);
|
|
||||||
|
|
||||||
$huburi = $discover->getHubLink();
|
|
||||||
if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
|
|
||||||
throw new FeedSubNoHubException();
|
|
||||||
}
|
}
|
||||||
|
// If huburi is empty we continue with ensureHub
|
||||||
|
} else {
|
||||||
|
// If we don't have that local feed URI
|
||||||
|
// stored then we create a new DB object.
|
||||||
$feedsub = new FeedSub();
|
$feedsub = new FeedSub();
|
||||||
$feedsub->uri = $feeduri;
|
$feedsub->uri = $feeduri;
|
||||||
$feedsub->huburi = $huburi;
|
|
||||||
$feedsub->sub_state = 'inactive';
|
$feedsub->sub_state = 'inactive';
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// discover the hub uri
|
||||||
|
$feedsub->ensureHub();
|
||||||
|
|
||||||
|
} catch (FeedSubNoHubException $e) {
|
||||||
|
// Only throw this exception if we can't handle huburi-less feeds
|
||||||
|
// (i.e. we have a fallback hub or we can do feed polling (nohub)
|
||||||
|
if (!common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($feedsub->id)) {
|
||||||
|
// if $feedsub doesn't have an id we'll insert it into the db here
|
||||||
$feedsub->created = common_sql_now();
|
$feedsub->created = common_sql_now();
|
||||||
$feedsub->modified = common_sql_now();
|
$feedsub->modified = common_sql_now();
|
||||||
|
|
||||||
$result = $feedsub->insert();
|
$result = $feedsub->insert();
|
||||||
if ($result === false) {
|
if ($result === false) {
|
||||||
throw new FeedDBException($feedsub);
|
throw new FeedDBException($feedsub);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $feedsub;
|
return $feedsub;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ensureHub will only do $this->update if !empty($this->id)
|
||||||
|
* because otherwise the object has not been created yet.
|
||||||
|
*/
|
||||||
|
public function ensureHub()
|
||||||
|
{
|
||||||
|
if ($this->sub_state !== 'inactive') {
|
||||||
|
throw new ServerException('Can only ensure WebSub hub for inactive (unsubscribed) feeds.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$discover = new FeedDiscovery();
|
||||||
|
$discover->discoverFromFeedURL($this->uri);
|
||||||
|
|
||||||
|
$huburi = $discover->getHubLink();
|
||||||
|
if (empty($huburi)) {
|
||||||
|
// Will be caught and treated with if statements in regards to
|
||||||
|
// fallback hub and feed polling (nohub) configuration.
|
||||||
|
throw new FeedSubNoHubException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$orig = !empty($this->id) ? clone($this) : null;
|
||||||
|
|
||||||
|
$this->huburi = $huburi;
|
||||||
|
|
||||||
|
if (!empty($this->id)) {
|
||||||
|
$this->update($orig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a subscription request to the hub for this feed.
|
* Send a subscription request to the hub for this feed.
|
||||||
* The hub will later send us a confirmation POST to /main/push/callback.
|
* The hub will later send us a confirmation POST to /main/push/callback.
|
||||||
@ -250,18 +296,27 @@ class FeedSub extends Managed_DataObject
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($this->huburi)) {
|
if (empty($this->huburi) && !common_config('feedsub', 'fallback_hub')) {
|
||||||
if (common_config('feedsub', 'fallback_hub')) {
|
/**
|
||||||
// No native hub on this feed?
|
* If the huburi is empty and we don't have a fallback hub,
|
||||||
// Use our fallback hub, which handles polling on our behalf.
|
* there is nowhere we can send an unsubscribe to.
|
||||||
} else if (common_config('feedsub', 'nohub')) {
|
*
|
||||||
// We need a feedpolling plugin (like FeedPoller) active so it will
|
* A plugin should handle the FeedSub above and set the proper state
|
||||||
// set the 'nohub' state to 'inactive' for us.
|
* if there is no hub. (instead of 'nohub' it should be 'inactive' if
|
||||||
return;
|
* the instance has enabled feed polling for feeds that don't publish
|
||||||
} else {
|
* PuSH/WebSub hubs. FeedPoller is a plugin which enables polling.
|
||||||
|
*
|
||||||
|
* Secondly, if we don't have the setting "nohub" enabled (i.e.)
|
||||||
|
* we're ready to poll ourselves, there is something odd with the
|
||||||
|
* database, such as a polling plugin that has been disabled.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!common_config('feedsub', 'nohub')) {
|
||||||
// TRANS: Server exception.
|
// TRANS: Server exception.
|
||||||
throw new ServerException(_m('Attempting to end PuSH subscription for feed with no hub.'));
|
throw new ServerException(_m('Attempting to end PuSH subscription for feed with no hub.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->doSubscribe('unsubscribe');
|
$this->doSubscribe('unsubscribe');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user