]> 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 787ef15326e195c167af3b54424ff1bc049f8351..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
  *
@@ -29,9 +29,7 @@
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET') && !defined('LACONICA')) {
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * List users for autocompletion
@@ -62,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));
@@ -92,30 +90,27 @@ class AutocompleteAction extends Action
     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();
+        $this->profiles=array();
         $term = $this->arg('term');
         $limit = $this->arg('limit');
         if($limit > 200) $limit=200; //prevent DOS attacks
         if(substr($term,0,1)=='@'){
-            //user search
+            //profile search
             $term=substr($term,1);
-            $user = new User();
-            $user->limit($limit);
-            $user->whereAdd('nickname like \'' . trim($user->escape($term), '\'') . '%\'');
-            if($user->find()){
-                while($user->fetch()) {
-                    $this->users[]=clone($user);
+            $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);
                 }
             }
         }
@@ -125,6 +120,9 @@ class AutocompleteAction extends Action
             $group = new User_group();
             $group->limit($limit);
             $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);
@@ -139,28 +137,34 @@ class AutocompleteAction extends Action
         parent::handle();
 
         $results = array();
-        foreach($this->users as $user){
-            $profile = $user->getProfile();
+        foreach($this->profiles as $profile){
             $avatarUrl = $profile->avatarUrl(AVATAR_MINI_SIZE);
+            $acct = $profile->getAcctUri();
+            $identifier = explode(':', $profile->getAcctUri(), 2)[1];
             $results[] = array(
-                'value' => '@'.$profile->nickname,
-                'nickname' => $profile->nickname,
-                'label'=> $profile->getFancyName(),
+                '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) {
                 $avatarUrl = $group->mini_logo;
             } else {
                 $avatarUrl = User_group::defaultLogo(AVATAR_MINI_SIZE);
             }
+            $acct = $profile->getAcctUri();
+            $identifier = explode(':', $profile->getAcctUri(), 2)[1];
             $results[] = array(
-                'value' => '!'.$group->nickname,
-                'nickname' => $group->nickname,
-                'label'=> $group->getFancyName(),
+                'value' => '!'.$group->getNickname(),
+                'nickname' => $group->getNickname(),
+                'acct_uri' => $acct,
+                'label'=> "${identifier} (".$group->getFullname().")",
                 'avatar' => $avatarUrl,
                 'type' => 'group');
         }