Fix story search not closing connections
Now story search will use loopbacks mongo connection to do search
This commit is contained in:
@ -2,15 +2,12 @@ var Rx = require('rx'),
|
|||||||
assign = require('object.assign'),
|
assign = require('object.assign'),
|
||||||
sanitizeHtml = require('sanitize-html'),
|
sanitizeHtml = require('sanitize-html'),
|
||||||
moment = require('moment'),
|
moment = require('moment'),
|
||||||
mongodb = require('mongodb'),
|
|
||||||
debug = require('debug')('freecc:cntr:story'),
|
debug = require('debug')('freecc:cntr:story'),
|
||||||
utils = require('../utils'),
|
utils = require('../utils'),
|
||||||
observeMethod = require('../utils/rx').observeMethod,
|
observeMethod = require('../utils/rx').observeMethod,
|
||||||
saveUser = require('../utils/rx').saveUser,
|
saveUser = require('../utils/rx').saveUser,
|
||||||
saveInstance = require('../utils/rx').saveInstance,
|
saveInstance = require('../utils/rx').saveInstance,
|
||||||
MongoClient = mongodb.MongoClient,
|
validator = require('validator');
|
||||||
validator = require('validator'),
|
|
||||||
secrets = require('../../config/secrets');
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ifNoUser401,
|
ifNoUser401,
|
||||||
@ -206,45 +203,54 @@ module.exports = function(app) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getStories(req, res, next) {
|
function getStories({ body: { search = '' } = {} }, res, next) {
|
||||||
MongoClient.connect(secrets.db, function(err, database) {
|
if (!search || typeof search !== 'string') {
|
||||||
if (err) {
|
return res.sendStatus(404);
|
||||||
return next(err);
|
}
|
||||||
|
|
||||||
|
const query = {
|
||||||
|
'$text': {
|
||||||
|
// protect against NoSQL injection
|
||||||
|
'$search': search.replace('$', '')
|
||||||
}
|
}
|
||||||
database.collection('story').find({
|
};
|
||||||
'$text': {
|
|
||||||
'$search': req.body.data ? req.body.data.searchValue : ''
|
const fields = {
|
||||||
}
|
headline: 1,
|
||||||
}, {
|
timePosted: 1,
|
||||||
headline: 1,
|
link: 1,
|
||||||
timePosted: 1,
|
description: 1,
|
||||||
link: 1,
|
rank: 1,
|
||||||
description: 1,
|
upVotes: 1,
|
||||||
rank: 1,
|
author: 1,
|
||||||
upVotes: 1,
|
image: 1,
|
||||||
author: 1,
|
storyLink: 1,
|
||||||
image: 1,
|
metaDescription: 1,
|
||||||
storyLink: 1,
|
textScore: {
|
||||||
metaDescription: 1,
|
$meta: 'textScore'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
sort: {
|
||||||
textScore: {
|
textScore: {
|
||||||
$meta: 'textScore'
|
$meta: 'textScore'
|
||||||
}
|
}
|
||||||
}, {
|
}
|
||||||
sort: {
|
};
|
||||||
textScore: {
|
|
||||||
$meta: 'textScore'
|
return app.dataSources.db.connector
|
||||||
}
|
.collection('story')
|
||||||
}
|
.find(query, fields, options)
|
||||||
}).toArray(function(err, items) {
|
.toArray(function(err, items) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
if (items !== null && items.length !== 0) {
|
if (items && items.length !== 0) {
|
||||||
return res.json(items);
|
return res.json(items);
|
||||||
}
|
}
|
||||||
return res.sendStatus(404);
|
return res.sendStatus(404);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function upvote(req, res, next) {
|
function upvote(req, res, next) {
|
||||||
|
@ -35,19 +35,14 @@ script.
|
|||||||
var getLinkedName = function getLinkedName(name) {
|
var getLinkedName = function getLinkedName(name) {
|
||||||
return name.toLowerCase().replace(/\s/g, '-');
|
return name.toLowerCase().replace(/\s/g, '-');
|
||||||
}
|
}
|
||||||
$.post('/stories/search',
|
$.post('/stories/search', { search: searchTerm })
|
||||||
{
|
.fail(function(xhr, textStatus, errorThrown) {
|
||||||
data: {
|
|
||||||
searchValue: searchTerm
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.fail(function (xhr, textStatus, errorThrown) {
|
|
||||||
$('#search-results').empty();
|
$('#search-results').empty();
|
||||||
var div = document.createElement("div");
|
var div = document.createElement("div");
|
||||||
$(div).html("<h3 class='text-center text-warning dotted-underline'><em>No Results Found</em></h3>");
|
$(div).html("<h3 class='text-center text-warning dotted-underline'><em>No Results Found</em></h3>");
|
||||||
$(div).appendTo($('#search-results'));
|
$(div).appendTo($('#search-results'));
|
||||||
})
|
})
|
||||||
.done(function (data, textStatus, xhr) {
|
.done(function(data, textStatus, xhr) {
|
||||||
$('#search-results').empty();
|
$('#search-results').empty();
|
||||||
var spacer = document.createElement('div');
|
var spacer = document.createElement('div');
|
||||||
$(spacer).html("<div class='spacer'></div>");
|
$(spacer).html("<div class='spacer'></div>");
|
||||||
|
Reference in New Issue
Block a user