mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-28 17:46:16 +01:00
closes #1917
This commit is contained in:
@@ -112,5 +112,6 @@
|
||||
"sort_by": "Sort by",
|
||||
"oldest_to_newest": "Oldest to Newest",
|
||||
"newest_to_oldest": "Newest to Oldest",
|
||||
"most_votes": "Most votes"
|
||||
"most_votes": "Most votes",
|
||||
"most_posts": "Most posts"
|
||||
}
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
"use strict";
|
||||
/* global define, config, templates, app, utils, ajaxify, socket, translator */
|
||||
|
||||
define('forum/category', ['composer', 'forum/pagination', 'forum/infinitescroll', 'share', 'navigator', 'forum/categoryTools'], function(composer, pagination, infinitescroll, share, navigator, categoryTools) {
|
||||
define('forum/category', [
|
||||
'composer',
|
||||
'forum/pagination',
|
||||
'forum/infinitescroll',
|
||||
'share',
|
||||
'navigator',
|
||||
'forum/categoryTools',
|
||||
'sort'
|
||||
], function(composer, pagination, infinitescroll, share, navigator, categoryTools, sort) {
|
||||
var Category = {};
|
||||
|
||||
$(window).on('action:ajaxify.start', function(ev, data) {
|
||||
@@ -33,6 +41,8 @@ define('forum/category', ['composer', 'forum/pagination', 'forum/infinitescroll'
|
||||
|
||||
categoryTools.init(cid);
|
||||
|
||||
sort.handleSort('categoryTopicSort', 'user.setCategorySort', 'category/' + ajaxify.variables.get('category_slug'));
|
||||
|
||||
enableInfiniteLoadingOrPagination();
|
||||
|
||||
if (!config.usePagination) {
|
||||
|
||||
@@ -11,8 +11,9 @@ define('forum/topic', [
|
||||
'forum/topic/events',
|
||||
'forum/topic/browsing',
|
||||
'forum/topic/posts',
|
||||
'navigator'
|
||||
], function(pagination, infinitescroll, threadTools, postTools, events, browsing, posts, navigator) {
|
||||
'navigator',
|
||||
'sort'
|
||||
], function(pagination, infinitescroll, threadTools, postTools, events, browsing, posts, navigator, sort) {
|
||||
var Topic = {},
|
||||
currentUrl = '';
|
||||
|
||||
@@ -44,7 +45,7 @@ define('forum/topic', [
|
||||
threadTools.init(tid, thread_state);
|
||||
events.init();
|
||||
|
||||
handleSorting();
|
||||
sort.handleSort('topicPostSort', 'user.setTopicSort', 'topic/' + ajaxify.variables.get('topic_slug'));
|
||||
|
||||
enableInfiniteLoadingOrPagination();
|
||||
|
||||
@@ -107,21 +108,6 @@ define('forum/topic', [
|
||||
return parts[parts.length - 1] ? parseInt(parts[parts.length - 1], 10) : 0;
|
||||
}
|
||||
|
||||
function handleSorting() {
|
||||
var threadSort = $('.thread-sort');
|
||||
threadSort.find('i').removeClass('fa-check');
|
||||
var currentSetting = threadSort.find('a[data-sort="' + config.topicPostSort + '"]');
|
||||
currentSetting.find('i').addClass('fa-check');
|
||||
|
||||
$('.thread-sort').on('click', 'a', function() {
|
||||
var newSetting = $(this).attr('data-sort');
|
||||
socket.emit('user.setTopicSort', newSetting, function(err) {
|
||||
config.topicPostSort = newSetting;
|
||||
ajaxify.go('topic/' + ajaxify.variables.get('topic_slug'));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function addBlockQuoteHandler() {
|
||||
$('#post-container').on('click', 'blockquote .toggle', function() {
|
||||
var blockQuote = $(this).parent('blockquote');
|
||||
|
||||
26
public/src/modules/sort.js
Normal file
26
public/src/modules/sort.js
Normal file
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
/* globals define, translator, templates */
|
||||
|
||||
define('sort', function() {
|
||||
var module = {};
|
||||
|
||||
module.handleSort = function (field, method, gotoOnSave) {
|
||||
var threadSort = $('.thread-sort');
|
||||
threadSort.find('i').removeClass('fa-check');
|
||||
var currentSetting = threadSort.find('a[data-sort="' + config[field] + '"]');
|
||||
currentSetting.find('i').addClass('fa-check');
|
||||
|
||||
$('.thread-sort').on('click', 'a', function() {
|
||||
var newSetting = $(this).attr('data-sort');
|
||||
socket.emit(method, newSetting, function(err) {
|
||||
if (err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
config[field] = newSetting;
|
||||
ajaxify.go(gotoOnSave);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return module;
|
||||
});
|
||||
@@ -40,10 +40,11 @@ var async = require('async'),
|
||||
topics: function(next) {
|
||||
Categories.getCategoryTopics({
|
||||
cid: data.cid,
|
||||
set: data.set,
|
||||
reverse: data.reverse,
|
||||
start: data.start,
|
||||
stop: data.end,
|
||||
uid: data.uid,
|
||||
targetUid: data.targetUid
|
||||
uid: data.uid
|
||||
}, next);
|
||||
},
|
||||
pageCount: function(next) {
|
||||
|
||||
@@ -29,6 +29,7 @@ module.exports = function(Categories) {
|
||||
function(next) {
|
||||
db.deleteAll([
|
||||
'cid:' + cid + ':tids',
|
||||
'cid:' + cid + ':tids:posts',
|
||||
'cid:' + cid + ':pids',
|
||||
'category:' + cid
|
||||
], next);
|
||||
|
||||
@@ -22,7 +22,7 @@ module.exports = function(Categories) {
|
||||
plugins.fireHook('filter:category.topics.prepare', data, next);
|
||||
},
|
||||
function(data, next) {
|
||||
Categories.getTopicIds(data.targetUid ? 'cid:' + data.cid + ':uid:' + data.targetUid + ':tids' : 'cid:' + data.cid + ':tids', data.start, data.stop, next);
|
||||
Categories.getTopicIds(data.set, data.reverse, data.start, data.stop, next);
|
||||
},
|
||||
function(tids, next) {
|
||||
topics.getTopicsByTids(tids, data.uid, next);
|
||||
@@ -56,8 +56,12 @@ module.exports = function(Categories) {
|
||||
});
|
||||
};
|
||||
|
||||
Categories.getTopicIds = function(set, start, stop, callback) {
|
||||
Categories.getTopicIds = function(set, reverse, start, stop, callback) {
|
||||
if (reverse) {
|
||||
db.getSortedSetRevRange(set, start, stop, callback);
|
||||
} else {
|
||||
db.getSortedSetRange(set, start, stop, callback);
|
||||
}
|
||||
};
|
||||
|
||||
Categories.getTopicIndex = function(tid, callback) {
|
||||
@@ -95,6 +99,9 @@ module.exports = function(Categories) {
|
||||
} else {
|
||||
db.sortedSetAdd('cid:' + cid + ':tids', postData.timestamp, postData.tid, next);
|
||||
}
|
||||
},
|
||||
function(next) {
|
||||
db.sortedSetIncrBy('cid:' + cid + ':tids:posts', 1, postData.tid, next);
|
||||
}
|
||||
], callback);
|
||||
});
|
||||
|
||||
@@ -55,6 +55,7 @@ apiController.getConfig = function(req, res, next) {
|
||||
config['css-buster'] = meta.css.hash;
|
||||
config.requireEmailConfirmation = parseInt(meta.config.requireEmailConfirmation, 10) === 1;
|
||||
config.topicPostSort = meta.config.topicPostSort || 'oldest_to_newest';
|
||||
config.categoryTopicSort = meta.config.categoryTopicSort || 'newest_to_oldest';
|
||||
config.csrf_token = req.csrfToken();
|
||||
config.searchEnabled = plugins.hasListeners('filter:search.query');
|
||||
|
||||
@@ -79,6 +80,7 @@ apiController.getConfig = function(req, res, next) {
|
||||
config.userLang = settings.language || config.defaultLang;
|
||||
config.openOutgoingLinksInNewTab = settings.openOutgoingLinksInNewTab;
|
||||
config.topicPostSort = settings.topicPostSort || config.topicPostSort;
|
||||
config.categoryTopicSort = settings.categoryTopicSort || config.categoryTopicSort;
|
||||
config.topicSearchEnabled = settings.topicSearchEnabled || false;
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
|
||||
@@ -158,11 +158,23 @@ categoriesController.get = function(req, res, next) {
|
||||
topicIndex = 0;
|
||||
}
|
||||
|
||||
var set = 'cid:' + cid + ':tids',
|
||||
reverse = false;
|
||||
|
||||
if (settings.categoryTopicSort === 'newest_to_oldest') {
|
||||
reverse = true;
|
||||
} else if (settings.categoryTopicSort === 'most_posts') {
|
||||
reverse = true;
|
||||
set = 'cid:' + cid + ':tids:posts';
|
||||
}
|
||||
|
||||
var start = (page - 1) * settings.topicsPerPage + topicIndex,
|
||||
end = start + settings.topicsPerPage - 1;
|
||||
|
||||
next(null, {
|
||||
cid: cid,
|
||||
set: set,
|
||||
reverse: reverse,
|
||||
start: start,
|
||||
end: end,
|
||||
uid: uid
|
||||
@@ -171,6 +183,9 @@ categoriesController.get = function(req, res, next) {
|
||||
function(payload, next) {
|
||||
user.getUidByUserslug(req.query.author, function(err, uid) {
|
||||
payload.targetUid = uid;
|
||||
if (uid) {
|
||||
payload.set = 'cid:' + cid + ':uid:' + uid + ':tids';
|
||||
}
|
||||
next(err, payload);
|
||||
});
|
||||
},
|
||||
|
||||
@@ -171,8 +171,11 @@ module.exports = function(Posts) {
|
||||
topics.updateTeaser(postData.tid, next);
|
||||
},
|
||||
function(next) {
|
||||
user.incrementUserPostCountBy(postData.uid, -1, next);
|
||||
db.sortedSetIncrBy('cid:' + topicData.cid + ':tids:posts', -1, postData.tid, next);
|
||||
},
|
||||
function(next) {
|
||||
user.incrementUserPostCountBy(postData.uid, -1, next);
|
||||
}
|
||||
], callback);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -125,6 +125,8 @@ function generateForCategory(req, res, next) {
|
||||
var uid = req.user ? req.user.uid : 0;
|
||||
categories.getCategoryById({
|
||||
cid: cid,
|
||||
set: 'cid:' + cid + ':tids',
|
||||
reverse: true,
|
||||
start: 0,
|
||||
end: 25,
|
||||
uid: uid
|
||||
|
||||
@@ -46,11 +46,28 @@ SocketCategories.loadMore = function(socket, data, callback) {
|
||||
return callback(new Error('[[error:no-privileges]]'));
|
||||
}
|
||||
|
||||
|
||||
var set = 'cid:' + data.cid + ':tids',
|
||||
reverse = false;
|
||||
|
||||
if (results.settings.categoryTopicSort === 'newest_to_oldest') {
|
||||
reverse = true;
|
||||
} else if (results.settings.categoryTopicSort === 'most_posts') {
|
||||
reverse = true;
|
||||
set = 'cid:' + data.cid + ':tids:posts';
|
||||
}
|
||||
|
||||
var start = parseInt(data.after, 10),
|
||||
end = start + results.settings.topicsPerPage - 1;
|
||||
|
||||
if (results.targetUid) {
|
||||
set = 'cid:' + data.cid + ':uid:' + results.targetUid + ':tids';
|
||||
}
|
||||
|
||||
categories.getCategoryTopics({
|
||||
cid: data.cid,
|
||||
set: set,
|
||||
reverse: reverse,
|
||||
start: start,
|
||||
stop: end,
|
||||
uid: socket.uid,
|
||||
|
||||
@@ -392,7 +392,7 @@ SocketTopics.moveAll = function(socket, data, callback) {
|
||||
return callback(err || new Error('[[error:no-privileges]]'));
|
||||
}
|
||||
|
||||
categories.getTopicIds('cid:' + data.currentCid + ':tids', 0, -1, function(err, tids) {
|
||||
categories.getTopicIds('cid:' + data.currentCid + ':tids', true, 0, -1, function(err, tids) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
@@ -298,6 +298,12 @@ SocketUser.setTopicSort = function(socket, sort, callback) {
|
||||
}
|
||||
};
|
||||
|
||||
SocketUser.setCategorySort = function(socket, sort, callback) {
|
||||
if (socket.uid) {
|
||||
user.setSetting(socket.uid, 'categoryTopicSort', sort, callback);
|
||||
}
|
||||
};
|
||||
|
||||
SocketUser.getOnlineAnonCount = function(socket, data, callback) {
|
||||
callback(null, module.parent.exports.getOnlineAnonCount());
|
||||
};
|
||||
|
||||
@@ -185,15 +185,26 @@ var winston = require('winston'),
|
||||
var topic;
|
||||
async.waterfall([
|
||||
function(next) {
|
||||
topics.getTopicFields(tid, ['cid', 'lastposttime', 'pinned', 'deleted'], next);
|
||||
topics.getTopicFields(tid, ['cid', 'lastposttime', 'pinned', 'deleted', 'postcount'], next);
|
||||
},
|
||||
function(topicData, next) {
|
||||
topic = topicData;
|
||||
db.sortedSetRemove('cid:' + topicData.cid + ':tids', tid, next);
|
||||
db.sortedSetsRemove([
|
||||
'cid:' + topicData.cid + ':tids',
|
||||
'cid:' + topicData.cid + ':tids:posts'
|
||||
], tid, next);
|
||||
},
|
||||
function(next) {
|
||||
var timestamp = parseInt(topic.pinned, 10) ? Math.pow(2, 53) : topic.lastposttime;
|
||||
async.parallel([
|
||||
function(next) {
|
||||
db.sortedSetAdd('cid:' + cid + ':tids', timestamp, tid, next);
|
||||
},
|
||||
function(next) {
|
||||
topic.postcount = topic.postcount || 0;
|
||||
db.sortedSetAdd('cid:' + cid + ':tids:posts', topic.postcount, tid, next);
|
||||
}
|
||||
], next);
|
||||
}
|
||||
], function(err) {
|
||||
if (err) {
|
||||
|
||||
@@ -76,6 +76,7 @@ module.exports = function(Topics) {
|
||||
|
||||
db.sortedSetsRemove([
|
||||
'cid:' + topicData.cid + ':tids',
|
||||
'cid:' + topicData.cid + ':tids:posts',
|
||||
'cid:' + topicData.cid + ':uid:' + topicData.uid + ':tids',
|
||||
'uid:' + topicData.uid + ':topics'
|
||||
], tid, callback);
|
||||
|
||||
@@ -21,7 +21,7 @@ var db = require('./database'),
|
||||
schemaDate, thisSchemaDate,
|
||||
|
||||
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
|
||||
latestSchema = Date.UTC(2014, 11, 20);
|
||||
latestSchema = Date.UTC(2015, 0, 8);
|
||||
|
||||
Upgrade.check = function(callback) {
|
||||
db.get('schemaDate', function(err, value) {
|
||||
@@ -451,8 +451,48 @@ Upgrade.upgrade = function(callback) {
|
||||
winston.info('[2014/12/20] Updating digest settings skipped');
|
||||
next();
|
||||
}
|
||||
},
|
||||
function(next) {
|
||||
thisSchemaDate = Date.UTC(2015, 0, 8);
|
||||
if (schemaDate < thisSchemaDate) {
|
||||
winston.info('[2015/01/08] Updating category topics sorted sets');
|
||||
|
||||
db.getSortedSetRange('topics:tid', 0, -1, function(err, tids) {
|
||||
if (err) {
|
||||
winston.error('[2014/12/20] Error encountered while updating digest settings');
|
||||
return next(err);
|
||||
}
|
||||
|
||||
var now = Date.now();
|
||||
|
||||
async.eachLimit(tids, 50, function(tid, next) {
|
||||
db.getObjectFields('topic:' + tid, ['cid', 'postcount'], function(err, topicData) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
if (Utils.isNumber(topicData.postcount) && topicData.cid) {
|
||||
db.sortedSetAdd('cid:' + topicData.cid + ':tids:posts', topicData.postcount, tid, next);
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
winston.error('[2015/01/08] Error encountered while Updating category topics sorted sets');
|
||||
return next(err);
|
||||
}
|
||||
winston.info('[2015/01/08] Updating category topics sorted sets done');
|
||||
Upgrade.update(thisSchemaDate, next);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
winston.info('[2015/01/08] Updating category topics sorted sets skipped');
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Add new schema updates here
|
||||
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 22!!!
|
||||
], function(err) {
|
||||
|
||||
@@ -65,6 +65,7 @@ module.exports = function(User) {
|
||||
settings.notificationSounds = parseInt(settings.notificationSounds, 10) === 1;
|
||||
settings.language = settings.language || meta.config.defaultLang || 'en_GB';
|
||||
settings.topicPostSort = settings.topicPostSort || meta.config.topicPostSort || 'oldest_to_newest';
|
||||
settings.categoryTopicSort = settings.categoryTopicSort || meta.config.categoryTopicSort || 'newest_to_oldest';
|
||||
settings.followTopicsOnCreate = (settings.followTopicsOnCreate === null || settings.followTopicsOnCreate === undefined) ? true : parseInt(settings.followTopicsOnCreate, 10) === 1;
|
||||
settings.followTopicsOnReply = parseInt(settings.followTopicsOnReply, 10) === 1;
|
||||
settings.sendChatNotifications = parseInt(settings.sendChatNotifications, 10) === 1;
|
||||
|
||||
@@ -12,6 +12,14 @@
|
||||
<option value="most_votes">Most Votes</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Default Topic Sorting</label>
|
||||
<select class="form-control" data-field="categoryTopicSort">
|
||||
<option value="newest_to_oldest">Newest to Oldest</option>
|
||||
<option value="oldest_to_newest">Oldest to Newest</option>
|
||||
<option value="most_posts">Most Posts</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>Seconds between Posts</label>
|
||||
|
||||
@@ -35,6 +35,8 @@ describe('Categories', function() {
|
||||
it('should retrieve a newly created category by its ID', function(done) {
|
||||
Categories.getCategoryById({
|
||||
cid: categoryObj.cid,
|
||||
set: 'cid:' + categoryObj.cid + ':tids',
|
||||
reverse: true,
|
||||
start: 0,
|
||||
end: -1,
|
||||
uid: 0
|
||||
@@ -52,6 +54,8 @@ describe('Categories', function() {
|
||||
it('should return a list of topics', function(done) {
|
||||
Categories.getCategoryTopics({
|
||||
cid: categoryObj.cid,
|
||||
set: 'cid:' + categoryObj.cid + ':tids',
|
||||
reverse: true,
|
||||
start: 0,
|
||||
stop: 10,
|
||||
uid: 0
|
||||
@@ -68,6 +72,8 @@ describe('Categories', function() {
|
||||
it('should return a list of topics by a specific user', function(done) {
|
||||
Categories.getCategoryTopics({
|
||||
cid: categoryObj.cid,
|
||||
set: 'cid:' + categoryObj.cid + ':uid:' + 1 + ':tids',
|
||||
reverse: true,
|
||||
start: 0,
|
||||
stop: 10,
|
||||
uid: 0,
|
||||
|
||||
Reference in New Issue
Block a user