]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
add statuses/retweets to API
authorEvan Prodromou <evan@status.net>
Sat, 12 Dec 2009 19:46:24 +0000 (14:46 -0500)
committerEvan Prodromou <evan@status.net>
Sat, 12 Dec 2009 19:46:24 +0000 (14:46 -0500)
actions/apistatusesretweets.php [new file with mode: 0644]
classes/Notice.php
lib/router.php

diff --git a/actions/apistatusesretweets.php b/actions/apistatusesretweets.php
new file mode 100644 (file)
index 0000000..c54a374
--- /dev/null
@@ -0,0 +1,116 @@
+<?php
+/**
+ * StatusNet, the distributed open-source microblogging tool
+ *
+ * Show up to 100 repeats of a notice
+ *
+ * PHP version 5
+ *
+ * LICENCE: 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/>.
+ *
+ * @category  API
+ * @package   StatusNet
+ * @author    Evan Prodromou <evan@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
+ * @link      http://status.net/
+ */
+
+if (!defined('STATUSNET')) {
+    exit(1);
+}
+
+require_once INSTALLDIR . '/lib/apiauth.php';
+require_once INSTALLDIR . '/lib/mediafile.php';
+
+/**
+ * Show up to 100 repeats of a notice
+ *
+ * @category API
+ * @package  StatusNet
+ * @author   Evan Prodromou <evan@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/
+ */
+
+class ApiStatusesRetweetsAction extends ApiAuthAction
+{
+    const MAXCOUNT = 100;
+
+    var $original = null;
+    var $cnt      = self::MAXCOUNT;
+
+    /**
+     * Take arguments for running
+     *
+     * @param array $args $_REQUEST args
+     *
+     * @return boolean success flag
+     *
+     */
+
+    function prepare($args)
+    {
+        parent::prepare($args);
+
+        $id = $this->trimmed('id');
+
+        $this->original = Notice::staticGet('id', $id);
+
+        if (empty($this->original)) {
+            $this->clientError(_('No such notice'),
+                               400, $this->format);
+            return false;
+        }
+
+        $cnt = $this->trimmed('count');
+
+        if (empty($cnt) || !is_integer($cnt)) {
+            $cnt = 100;
+        } else {
+            $this->cnt = min((int)$cnt, self::MAXCOUNT);
+        }
+
+        return true;
+    }
+
+    /**
+     * Handle the request
+     *
+     * Make a new notice for the update, save it, and show it
+     *
+     * @param array $args $_REQUEST data (unused)
+     *
+     * @return void
+     */
+
+    function handle($args)
+    {
+        parent::handle($args);
+
+        $strm = $this->original->repeatStream($this->cnt);
+
+        switch ($this->format) {
+        case 'xml':
+            $this->showXmlTimeline($strm);
+            break;
+        case 'json':
+            $this->showJsonTimeline($strm);
+            break;
+        default:
+            $this->clientError(_('API method not found!'), $code = 404);
+            break;
+        }
+    }
+}
index 82753fbdd69fe77b7ba966029671e6dc56dd8c2c..ec80f763ffd9b0aa5770b7171c5bf73e0a351c4c 100644 (file)
@@ -441,10 +441,23 @@ class Notice extends Memcached_DataObject
         $this->blowTagCache($blowLast);
         $this->blowGroupCache($blowLast);
         $this->blowConversationCache($blowLast);
+        $this->blowRepeatCache();
         $profile = Profile::staticGet($this->profile_id);
         $profile->blowNoticeCount();
     }
 
+    function blowRepeatCache()
+    {
+        if (!empty($this->repeat_of)) {
+            $cache = common_memcache();
+            if (!empty($cache)) {
+                // XXX: only blow if <100 in cache
+                $ck = common_cache_key('notice:repeats:'.$this->repeat_of);
+                $result = $cache->delete($ck);
+            }
+        }
+    }
+
     function blowConversationCache($blowLast=false)
     {
         $cache = common_memcache();
@@ -1455,4 +1468,58 @@ class Notice extends Memcached_DataObject
         return self::saveNew($repeater_id, $content, $source,
                              array('repeat_of' => $this->id));
     }
+
+    // These are supposed to be in chron order!
+
+    function repeatStream($limit=100)
+    {
+        $cache = common_memcache();
+
+        if (empty($cache)) {
+            $ids = $this->_repeatStreamDirect($limit);
+        } else {
+            $idstr = $cache->get(common_cache_key('notice:repeats:'.$this->id));
+            if (!empty($idstr)) {
+                $ids = explode(',', $idstr);
+            } else {
+                $ids = $this->_repeatStreamDirect(100);
+                $cache->set(common_cache_key('notice:repeats:'.$this->id), implode(',', $ids));
+            }
+            if ($limit < 100) {
+                // We do a max of 100, so slice down to limit
+                $ids = array_slice($ids, 0, $limit);
+            }
+        }
+
+        return Notice::getStreamByIds($ids);
+    }
+
+    function _repeatStreamDirect($limit)
+    {
+        $notice = new Notice();
+
+        $notice->selectAdd(); // clears it
+        $notice->selectAdd('id');
+
+        $notice->repeat_of = $this->id;
+
+        $notice->orderBy('created'); // NB: asc!
+
+        if (!is_null($offset)) {
+            $notice->limit($offset, $limit);
+        }
+
+        $ids = array();
+
+        if ($notice->find()) {
+            while ($notice->fetch()) {
+                $ids[] = $notice->id;
+            }
+        }
+
+        $notice->free();
+        $notice = NULL;
+
+        return $ids;
+    }
 }
index 835339720d10012f42863b018b0cdfe21c2b9bf5..b0b95b080e627e911d07998701926971c9d9d6aa 100644 (file)
@@ -364,6 +364,11 @@ class Router
                               'id' => '[0-9]+',
                               'format' => '(xml|json)'));
 
+            $m->connect('api/statuses/retweets/:id.:format',
+                        array('action' => 'ApiStatusesRetweets',
+                              'id' => '[0-9]+',
+                              'format' => '(xml|json)'));
+
             // users
 
             $m->connect('api/users/show.:format',