]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Merge branch '1.0.x' of gitorious.org:statusnet/mainline into 1.0.x
authorEvan Prodromou <evan@status.net>
Fri, 15 Jul 2011 21:46:19 +0000 (17:46 -0400)
committerEvan Prodromou <evan@status.net>
Fri, 15 Jul 2011 21:46:19 +0000 (17:46 -0400)
actions/apiconversation.php [new file with mode: 0644]
actions/conversation.php
classes/Memcached_DataObject.php
classes/Notice.php
lib/arraywrapper.php
lib/conversationnoticestream.php
lib/noticestream.php
lib/router.php
plugins/Bookmark/BookmarkPlugin.php
plugins/Meteor/MeteorPlugin.php

diff --git a/actions/apiconversation.php b/actions/apiconversation.php
new file mode 100644 (file)
index 0000000..fefb5b6
--- /dev/null
@@ -0,0 +1,242 @@
+<?php
+/**
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2011, StatusNet, Inc.
+ *
+ * Show a stream of notices in a particular conversation
+ * 
+ * PHP version 5
+ *
+ * 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 2011 StatusNet, Inc.
+ * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
+ * @link      http://status.net/
+ */
+
+if (!defined('STATUSNET')) {
+    // This check helps protect against security problems;
+    // your code file can't be executed directly from the web.
+    exit(1);
+}
+
+require_once INSTALLDIR . '/lib/apiauth.php';
+
+/**
+ * Show a stream of notices in a particular conversation
+ *
+ * @category  API
+ * @package   StatusNet
+ * @author    Evan Prodromou <evan@status.net>
+ * @copyright 2011 StatusNet, Inc.
+ * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
+ * @link      http://status.net/
+ */
+
+class ApiconversationAction extends ApiAuthAction
+{
+       protected $conversation = null;
+       protected $notices      = null;
+       
+    /**
+     * For initializing members of the class.
+     *
+     * @param array $argarray misc. arguments
+     *
+     * @return boolean true
+     */
+
+    function prepare($argarray)
+    {
+        parent::prepare($argarray);
+        
+        $convId = $this->trimmed('id');
+        
+        if (empty($convId)) {
+               throw new ClientException(_m('no conversation id'));
+        }
+        
+        $this->conversation = Conversation::staticGet('id', $convId);
+        
+        if (empty($this->conversation)) {
+               throw new ClientException(sprintf(_m('No conversation with id %d'), $convId),
+                                                                 404);
+        }
+        
+        $profile = Profile::current();
+        
+        $stream = new ConversationNoticeStream($convId, $profile);
+        
+        $notice = $stream->getNotices(($this->page-1) * $this->count,
+                                      $this->count,
+                                      $this->since_id,
+                                      $this->max_id);
+        
+        $this->notices = $notice->fetchAll();
+                                 
+        return true;
+    }
+
+    /**
+     * Handler method
+     *
+     * @param array $argarray is ignored since it's now passed in in prepare()
+     *
+     * @return void
+     */
+
+    function handle($argarray=null)
+    {
+        $sitename   = common_config('site', 'name');
+        // TRANS: Timeline title for user and friends. %s is a user nickname.
+        $title      = _("Conversation");
+        $id         = common_local_url('apiconversation', array('id' => $this->conversation->id, 'format' => $this->format));
+        $link       = common_local_url('conversation', array('id' => $this->conversation->id));
+
+        $self       = $id;
+        
+        switch($this->format) {
+        case 'xml':
+            $this->showXmlTimeline($this->notices);
+            break;
+        case 'rss':
+            $this->showRssTimeline(
+                $this->notices,
+                $title,
+                $link,
+                null,
+                null,
+                null,
+                $self
+            );
+            break;
+        case 'atom':
+
+            header('Content-Type: application/atom+xml; charset=utf-8');
+
+            $atom = new AtomNoticeFeed($this->auth_user);
+
+            $atom->setId($id);
+            $atom->setTitle($title);
+            $atom->setUpdated('now');
+
+            $atom->addLink($link);
+            $atom->setSelfLink($self);
+
+            $atom->addEntryFromNotices($this->notices);
+            $this->raw($atom->getString());
+
+            break;
+        case 'json':
+            $this->showJsonTimeline($this->notices);
+            break;
+        case 'as':
+            header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
+            $doc = new ActivityStreamJSONDocument($this->auth_user);
+            $doc->setTitle($title);
+            $doc->addLink($link, 'alternate', 'text/html');
+            $doc->addItemsFromNotices($this->notices);
+            $this->raw($doc->asString());
+            break;
+        default:
+            // TRANS: Client error displayed when coming across a non-supported API method.
+            $this->clientError(_('API method not found.'), $code = 404);
+            break;
+        }
+    }
+
+    /**
+     * Return true if read only.
+     *
+     * MAY override
+     *
+     * @param array $args other arguments
+     *
+     * @return boolean is read only action?
+     */
+
+    function isReadOnly($args)
+    {
+        if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
+            $_SERVER['REQUEST_METHOD'] == 'HEAD') {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    /**
+     * Return last modified, if applicable.
+     *
+     * MAY override
+     *
+     * @return string last modified http header
+     */
+    function lastModified()
+    {
+        if (!empty($this->notices) && (count($this->notices) > 0)) {
+            return strtotime($this->notices[0]->created);
+        }
+
+        return null;
+    }
+
+    /**
+     * Return etag, if applicable.
+     *
+     * MAY override
+     *
+     * @return string etag http header
+     */
+
+    function etag()
+    {
+        if (!empty($this->notices) && (count($this->notices) > 0)) {
+
+            $last = count($this->notices) - 1;
+
+            return '"' . implode(
+                ':',
+                array($this->arg('action'),
+                      common_user_cache_hash($this->auth_user),
+                      common_language(),
+                      $this->user->id,
+                      strtotime($this->notices[0]->created),
+                      strtotime($this->notices[$last]->created))
+            )
+            . '"';
+        }
+        
+        return null;
+    }
+
+    /**
+     * Does this require authentication?
+     *
+     * @return boolean true if delete, else false
+     */
+
+    function requiresAuth()
+    {
+        if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
+            $_SERVER['REQUEST_METHOD'] == 'HEAD') {
+            return false;
+        } else {
+            return true;
+        }
+    }
+}
\ No newline at end of file
index f33d267d35c02df306e9b211535c976b68b3f049..637e86e4b26eb95b8d26cdb429878ab76314f856 100644 (file)
@@ -132,4 +132,33 @@ class ConversationAction extends Action
     {
         return true;
     }
