mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-28 17:46:16 +01:00
ability to search posts by user
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
{
|
{
|
||||||
"results_matching": "%1 result(s) matching \"%2\", (%3 seconds)",
|
"results_matching": "%1 result(s) matching \"%2\", (%3 seconds)",
|
||||||
"no-matches": "No matches found",
|
"no-matches": "No matches found",
|
||||||
"in": "In"
|
"in": "In",
|
||||||
|
"by": "By",
|
||||||
|
"posted-by": "Posted by"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -437,7 +437,7 @@ app.uid = null;
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
var input = $(this).find('input');
|
var input = $(this).find('input');
|
||||||
|
|
||||||
search.query(input.val(), 'posts', function() {
|
search.query({term: input.val(), in: 'posts'}, function() {
|
||||||
input.val('');
|
input.val('');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,18 +1,51 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
/* globals app, define, utils, socket*/
|
||||||
|
|
||||||
define('forum/search', ['search'], function(searchModule) {
|
define('forum/search', ['search'], function(searchModule) {
|
||||||
var Search = {};
|
var Search = {};
|
||||||
|
|
||||||
Search.init = function() {
|
Search.init = function() {
|
||||||
var searchQuery = $('#results').attr('data-search-query');
|
var searchQuery = $('#results').attr('data-search-query');
|
||||||
var regexes = [];
|
|
||||||
var searchTerms = searchQuery.split(' ');
|
|
||||||
|
|
||||||
$('#advanced-search input').val(searchQuery);
|
$('#advanced-search #search-input').val(searchQuery);
|
||||||
var params = utils.params();
|
var params = utils.params();
|
||||||
|
var select = $('#advanced-search select');
|
||||||
if (params && params.in) {
|
if (params && params.in) {
|
||||||
$('#advanced-search select').val(params.in);
|
select.val(params.in);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (params && params.by) {
|
||||||
|
$('.by-container #posted-by-input').val(params.by);
|
||||||
|
}
|
||||||
|
|
||||||
|
select.on('change', function() {
|
||||||
|
$('.by-container').toggleClass('hide', select.val() !== 'posts');
|
||||||
|
});
|
||||||
|
|
||||||
|
highlightMatches(searchQuery);
|
||||||
|
|
||||||
|
$('#advanced-search').off('submit').on('submit', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
var input = $(this).find('#search-input');
|
||||||
|
var searchIn = $(this).find('select');
|
||||||
|
var postedBy = $(this).find('#posted-by-input');
|
||||||
|
|
||||||
|
searchModule.query({
|
||||||
|
term: input.val(),
|
||||||
|
in: searchIn.val(),
|
||||||
|
by: postedBy.val()
|
||||||
|
}, function() {
|
||||||
|
input.val('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
enableAutoComplete();
|
||||||
|
};
|
||||||
|
|
||||||
|
function highlightMatches(searchQuery) {
|
||||||
|
var searchTerms = searchQuery.split(' ');
|
||||||
|
var regexes = [];
|
||||||
for (var i=0; i<searchTerms.length; ++i) {
|
for (var i=0; i<searchTerms.length; ++i) {
|
||||||
var regex = new RegExp(searchTerms[i], 'gi');
|
var regex = new RegExp(searchTerms[i], 'gi');
|
||||||
regexes.push({regex: regex, term: searchTerms[i]});
|
regexes.push({regex: regex, term: searchTerms[i]});
|
||||||
@@ -26,18 +59,30 @@ define('forum/search', ['search'], function(searchModule) {
|
|||||||
}
|
}
|
||||||
result.html(text).find('img').addClass('img-responsive');
|
result.html(text).find('img').addClass('img-responsive');
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
$('#advanced-search').off('submit').on('submit', function(e) {
|
function enableAutoComplete() {
|
||||||
e.preventDefault();
|
var input = $('.by-container #posted-by-input');
|
||||||
var input = $(this).find('input');
|
input.autocomplete({
|
||||||
var searchIn = $(this).find('select');
|
delay: 100,
|
||||||
|
source: function(request, response) {
|
||||||
|
socket.emit('user.search', {query: request.term}, function(err, result) {
|
||||||
|
if (err) {
|
||||||
|
return app.alertError(err.message);
|
||||||
|
}
|
||||||
|
|
||||||
searchModule.query(input.val(), searchIn.val(), function() {
|
if (result && result.users) {
|
||||||
input.val('');
|
var names = result.users.map(function(user) {
|
||||||
});
|
return user && user.username;
|
||||||
|
});
|
||||||
|
response(names);
|
||||||
|
}
|
||||||
|
$('.ui-autocomplete a').attr('data-ajaxify', 'false');
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
return Search;
|
return Search;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -115,21 +115,20 @@ define('forum/users', function() {
|
|||||||
|
|
||||||
notify.html('<i class="fa fa-spinner fa-spin"></i>');
|
notify.html('<i class="fa fa-spinner fa-spin"></i>');
|
||||||
|
|
||||||
socket.emit('user.search', username, function(err, data) {
|
socket.emit('user.search', {query: username}, function(err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
reset();
|
reset();
|
||||||
return app.alertError(err.message);
|
return app.alertError(err.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
reset();
|
return reset();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
templates.parse('users', 'users', data, function(html) {
|
templates.parse('users', 'users', data, function(html) {
|
||||||
translator.translate(html, function(translated) {
|
translator.translate(html, function(translated) {
|
||||||
$('#users-container').html(translated);
|
$('#users-container').html(translated);
|
||||||
|
|
||||||
if (!data.users.length) {
|
if (!data.users.length) {
|
||||||
translator.translate('[[error:no-user]]', function(translated) {
|
translator.translate('[[error:no-user]]', function(translated) {
|
||||||
notify.html(translated);
|
notify.html(translated);
|
||||||
|
|||||||
@@ -7,7 +7,11 @@ define('search', ['navigator'], function(nav) {
|
|||||||
current: {}
|
current: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
Search.query = function(term, searchIn, callback) {
|
Search.query = function(data, callback) {
|
||||||
|
var term = data.term;
|
||||||
|
var searchIn = data.in || 'posts';
|
||||||
|
var postedBy = data.by || '';
|
||||||
|
|
||||||
// Detect if a tid was specified
|
// Detect if a tid was specified
|
||||||
var topicSearch = term.match(/in:topic-([\d]+)/);
|
var topicSearch = term.match(/in:topic-([\d]+)/);
|
||||||
|
|
||||||
@@ -19,8 +23,11 @@ define('search', ['navigator'], function(nav) {
|
|||||||
} catch(e) {
|
} catch(e) {
|
||||||
return app.alertError('[[error:invalid-search-term]]');
|
return app.alertError('[[error:invalid-search-term]]');
|
||||||
}
|
}
|
||||||
|
var query = {in: searchIn};
|
||||||
ajaxify.go('search/' + term + (searchIn ? '?in=' + searchIn : ''));
|
if (postedBy && searchIn === 'posts') {
|
||||||
|
query.by = postedBy;
|
||||||
|
}
|
||||||
|
ajaxify.go('search/' + term + '?' + decodeURIComponent($.param(query)));
|
||||||
callback();
|
callback();
|
||||||
} else {
|
} else {
|
||||||
var cleanedTerm = term.replace(topicSearch[0], ''),
|
var cleanedTerm = term.replace(topicSearch[0], ''),
|
||||||
|
|||||||
@@ -28,7 +28,12 @@ searchController.search = function(req, res, next) {
|
|||||||
|
|
||||||
req.params.term = validator.escape(req.params.term);
|
req.params.term = validator.escape(req.params.term);
|
||||||
|
|
||||||
search.search(req.params.term, req.query.in, uid, function(err, results) {
|
search.search({
|
||||||
|
query: req.params.term,
|
||||||
|
searchIn: req.query.in,
|
||||||
|
postedBy: req.query.by,
|
||||||
|
uid: uid
|
||||||
|
}, function(err, results) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ var search = {};
|
|||||||
|
|
||||||
module.exports = search;
|
module.exports = search;
|
||||||
|
|
||||||
search.search = function(query, searchIn, uid, callback) {
|
search.search = function(data, callback) {
|
||||||
function done(err, data) {
|
function done(err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@@ -20,12 +20,16 @@ search.search = function(query, searchIn, uid, callback) {
|
|||||||
result.search_query = query;
|
result.search_query = query;
|
||||||
result[searchIn] = data;
|
result[searchIn] = data;
|
||||||
result.matchCount = data.length;
|
result.matchCount = data.length;
|
||||||
|
result.hidePostedBy = searchIn !== 'posts';
|
||||||
result.time = (process.elapsedTimeSince(start) / 1000).toFixed(2);
|
result.time = (process.elapsedTimeSince(start) / 1000).toFixed(2);
|
||||||
callback(null, result);
|
callback(null, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
var start = process.hrtime();
|
var start = process.hrtime();
|
||||||
searchIn = searchIn || 'posts';
|
|
||||||
|
var query = data.query;
|
||||||
|
var searchIn = data.searchIn || 'posts';
|
||||||
|
var uid = data.uid || 0;
|
||||||
|
|
||||||
var result = {
|
var result = {
|
||||||
posts: [],
|
posts: [],
|
||||||
@@ -34,7 +38,7 @@ search.search = function(query, searchIn, uid, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (searchIn === 'posts') {
|
if (searchIn === 'posts') {
|
||||||
searchInPosts(query, uid, done);
|
searchInPosts(query, data.postedBy, uid, done);
|
||||||
} else if (searchIn === 'users') {
|
} else if (searchIn === 'users') {
|
||||||
searchInUsers(query, done);
|
searchInUsers(query, done);
|
||||||
} else if (searchIn === 'tags') {
|
} else if (searchIn === 'tags') {
|
||||||
@@ -44,13 +48,20 @@ search.search = function(query, searchIn, uid, callback) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function searchInPosts(query, uid, callback) {
|
function searchInPosts(query, postedBy, uid, callback) {
|
||||||
async.parallel({
|
async.parallel({
|
||||||
pids: function(next) {
|
pids: function(next) {
|
||||||
searchQuery('post', query, next);
|
searchQuery('post', query, next);
|
||||||
},
|
},
|
||||||
tids: function(next) {
|
tids: function(next) {
|
||||||
searchQuery('topic', query, next);
|
searchQuery('topic', query, next);
|
||||||
|
},
|
||||||
|
postedByUid: function(next) {
|
||||||
|
if (postedBy) {
|
||||||
|
user.getUidByUsername(postedBy, next);
|
||||||
|
} else {
|
||||||
|
next(null, 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, function (err, results) {
|
}, function (err, results) {
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -75,6 +86,14 @@ function searchInPosts(query, uid, callback) {
|
|||||||
},
|
},
|
||||||
function(pids, next) {
|
function(pids, next) {
|
||||||
posts.getPostSummaryByPids(pids, uid, {stripTags: true, parse: false}, next);
|
posts.getPostSummaryByPids(pids, uid, {stripTags: true, parse: false}, next);
|
||||||
|
},
|
||||||
|
function(posts, next) {
|
||||||
|
if (postedBy) {
|
||||||
|
posts = posts.filter(function(post) {
|
||||||
|
return post && parseInt(post.uid, 10) === parseInt(results.postedByUid, 10);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
next(null, posts);
|
||||||
}
|
}
|
||||||
], callback);
|
], callback);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -62,11 +62,14 @@ SocketUser.increaseViewCount = function(socket, uid, callback) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketUser.search = function(socket, username, callback) {
|
SocketUser.search = function(socket, data, callback) {
|
||||||
|
if (!data) {
|
||||||
|
return callback(new Error('[[error:invalid-data]]'))
|
||||||
|
}
|
||||||
if (!socket.uid) {
|
if (!socket.uid) {
|
||||||
return callback(new Error('[[error:not-logged-in]]'));
|
return callback(new Error('[[error:not-logged-in]]'));
|
||||||
}
|
}
|
||||||
user.search({query: username}, callback);
|
user.search({query: data.query}, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Password Reset
|
// Password Reset
|
||||||
|
|||||||
Reference in New Issue
Block a user