summaryrefslogtreecommitdiff
path: root/plugins/OStatus/classes/HubSub.php
diff options
context:
space:
mode:
authorZach Copley <zach@status.net>2010-02-22 01:23:24 -0800
committerZach Copley <zach@status.net>2010-02-22 01:23:24 -0800
commit35be39e30eacda1b0425a2ae9f8e58cd0867d157 (patch)
treedf86f17a666e5d0bb775dbcef0fc4eb5b1c777f3 /plugins/OStatus/classes/HubSub.php
parent47300a2ae9a51108fbf59a57cf5ab6e8867b54a6 (diff)
parent17ed30dffc1c05259baf2f0387089547e39684d7 (diff)
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing
Diffstat (limited to 'plugins/OStatus/classes/HubSub.php')
-rw-r--r--plugins/OStatus/classes/HubSub.php123
1 files changed, 72 insertions, 51 deletions
diff --git a/plugins/OStatus/classes/HubSub.php b/plugins/OStatus/classes/HubSub.php
index a81de68e6..eae2928c3 100644
--- a/plugins/OStatus/classes/HubSub.php
+++ b/plugins/OStatus/classes/HubSub.php
@@ -30,11 +30,11 @@ class HubSub extends Memcached_DataObject
public $topic;
public $callback;
public $secret;
- public $challenge;
public $lease;
public $sub_start;
public $sub_end;
public $created;
+ public $modified;
public /*static*/ function staticGet($topic, $callback)
{
@@ -61,11 +61,11 @@ class HubSub extends Memcached_DataObject
'topic' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
'callback' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
'secret' => DB_DATAOBJECT_STR,
- 'challenge' => DB_DATAOBJECT_STR,
'lease' => DB_DATAOBJECT_INT,
'sub_start' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
'sub_end' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
- 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
+ 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
+ 'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
}
static function schemaDef()
@@ -82,8 +82,6 @@ class HubSub extends Memcached_DataObject
255, false),
new ColumnDef('secret', 'text',
null, true),
- new ColumnDef('challenge', 'varchar',
- 32, true),
new ColumnDef('lease', 'int',
null, true),
new ColumnDef('sub_start', 'datetime',
@@ -91,6 +89,8 @@ class HubSub extends Memcached_DataObject
new ColumnDef('sub_end', 'datetime',
null, true),
new ColumnDef('created', 'datetime',
+ null, false),
+ new ColumnDef('modified', 'datetime',
null, false));
}
@@ -148,85 +148,106 @@ class HubSub extends Memcached_DataObject
}
/**
- * Send a verification ping to subscriber
+ * Schedule a future verification ping to the subscriber.
+ * If queues are disabled, will be immediate.
+ *
+ * @param string $mode 'subscribe' or 'unsubscribe'
+ * @param string $token hub.verify_token value, if provided by client
+ */
+ function scheduleVerify($mode, $token=null, $retries=null)
+ {
+ if ($retries === null) {
+ $retries = intval(common_config('ostatus', 'hub_retries'));
+ }
+ $data = array('sub' => clone($this),
+ 'mode' => $mode,
+ 'token' => $token,
+ 'retries' => $retries);
+ $qm = QueueManager::get();
+ $qm->enqueue($data, 'hubverify');
+ }
+
+ /**
+ * Send a verification ping to subscriber, and if confirmed apply the changes.
+ * This may create, update, or delete the database record.
+ *
* @param string $mode 'subscribe' or 'unsubscribe'
* @param string $token hub.verify_token value, if provided by client
+ * @throws ClientException on failure
*/
function verify($mode, $token=null)
{
assert($mode == 'subscribe' || $mode == 'unsubscribe');
- // Is this needed? data object fun...
- $clone = clone($this);
- $clone->challenge = common_good_rand(16);
- $clone->update($this);
- $this->challenge = $clone->challenge;
- unset($clone);
-
+ $challenge = common_good_rand(32);
$params = array('hub.mode' => $mode,
'hub.topic' => $this->topic,
- 'hub.challenge' => $this->challenge);
+ 'hub.challenge' => $challenge);
if ($mode == 'subscribe') {
$params['hub.lease_seconds'] = $this->lease;
}
if ($token !== null) {
$params['hub.verify_token'] = $token;
}
- $url = $this->callback . '?' . http_build_query($params, '', '&'); // @fixme ugly urls
- try {
- $request = new HTTPClient();
- $response = $request->get($url);
- $status = $response->getStatus();
-
- if ($status >= 200 && $status < 300) {
- $fail = false;
- } else {
- // @fixme how can we schedule a second attempt?
- // Or should we?
- $fail = "Returned HTTP $status";
- }
- } catch (Exception $e) {
- $fail = $e->getMessage();
+ // Any existing query string parameters must be preserved
+ $url = $this->callback;
+ if (strpos('?', $url) !== false) {
+ $url .= '&';
+ } else {
+ $url .= '?';
}
- if ($fail) {
- // @fixme how can we schedule a second attempt?
- // or save a fail count?
- // Or should we?
- common_log(LOG_ERR, "Failed to verify $mode for $this->topic at $this->callback: $fail");
- return false;
+ $url .= http_build_query($params, '', '&');
+
+ $request = new HTTPClient();
+ $response = $request->get($url);
+ $status = $response->getStatus();
+
+ if ($status >= 200 && $status < 300) {
+ common_log(LOG_INFO, "Verified $mode of $this->callback:$this->topic");
} else {
- if ($mode == 'subscribe') {
- // Establish or renew the subscription!
- // This seems unnecessary... dataobject fun!
- $clone = clone($this);
- $clone->challenge = null;
- $clone->setLease($this->lease);
- $clone->update($this);
- unset($clone);
+ throw new ClientException("Hub subscriber verification returned HTTP $status");
+ }
- $this->challenge = null;
- $this->setLease($this->lease);
- common_log(LOG_ERR, "Verified $mode of $this->callback:$this->topic for $this->lease seconds");
- } else if ($mode == 'unsubscribe') {
- common_log(LOG_ERR, "Verified $mode of $this->callback:$this->topic");
- $this->delete();
+ $old = HubSub::staticGet($this->topic, $this->callback);
+ if ($mode == 'subscribe') {
+ if ($old) {
+ $this->update($old);
+ } else {
+ $ok = $this->insert();
+ }
+ } else if ($mode == 'unsubscribe') {
+ if ($old) {
+ $old->delete();
+ } else {
+ // That's ok, we're already unsubscribed.
}
- return true;
}
}
/**
* Insert wrapper; transparently set the hash key from topic and callback columns.
- * @return boolean success
+ * @return mixed success
*/
function insert()
{
$this->hashkey = self::hashkey($this->topic, $this->callback);
+ $this->created = common_sql_now();
+ $this->modified = common_sql_now();
return parent::insert();
}
/**
+ * Update wrapper; transparently update modified column.
+ * @return boolean success
+ */
+ function update($old=null)
+ {
+ $this->modified = common_sql_now();
+ return parent::update($old);
+ }
+
+ /**
* Schedule delivery of a 'fat ping' to the subscriber's callback
* endpoint. If queues are disabled, this will run immediately.
*