getItem: check for secondary id match, then only look for exact title or original title match

This commit is contained in:
rolux 2013-03-06 10:02:45 +00:00
parent b802dfa296
commit 0372eff998

View file

@ -714,41 +714,64 @@ pandora.getInfoHeight = function(includeHidden) {
pandora.getItem = function(state, str, callback) { pandora.getItem = function(state, str, callback) {
if (state.type == pandora.site.itemName.plural.toLowerCase()) { if (state.type == pandora.site.itemName.plural.toLowerCase()) {
var sortKey = Ox.getObjectById(pandora.site.itemKeys, 'votes') var secondaryId = pandora.site.itemKeys.filter(function(key) {
return key.secondaryId;
}).map(function(key) {
return key.id;
})[0],
sortKey = Ox.getObjectById(pandora.site.itemKeys, 'votes')
? 'votes' ? 'votes'
: 'timesaccessed'; : 'timesaccessed';
Ox.getObjectById(pandora.site.itemKeys, 'alt')
pandora.api.get({id: str, keys: ['id']}, function(result) { pandora.api.get({id: str, keys: ['id']}, function(result) {
if (result.status.code == 200) { if (result.status.code == 200) {
state.item = result.data.id; state.item = result.data.id;
callback(); callback();
} else { } else {
pandora.api.find({ (secondaryId ? pandora.api.find : Ox.noop)({
query: { query: {
conditions: [{key: 'title', value: str, operator: '='}], conditions: [{key: secondaryId, value: str, operator: '=='}],
operator: '&' operator: '&'
}, },
sort: [{key: sortKey, operator: ''}], sort: [{key: sortKey, operator: '-'}],
range: [0, 1],
keys: ['id']
}, function(result) {
if (result && result.data.items.length) {
state.item = result.data.items[0].id;
callback();
} else {
pandora.api.find({
query: {
conditions: [{key: 'title', value: str, operator: '=='}],
operator: '&'
},
sort: [{key: sortKey, operator: '-'}],
range: [0, 100], range: [0, 100],
keys: ['id', 'title', sortKey] keys: ['id', 'title', sortKey]
}, function(result) { }, function(result) {
if (result.data.items.length) { if (result.data.items.length) {
var items = Ox.filter(Ox.map(result.data.items, function(item) { var regexp = new RegExp('^' + Ox.escapeRegExp(str) + '$', 'i'),
// test if exact match or word match items = result.data.items.map(function(item) {
var sort = new RegExp('^' + str + '$', 'i').test(item.title) ? 2000000 return {
: new RegExp('\\b' + str + '\\b', 'i').test(item.title) ? 1000000 : 0; id: item.id,
return sort ? {id: item.id, sort: sort + (parseInt(item[sortKey]) || 0)} : null; // prefer title match over originalTitle match
// fixme: remove the (...|| 0) check once the backend sends correct data sort: (item.title == str ? 1000000 : 0)
})); + (parseInt(item[sortKey]) || 0)
if (items.length) { // fixme: remove the (...|| 0) check
// once the backend sends correct data
};
});
state.item = items.sort(function(a, b) { state.item = items.sort(function(a, b) {
return b.sort - a.sort; return b.sort - a.sort;
})[0].id; })[0].id;
} }
}
callback(); callback();
}); });
} }
}); });
}
});
} else if (state.type == 'texts') { } else if (state.type == 'texts') {
pandora.api.getText({id: str}, function(result) { pandora.api.getText({id: str}, function(result) {
if (result.status.code == 200) { if (result.status.code == 200) {