]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/api.php
add geo output to statuses in json, xml, atom, rss in API
[quix0rs-gnu-social.git] / lib / api.php
index 0801d282396d55362041621ec28fe754a029e1d3..5e66639c4f64075dfd78d632dae6614e413762e6 100644 (file)
  *
  * @category  API
  * @package   StatusNet
+ * @author    Craig Andrews <candrews@integralblue.com>
+ * @author    Dan Moore <dan@moore.cx>
+ * @author    Evan Prodromou <evan@status.net>
+ * @author    Jeffery To <jeffery.to@gmail.com>
+ * @author    Toby Inkster <mail@tobyinkster.co.uk>
  * @author    Zach Copley <zach@status.net>
  * @copyright 2009 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
@@ -36,6 +41,11 @@ if (!defined('STATUSNET')) {
  *
  * @category API
  * @package  StatusNet
+ * @author   Craig Andrews <candrews@integralblue.com>
+ * @author   Dan Moore <dan@moore.cx>
+ * @author   Evan Prodromou <evan@status.net>
+ * @author   Jeffery To <jeffery.to@gmail.com>
+ * @author   Toby Inkster <mail@tobyinkster.co.uk>
  * @author   Zach Copley <zach@status.net>
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link     http://status.net/
@@ -50,7 +60,7 @@ class ApiAction extends Action
      var $max_id   = null;
      var $since_id = null;
      var $since    = null;
-     
+
     /**
      * Initialization.
      *
@@ -62,14 +72,14 @@ class ApiAction extends Action
     function prepare($args)
     {
         parent::prepare($args);
-        
+
         $this->format   = $this->arg('format');
         $this->page     = (int)$this->arg('page', 1);
         $this->count    = (int)$this->arg('count', 20);
         $this->max_id   = (int)$this->arg('max_id', 0);
         $this->since_id = (int)$this->arg('since_id', 0);
         $this->since    = $this->arg('since');
-        
+
         return true;
     }
 
@@ -124,11 +134,28 @@ class ApiAction extends Action
         $twitter_user['protected'] = false; # not supported by StatusNet yet
         $twitter_user['followers_count'] = $profile->subscriberCount();
 
-        // To be supported soon...
-        $twitter_user['profile_background_color'] = '';
-        $twitter_user['profile_text_color'] = '';
-        $twitter_user['profile_link_color'] = '';
-        $twitter_user['profile_sidebar_fill_color'] = '';
+        $defaultDesign = Design::siteDesign();
+        $design        = null;
+        $user          = $profile->getUser();
+
+        // Note: some profiles don't have an associated user
+
+        if (!empty($user)) {
+            $design = $user->getDesign();
+        }
+
+        if (empty($design)) {
+            $design = $defaultDesign;
+        }
+
+        $color = Design::toWebColor(empty($design->backgroundcolor) ? $defaultDesign->backgroundcolor : $design->backgroundcolor);
+        $twitter_user['profile_background_color'] = ($color == null) ? '' : '#'.$color->hexValue();
+        $color = Design::toWebColor(empty($design->textcolor) ? $defaultDesign->textcolor : $design->textcolor);
+        $twitter_user['profile_text_color'] = ($color == null) ? '' : '#'.$color->hexValue();
+        $color = Design::toWebColor(empty($design->linkcolor) ? $defaultDesign->linkcolor : $design->linkcolor);
+        $twitter_user['profile_link_color'] = ($color == null) ? '' : '#'.$color->hexValue();
+        $color = Design::toWebColor(empty($design->sidebarcolor) ? $defaultDesign->sidebarcolor : $design->sidebarcolor);
+        $twitter_user['profile_sidebar_fill_color'] = ($color == null) ? '' : '#'.$color->hexValue();
         $twitter_user['profile_sidebar_border_color'] = '';
 
         $twitter_user['friends_count'] = $profile->subscriptionCount();
@@ -137,9 +164,6 @@ class ApiAction extends Action
 
         $twitter_user['favourites_count'] = $profile->faveCount(); // British spelling!
 
-        // Need to pull up the user for some of this
-        $user = User::staticGet($profile->id);
-
         $timezone = 'UTC';
 
         if ($user->timezone) {
@@ -213,6 +237,15 @@ class ApiAction extends Action
         $twitter_status['in_reply_to_screen_name'] =
             ($replier_profile) ? $replier_profile->nickname : null;
 
+        if (isset($notice->lat) && isset($notice->lon)) {
+            // This is the format that GeoJSON expects stuff to be in
+            $twitter_status['geo'] = array('type' => 'Point',
+                                           'coordinates' => array((float) $notice->lat,
+                                                                  (float) $notice->lon));
+        } else {
+            $twitter_status['geo'] = null;
+        }
+
         if (isset($this->auth_user)) {
             $twitter_status['favorited'] = $this->auth_user->hasFave($notice);
         } else {
@@ -337,10 +370,19 @@ class ApiAction extends Action
         $entry['pubDate'] = common_date_rfc2822($notice->created);
         $entry['guid'] = $entry['link'];
 
+        if (isset($notice->lat) && isset($notice->lon)) {
+            // This is the format that GeoJSON expects stuff to be in.
+            // showGeoRSS() below uses it for XML output, so we reuse it
+            $entry['geo'] = array('type' => 'Point',
+                                  'coordinates' => array((float) $notice->lat,
+                                                         (float) $notice->lon));
+        } else {
+            $entry['geo'] = null;
+        }
+
         return $entry;
     }
 
-
     function twitterRelationshipArray($source, $target)
     {
         $relationship = array();
@@ -416,6 +458,9 @@ class ApiAction extends Action
             case 'attachments':
                 $this->showXmlAttachments($twitter_status['attachments']);
                 break;
+            case 'geo':
+                $this->showGeoRSS($value);
+                break;
             default:
                 $this->element($element, null, $value);
             }
@@ -459,6 +504,18 @@ class ApiAction extends Action
         }
     }
 
+    function showGeoRSS($geo)
+    {
+        if (empty($geo)) {
+            // empty geo element
+            $this->element('geo');
+        } else {
+            $this->elementStart('geo', array('xmlns:georss' => 'http://www.georss.org/georss'));
+            $this->element('georss:point', null, $geo['coordinates'][0] . ' ' . $geo['coordinates'][1]);
+            $this->elementEnd('geo');
+        }
+    }
+
     function showTwitterRssItem($entry)
     {
         $this->elementStart('item');
@@ -480,6 +537,7 @@ class ApiAction extends Action
             }
         }
 
+        $this->showGeoRSS($entry['geo']);
         $this->elementEnd('item');
     }
 
@@ -504,7 +562,6 @@ class ApiAction extends Action
         $this->endDocument('json');
     }
 
-
     function showXmlTimeline($notice)
     {
 
@@ -624,7 +681,6 @@ class ApiAction extends Action
         $this->endTwitterRss();
     }
 
-
     function showTwitterAtomEntry($entry)
     {
         $this->elementStart('entry');
@@ -894,7 +950,7 @@ class ApiAction extends Action
         $this->endDocument('json');
     }
 
-    function show_single_xml_group($group)
+    function showSingleXmlGroup($group)
     {
         $this->initDocument('xml');
         $twitter_group = $this->twitterGroupArray($group);