Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / services / typeaheads / user-typeahead.js
blob7d4c2e654d4569930920530e39a78d54234788ee
1 'use strict';
3 var troupeService = require('gitter-web-rooms/lib/troupe-service');
4 var oneToOneTypeahead = require('./user-typeahead-one-to-one');
5 var elasticTypeahead = require('./user-typeahead-elastic');
6 var userSearchService = require('../user-search-service');
7 var env = require('gitter-web-env');
8 var errorReporter = env.errorReporter;
9 var logger = env.logger;
11 module.exports.query = function(text, options) {
12 var roomId = (options || {}).roomId;
14 if (roomId) {
15 return troupeService.findById(roomId).then(function(room) {
16 if (room.oneToOne) {
17 return oneToOneTypeahead.query(text, room);
18 } else {
19 // elastic typeahead doesnt know about oneToOnes
20 return alphaFallbackTypeahead(text, { roomId: roomId });
22 });
23 } else {
24 return alphaFallbackTypeahead(text);
28 // while we are using elasticsearch 5 alphas, we better have a fallback
29 function alphaFallbackTypeahead(text, options) {
30 return elasticTypeahead.query(text, options).catch(function(err) {
31 logger.error('typeahead alpha experienced an error, falling back to old typeahead', {
32 error: err.message
33 });
34 errorReporter(err, { text: text, options: options }, { module: 'user-typeahead' });
36 return oldTypeahead(text, options);
37 });
40 function oldTypeahead(text, options) {
41 var roomId = (options || {}).roomId || '*';
43 if (roomId) {
44 return userSearchService.searchForUsersInRoom(text, roomId).then(function(res) {
45 return res.results;
46 });
47 } else {
48 return userSearchService.globalUserSearch(text).then(function(res) {
49 return res.results;
50 });