mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-01 20:30:07 +01:00
Merge remote-tracking branch 'origin/master' into 0.7.0
This commit is contained in:
@@ -233,10 +233,6 @@ define('forum/category', [
|
||||
|
||||
topic.hide().fadeIn('slow');
|
||||
|
||||
socket.emit('categories.getPageCount', ajaxify.variables.get('category_id'), function(err, newPageCount) {
|
||||
pagination.recreatePaginationLinks(newPageCount);
|
||||
});
|
||||
|
||||
topic.find('span.timeago').timeago();
|
||||
app.createUserTooltips();
|
||||
updateTopicCount();
|
||||
|
||||
@@ -11,69 +11,13 @@ define('forum/pagination', function() {
|
||||
pagination.currentPage = parseInt(currentPage, 10);
|
||||
pagination.pageCount = parseInt(pageCount, 10);
|
||||
|
||||
pagination.recreatePaginationLinks(pageCount);
|
||||
|
||||
$('.pagination')
|
||||
.on('click', '.previous', function() {
|
||||
return pagination.loadPage(pagination.currentPage - 1);
|
||||
}).on('click', '.next', function() {
|
||||
return pagination.loadPage(pagination.currentPage + 1);
|
||||
}).on('click', '.select_page', function(e) {
|
||||
e.preventDefault();
|
||||
bootbox.prompt('Enter page number:', function(pageNum) {
|
||||
pagination.loadPage(pageNum);
|
||||
});
|
||||
$('.pagination').on('click', '.select_page', function(e) {
|
||||
e.preventDefault();
|
||||
bootbox.prompt('Enter page number:', function(pageNum) {
|
||||
pagination.loadPage(pageNum);
|
||||
});
|
||||
};
|
||||
|
||||
pagination.recreatePaginationLinks = function(newPageCount) {
|
||||
pagination.pageCount = parseInt(newPageCount, 10);
|
||||
|
||||
var pagesToShow = determinePagesToShow();
|
||||
|
||||
var html = '';
|
||||
for(var i=0; i<pagesToShow.length; ++i) {
|
||||
if(i > 0) {
|
||||
if (pagesToShow[i] - 1 !== pagesToShow[i-1]) {
|
||||
html += '<li><a class="select_page" href="#">|</a></li>';
|
||||
}
|
||||
}
|
||||
html += '<li class="page" data-page="' + pagesToShow[i] + '"><a href="#">' + pagesToShow[i] + '</a></li>';
|
||||
}
|
||||
|
||||
$('.pagination li.page').remove();
|
||||
$('.pagination li .select_page').parent().remove();
|
||||
$(html).insertAfter($('.pagination li.previous'));
|
||||
|
||||
updatePageLinks();
|
||||
};
|
||||
|
||||
function determinePagesToShow() {
|
||||
var pagesToShow = [1];
|
||||
if(pagination.pageCount !== 1) {
|
||||
pagesToShow.push(pagination.pageCount);
|
||||
}
|
||||
|
||||
var previous = pagination.currentPage - 1;
|
||||
var next = pagination.currentPage + 1;
|
||||
|
||||
if(previous > 1 && pagesToShow.indexOf(previous) === -1) {
|
||||
pagesToShow.push(previous);
|
||||
}
|
||||
|
||||
if(next < pagination.pageCount && pagesToShow.indexOf(next) === -1) {
|
||||
pagesToShow.push(next);
|
||||
}
|
||||
|
||||
if(pagesToShow.indexOf(pagination.currentPage) === -1) {
|
||||
pagesToShow.push(pagination.currentPage);
|
||||
}
|
||||
|
||||
pagesToShow.sort(function(a, b) {
|
||||
return parseInt(a, 10) - parseInt(b, 10);
|
||||
});
|
||||
return pagesToShow;
|
||||
}
|
||||
};
|
||||
|
||||
pagination.loadPage = function(page, callback) {
|
||||
page = parseInt(page, 10);
|
||||
@@ -89,20 +33,5 @@ define('forum/pagination', function() {
|
||||
return true;
|
||||
};
|
||||
|
||||
function updatePageLinks() {
|
||||
$('.pagination').toggleClass('hide', pagination.pageCount === 0 || pagination.pageCount === 1);
|
||||
|
||||
$('.pagination .next').toggleClass('disabled', pagination.currentPage === pagination.pageCount);
|
||||
$('.pagination .previous').toggleClass('disabled', pagination.currentPage === 1);
|
||||
|
||||
$('.pagination .page').removeClass('active');
|
||||
$('.pagination .page[data-page="' + pagination.currentPage + '"]').addClass('active');
|
||||
$('.pagination .page').each(function(index, element) {
|
||||
var li = $(this);
|
||||
var page = li.attr('data-page');
|
||||
li.find('a').attr('href', window.location.pathname + '?page=' + page);
|
||||
});
|
||||
}
|
||||
|
||||
return pagination;
|
||||
});
|
||||
|
||||
@@ -100,7 +100,7 @@ define('forum/users', function() {
|
||||
doSearch();
|
||||
});
|
||||
|
||||
$('.pagination').on('click', 'a', function() {
|
||||
$('.users').on('click', '.pagination a', function() {
|
||||
doSearch($(this).attr('data-page'));
|
||||
return false;
|
||||
})
|
||||
@@ -150,9 +150,9 @@ define('forum/users', function() {
|
||||
return reset();
|
||||
}
|
||||
|
||||
templates.parse('users', 'pages', data, function(html) {
|
||||
$('.pagination').html(html);
|
||||
});
|
||||
templates.parse('partials/paginator', {pagination: data.pagination}, function(html) {
|
||||
$('.pagination-container').replaceWith(html);
|
||||
})
|
||||
|
||||
templates.parse('users', 'users', data, function(html) {
|
||||
translator.translate(html, function(translated) {
|
||||
|
||||
@@ -9,6 +9,7 @@ var categoriesController = {},
|
||||
topics = require('../topics'),
|
||||
meta = require('../meta'),
|
||||
plugins = require('../plugins'),
|
||||
pagination = require('../pagination'),
|
||||
helpers = require('./helpers'),
|
||||
utils = require('../../public/src/utils');
|
||||
|
||||
@@ -255,16 +256,11 @@ categoriesController.get = function(req, res, next) {
|
||||
data.currentPage = page;
|
||||
data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1;
|
||||
|
||||
if (!res.locals.isAPI) {
|
||||
// Paginator for noscript
|
||||
data.pages = [];
|
||||
for(var x=1;x<=data.pageCount;x++) {
|
||||
data.pages.push({
|
||||
page: x,
|
||||
active: x === parseInt(page, 10)
|
||||
});
|
||||
}
|
||||
}
|
||||
pagination.create(data.currentPage, data.pageCount, data);
|
||||
|
||||
data.pagination.rel.forEach(function(rel) {
|
||||
res.locals.linkTags.push(rel);
|
||||
});
|
||||
|
||||
res.render('category', data);
|
||||
});
|
||||
|
||||
@@ -13,6 +13,7 @@ var topicsController = {},
|
||||
privileges = require('../privileges'),
|
||||
plugins = require('../plugins'),
|
||||
helpers = require('./helpers'),
|
||||
pagination = require('../pagination'),
|
||||
utils = require('../../public/src/utils');
|
||||
|
||||
topicsController.get = function(req, res, next) {
|
||||
@@ -257,16 +258,11 @@ topicsController.get = function(req, res, next) {
|
||||
|
||||
topics.increaseViewCount(tid);
|
||||
|
||||
if (!res.locals.isAPI) {
|
||||
// Paginator for noscript
|
||||
data.pages = [];
|
||||
for(var x=1; x<=data.pageCount; x++) {
|
||||
data.pages.push({
|
||||
page: x,
|
||||
active: x === parseInt(page, 10)
|
||||
});
|
||||
}
|
||||
}
|
||||
pagination.create(data.currentPage, data.pageCount, data);
|
||||
|
||||
data.pagination.rel.forEach(function(rel) {
|
||||
res.locals.linkTags.push(rel);
|
||||
});
|
||||
|
||||
res.render('topic', data);
|
||||
});
|
||||
|
||||
@@ -5,8 +5,8 @@ var winston = require('winston');
|
||||
|
||||
var ratelimit = {};
|
||||
|
||||
var allowedCallsPerSecond = 20;
|
||||
|
||||
var allowedCalls = 100;
|
||||
var timeframe = 10000;
|
||||
|
||||
ratelimit.isFlooding = function(socket) {
|
||||
socket.callsPerSecond = socket.callsPerSecond || 0;
|
||||
@@ -18,12 +18,12 @@ ratelimit.isFlooding = function(socket) {
|
||||
var now = Date.now();
|
||||
socket.elapsedTime += now - socket.lastCallTime;
|
||||
|
||||
if (socket.callsPerSecond > allowedCallsPerSecond && socket.elapsedTime < 1000) {
|
||||
if (socket.callsPerSecond > allowedCalls && socket.elapsedTime < timeframe) {
|
||||
winston.warn('Flooding detected! Calls : ' + socket.callsPerSecond + ', Duration : ' + socket.elapsedTime);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (socket.elapsedTime >= 1000) {
|
||||
if (socket.elapsedTime >= timeframe) {
|
||||
socket.elapsedTime = 0;
|
||||
socket.callsPerSecond = 0;
|
||||
}
|
||||
|
||||
66
src/pagination.js
Normal file
66
src/pagination.js
Normal file
@@ -0,0 +1,66 @@
|
||||
'use strict';
|
||||
|
||||
var pagination = {};
|
||||
|
||||
pagination.create = function(currentPage, pageCount, data) {
|
||||
|
||||
if (pageCount <= 1) {
|
||||
data.pagination = {
|
||||
prev: {page: 1, active: currentPage > 1},
|
||||
next: {page: 1, active: currentPage < pageCount},
|
||||
rel: [],
|
||||
pages: []
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
var pagesToShow = [1];
|
||||
if (pageCount !== 1) {
|
||||
pagesToShow.push(pageCount);
|
||||
}
|
||||
|
||||
currentPage = parseInt(currentPage, 10) || 1;
|
||||
var previous = Math.max(1, currentPage - 1);
|
||||
var next = Math.min(pageCount, currentPage + 1);
|
||||
|
||||
var startPage = currentPage - 2;
|
||||
for(var i=0; i<5; ++i) {
|
||||
var p = startPage + i;
|
||||
if (p >= 1 && p <= pageCount && pagesToShow.indexOf(p) === -1) {
|
||||
pagesToShow.push(startPage + i);
|
||||
}
|
||||
}
|
||||
|
||||
pagesToShow.sort(function(a, b) {
|
||||
return a - b;
|
||||
});
|
||||
|
||||
var pages = pagesToShow.map(function(page) {
|
||||
return {page: page, active: page === currentPage};
|
||||
});
|
||||
|
||||
data.pagination = {
|
||||
prev: {page: previous, active: currentPage > 1},
|
||||
next: {page: next, active: currentPage < pageCount},
|
||||
rel: [],
|
||||
pages: pages
|
||||
};
|
||||
|
||||
if (currentPage < pageCount) {
|
||||
data.pagination.rel.push({
|
||||
rel: 'next',
|
||||
href: '?page=' + next
|
||||
});
|
||||
}
|
||||
|
||||
if (currentPage > 1) {
|
||||
data.pagination.rel.push({
|
||||
rel: 'prev',
|
||||
href: '?page=' + previous
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
module.exports = pagination;
|
||||
@@ -147,7 +147,7 @@ function onMessage(socket, payload) {
|
||||
socket.previousEvents.shift();
|
||||
}
|
||||
|
||||
if (ratelimit.isFlooding(socket)) {
|
||||
if (!eventName.startsWith('admin.') && ratelimit.isFlooding(socket)) {
|
||||
winston.warn('[socket.io] Too many emits! Disconnecting uid : ' + socket.uid + '. Events : ' + socket.previousEvents);
|
||||
return socket.disconnect();
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
var async = require('async'),
|
||||
meta = require('../meta'),
|
||||
user = require('../user'),
|
||||
pagination = require('../pagination'),
|
||||
db = require('../database');
|
||||
|
||||
module.exports = function(User) {
|
||||
@@ -49,23 +50,19 @@ module.exports = function(User) {
|
||||
},
|
||||
function(userData, next) {
|
||||
|
||||
var pages = [];
|
||||
if (matchCount > resultsPerPage) {
|
||||
pageCount = Math.ceil(matchCount / resultsPerPage);
|
||||
var currentPage = Math.max(1, Math.ceil((start + 1) / resultsPerPage));
|
||||
for(var i=1; i<=pageCount; ++i) {
|
||||
pages.push({page: i, active: i === currentPage});
|
||||
}
|
||||
}
|
||||
|
||||
var diff = process.hrtime(startTime);
|
||||
var timing = (diff[0] * 1e3 + diff[1] / 1e6).toFixed(1);
|
||||
next(null, {
|
||||
var data = {
|
||||
timing: timing,
|
||||
users: userData,
|
||||
matchCount: matchCount,
|
||||
pages: pages
|
||||
});
|
||||
matchCount: matchCount
|
||||
};
|
||||
|
||||
var currentPage = Math.max(1, Math.ceil((start + 1) / resultsPerPage));
|
||||
pageCount = Math.ceil(matchCount / resultsPerPage);
|
||||
pagination.create(currentPage, pageCount, data);
|
||||
|
||||
next(null, data);
|
||||
}
|
||||
], callback);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user