123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- defined('GNUSOCIAL') || die();
- class AutocompleteAction extends Action
- {
- protected $needLogin = true;
- private $result;
-
- public 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__));
- }
-
- public 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 = [];
- $this->profiles = [];
- $term = $this->arg('term');
- $limit = (int) $this->arg('limit');
-
- if ($limit > 200) {
- $limit = 200;
- }
- if (substr($term, 0, 1) === '@') {
-
- $term = substr($term, 1);
- $user_table = common_database_tablename('user');
- $profile = new Profile();
- $profile->_join .= sprintf(
- "\n" . <<<'END'
- INNER JOIN (
- SELECT id FROM %s
- UNION
- SELECT subscribed AS id FROM subscription WHERE subscriber = %d
- ) AS t1 USING (id)
- END,
- $user_table,
- $this->scoped->id
- );
- $profile->whereAdd('nickname LIKE \'' . trim($profile->escape($term), '\'') . '%\'');
- $profile->orderBy('created, id');
- $profile->limit($limit);
- if ($profile->find()) {
- while ($profile->fetch()) {
- $this->profiles[] = clone($profile);
- }
- }
- } elseif (substr($term, 0, 1) === '!') {
- //group search
- $term = substr($term, 1);
- $group = new User_group();
- //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
- ));
- $group->whereAdd('nickname LIKE \'' . trim($group->escape($term), '\'') . '%\'');
- $group->orderBy('created, id');
- $group->limit($limit);
- 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();
- // sigh.... encapsulate this upstream!
- 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);
- }
- /**
- * Is this action read-only?
- *
- * @param array $args other arguments
- *
- * @return boolean is read only action?
- */
- public function isReadOnly($args)
- {
- return true;
- }
- }
|