Unverified Commit 8e887f88 authored by Jonne Haß's avatar Jonne Haß
Browse files

Merge pull request #6951 from svbergerem/contacts-search

Modify search to include contacts
parents 79117d1a cefffc60
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -169,6 +169,7 @@ before.
* Add 'Be excellent to each other!' to the sidebar [#6914](https://github.com/diaspora/diaspora/pull/6910)
* Expose Sidekiq dead queue configuration options
* Properly support pluralization in timeago strings [#6926](https://github.com/diaspora/diaspora/pull/6926)
* Return all contacts in people search [#6951](https://github.com/diaspora/diaspora/pull/6951)

# 0.5.11.0

+2 −1
Original line number Diff line number Diff line
@@ -31,7 +31,8 @@ app.views.PublisherMention = app.views.SearchBase.extend({
    app.views.SearchBase.prototype.initialize.call(this, {
      typeaheadInput: this.typeaheadInput,
      customSearch: true,
      autoselect: true
      autoselect: true,
      remoteRoute: "/contacts"
    });
  },

+0 −5
Original line number Diff line number Diff line
@@ -25,11 +25,6 @@ app.views.SearchBase = app.views.Base.extend({
        return this.bloodhoundTokenizer(datum.name).concat(datum.handle);
      }.bind(this),
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      prefetch: {
        url: "/contacts.json",
        transform: this.transformBloodhoundResponse,
        cache: false
      },
      sufficient: 5
    };

+3 −4
Original line number Diff line number Diff line
@@ -14,11 +14,10 @@ class ContactsController < ApplicationController
      # Used by the mobile site
      format.mobile { set_up_contacts_mobile }

      # Used to populate mentions in the publisher
      # Used for mentions in the publisher
      format.json {
        aspect_ids = params[:aspect_ids] || current_user.aspects.map(&:id)
        @people = Person.all_from_aspects(aspect_ids, current_user).for_json
        render :json => @people.to_json
        @people = Person.search(params[:q], current_user, only_contacts: true).limit(15)
        render json: @people
      }
    end
  end
+17 −19
Original line number Diff line number Diff line
@@ -56,7 +56,9 @@ class Person < ActiveRecord::Base
  validates :serialized_public_key, :presence => true
  validates :diaspora_handle, :uniqueness => true

  scope :searchable, -> { joins(:profile).where(:profiles => {:searchable => true}) }
  scope :searchable, -> (user) {
    joins(:profile).where("profiles.searchable = true OR contacts.user_id = ?", user.id)
  }
  scope :remote, -> { where('people.owner_id IS NULL') }
  scope :local, -> { where('people.owner_id IS NOT NULL') }
  scope :for_json, -> {
@@ -143,27 +145,23 @@ class Person < ActiveRecord::Base
    [where_clause, q_tokens]
  end

  def self.search(query, user)
    return self.where("1 = 0") if query.to_s.blank? || query.to_s.length < 2
  def self.search(search_str, user, only_contacts: false)
    search_str.strip!
    return none if search_str.blank? || search_str.size < 2

    sql, tokens = self.search_query_string(query)
    sql, tokens = search_query_string(search_str)

    Person.searchable.where(sql, *tokens).joins(
    query = if only_contacts
              joins(:contacts).where(contacts: {user_id: user.id})
            else
              joins(
                "LEFT OUTER JOIN contacts ON contacts.user_id = #{user.id} AND contacts.person_id = people.id"
    ).includes(:profile
    ).order(search_order)
              ).searchable(user)
            end

  # @return [Array<String>] postgreSQL and mysql deal with null values in orders differently, it seems.
  def self.search_order
    @search_order ||= Proc.new {
      order = if AppConfig.postgres?
        "ASC"
      else
        "DESC"
      end
      ["contacts.user_id #{order}", "profiles.last_name ASC", "profiles.first_name ASC"]
    }.call
    query.where(sql, *tokens)
         .includes(:profile)
         .order(["contacts.user_id IS NULL", "profiles.last_name ASC", "profiles.first_name ASC"])
  end

  def name(opts = {})
Loading