diff --git a/app/javascript/chat/__mocks__/algoliasearchUsers.js b/app/javascript/chat/__mocks__/algoliasearchUsers.js deleted file mode 100644 index 094f5f2fc..000000000 --- a/app/javascript/chat/__mocks__/algoliasearchUsers.js +++ /dev/null @@ -1,42 +0,0 @@ -global.document.head.innerHTML = - "" + - "" + - ""; - -const mockIndex = { - search: query => - new Promise(resolve => { - process.nextTick(() => { - const searchResults = { - ma: { - hits: [ - { - name: 'mat', - path: 'some_path', - title: 'some_title', - id: 'some_id', - }, - ], - nbHits: 1, - page: 0, - nbPages: 1, - hitsPerPage: 10, - processingTimeMS: 1, - exhaustiveNbHits: true, - query: 'ma', - params: - 'query=ma&hitsPerPage=10&filters=supported%3Atrue&restrictIndices=searchables_development%2CTag_development%2Cordered_articles_development%2Cordered_articles_by_published_at_development%2Cordered_articles_by_positive_reactions_count_development', - }, - }; - - const results = searchResults[query] || { hits: [] }; - - resolve(results); - }); - }), -}; -const client = { - initIndex: _index => mockIndex, // eslint-ignore-line -}; - -export default jest.fn().mockImplementation((_id_, _key) => client); // eslint-ignore-line diff --git a/app/javascript/chat/__tests__/__snapshots__/channelDetails.test.jsx.snap b/app/javascript/chat/__tests__/__snapshots__/channelDetails.test.jsx.snap index da6d42d1b..d3427cac5 100644 --- a/app/javascript/chat/__tests__/__snapshots__/channelDetails.test.jsx.snap +++ b/app/javascript/chat/__tests__/__snapshots__/channelDetails.test.jsx.snap @@ -158,7 +158,7 @@ preact-render-spy (1 nodes)

Invite Members

diff --git a/app/javascript/chat/__tests__/channelDetails.test.jsx b/app/javascript/chat/__tests__/channelDetails.test.jsx index 69a2bda4f..327111b4f 100644 --- a/app/javascript/chat/__tests__/channelDetails.test.jsx +++ b/app/javascript/chat/__tests__/channelDetails.test.jsx @@ -4,13 +4,11 @@ import { shallow } from 'preact-render-spy'; import { JSDOM } from 'jsdom'; import fetch from 'jest-fetch-mock'; import ChannelDetails from '../channelDetails'; -import algoliasearch from '../__mocks__/algoliasearchUsers'; global.fetch = fetch; const doc = new JSDOM(''); global.document = doc; global.window = doc.defaultView; -global.window.algoliasearch = algoliasearch; global.window.currentUser = { id: 'modID' }; const channelDetails = mod => { @@ -106,9 +104,7 @@ describe('', () => { .at(i) .childAt(1) .attr('data-content'), - ).toEqual( - `sidecar-user`, - ); + ).toEqual(`sidecar-user`); } // mod only divs @@ -292,9 +288,7 @@ describe('', () => { .at(i) .childAt(1) .attr('data-content'), - ).toEqual( - `sidecar-user`, - ); + ).toEqual(`sidecar-user`); } // user only divs diff --git a/app/javascript/chat/channelDetails.jsx b/app/javascript/chat/channelDetails.jsx index b7bb0370f..8d0756b5f 100644 --- a/app/javascript/chat/channelDetails.jsx +++ b/app/javascript/chat/channelDetails.jsx @@ -1,5 +1,6 @@ import { h, Component } from 'preact'; import PropTypes from 'prop-types'; +import debounceAction from '../src/utils/debounceAction'; class ChannelDetails extends Component { static propTypes = { @@ -9,34 +10,36 @@ class ChannelDetails extends Component { constructor(props) { super(props); + this.debouncedUserSearch = debounceAction( + this.triggerUserSearch.bind(this), + { config: { leading: true } }, + ); + this.state = { searchedUsers: [], - // invitations: [], hasLeftChannel: false, }; - - const algoliaId = document.querySelector("meta[name='algolia-public-id']") - .content; - const algoliaKey = document.querySelector("meta[name='algolia-public-key']") - .content; - const env = document.querySelector("meta[name='environment']").content; - const client = algoliasearch(algoliaId, algoliaKey); // eslint-disable-line no-undef - this.index = client.initIndex(`searchables_${env}`); } triggerUserSearch = e => { const component = this; const query = e.target.value; - const filters = { - hitsPerPage: 20, - attributesToRetrieve: ['id', 'title', 'path', 'user'], - attributesToHighlight: [], - filters: 'class_name:User', - }; if (query.length > 0) { - this.index.search(query, filters).then(content => { - component.setState({ searchedUsers: content.hits }); - }); + const searchHash = { per_page: 20, search_fields: query }; + const searchParams = new URLSearchParams(searchHash).toString(); + fetch(`/search/users?${searchParams}`, { + method: 'GET', + headers: { + Accept: 'application/json', + 'X-CSRF-Token': window.csrfToken, + 'Content-Type': 'application/json', + }, + credentials: 'same-origin', + }) + .then(response => response.json()) + .then(response => { + component.setState({ searchedUsers: response.result }); + }); } else { component.setState({ searchedUsers: [] }); } @@ -189,7 +192,7 @@ class ChannelDetails extends Component { modSection = (

Invite Members

- +
{searchedUsers}

Pending Invites:

{pendingInvites} diff --git a/lib/data_update_scripts/20200313123108_index_users_to_elasticsearch.rb b/lib/data_update_scripts/20200313123108_index_users_to_elasticsearch.rb new file mode 100644 index 000000000..b9d22acba --- /dev/null +++ b/lib/data_update_scripts/20200313123108_index_users_to_elasticsearch.rb @@ -0,0 +1,11 @@ +module DataUpdateScripts + class IndexUsersToElasticsearch + def run + User.select(:id).find_each do |user| + Search::IndexToElasticsearchWorker.set(queue: :low_priority).perform_async( + "User", user.id + ) + end + end + end +end diff --git a/spec/lib/data_update_scripts/index_users_to_elasticsearch_spec.rb b/spec/lib/data_update_scripts/index_users_to_elasticsearch_spec.rb index b51aa06ca..0ad2ed478 100644 --- a/spec/lib/data_update_scripts/index_users_to_elasticsearch_spec.rb +++ b/spec/lib/data_update_scripts/index_users_to_elasticsearch_spec.rb @@ -1,5 +1,5 @@ require "rails_helper" -require Rails.root.join("lib/data_update_scripts/20200305201627_index_users_to_elasticsearch.rb") +require Rails.root.join("lib/data_update_scripts/20200313123108_index_users_to_elasticsearch.rb") describe DataUpdateScripts::IndexUsersToElasticsearch, elasticsearch: true do it "indexes users to Elasticsearch" do