+    
+    function getFeeds()
+    {
+       
+        return array(new Feed(Feed::JSON,
+                              common_local_url('apiconversation',
+                                               array(
+                                                    'id' => $this->id,
+                                                    'format' => 'as')),
+                              // TRANS: Title for link to notice feed.
+                              // TRANS: %s is a user nickname.
+                              _('Conversation feed (Activity Streams JSON)')),
+                     new Feed(Feed::RSS2,
+                              common_local_url('apiconversation',
+                                               array(
+                                                    'id' => $this->id,
+                                                    'format' => 'rss')),
+                              // TRANS: Title for link to notice feed.
+                              // TRANS: %s is a user nickname.
+                              _('Conversation feed (RSS 2.0)')),
+                     new Feed(Feed::ATOM,
+                              common_local_url('apiconversation',
+                                               array(
+                                                    'id' => $this->id,
+                                                    'format' => 'atom')),
+                              // TRANS: Title for link to notice feed.
+                              // TRANS: %s is a user nickname.
+                              _('Conversation feed (Activity Streams JSON)')));
+    }
 }
index 0e60b7fed586ecc3143aefaff0366bda4469f950..0eae9fb42a835e79087a0845f2449f85afd52e28 100644 (file)
@@ -63,7 +63,91 @@ class Memcached_DataObject extends Safe_DataObject
         }
         return $i;
     }
+    
+    /**
+     * Get multiple items from the database by key
+     * 
+     * @param string  $cls       Class to fetch
+     * @param string  $keyCol    name of column for key
+     * @param array   $keyVals   key values to fetch
+     * @param boolean $skipNulls return only non-null results?
+     * 
+     * @return array Array of objects, in order
+     */
+    function multiGet($cls, $keyCol, $keyVals, $skipNulls=true)
+    {
+       $result = array_fill_keys($keyVals, null);
+       
+       $toFetch = array();
+       
+       foreach ($keyVals as $keyVal) {
+               $i = self::getcached($cls, $keyCol, $keyVal);
+               if ($i !== false) {
+                       $result[$keyVal] = $i;
+               } else if (!empty($keyVal)) {
+                       $toFetch[] = $keyVal;
+               }
+       }
+       
+       if (count($toFetch) > 0) {
+            $i = DB_DataObject::factory($cls);
+            if (empty($i)) {
+               throw new Exception(_('Cannot instantiate class ' . $cls));
+            }
+               $i->whereAddIn($keyCol, $toFetch, $i->columnType($keyCol));
+               if ($i->find()) {
+                       while ($i->fetch()) {
+                               $copy = clone($i);
+                               $copy->encache();
+                               $result[$i->$keyCol] = $copy;
+                       }
+               }
+               
+               // Save state of DB misses
+               
+               foreach ($toFetch as $keyVal) {
+                       if (empty($result[$keyVal])) {
+                       // save the fact that no such row exists
+                       $c = self::memcache();
+                       if (!empty($c)) {
+                       $ck = self::cachekey($cls, $keyCol, $keyVal);
+                       $c->set($ck, null);
+                       }       
+                       }
+               }
+       }
+       
+       $values = array_values($result);
+       
+       if ($skipNulls) {
+               $tmp = array();
+               foreach ($values as $value) {
+                       if (!empty($value)) {
+                               $tmp[] = $value;
+                       }
+               }
+               $values = $tmp;
+       }
+       
+       return new ArrayWrapper($values);
+    }
 
