Add a table and DB_DataObject class for storing cloud subscriptions
This commit is contained in:
		| @@ -95,14 +95,15 @@ class RSSCloudPlugin extends Plugin | |||||||
|     { |     { | ||||||
|         switch ($cls) |         switch ($cls) | ||||||
|         { |         { | ||||||
|  |          case 'RSSCloudSubscription': | ||||||
|  |             include_once(INSTALLDIR . '/plugins/RSSCloud/RSSCloudSubscription.php'); | ||||||
|  |             return false; | ||||||
|          case 'RSSCloudNotifier': |          case 'RSSCloudNotifier': | ||||||
|             require_once(INSTALLDIR . '/plugins/RSSCloud/RSSCloudNotifier.php'); |             include_once(INSTALLDIR . '/plugins/RSSCloud/RSSCloudNotifier.php'); | ||||||
|             return false; |             return false; | ||||||
|          case 'RSSCloudRequestNotifyAction': |          case 'RSSCloudRequestNotifyAction': | ||||||
|          case 'LoggingAggregatorAction': |          case 'LoggingAggregatorAction': | ||||||
|             common_debug(mb_substr($cls, 0, -6) . '.php'); |             include_once(INSTALLDIR . '/plugins/RSSCloud/' . mb_substr($cls, 0, -6) . '.php'); | ||||||
|             require_once(INSTALLDIR . '/plugins/RSSCloud/' . mb_substr($cls, 0, -6) . '.php'); |  | ||||||
|             return false; |             return false; | ||||||
|          default: |          default: | ||||||
|             return true; |             return true; | ||||||
| @@ -177,5 +178,23 @@ class RSSCloudPlugin extends Plugin | |||||||
|                 $notice->is_local == Notice::LOCAL_NONPUBLIC); |                 $notice->is_local == Notice::LOCAL_NONPUBLIC); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     function onCheckSchema() { | ||||||
|  |         $schema = Schema::get(); | ||||||
|  |         $schema->ensureTable('rsscloud_subscription', | ||||||
|  |                              array(new ColumnDef('subscribed', 'integer', | ||||||
|  |                                                  null, false, 'PRI'), | ||||||
|  |                                    new ColumnDef('url', 'varchar', | ||||||
|  |                                                  '255', false, 'PRI'), | ||||||
|  |                                    new ColumnDef('failures', 'integer', | ||||||
|  |                                                  null, false, 'MUL'), | ||||||
|  |                                    new ColumnDef('created', 'datetime', | ||||||
|  |                                                  null, false), | ||||||
|  |                                    new ColumnDef('modified', 'timestamp') | ||||||
|  |                                   ) | ||||||
|  |                             ); | ||||||
|  |          return true; | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -51,9 +51,9 @@ class RSSCloudRequestNotifyAction extends Action | |||||||
|         $this->path      = $this->arg('path'); |         $this->path      = $this->arg('path'); | ||||||
|         $this->protocol  = $this->arg('protocol'); |         $this->protocol  = $this->arg('protocol'); | ||||||
|         $this->procedure = $this->arg('notifyProcedure'); |         $this->procedure = $this->arg('notifyProcedure'); | ||||||
|         $this->feeds     = $this->getFeeds(); |         $this->domain    = $this->arg('domain'); | ||||||
|          |          | ||||||
|         $this->subscriber_url = 'http://' . $this->ip . ':' . $this->port . $this->path; |         $this->feeds     = $this->getFeeds(); | ||||||
|  |  | ||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
| @@ -102,7 +102,26 @@ class RSSCloudRequestNotifyAction extends Action | |||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         $endpoint = $ip . ':' . $port . $path; |         // We have to validate everything before saving anything. | ||||||
|  |         // We only return one success or failure no matter how  | ||||||
|  |         // many feeds the subscriber is trying to subscribe to | ||||||
|  |  | ||||||
|  |         foreach ($this->feeds as $feed) { | ||||||
|  |              | ||||||
|  |             if (!$this->validateFeed($feed)) { | ||||||
|  |                 $msg = 'Feed subscription failed - Not a valid feed.'; | ||||||
|  |                 $this->showResult(false, $msg); | ||||||
|  |                 return; | ||||||
|  |             } | ||||||
|  |              | ||||||
|  |             if (!$this->testNotificationHandler($feed)) { | ||||||
|  |                 $msg = 'Feed subscription failed - ' . | ||||||
|  |                 'notification handler doesn\'t respond correctly.'; | ||||||
|  |                 $this->showResult(false, $msg); | ||||||
|  |                 return;   | ||||||
|  |             } | ||||||
|  |              | ||||||
|  |         } | ||||||
|  |  | ||||||
|         foreach ($this->feeds as $feed) { |         foreach ($this->feeds as $feed) { | ||||||
|             $this->saveSubscription($feed); |             $this->saveSubscription($feed); | ||||||
| @@ -117,20 +136,25 @@ class RSSCloudRequestNotifyAction extends Action | |||||||
|         $this->showResult(true, $msg);         |         $this->showResult(true, $msg);         | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     function validateFeed($feed) | ||||||
|  |     { | ||||||
|  |         $user = $this->userFromFeed($feed); | ||||||
|  |  | ||||||
|  |         if (empty($user)) { | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return true; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|     function getFeeds() |     function getFeeds() | ||||||
|     { |     { | ||||||
|         $feeds = array(); |         $feeds = array(); | ||||||
|              |              | ||||||
|         foreach ($this->args as $key => $feed ) { |         while (list($key, $feed) = each ($this->args)) {             | ||||||
|             if (preg_match('|url\d+|', $key)) { |             if (preg_match('/^url\d*$/', $key)) { | ||||||
|  |  | ||||||
|                 if ($this->testFeed($feed)) { |  | ||||||
|                 $feeds[] = $feed; |                 $feeds[] = $feed; | ||||||
|                 } else { |  | ||||||
|                     $msg = 'RSSCloud Plugin - ' . $this->ip . ' tried to subscribe ' . |  | ||||||
|                            'to a non-existent feed: ' . $feed; |  | ||||||
|                     common_log(LOG_WARN, $msg); |  | ||||||
|                 } |  | ||||||
|             }  |             }  | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -139,33 +163,32 @@ class RSSCloudRequestNotifyAction extends Action | |||||||
|  |  | ||||||
|     function testNotificationHandler($feed) |     function testNotificationHandler($feed) | ||||||
|     {         |     {         | ||||||
|  |         common_debug("RSSCloudPlugin - testNotificationHandler()"); | ||||||
|  |          | ||||||
|         $notifier = new RSSCloudNotifier(); |         $notifier = new RSSCloudNotifier(); | ||||||
|         return $notifier->postUpdate($endpoint, $feed); |          | ||||||
|  |         if (isset($this->domain)) { | ||||||
|  |              | ||||||
|  |             //get | ||||||
|  |              | ||||||
|  |             $this->url = 'http://' . $this->domain . ':' . $this->port . '/' . $this->path; | ||||||
|  |              | ||||||
|  |             common_debug('domain set need to send challenge'); | ||||||
|  |              | ||||||
|  |         } else { | ||||||
|  |              | ||||||
|  |             //post | ||||||
|  |              | ||||||
|  |             $this->url = 'http://' . $this->ip . ':' . $this->port . '/' . $this->path; | ||||||
|  |              | ||||||
|  |             //return $notifier->postUpdate($endpoint, $feed); | ||||||
|  |  | ||||||
|         }    |         }    | ||||||
|  |  | ||||||
|     // returns valid user or false |  | ||||||
|     function testFeed($feed) |  | ||||||
|     { |  | ||||||
|         $user = $this->userFromFeed($feed); |  | ||||||
|  |  | ||||||
|         if (!empty($user)) { |  | ||||||
|  |  | ||||||
|             common_debug("Valid feed: $feed"); |  | ||||||
|  |  | ||||||
|             // OK, so this is a valid profile feed url, now let's see if the |  | ||||||
|             // other system reponds to our notifications before we |  | ||||||
|             // add the sub... |  | ||||||
|  |  | ||||||
|             if ($this->testNotificationHandler($feed)) { |  | ||||||
|         return true; |         return true; | ||||||
|             } |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|         return false; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     // this actually does the validating and figuring out the |  | ||||||
|     // user, which it returns |  | ||||||
|     function userFromFeed($feed) |     function userFromFeed($feed) | ||||||
|     { |     { | ||||||
|         // We only do profile feeds |         // We only do profile feeds | ||||||
| @@ -185,45 +208,39 @@ class RSSCloudRequestNotifyAction extends Action | |||||||
|  |  | ||||||
|     function saveSubscription($feed) |     function saveSubscription($feed) | ||||||
|     { |     { | ||||||
|         // check to see if we already have a profile for this subscriber |         $user = $this->userFromFeed($feed); | ||||||
|          |          | ||||||
|         $other = Remote_profile::staticGet('uri', $this->subscriber_url); |         common_debug('user = ' . $user->id); | ||||||
|          |          | ||||||
|         if ($other === false) { |         $sub = RSSCloudSubscription::getSubscription($user->id, $this->url); | ||||||
|             $other->saveProfile(); |  | ||||||
|         } |  | ||||||
|          |          | ||||||
|         $user = userFromFeed($feed); |         if ($sub) { | ||||||
|  |             common_debug("already subscribed to that!"); | ||||||
|         $result = subs_subscribe_to($user, $other); |  | ||||||
|  |  | ||||||
|         if ($result != true) { |  | ||||||
|             $msg = "RSSPlugin - got '$result' trying to subscribe " . |  | ||||||
|                    "$this->subscriber_url to $user->nickname" . "'s profile feed."; |  | ||||||
|             common_log(LOG_WARN, $msg); |  | ||||||
|         } else { |         } else { | ||||||
|             $msg = 'RSSCloud plugin - subscribe: ' . $this->subscriber_url . |             common_debug('No feed for user ' . $user->id . ' notify: ' . $this->url); | ||||||
|                    ' subscribed to ' . $feed; |  | ||||||
|  |  | ||||||
|             common_log(LOG_INFO, $msg); |  | ||||||
|         } |  | ||||||
|         } |         } | ||||||
|          |          | ||||||
|     function saveProfile() |         common_debug('RSSPlugin - saveSubscription'); | ||||||
|     { |         // turn debugging high | ||||||
|         common_debug("Saving remote profile for $this->subscriber_url"); |         DB_DataObject::debugLevel(5); | ||||||
|          |          | ||||||
|         // XXX: We need to add a field to Remote_profile to indicate the kind |         $sub = new RSSCloudSubscription(); | ||||||
|         // of remote profile?  i.e: OMB, RSSCloud, PuSH, Twitter |  | ||||||
|          |          | ||||||
|         $remote                = new Remote_profile(); |         $sub->subscribed = $user->id; | ||||||
|         $remote->uri           = $this->subscriber_url; |         $sub->url        = $this->url; | ||||||
|         $remote->postnoticeurl = $this->subscriber_url; |         $sub->created    = common_sql_now(); | ||||||
|         $remote->created       = DB_DataObject_Cast::dateTime(); |  | ||||||
|          |          | ||||||
|         if (!$remote->insert()) { |         // auto timestamp doesn't seem to work for me | ||||||
|             throw new Exception(_('RSSCloud plugin - Error inserting remote profile!')); |          | ||||||
|  |         $sub->modified   = common_sql_now(); | ||||||
|  |          | ||||||
|  |         if (!$sub->insert()) { | ||||||
|  |             common_log_db_error($sub, 'INSERT', __FILE__); | ||||||
|  |             return false; | ||||||
|         } |         } | ||||||
|  |         DB_DataObject::debugLevel(); | ||||||
|  |          | ||||||
|  |         return true; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     function showResult($success, $msg) |     function showResult($success, $msg) | ||||||
|   | |||||||
							
								
								
									
										82
									
								
								plugins/RSSCloud/RSSCloudSubscription.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								plugins/RSSCloud/RSSCloudSubscription.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,82 @@ | |||||||
|  | <?php | ||||||
|  | /* | ||||||
|  |  * StatusNet - the distributed open-source microblogging tool | ||||||
|  |  * Copyright (C) 2008, 2009, StatusNet, Inc. | ||||||
|  |  * | ||||||
|  |  * This program is free software: you can redistribute it and/or modify | ||||||
|  |  * it under the terms of the GNU Affero General Public License as published by | ||||||
|  |  * the Free Software Foundation, either version 3 of the License, or | ||||||
|  |  * (at your option) any later version. | ||||||
|  |  * | ||||||
|  |  * This program is distributed in the hope that it will be useful, | ||||||
|  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
|  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||||
|  |  * GNU Affero General Public License for more details. | ||||||
|  |  * | ||||||
|  |  * You should have received a copy of the GNU Affero General Public License | ||||||
|  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | if (!defined('STATUSNET') && !defined('LACONICA')) { | ||||||
|  |     exit(1); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Table Definition for rsscloud_subscription | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | require_once INSTALLDIR . '/classes/Memcached_DataObject.php'; | ||||||
|  |  | ||||||
|  | class RSSCloudSubscription extends Memcached_DataObject { | ||||||
|  |  | ||||||
|  |     var $__table='rsscloud_subscription'; // table name | ||||||
|  |     var $subscribed;                      // int    primary key user id | ||||||
|  |     var $url;                             // string primary key | ||||||
|  |     var $failures;                        // int | ||||||
|  |     var $created;                         // datestamp() | ||||||
|  |     var $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP | ||||||
|  |  | ||||||
|  |     function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('DataObjects_Grp',$k,$v); } | ||||||
|  |  | ||||||
|  |     function table() | ||||||
|  |     { | ||||||
|  |         global $_DB_DATAOBJECT; | ||||||
|  |         $dbtype = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype']; | ||||||
|  |  | ||||||
|  |         $cols = array( | ||||||
|  |             'subscribed'       => DB_DATAOBJECT_INT, | ||||||
|  |             'url'              => DB_DATAOBJECT_STR, | ||||||
|  |             'failures'         => DB_DATAOBJECT_INT, | ||||||
|  |             'created'          => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME, | ||||||
|  |             'modified'         => ($dbtype == 'mysql') ? | ||||||
|  |                 DB_DATAOBJECT_MYSQLTIMESTAMP : | ||||||
|  |                 DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |         // common_debug(var_export($cols, true)); | ||||||
|  |         return $cols; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     function keys() | ||||||
|  |     { | ||||||
|  |         return array('subscribed', 'url'); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     static function getSubscription($subscribed, $url) | ||||||
|  |     { | ||||||
|  |         $sub = new RSSCloudSubscription(); | ||||||
|  |         $sub->whereAdd("subscribed = $subscribed"); | ||||||
|  |         $sub->whereAdd("url = $url"); | ||||||
|  |         $sub->limit(1); | ||||||
|  |  | ||||||
|  |         if ($sub->find()) { | ||||||
|  |             $sub->fetch(); | ||||||
|  |             return $sub; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | } | ||||||
|  | ?> | ||||||
		Reference in New Issue
	
	Block a user