OStatus: garbage collect unused PuSH subscriptions when the last local subscriber unsubs

This commit is contained in:
Brion Vibber 2010-02-11 00:09:20 +00:00
parent 4ae760cb62
commit 71151b2583
3 changed files with 48 additions and 16 deletions

View File

@ -235,9 +235,26 @@ class OStatusPlugin extends Plugin
} }
/**
* Garbage collect unused feeds on unsubscribe
*/
function onEndUnsubscribe($user, $other)
{
$feed = Feedinfo::staticGet('profile_id', $other->id);
if ($feed) {
$sub = new Subscription();
$sub->subscribed = $other->id;
$sub->limit(1);
if (!$sub->find(true)) {
common_log(LOG_INFO, "Unsubscribing from now-unused feed $feed->feeduri on hub $feed->huburi");
$feed->unsubscribe();
}
}
return true;
}
function onCheckSchema() { function onCheckSchema() {
// warning: the autoincrement doesn't seem to set.
// alter table feedinfo change column id id int(11) not null auto_increment;
$schema = Schema::get(); $schema = Schema::get();
$schema->ensureTable('feedinfo', Feedinfo::schemaDef()); $schema->ensureTable('feedinfo', Feedinfo::schemaDef());
$schema->ensureTable('hubsub', HubSub::schemaDef()); $schema->ensureTable('hubsub', HubSub::schemaDef());

View File

@ -91,14 +91,19 @@ class PushCallbackAction extends Action
#} #}
// OK! // OK!
common_log(LOG_INFO, __METHOD__ . ': sub confirmed'); if ($mode == 'subscribe') {
$feedinfo->sub_start = common_sql_date(time()); common_log(LOG_INFO, __METHOD__ . ': sub confirmed');
if ($lease_seconds > 0) { $feedinfo->sub_start = common_sql_date(time());
$feedinfo->sub_end = common_sql_date(time() + $lease_seconds); if ($lease_seconds > 0) {
$feedinfo->sub_end = common_sql_date(time() + $lease_seconds);
} else {
$feedinfo->sub_end = null;
}
$feedinfo->update();
} else { } else {
$feedinfo->sub_end = null; common_log(LOG_INFO, __METHOD__ . ": unsub confirmed; deleting sub record for $topic");
$feedinfo->delete();
} }
$feedinfo->update();
print $challenge; print $challenge;
} }

View File

@ -112,9 +112,9 @@ class Feedinfo extends Memcached_DataObject
/*extra*/ null, /*extra*/ null,
/*auto_increment*/ true), /*auto_increment*/ true),
new ColumnDef('profile_id', 'integer', new ColumnDef('profile_id', 'integer',
null, true), null, true, 'UNI'),
new ColumnDef('group_id', 'integer', new ColumnDef('group_id', 'integer',
null, true), null, true, 'UNI'),
new ColumnDef('feeduri', 'varchar', new ColumnDef('feeduri', 'varchar',
255, false, 'UNI'), 255, false, 'UNI'),
new ColumnDef('homeuri', 'varchar', new ColumnDef('homeuri', 'varchar',
@ -160,7 +160,7 @@ class Feedinfo extends Memcached_DataObject
function keyTypes() function keyTypes()
{ {
return array('id' => 'K', 'feeduri' => 'U'); // @fixme we'll need a profile_id key at least return array('id' => 'K', 'profile_id' => 'U', 'group_id' => 'U', 'feeduri' => 'U');
} }
function sequenceKey() function sequenceKey()
@ -261,11 +261,11 @@ class Feedinfo extends Memcached_DataObject
/** /**
* 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 /feedsub/callback. * The hub will later send us a confirmation POST to /main/push/callback.
* *
* @return bool true on success, false on failure * @return bool true on success, false on failure
*/ */
public function subscribe() public function subscribe($mode='subscribe')
{ {
if (common_config('feedsub', 'nohub')) { if (common_config('feedsub', 'nohub')) {
// Fake it! We're just testing remote feeds w/o hubs. // Fake it! We're just testing remote feeds w/o hubs.
@ -278,7 +278,7 @@ class Feedinfo extends Memcached_DataObject
try { try {
$callback = common_local_url('pushcallback', array('feed' => $this->id)); $callback = common_local_url('pushcallback', array('feed' => $this->id));
$headers = array('Content-Type: application/x-www-form-urlencoded'); $headers = array('Content-Type: application/x-www-form-urlencoded');
$post = array('hub.mode' => 'subscribe', $post = array('hub.mode' => $mode,
'hub.callback' => $callback, 'hub.callback' => $callback,
'hub.verify' => 'async', 'hub.verify' => 'async',
'hub.verify_token' => $this->verify_token, 'hub.verify_token' => $this->verify_token,
@ -308,6 +308,16 @@ class Feedinfo extends Memcached_DataObject
} }
} }
/**
* Send an unsubscription 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
*/
public function unsubscribe() {
return $this->subscribe('unsubscribe');
}
/** /**
* Read and post notices for updates from the feed. * Read and post notices for updates from the feed.
* Currently assumes that all items in the feed are new, * Currently assumes that all items in the feed are new,