class RSSCloudNotifier {
+ const MAX_FAILURES = 3;
+
function challenge($endpoint, $feed)
{
$code = common_confirmation_code(128);
if ($status >= 200 && $status < 300) {
+ // NOTE: the spec says that the body must contain the string
+ // challenge. It doesn't say that the body must contain the
+ // challenge string ONLY, although that seems to be the way
+ // the other implementations have interpreted it.
+
if (strpos($body, $code) !== false) {
common_log(LOG_INFO, 'RSSCloud plugin - ' .
"success testing notify handler: $endpoint");
common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
$endpoint . ' that feed ' . $feed .
' has changed: got HTTP ' . $status);
- common_debug('body = ' . var_export($response->getBody(), true));
return false;
}
}
+ function notify($profile)
+ {
+ $feed = common_path('api/statuses/user_timeline/') .
+ $profile->nickname . '.rss';
+
+ $cloudSub = new RSSCloudSubscription();
+ $cloudSub->subscribed = $profile->id;
+
+ if ($cloudSub->find()) {
+ while ($cloudSub->fetch()) {
+ $result = $this->postUpdate($cloudSub->url, $feed);
+ if ($result == false) {
+ $this->handleFailure($cloudSub);
+ }
+ }
+ }
+ }
+
+ function handleFailure($cloudSub)
+ {
+ $failCnt = $cloudSub->failures + 1;
+
+ if ($failCnt == self::MAX_FAILURES) {
+
+ common_log(LOG_INFO,
+ 'Deleting RSSCloud subcription (max failure count reached), profile: ' .
+ $cloudSub->subscribed .
+ ' handler: ' .
+ $cloudSub->url);
+
+ // XXX: WTF! ->delete() doesn't work. Clearly, there are some issues with
+ // the DB_DataObject, or my understanding of it. Have to drop into SQL.
+
+ // $result = $cloudSub->delete();
+
+ $qry = 'DELETE from rsscloud_subscription' .
+ ' WHERE subscribed = ' . $cloudSub->subscribed .
+ ' AND url = \'' . $cloudSub->url . '\'';
+
+ $result = $cloudSub->query($qry);
+
+ if (!$result) {
+ common_log_db_error($cloudSub, 'DELETE', __FILE__);
+ common_log(LOG_ERR, 'Could not delete RSSCloud subscription.');
+ }
+
+ } else {
+
+ common_debug('Updating failure count on RSSCloud subscription. ' . $failCnt);
+
+ $failCnt = $cloudSub->failures + 1;
+
+ // XXX: ->update() not working either, gar!
+
+ $qry = 'UPDATE rsscloud_subscription' .
+ ' SET failures = ' . $failCnt .
+ ' WHERE subscribed = ' . $cloudSub->subscribed .
+ ' AND url = \'' . $cloudSub->url . '\'';
+
+ common_debug($qry);
+
+ $result = $cloudSub->query($qry);
+
+ if (!$result) {
+ common_log_db_error($cloudsub, 'UPDATE', __FILE__);
+ common_log(LOG_ERR, 'Could not update failure count on RSSCloud subscription');
+ }
+ }
+ }
+
}
<?php
-
/**
* Action to let RSSCloud aggregators request update notification when
* user profile feeds change.
$this->ip = $_SERVER['REMOTE_ADDR'];
$this->port = $this->arg('port');
$this->path = $this->arg('path');
+
+ if ($this->path[0] != '/') {
+ $this->path = '/' . $this->path;
+ }
+
$this->protocol = $this->arg('protocol');
$this->procedure = $this->arg('notifyProcedure');
$this->domain = $this->arg('domain');
$missing[] = 'port';
}
- $path = $this->arg('path');
-
if (empty($this->path)) {
$missing[] = 'path';
}
- $protocol = $this->arg('protocol');
-
if (empty($this->protocol)) {
$missing[] = 'protocol';
}
$this->saveSubscription($feed);
}
- // XXX: What to do about deleting stale subscriptions? 25 hours seems harsh.
- // WordPress doesn't ever remove subscriptions.
+ // XXX: What to do about deleting stale subscriptions?
+ // 25 hours seems harsh. WordPress doesn't ever remove
+ // subscriptions.
- $msg = 'Thanks for the registration. It worked. When the feed(s) update(s) we\'ll notify you. ' .
- ' Don\'t forget to re-register after 24 hours, your subscription will expire in 25.';
+ $msg = 'Thanks for the subscription. ' .
+ 'When the feed(s) update(s) we\'ll notify you.';
$this->showResult(true, $msg);
}
{
common_debug("RSSCloudPlugin - testNotificationHandler()");
+ $notifyUrl = $this->getNotifyUrl();
+
$notifier = new RSSCloudNotifier();
if (isset($this->domain)) {
// 'domain' param set, so we have to use GET and send a challenge
- $endpoint = 'http://' . $this->domain . ':' . $this->port . '/' . $this->path;
-
common_log(LOG_INFO, 'Testing notification handler with challenge: ' .
- $endpoint);
-
- return $notifier->challenge($endpoint, $feed);
+ $notifyUrl);
+ return $notifier->challenge($notifyUrl, $feed);
} else {
-
- $endpoint = 'http://' . $this->ip . ':' . $this->port . '/' . $this->path;
-
common_log(LOG_INFO, 'Testing notification handler: ' .
- $endpoint);
+ $notifyUrl);
- return $notifier->postUpdate($endpoint, $feed);
+ return $notifier->postUpdate($notifyUrl, $feed);
}
-
}
+ function getNotifyUrl()
+ {
+ if (isset($this->domain)) {
+ return 'http://' . $this->domain . ':' . $this->port . $this->path;
+ } else {
+ return 'http://' . $this->ip . ':' . $this->port . $this->path;
+ }
+ }
+
function userFromFeed($feed)
{
// We only do profile feeds
- // XXX: Add cloud element to RSS 1.0 feeds
+ // XXX: Add cloud element to RSS 1.0 feeds?
$path = common_path('api/statuses/user_timeline/');
$valid = '%^' . $path . '(?<nickname>.*)\.rss$%';
{
$user = $this->userFromFeed($feed);
- $sub = RSSCloudSubscription::getSubscription($user->id, $this->url);
+ $notifyUrl = $this->getNotifyUrl();
+
+ $sub = RSSCloudSubscription::getSubscription($user->id, $notifyUrl);
if ($sub) {
common_debug("Already subscribed to that!");
$sub = new RSSCloudSubscription();
$sub->subscribed = $user->id;
- $sub->url = $this->url;
+ $sub->url = $notifyUrl;
$sub->created = common_sql_now();
- // auto timestamp doesn't seem to work for me
-
- // $sub->modified = common_sql_now();
-
if (!$sub->insert()) {
common_log_db_error($sub, 'INSERT', __FILE__);
return false;
}
- DB_DataObject::debugLevel();
}
return true;