]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Autocomplete/actions/autocomplete.php
Autocomplete: Fix $profile being null for groups
[quix0rs-gnu-social.git] / plugins / Autocomplete / actions / autocomplete.php
index 2e66fec93dad4ac8bcc8a78c7afbce397bf4af2c..29921a5564a377db07c9fc79cfde3cc33b22d919 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,7 +29,7 @@
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET') && !defined('LACONICA')) {
+if (!defined('GNUSOCIAL') && !defined('STATUSNET')) {
     exit(1);
 }
 
@@ -62,8 +62,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));
@@ -89,33 +89,30 @@ class AutocompleteAction extends Action
             $this->lastModified())) . '"';
     }
 
-    protected 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();
+        $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 +122,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,35 +139,35 @@ class AutocompleteAction extends Action
         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 = split(':', $profile->getAcctUri(), 2)[1];
             $results[] = array(
-                'value' => '@'.$profile->nickname,
-                'nickname' => $profile->nickname,
-                'label'=> $profile->getFancyName(),
-                '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 = split(':', $profile->getAcctUri(), 2)[1];
             $results[] = array(
-                'value' => '!'.$group->nickname,
-                'nickname' => $group->nickname,
-                'label'=> $group->getFancyName(),
-                'avatar' => $avatar,
+                'value' => '!'.$group->getNickname(),
+                'nickname' => $group->getNickname(),
+                'acct_uri' => $acct,
+                'label'=> "${identifier} (".$group->getFullname().")",
+                'avatar' => $avatarUrl,
                 'type' => 'group');
         }
         print json_encode($results);