3 * StatusNet, the distributed open-source microblogging tool
5 * List users for autocompletion
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Craig Andrews <candrews@integralblue.com>
25 * @author Mikael Nordfeldth <mmn@hethane.se>
26 * @copyright 2008-2009 StatusNet, Inc.
27 * @copyright 2009-2013 Free Software Foundation, Inc http://www.fsf.org
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://status.net/
32 if (!defined('STATUSNET') && !defined('LACONICA')) {
37 * List users for autocompletion
39 * This is the form for adding a new g
43 * @author Craig Andrews <candrews@integralblue.com>
44 * @author Mikael Nordfeldth <mmn@hethane.se>
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46 * @link http://status.net/
48 class AutocompleteAction extends Action
50 protected $needLogin = true;
55 * Last-modified date for page
57 * When was the content of this page last modified? Based on notice,
60 * @return int last-modified date as unix timestamp
62 function lastModified()
65 foreach($this->users as $user){
66 $max = max($max,strtotime($user->modified),strtotime($user->getProfile()->modified));
68 foreach($this->groups as $group){
69 $max = max($max,strtotime($group->modified));
75 * An entity tag for this page
77 * Shows the ETag for the page, based on the notice ID and timestamps
78 * for the notice, profile, and avatar. It's weak, since we change
79 * the date text "one hour ago", etc.
85 return '"' . implode(':', array($this->arg('action'),
86 common_user_cache_hash(),
87 crc32($this->arg('term')), //the actual string can have funny characters in we don't want showing up in the etag
89 $this->lastModified())) . '"';
92 protected function prepare($args)
94 // If we die, show short error messages.
95 StatusNet::setApi(true);
97 parent::prepare($args);
99 $cur = common_current_user();
101 // TRANS: Client exception in autocomplete plugin.
102 throw new ClientException(_m('Access forbidden.'), true);
105 $this->groups=array();
106 $this->users=array();
107 $term = $this->arg('term');
108 $limit = $this->arg('limit');
109 if($limit > 200) $limit=200; //prevent DOS attacks
110 if(substr($term,0,1)=='@'){
112 $term=substr($term,1);
114 $user->limit($limit);
115 $user->whereAdd('nickname like \'' . trim($user->escape($term), '\'') . '%\'');
117 while($user->fetch()) {
118 $this->users[]=clone($user);
122 if(substr($term,0,1)=='!'){
124 $term=substr($term,1);
125 $group = new User_group();
126 $group->limit($limit);
127 $group->whereAdd('nickname like \'' . trim($group->escape($term), '\'') . '%\'');
129 while($group->fetch()) {
130 $this->groups[]=clone($group);
137 protected function handle()
142 foreach($this->users as $user){
143 $profile = $user->getProfile();
144 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
145 // sigh.... encapsulate this upstream!
147 $avatar = $avatar->displayUrl();
149 $avatar = Avatar::defaultImage(AVATAR_MINI_SIZE);
152 'value' => '@'.$profile->nickname,
153 'nickname' => $profile->nickname,
154 'label'=> $profile->getFancyName(),
159 foreach($this->groups as $group){
160 // sigh.... encapsulate this upstream!
161 if ($group->mini_logo) {
162 $avatar = $group->mini_logo;
164 $avatar = User_group::defaultLogo(AVATAR_MINI_SIZE);
167 'value' => '!'.$group->nickname,
168 'nickname' => $group->nickname,
169 'label'=> $group->getFancyName(),
173 print json_encode($results);
177 * Is this action read-only?
179 * @param array $args other arguments
181 * @return boolean is read only action?
183 function isReadOnly($args)