From b8c36bd2da628c08cbab9a034713fb2f9dc4964a Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 4 Sep 2014 12:21:57 +1000 Subject: [PATCH] FIX: broken search for admins when selecting topics --- .../discourse/controllers/search.js.es6 | 50 +----------------- .../javascripts/discourse/lib/search.js | 52 ++++++++++++++++++- .../discourse/views/choose-topic.js.es6 | 6 +-- 3 files changed, 55 insertions(+), 53 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/search.js.es6 b/app/assets/javascripts/discourse/controllers/search.js.es6 index 615d2f9e3..8306574ba 100644 --- a/app/assets/javascripts/discourse/controllers/search.js.es6 +++ b/app/assets/javascripts/discourse/controllers/search.js.es6 @@ -52,55 +52,7 @@ export default Em.Controller.extend(Discourse.Presence, { typeFilter: typeFilter, searchContext: context }).then(function(results) { - if (results) { - - // Topics might not be included - if (!results.topics) { results.topics = []; } - - var topicMap = {}; - results.topics = results.topics.map(function(topic){ - topic = Discourse.Topic.create(topic); - topicMap[topic.id] = topic; - return topic; - }); - - results.posts = results.posts.map(function(post){ - post = Discourse.Post.create(post); - post.set('topic', topicMap[post.topic_id]); - return post; - }); - - results.users = results.users.map(function(user){ - user = Discourse.User.create(user); - return user; - }); - - results.categories = results.categories.map(function(category){ - category = Discourse.Category.create(category); - return category; - }); - - var r = results.grouped_search_result; - results.resultTypes = []; - - // TODO: consider refactoring front end to take a better structure - [['topic','posts'],['user','users'],['category','categories']].forEach(function(pair){ - var type = pair[0], name = pair[1]; - if(results[name].length > 0) { - results.resultTypes.push({ - results: results[name], - displayType: (context && Em.get(context, 'type') === 'topic' && type === 'topic') ? 'post' : type, - type: type, - more: r['more_' + name] - }); - } - }); - - results.displayType = self.get('searchContext') === 'topic' ? 'post' : results.type; - - var noResults = !!((results.topics.length === 0) && (results.posts.length === 0) && (results.categories.length === 0)); - self.setProperties({ noResults: noResults, content: noResults ? null : Em.Object.create(results) }); - } + self.setProperties({ noResults: !results, content: results }); self.set('loading', false); }).catch(function() { self.set('loading', false); diff --git a/app/assets/javascripts/discourse/lib/search.js b/app/assets/javascripts/discourse/lib/search.js index f71766c1f..6e5aa5932 100644 --- a/app/assets/javascripts/discourse/lib/search.js +++ b/app/assets/javascripts/discourse/lib/search.js @@ -32,7 +32,57 @@ Discourse.Search = { }; } - return Discourse.ajax('/search', { data: data }); + var promise = Discourse.ajax('/search', { data: data }); + + promise.then(function(results){ + // Topics might not be included + if (!results.topics) { results.topics = []; } + + var topicMap = {}; + results.topics = results.topics.map(function(topic){ + topic = Discourse.Topic.create(topic); + topicMap[topic.id] = topic; + return topic; + }); + + results.posts = results.posts.map(function(post){ + post = Discourse.Post.create(post); + post.set('topic', topicMap[post.topic_id]); + return post; + }); + + results.users = results.users.map(function(user){ + user = Discourse.User.create(user); + return user; + }); + + results.categories = results.categories.map(function(category){ + category = Discourse.Category.create(category); + return category; + }); + + var r = results.grouped_search_result; + results.resultTypes = []; + + // TODO: consider refactoring front end to take a better structure + [['topic','posts'],['user','users'],['category','categories']].forEach(function(pair){ + var type = pair[0], name = pair[1]; + if(results[name].length > 0) { + results.resultTypes.push({ + results: results[name], + displayType: (opts.searchContext && opts.searchContext.type === 'topic' && type === 'topic') ? 'post' : type, + type: type, + more: r['more_' + name] + }); + } + }); + + var noResults = !!((results.topics.length === 0) && (results.posts.length === 0) && (results.categories.length === 0)); + + return noResults ? null : Em.Object.create(results); + }); + + return promise; } }; diff --git a/app/assets/javascripts/discourse/views/choose-topic.js.es6 b/app/assets/javascripts/discourse/views/choose-topic.js.es6 index 70cb7a810..fafcbd2e5 100644 --- a/app/assets/javascripts/discourse/views/choose-topic.js.es6 +++ b/app/assets/javascripts/discourse/views/choose-topic.js.es6 @@ -30,9 +30,9 @@ export default Discourse.View.extend({ self.setProperties({ topics: null, loading: false }); return; } - Discourse.Search.forTerm(title, {typeFilter: 'topic', searchForId: true}).then(function (facets) { - if (facets && facets[0] && facets[0].results) { - self.set('topics', facets[0].results); + Discourse.Search.forTerm(title, {typeFilter: 'topic', searchForId: true}).then(function (results) { + if (results && results.posts && results.posts.length > 0) { + self.set('topics', results.posts.mapBy('topic')); } else { self.setProperties({ topics: null, loading: false }); }