Sha256: 00fcd3db6b7b7629256672bf113d7e47bb64585c377b2cc6f64d5ac9361af7e9

Contents?: true

Size: 1.13 KB

Versions: 10

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Thredded
  class AutocompleteUsersController < Thredded::ApplicationController
    MAX_RESULTS = 20

    def index
      authorize_creating Thredded::PrivateTopicForm.new(user: thredded_current_user).private_topic
      users = params.key?(:q) ? users_by_prefix : users_by_ids
      render json: {
        results: users.map do |user|
          { id:         user.id,
            name:       user.send(Thredded.user_name_column),
            avatar_url: Thredded.avatar_url.call(user) }
        end
      }
    end

    private

    def users_by_prefix
      query = params[:q].to_s.strip
      if query.length >= Thredded.autocomplete_min_length
        DbTextSearch::CaseInsensitive.new(users_scope, Thredded.user_name_column).prefix(query)
          .where.not(id: thredded_current_user.id)
          .limit(MAX_RESULTS)
      else
        []
      end
    end

    def users_by_ids
      ids = params[:ids].to_s.split(',')
      if ids.present?
        users_scope.where(id: ids)
      else
        []
      end
    end

    def users_scope
      thredded_current_user.thredded_can_message_users
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
thredded-0.14.1 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.14.0 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.13.8 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.13.7 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.13.6 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.13.5 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.13.4 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.13.3 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.13.2 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.13.1 app/controllers/thredded/autocomplete_users_controller.rb