+       function columnType($columnName)
+       {
+               $keys = $this->table();
+               if (!array_key_exists($columnName, $keys)) {
+                       throw new Exception('Unknown key column ' . $columnName . ' in ' . join(',', array_keys($keys)));
+               }
+               
+               $def = $keys[$columnName];
+               
+               if ($def & DB_DATAOBJECT_INT) {
+                       return 'integer';
+               } else {
+                       return 'string';
+               }
+       }
+       
     /**
      * @fixme Should this return false on lookup fail to match staticGet?
      */
index 6eb4d9001e4c032f66d78ba8cac8f4f46c1c5a1d..650dca051b359727ec00cc64c8f44b07cd280a3e 100644 (file)
@@ -84,6 +84,11 @@ class Notice extends Memcached_DataObject
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
 
+       function multiGet($kc, $kvs, $skipNulls=true)
+       {
+               return Memcached_DataObject::multiGet('Notice', $kc, $kvs, $skipNulls);
+       }
+       
     /* Notice types */
     const LOCAL_PUBLIC    =  1;
     const REMOTE          =  0;
@@ -1856,7 +1861,11 @@ class Notice extends Memcached_DataObject
         } else {
             $idstr = $cache->get(Cache::key('notice:repeats:'.$this->id));
             if ($idstr !== false) {
-                $ids = explode(',', $idstr);
+               if (empty($idstr)) {
+                       $ids = array();
+               } else {
+                       $ids = explode(',', $idstr);
+               }
             } else {
                 $ids = $this->_repeatStreamDirect(100);
                 $cache->set(Cache::key('notice:repeats:'.$this->id), implode(',', $ids));
@@ -1885,18 +1894,7 @@ class Notice extends Memcached_DataObject
             $notice->limit(0, $limit);
         }
 
-        $ids = array();
-
-        if ($notice->find()) {
-            while ($notice->fetch()) {
-                $ids[] = $notice->id;
-            }
-        }
-
-        $notice->free();
-        $notice = NULL;
-
-        return $ids;
+        return $notice->fetchAll('id');
     }
 
     function locationOptions($lat, $lon, $location_id, $location_ns, $profile = null)
index f9d3c3cf946313d14b35b2ee4cfca2fd1e59a706..30cb6cfdc79da2647580d89aa32d20f6ff8a6c47 100644 (file)
@@ -48,6 +48,16 @@ class ArrayWrapper
         }
     }
 
+       function fetchAll($k= false, $v = false, $method = false)
+       {
+               if ($k !== false || $v !== false || $method !== false)
+               {
+                       $item =& $this->_items[$this->_i];
+                       return $item->fetchAll($k, $v, $method);
+               }
+               return $this->_items;
+       }
+       
     function __set($name, $value)
     {
         $item =& $this->_items[$this->_i];
index 66075db84f436a62c095174f0036b8231fddf4a5..adf610ffe7b2841c0d15260cc093b3d0216a573b 100644 (file)
@@ -95,14 +95,6 @@ class RawConversationNoticeStream extends NoticeStream
         Notice::addWhereSinceId($notice, $since_id);
         Notice::addWhereMaxId($notice, $max_id);
 
-        $ids = array();
-
-        if ($notice->find()) {
-            while ($notice->fetch()) {
-                $ids[] = $notice->id;
-            }
-        }
-
-        return $ids;
+        return $notice->fetchAll('id');
     }
 }
\ No newline at end of file
index be28aa61867526f56214ca9ed7f9f6b9534e20a7..e9ff47b68c154eb73641cbaf97dbb1b0126c2cae 100644 (file)
@@ -59,42 +59,6 @@ abstract class NoticeStream
 
     static function getStreamByIds($ids)
     {
-        $cache = Cache::instance();
-
-        if (!empty($cache)) {
-            $notices = array();
-            foreach ($ids as $id) {
-                $n = Notice::staticGet('id', $id);
-                if (!empty($n)) {
-                    $notices[] = $n;
-                }
-            }
-            return new ArrayWrapper($notices);
-        } else {
-            $notice = new Notice();
-            if (empty($ids)) {
-                //if no IDs requested, just return the notice object
-                return $notice;
-            }
-            $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
-
-            $notice->find();
-
-            $temp = array();
-
-            while ($notice->fetch()) {
-                $temp[$notice->id] = clone($notice);
-            }
-
-            $wrapped = array();
-
-            foreach ($ids as $id) {
-                if (array_key_exists($id, $temp)) {
-                    $wrapped[] = $temp[$id];
-                }
-            }
-
-            return new ArrayWrapper($wrapped);
-        }
+       return Notice::multiGet('id', $ids);
     }
 }
