FIX: broken search for admins when selecting topics

This commit is contained in:
Sam 2014-09-04 12:21:57 +10:00
parent d6a5552340
commit b8c36bd2da
3 changed files with 55 additions and 53 deletions

View file

@ -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);

View file

@ -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;
}
};

View file

@ -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 });
}