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')) { exit(1); }
35 * List users for autocompletion
37 * This is the form for adding a new g
41 * @author Craig Andrews <candrews@integralblue.com>
42 * @author Mikael Nordfeldth <mmn@hethane.se>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
46 class AutocompleteAction extends Action
48 protected $needLogin = true;
53 * Last-modified date for page
55 * When was the content of this page last modified? Based on notice,
58 * @return int last-modified date as unix timestamp
60 function lastModified()
63 foreach($this->profiles as $profile){
64 $max = max($max, strtotime($profile->modified));
66 foreach($this->groups as $group){
67 $max = max($max,strtotime($group->modified));
73 * An entity tag for this page
75 * Shows the ETag for the page, based on the notice ID and timestamps
76 * for the notice, profile, and avatar. It's weak, since we change
77 * the date text "one hour ago", etc.
83 return '"' . implode(':', array($this->arg('action'),
84 common_user_cache_hash(),
85 crc32($this->arg('term')), //the actual string can have funny characters in we don't want showing up in the etag
87 $this->lastModified())) . '"';
90 protected function prepare(array $args=array())
92 // If we die, show short error messages.
93 GNUsocial::setApi(true);
95 parent::prepare($args);
97 $this->groups=array();
98 $this->profiles=array();
99 $term = $this->arg('term');
100 $limit = $this->arg('limit');
101 if($limit > 200) $limit=200; //prevent DOS attacks
102 if(substr($term,0,1)=='@'){
104 $term=substr($term,1);
105 $profile = new Profile();
106 $profile->limit($limit);
107 $profile->whereAdd('nickname like \'' . trim($profile->escape($term), '\'') . '%\'');
108 $profile->whereAdd(sprintf('id in (SELECT id FROM user) OR '
109 . 'id in (SELECT subscribed from subscription'
110 . ' where subscriber = %d)', $this->scoped->id));
111 if ($profile->find()) {
112 while($profile->fetch()) {
113 $this->profiles[]=clone($profile);
117 if(substr($term,0,1)=='!'){
119 $term=substr($term,1);
120 $group = new User_group();
121 $group->limit($limit);
122 $group->whereAdd('nickname like \'' . trim($group->escape($term), '\'') . '%\'');
123 //Can't post to groups we're not subscribed to...:
124 $group->whereAdd(sprintf('id in (SELECT group_id FROM group_member'
125 . ' WHERE profile_id = %d)', $this->scoped->id));
127 while($group->fetch()) {
128 $this->groups[]=clone($group);
135 protected function handle()
140 foreach($this->profiles as $profile){
141 $avatarUrl = $profile->avatarUrl(AVATAR_MINI_SIZE);
142 $acct = $profile->getAcctUri();
143 $identifier = explode(':', $profile->getAcctUri(), 2)[1];
145 'value' => '@'.$identifier,
146 'nickname' => $profile->getNickname(),
148 'label'=> "${identifier} (".$profile->getFullname().")",
149 'avatar' => $avatarUrl,
153 foreach($this->groups as $group){
154 $profile = $group->getProfile();
155 // sigh.... encapsulate this upstream!
156 if ($group->mini_logo) {
157 $avatarUrl = $group->mini_logo;
159 $avatarUrl = User_group::defaultLogo(AVATAR_MINI_SIZE);
161 $acct = $profile->getAcctUri();
162 $identifier = explode(':', $profile->getAcctUri(), 2)[1];
164 'value' => '!'.$group->getNickname(),
165 'nickname' => $group->getNickname(),
167 'label'=> "${identifier} (".$group->getFullname().")",
168 'avatar' => $avatarUrl,
171 print json_encode($results);
175 * Is this action read-only?
177 * @param array $args other arguments
179 * @return boolean is read only action?
181 function isReadOnly($args)