index 536797dce17ec6d224b2fb56ffaf9a99668040ea..180d8f791b97d3bc351984ad9effbf17f1ca2715 100644 (file)
@@ -763,6 +763,11 @@ class Router
                         array('action' => 'ApiGroupProfileUpdate',
                               'id' => '[a-zA-Z0-9]+',
                               'format' => '(xml|json)'));
+                              
+            $m->connect('api/statusnet/conversation/:id.:format',
+                        array('action' => 'apiconversation',
+                              'id' => '[0-9]+',
+                              'format' => '(xml|json|rss|atom|as)'));
 
             // Lists (people tags)
 
index e383f805608ec85cefd9be36a62529d3dbf7e07a..77b8a8483cfc6f244bdd3d041c6f61a0ff650139 100644 (file)
@@ -292,19 +292,27 @@ class BookmarkPlugin extends MicroAppPlugin
 
     function onStartOpenNoticeListItemElement($nli)
     {
+       if (!$this->isMyNotice($nli->notice)) {
+               return true;
+       }
+       
         $nb = Bookmark::getByNotice($nli->notice);
-        if (!empty($nb)) {
-            $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
-            $class = 'hentry notice bookmark';
-            if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
-                $class .= ' limited-scope';
-            }
-            $nli->out->elementStart('li', array('class' => $class,
-                                                 'id' => 'notice-' . $id));
-            Event::handle('EndOpenNoticeListItemElement', array($nli));
-            return false;
+        
+        if (empty($nb)) {
+               $this->log(LOG_INFO, "Notice {$nli->notice->id} has bookmark class but no matching Bookmark record.");
+               return true;
         }
-        return true;
+               
+           $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
+           $class = 'hentry notice bookmark';
+           if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
+               $class .= ' limited-scope';
+           }
+           $nli->out->elementStart('li', array('class' => $class,
+                                               'id' => 'notice-' . $id));
+                                               
+           Event::handle('EndOpenNoticeListItemElement', array($nli));
+           return false;
     }
 
     /**
@@ -355,12 +363,15 @@ class BookmarkPlugin extends MicroAppPlugin
      */
     function deleteRelated($notice)
     {
-        $nb = Bookmark::getByNotice($notice);
-
-        if (!empty($nb)) {
-            $nb->delete();
-        }
-
+       if ($this->isMyNotice($notice)) {
+               
+               $nb = Bookmark::getByNotice($notice);
+
+               if (!empty($nb)) {
+               $nb->delete();
+               }
+       }
+       
         return true;
     }
 
index cbc280a498d97888f25afa6cff9a73fc2c67b99e..3f963eb7328ec95952af05a2667b1ca19b00a533 100644 (file)
@@ -49,10 +49,11 @@ class MeteorPlugin extends RealtimePlugin
     public $controlport   = null;
     public $controlserver = null;
     public $channelbase   = null;
+    public $protocol      = null;
     public $persistent    = true;
     protected $_socket    = null;
 
-    function __construct($webserver=null, $webport=4670, $controlport=4671, $controlserver=null, $channelbase='')
+    function __construct($webserver=null, $webport=4670, $controlport=4671, $controlserver=null, $channelbase='', $protocol='http')
     {
         global $config;
 
@@ -61,7 +62,8 @@ class MeteorPlugin extends RealtimePlugin
         $this->controlport   = $controlport;
         $this->controlserver = (empty($controlserver)) ? $webserver : $controlserver;
         $this->channelbase   = $channelbase;
-
+               $this->protocol      = $protocol;
+               
         parent::__construct();
     }
 
@@ -74,7 +76,8 @@ class MeteorPlugin extends RealtimePlugin
                           'webport',
                           'controlport',
                           'controlserver',
-                          'channelbase');
+                          'channelbase',
+                          'protocol');
         foreach ($settings as $name) {
             $val = common_config('meteor', $name);
             if ($val !== false) {
@@ -88,7 +91,11 @@ class MeteorPlugin extends RealtimePlugin
     function _getScripts()
     {
         $scripts = parent::_getScripts();
-        $scripts[] = 'http://'.$this->webserver.(($this->webport == 80) ? '':':'.$this->webport).'/meteor.js';
+        if ($this->protocol == 'https') {
+               $scripts[] = 'https://'.$this->webserver.(($this->webport == 443) ? '':':'.$this->webport).'/meteor.js';
+        } else {
+               $scripts[] = 'http://'.$this->webserver.(($this->webport == 80) ? '':':'.$this->webport).'/meteor.js';
+        }
         $scripts[] = $this->path('meteorupdater.min.js');
         return $scripts;
     }