]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Autocomplete/actions/autocomplete.php
split() is deprecated and should be explode()
[quix0rs-gnu-social.git] / plugins / Autocomplete / actions / autocomplete.php
index 0bb1b783dbc6b9fa51c753d721ca9e9c6c470dc0..04acaefd38c5a5557035ac79b4af1f78fcc93194 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * StatusNet, the distributed open-source microblogging tool
  *
- * List users for autocompletion
+ * List profiles and groups for autocompletion
  *
  * PHP version 5
  *
  * @category  Plugin
  * @package   StatusNet
  * @author    Craig Andrews <candrews@integralblue.com>
+ * @author    Mikael Nordfeldth <mmn@hethane.se>
  * @copyright 2008-2009 StatusNet, Inc.
- * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
+ * @copyright 2009-2013 Free Software Foundation, Inc http://www.fsf.org
  * @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') && !defined('LACONICA')) {
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * List users for autocompletion
@@ -40,11 +39,14 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
  * @category Plugin
  * @package  StatusNet
  * @author   Craig Andrews <candrews@integralblue.com>
+ * @author   Mikael Nordfeldth <mmn@hethane.se>
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link     http://status.net/
  */
 class AutocompleteAction extends Action
 {
+    protected $needLogin = true;
+
     private $result;
 
     /**
@@ -58,8 +60,8 @@ class AutocompleteAction extends Action
     function lastModified()
     {
         $max=0;
-        foreach($this->users as $user){
-            $max = max($max,strtotime($user->modified),strtotime($user->getProfile()->modified));
+        foreach($this->profiles as $profile){
+            $max = max($max, strtotime($profile->modified));
         }
         foreach($this->groups as $group){
             $max = max($max,strtotime($group->modified));
@@ -80,46 +82,47 @@ class AutocompleteAction extends Action
     {
         return '"' . implode(':', array($this->arg('action'),
             common_user_cache_hash(),
-            crc32($this->arg('q')), //the actual string can have funny characters in we don't want showing up in the etag
+            crc32($this->arg('term')), //the actual string can have funny characters in we don't want showing up in the etag
             $this->arg('limit'),
             $this->lastModified())) . '"';
     }
 
-    function prepare($args)
+    protected function prepare(array $args=array())
     {
         // If we die, show short error messages.
-        StatusNet::setApi(true);
+        GNUsocial::setApi(true);
 
         parent::prepare($args);
 
-        $cur = common_current_user();
-        if (!$cur) {
-            // TRANS: Client exception in autocomplete plugin.
-            throw new ClientException(_m('Access forbidden.'), true);
-        }
         $this->groups=array();
-        $this->users=array();
-        $q = $this->arg('q');
+        $this->profiles=array();
+        $term = $this->arg('term');
         $limit = $this->arg('limit');
         if($limit > 200) $limit=200; //prevent DOS attacks
-        if(substr($q,0,1)=='@'){
-            //user search
-            $q=substr($q,1);
-            $user = new User();
-            $user->limit($limit);
-            $user->whereAdd('nickname like \'' . trim($user->escape($q), '\'') . '%\'');
-            if($user->find()){
-                while($user->fetch()) {
-                    $this->users[]=clone($user);
+        if(substr($term,0,1)=='@'){
+            //profile search
+            $term=substr($term,1);
+            $profile = new Profile();
+            $profile->limit($limit);
+            $profile->whereAdd('nickname like \'' . trim($profile->escape($term), '\'') . '%\'');
+            $profile->whereAdd(sprintf('id in (SELECT id FROM user) OR '
+                               . 'id in (SELECT subscribed from subscription'
+                               . ' where subscriber = %d)', $this->scoped->id));
+            if ($profile->find()) {
+                while($profile->fetch()) {
+                    $this->profiles[]=clone($profile);
                 }
             }
         }
-        if(substr($q,0,1)=='!'){
+        if(substr($term,0,1)=='!'){
             //group search
-            $q=substr($q,1);
+            $term=substr($term,1);
             $group = new User_group();
             $group->limit($limit);
-            $group->whereAdd('nickname like \'' . trim($group->escape($q), '\'') . '%\'');
+            $group->whereAdd('nickname like \'' . trim($group->escape($term), '\'') . '%\'');
+            //Can't post to groups we're not subscribed to...:
+            $group->whereAdd(sprintf('id in (SELECT group_id FROM group_member'
+                             . ' WHERE profile_id = %d)', $this->scoped->id));
             if($group->find()){
                 while($group->fetch()) {
                     $this->groups[]=clone($group);
@@ -129,42 +132,43 @@ class AutocompleteAction extends Action
         return true;
     }
 
-    function handle($args)
+    protected function handle()
     {
-        parent::handle($args);
+        parent::handle();
+
         $results = array();
-        foreach($this->users as $user){
-            $profile = $user->getProfile();
-            $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
-            // sigh.... encapsulate this upstream!
-            if ($avatar) {
-                $avatar = $avatar->displayUrl();
-            } else {
-                $avatar = Avatar::defaultImage(AVATAR_MINI_SIZE);
-            }
+        foreach($this->profiles as $profile){
+            $avatarUrl = $profile->avatarUrl(AVATAR_MINI_SIZE);
+            $acct = $profile->getAcctUri();
+            $identifier = explode(':', $profile->getAcctUri(), 2)[1];
             $results[] = array(
-                'nickname' => $user->nickname,
-                'fullname'=> $profile->fullname,
-                'avatar' => $avatar,
+                'value' => '@'.$identifier,
+                'nickname' => $profile->getNickname(),
+                'acct_uri' => $acct,
+                'label'=> "${identifier} (".$profile->getFullname().")",
+                'avatar' => $avatarUrl,
                 'type' => 'user'
             );
         }
         foreach($this->groups as $group){
+            $profile = $group->getProfile();
             // sigh.... encapsulate this upstream!
             if ($group->mini_logo) {
-                $avatar = $group->mini_logo;
+                $avatarUrl = $group->mini_logo;
             } else {
-                $avatar = User_group::defaultLogo(AVATAR_MINI_SIZE);
+                $avatarUrl = User_group::defaultLogo(AVATAR_MINI_SIZE);
             }
+            $acct = $profile->getAcctUri();
+            $identifier = explode(':', $profile->getAcctUri(), 2)[1];
             $results[] = array(
-                'nickname' => $group->nickname,
-                'fullname'=> $group->fullname,
-                'avatar' => $avatar,
+                'value' => '!'.$group->getNickname(),
+                'nickname' => $group->getNickname(),
+                'acct_uri' => $acct,
+                'label'=> "${identifier} (".$group->getFullname().")",
+                'avatar' => $avatarUrl,
                 'type' => 'group');
         }
-        foreach($results as $result) {
-            print json_encode($result) . "\n";
-        }
+        print json_encode($results);
     }
 
     /**