123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class AutocompleteAction extends Action
- {
- protected $needLogin = true;
- private $result;
-
- function lastModified()
- {
- $max=0;
- foreach($this->profiles as $profile){
- $max = max($max, strtotime($profile->modified));
- }
- foreach($this->groups as $group){
- $max = max($max,strtotime($group->modified));
- }
-
-
- return max($max, filemtime(__FILE__));
- }
-
- function etag()
- {
- return '"' . implode(':', array($this->arg('action'),
- common_user_cache_hash(),
- crc32($this->arg('term')),
- $this->arg('limit'),
- $this->lastModified())) . '"';
- }
- protected function prepare(array $args=array())
- {
-
- GNUsocial::setApi(true);
- parent::prepare($args);
- $this->groups=array();
- $this->profiles=array();
- $term = $this->arg('term');
- $limit = $this->arg('limit');
- if($limit > 200) $limit=200;
- if(substr($term,0,1)=='@'){
-
- $term=substr($term,1);
- $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);
- }
- }
- }
- if(substr($term,0,1)=='!'){
-
- $term=substr($term,1);
- $group = new User_group();
- $group->limit($limit);
- $group->whereAdd('nickname like \'' . trim($group->escape($term), '\'') . '%\'');
-
- $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);
- }
- }
- }
- return true;
- }
- protected function handle()
- {
- parent::handle();
- $results = array();
- foreach($this->profiles as $profile){
- $avatarUrl = $profile->avatarUrl(AVATAR_MINI_SIZE);
- $acct = $profile->getAcctUri();
- $identifier = explode(':', $profile->getAcctUri(), 2)[1];
- $results[] = array(
- 'value' => '@'.$identifier,
- 'nickname' => $profile->getNickname(),
- 'acct_uri' => $acct,
- 'label'=> "${identifier} (".$profile->getFullname().")",
- 'avatar' => $avatarUrl,
- 'type' => 'user'
- );
- }
- foreach($this->groups as $group){
- $profile = $group->getProfile();
-
- 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' => '!'.$identifier,
- 'nickname' => $group->getNickname(),
- 'acct_uri' => $acct,
- 'label'=> "${identifier} (".$group->getFullname().")",
- 'avatar' => $avatarUrl,
- 'type' => 'group');
- }
- print json_encode($results);
- }
-
- function isReadOnly($args)
- {
- return true;
- }
- }
|