From 8a5e12f6f3dc8d7952e94df9a7079ef86d47ad62 Mon Sep 17 00:00:00 2001 From: Arsen Melikyan Date: Sun, 1 Nov 2015 16:58:28 +0400 Subject: [PATCH] find users news by typing their usernames in search box and fix FF CSS issue --- client/less/main.less | 3 ++- server/boot/story.js | 22 ++++++++++++++++++++++ server/views/stories/news-nav.jade | 9 +++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/client/less/main.less b/client/less/main.less index 8d3ceb2ad1..64b89d0f48 100644 --- a/client/less/main.less +++ b/client/less/main.less @@ -814,7 +814,8 @@ iframe.iphone { .news-box-search { @media (min-width: 768px) { - margin-top: -50px; + margin-top: -30px; + padding-bottom: 20px; } @media (max-width: 767px) { padding: 5px; diff --git a/server/boot/story.js b/server/boot/story.js index 8e00af3f16..58a4c53fc4 100755 --- a/server/boot/story.js +++ b/server/boot/story.js @@ -62,6 +62,7 @@ module.exports = function(app) { var findStoryById = observeMethod(Story, 'findById'); var countStories = observeMethod(Story, 'count'); + router.post('/news/userstories', userStories); router.get('/news/hot', hotJSON); router.get('/stories/hotStories', hotJSON); router.get( @@ -104,6 +105,27 @@ module.exports = function(app) { ); } + function userStories({ body: { search = '' } = {} }, res, next) { + if (!search || typeof search !== 'string') { + return res.sendStatus(404); + } + + return app.dataSources.db.connector + .collection('story') + .find({ + 'author.username': search.replace('$', '') + }) + .toArray(function(err, items) { + if (err) { + return next(err); + } + if (items && items.length !== 0) { + return res.json(items.sort(sortByRank)); + } + return res.sendStatus(404); + }); + } + function hot(req, res) { return res.render('stories/index', { title: 'Top Stories on Camper News', diff --git a/server/views/stories/news-nav.jade b/server/views/stories/news-nav.jade index 668e25952c..2fdd18582f 100644 --- a/server/views/stories/news-nav.jade +++ b/server/views/stories/news-nav.jade @@ -31,11 +31,16 @@ script. }); function executeSearch() { $('#stories').empty(); - var searchTerm = $('#searchArea').val(); + var searchTerm = $('#searchArea').val(), + url = '/stories/search'; + if (searchTerm.match(/^\@\w+$/)) { + url = '/news/userstories'; + searchTerm = searchTerm.match(/^\@\w+$/)[0].split('@')[1]; + } var getLinkedName = function getLinkedName(name) { return name.toLowerCase().replace(/\s/g, '-'); } - $.post('/stories/search', { search: searchTerm }) + $.post(url, { search: searchTerm }) .fail(function(xhr, textStatus, errorThrown) { $('#search-results').empty(); var div = document.createElement("div");