3 * StatusNet, the distributed open-source microblogging tool
5 * List profiles and groups 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('GNUSOCIAL') && !defined('STATUSNET')) {
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->profiles as $profile){
66 $max = max($max, strtotime($profile->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(array $args=array())
94 // If we die, show short error messages.
95 StatusNet::setApi(true);
97 parent::prepare($args);
99 $this->groups=array();
100 $this->profiles=array();
101 $term = $this->arg('term');
102 $limit = $this->arg('limit');
103 if($limit > 200) $limit=200; //prevent DOS attacks
104 if(substr($term,0,1)=='@'){
106 $term=substr($term,1);
107 $profile = new Profile();
108 $profile->limit($limit);
109 $profile->whereAdd('nickname like \'' . trim($profile->escape($term), '\'') . '%\'');
110 $profile->whereAdd(sprintf('id in (SELECT id FROM user) OR '
111 . 'id in (SELECT subscribed from subscription'
112 . ' where subscriber = %d)', $this->scoped->id));
113 if ($profile->find()) {
114 while($profile->fetch()) {
115 $this->profiles[]=clone($profile);
119 if(substr($term,0,1)=='!'){
121 $term=substr($term,1);
122 $group = new User_group();
123 $group->limit($limit);
124 $group->whereAdd('nickname like \'' . trim($group->escape($term), '\'') . '%\'');
125 //Can't post to groups we're not subscribed to...:
126 $group->whereAdd(sprintf('id in (SELECT group_id FROM group_member'
127 . ' WHERE profile_id = %d)', $this->scoped->id));
129 while($group->fetch()) {
130 $this->groups[]=clone($group);
137 protected function handle()
142 foreach($this->profiles as $profile){
143 $avatarUrl = $profile->avatarUrl(AVATAR_MINI_SIZE);
145 'value' => '@'.$profile->nickname,
146 'nickname' => $profile->nickname,
147 'label'=> $profile->getFancyName(),
148 'avatar' => $avatarUrl,
152 foreach($this->groups as $group){
153 // sigh.... encapsulate this upstream!
154 if ($group->mini_logo) {
155 $avatarUrl = $group->mini_logo;
157 $avatarUrl = User_group::defaultLogo(AVATAR_MINI_SIZE);
160 'value' => '!'.$group->nickname,
161 'nickname' => $group->nickname,
162 'label'=> $group->getFancyName(),
163 'avatar' => $avatarUrl,
166 print json_encode($results);
170 * Is this action read-only?
172 * @param array $args other arguments
174 * @return boolean is read only action?
176 function isReadOnly($args)