Compare commits

...

27 Commits

Author SHA1 Message Date
Barış Soner Uşaklı
a852b374e9 feat: pass req.query to getUnreadData 2021-05-27 10:17:16 -04:00
Barış Soner Uşaklı
55835b3b72 feat: merge 2021-05-25 12:54:12 -04:00
Barış Soner Uşaklı
a79707ef9a fix: merge 2021-05-25 12:13:26 -04:00
Barış Soner Uşaklı
a68fc1dc50 feat: add req.query to flags.list/getCount 2021-05-19 10:11:37 -04:00
Barış Soner Uşaklı
536b66dc89 feat: add filter:flags.getFlagIdsWithFilters 2021-05-18 11:00:26 -04:00
Barış Soner Uşaklı
75d60bfaf2 feat: add filter:user.getWatchedCategories 2021-05-17 12:12:57 -04:00
Barış Soner Uşaklı
9f6ad7637e feat: pass req.query to getUserDataByUserSlug 2021-05-17 10:32:26 -04:00
Barış Soner Uşaklı
fdf7e65331 fix: inf scroll with subfolder install 2021-05-17 10:22:01 -04:00
Barış Soner Uşaklı
1cc32705fe feat: load user posts/topics via xhr on infinitescroll 2021-05-17 10:21:49 -04:00
Barış Soner Uşaklı
a60ea2ec7a fix: lint 2021-05-12 10:50:14 -04:00
Barış Soner Uşaklı
4066d994ff fix: tests 2021-05-12 10:50:05 -04:00
Barış Soner Uşaklı
de9bec2bb1 fix: tests 2021-05-12 10:49:55 -04:00
Barış Soner Uşaklı
3aecd20096 feat: add template to hook 2021-05-12 10:49:45 -04:00
Barış Soner Uşaklı
54804d4789 feat: add filter:account.getPostsFromUserSet 2021-05-12 10:49:34 -04:00
Barış Soner Uşaklı
55cefcba35 feat: #9533, allow redirect in build hooks 2021-05-07 09:34:37 -04:00
Barış Soner Uşaklı
3da8b93578 feat: add filter:categories.copySettingsFrom 2021-04-30 10:08:02 -04:00
Barış Soner Uşaklı
b265e6f68b feat: add filter 2021-04-26 11:02:55 -04:00
Barış Soner Uşaklı
a6a02fb773 feat: merge 2021-04-15 12:52:58 -04:00
Barış Soner Uşaklı
49583fe48b feat: allow slugs 2021-04-12 17:28:04 -04:00
Barış Soner Uşaklı
14e211fb68 fix: #9473 (#9476) 2021-04-08 14:16:59 -04:00
Tudor-Dan Ravoiu
7b98fab95c Translate categories (#9472)
* use appParseAndTranslate to translate category names

* translate parent categories on new category creation

Co-authored-by: Tudor-Dan Ravoiu <tudor-dan.ravoiu@ubisoft.com>
2021-04-08 09:10:01 -04:00
Barış Soner Uşaklı
a373731570 feat: add reverse of recent to getSortedTopics 2021-04-06 12:59:21 -04:00
Barış Soner Uşaklı
a76b5d15e0 feat: pass all data to filter:category.get 2021-03-31 12:09:38 -04:00
Julian Lam
a06b84d258 chore: bump version 2021-03-26 16:24:08 -04:00
Barış Soner Uşaklı
ecf212d307 feat: add hooks to language loading (#9426)
and flushNamespace method
2021-03-26 16:23:18 -04:00
Julian Lam
dc6fc65322 chore: bump version 2021-03-24 12:56:05 -04:00
Barış Soner Uşaklı
27b481765b fix: #9420, paginate after loading notifications 2021-03-24 12:54:47 -04:00
49 changed files with 361 additions and 243 deletions

View File

@@ -2,7 +2,7 @@
"name": "nodebb", "name": "nodebb",
"license": "GPL-3.0", "license": "GPL-3.0",
"description": "NodeBB Forum", "description": "NodeBB Forum",
"version": "1.16.2-beta.0", "version": "1.16.2-beta.2",
"homepage": "http://www.nodebb.org", "homepage": "http://www.nodebb.org",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@@ -109,9 +109,9 @@ define('admin/manage/categories', [
name: '[[admin/manage/categories:parent-category-none]]', name: '[[admin/manage/categories:parent-category-none]]',
icon: 'fa-none', icon: 'fa-none',
}); });
Benchpress.render('admin/partials/categories/create', { app.parseAndTranslate('admin/partials/categories/create', {
categories: categories, categories: categories,
}).then(function (html) { }, function (html) {
var modal = bootbox.dialog({ var modal = bootbox.dialog({
title: '[[admin/manage/categories:alert.create]]', title: '[[admin/manage/categories:alert.create]]',
message: html, message: html,

View File

@@ -119,9 +119,9 @@ define('admin/manage/category', [
return app.alertError(err.message); return app.alertError(err.message);
} }
Benchpress.render('admin/partials/categories/copy-settings', { app.parseAndTranslate('admin/partials/categories/copy-settings', {
categories: allCategories, categories: allCategories,
}).then(function (html) { }, function (html) {
var selectedCid; var selectedCid;
var modal = bootbox.dialog({ var modal = bootbox.dialog({
title: '[[modules:composer.select_category]]', title: '[[modules:composer.select_category]]',

View File

@@ -9,7 +9,7 @@ define('forum/account/best', ['forum/account/header', 'forum/account/posts'], fu
$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); $('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive');
posts.handleInfiniteScroll('posts.loadMoreBestPosts', 'account/best'); posts.handleInfiniteScroll('account/best');
}; };
return Best; return Best;

View File

@@ -9,7 +9,7 @@ define('forum/account/bookmarks', ['forum/account/header', 'forum/account/posts'
$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); $('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive');
posts.handleInfiniteScroll('posts.loadMoreBookmarks', 'account/bookmarks'); posts.handleInfiniteScroll('account/bookmarks');
}; };
return Bookmarks; return Bookmarks;

View File

@@ -9,7 +9,7 @@ define('forum/account/downvoted', ['forum/account/header', 'forum/account/posts'
$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); $('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive');
posts.handleInfiniteScroll('posts.loadMoreDownVotedPosts', 'account/downvoted'); posts.handleInfiniteScroll('account/downvoted');
}; };
return Downvoted; return Downvoted;

View File

@@ -0,0 +1,13 @@
'use strict';
define('forum/account/ignored', ['forum/account/header', 'forum/account/topics'], function (header, topics) {
var AccountIgnored = {};
AccountIgnored.init = function () {
header.init();
topics.handleInfiniteScroll('account/ignored');
};
return AccountIgnored;
});

View File

@@ -3,20 +3,21 @@
define('forum/account/posts', ['forum/account/header', 'forum/infinitescroll'], function (header, infinitescroll) { define('forum/account/posts', ['forum/account/header', 'forum/infinitescroll'], function (header, infinitescroll) {
var AccountPosts = {}; var AccountPosts = {};
var method;
var template; var template;
var page = 1;
AccountPosts.init = function () { AccountPosts.init = function () {
header.init(); header.init();
$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); $('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive');
AccountPosts.handleInfiniteScroll('posts.loadMoreUserPosts', 'account/posts'); AccountPosts.handleInfiniteScroll('account/posts');
}; };
AccountPosts.handleInfiniteScroll = function (_method, _template) { AccountPosts.handleInfiniteScroll = function (_template) {
method = _method;
template = _template; template = _template;
page = ajaxify.data.pagination.currentPage;
if (!config.usePagination) { if (!config.usePagination) {
infinitescroll.init(loadMore); infinitescroll.init(loadMore);
} }
@@ -26,17 +27,16 @@ define('forum/account/posts', ['forum/account/header', 'forum/infinitescroll'],
if (direction < 0) { if (direction < 0) {
return; return;
} }
var params = utils.params();
page += 1;
params.page = page;
infinitescroll.loadMore(method, { infinitescroll.loadMoreXhr(params, function (data, done) {
uid: ajaxify.data.theirid,
after: $('[component="posts"]').attr('data-nextstart'),
}, function (data, done) {
if (data.posts && data.posts.length) { if (data.posts && data.posts.length) {
onPostsLoaded(data.posts, done); onPostsLoaded(data.posts, done);
} else { } else {
done(); done();
} }
$('[component="posts"]').attr('data-nextstart', data.nextStart);
}); });
} }

View File

@@ -3,21 +3,19 @@
define('forum/account/topics', ['forum/account/header', 'forum/infinitescroll'], function (header, infinitescroll) { define('forum/account/topics', ['forum/account/header', 'forum/infinitescroll'], function (header, infinitescroll) {
var AccountTopics = {}; var AccountTopics = {};
var method;
var template; var template;
var set; var page = 1;
AccountTopics.init = function () { AccountTopics.init = function () {
header.init(); header.init();
AccountTopics.handleInfiniteScroll('topics.loadMoreUserTopics', 'account/topics'); AccountTopics.handleInfiniteScroll('account/topics');
}; };
AccountTopics.handleInfiniteScroll = function (_method, _template, _set) { AccountTopics.handleInfiniteScroll = function (_template) {
method = _method;
template = _template; template = _template;
set = _set; page = ajaxify.data.pagination.currentPage;
if (!config.usePagination) { if (!config.usePagination) {
infinitescroll.init(loadMore); infinitescroll.init(loadMore);
} }
@@ -27,20 +25,16 @@ define('forum/account/topics', ['forum/account/header', 'forum/infinitescroll'],
if (direction < 0) { if (direction < 0) {
return; return;
} }
var params = utils.params();
page += 1;
params.page = page;
infinitescroll.loadMore(method, { infinitescroll.loadMoreXhr(params, function (data, done) {
set: set,
uid: ajaxify.data.theirid,
after: $('[component="category"]').attr('data-nextstart'),
count: config.topicsPerPage,
}, function (data, done) {
if (data.topics && data.topics.length) { if (data.topics && data.topics.length) {
onTopicsLoaded(data.topics, done); onTopicsLoaded(data.topics, done);
} else { } else {
done(); done();
} }
$('[component="category"]').attr('data-nextstart', data.nextStart);
}); });
} }

View File

@@ -9,7 +9,7 @@ define('forum/account/upvoted', ['forum/account/header', 'forum/account/posts'],
$('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); $('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive');
posts.handleInfiniteScroll('posts.loadMoreUpVotedPosts', 'account/upvoted'); posts.handleInfiniteScroll('account/upvoted');
}; };
return Upvoted; return Upvoted;

View File

@@ -7,7 +7,7 @@ define('forum/account/watched', ['forum/account/header', 'forum/account/topics']
AccountWatched.init = function () { AccountWatched.init = function () {
header.init(); header.init();
topics.handleInfiniteScroll('topics.loadMoreFromSet', 'account/watched', 'uid:' + ajaxify.data.theirid + ':followed_tids'); topics.handleInfiniteScroll('account/watched');
}; };
return AccountWatched; return AccountWatched;

View File

@@ -78,6 +78,25 @@ define('forum/infinitescroll', function () {
}); });
}; };
scroll.loadMoreXhr = function (data, callback) {
if (loadingMore) {
return;
}
loadingMore = true;
var url = config.relative_path + '/api' + location.pathname.replace(new RegExp('^' + config.relative_path), '');
var hookData = { url: url, data: data };
$(window).trigger('action:infinitescroll.loadmore.xhr', hookData);
$.get(url, data, function (data) {
callback(data, function () {
loadingMore = false;
});
}).fail(function (jqXHR) {
loadingMore = false;
app.alertError(String(jqXHR.responseJSON || jqXHR.statusText));
});
};
scroll.removeExtra = function (els, direction, count) { scroll.removeExtra = function (els, direction, count) {
if (els.length <= count) { if (els.length <= count) {
return; return;

View File

@@ -2,7 +2,19 @@
(function (factory) { (function (factory) {
function loadClient(language, namespace) { function loadClient(language, namespace) {
return Promise.resolve(jQuery.getJSON([config.assetBaseUrl, 'language', language, namespace].join('/') + '.json?' + config['cache-buster'])); return new Promise(function (resolve, reject) {
jQuery.getJSON([config.assetBaseUrl, 'language', language, namespace].join('/') + '.json?' + config['cache-buster'], function (data) {
const payload = {
language: language,
namespace: namespace,
data: data,
};
$(window).trigger('action:translator.loadClient', payload);
resolve(payload.promise ? Promise.resolve(payload.promise) : data);
}).fail(function (jqxhr, textStatus, error) {
reject(new Error(textStatus + ', ' + error));
});
});
} }
var warn = function () { console.warn.apply(console, arguments); }; var warn = function () { console.warn.apply(console, arguments); };
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
@@ -545,6 +557,18 @@
}); });
}, },
flushNamespace: function (namespace) {
Object.keys(Translator.cache).forEach(function (code) {
if (Translator.cache[code] &&
Translator.cache[code].translations &&
Translator.cache[code].translations[namespace]
) {
Translator.cache[code].translations[namespace] = null;
}
});
},
/** /**
* Legacy translator function for backwards compatibility * Legacy translator function for backwards compatibility
*/ */

View File

@@ -150,6 +150,11 @@ module.exports = function (Categories) {
if (copyParent) { if (copyParent) {
destination.parentCid = source.parentCid || 0; destination.parentCid = source.parentCid || 0;
} }
await plugins.hooks.fire('filter:categories.copySettingsFrom', {
source: source,
destination: destination,
copyParent: copyParent,
});
await db.setObject('category:' + toCid, destination); await db.setObject('category:' + toCid, destination);

View File

@@ -57,9 +57,11 @@ Categories.getCategoryById = async function (data) {
category.isIgnored = watchState[0] === Categories.watchStates.ignoring; category.isIgnored = watchState[0] === Categories.watchStates.ignoring;
category.parent = parent; category.parent = parent;
calculateTopicPostCount(category); calculateTopicPostCount(category);
const result = await plugins.hooks.fire('filter:category.get', { category: category, uid: data.uid }); const result = await plugins.hooks.fire('filter:category.get', {
category: category,
...data,
});
return result.category; return result.category;
}; };

View File

@@ -7,6 +7,7 @@ const validator = require('validator');
const meta = require('../meta'); const meta = require('../meta');
const plugins = require('../plugins'); const plugins = require('../plugins');
const middleware = require('../middleware'); const middleware = require('../middleware');
const helpers = require('../middleware/helpers');
exports.handle404 = function handle404(req, res) { exports.handle404 = function handle404(req, res) {
const relativePath = nconf.get('relative_path'); const relativePath = nconf.get('relative_path');
@@ -22,7 +23,13 @@ exports.handle404 = function handle404(req, res) {
if (isClientScript.test(req.url)) { if (isClientScript.test(req.url)) {
res.type('text/javascript').status(404).send('Not Found'); res.type('text/javascript').status(404).send('Not Found');
} else if (req.path.startsWith(relativePath + '/assets/uploads') || (req.get('accept') && !req.get('accept').includes('text/html')) || req.path === '/favicon.ico') { } else if (
!res.locals.isAPI && (
req.path.startsWith(`${relativePath}/assets/uploads`) ||
(req.get('accept') && !req.get('accept').includes('text/html')) ||
req.path === '/favicon.ico'
)
) {
meta.errors.log404(req.path || ''); meta.errors.log404(req.path || '');
res.sendStatus(404); res.sendStatus(404);
} else if (req.accepts('html')) { } else if (req.accepts('html')) {
@@ -41,8 +48,16 @@ exports.send404 = async function (req, res) {
res.status(404); res.status(404);
const path = String(req.path || ''); const path = String(req.path || '');
if (res.locals.isAPI) { if (res.locals.isAPI) {
return res.json({ path: validator.escape(path.replace(/^\/api/, '')), title: '[[global:404.title]]' }); return res.json({
path: validator.escape(path.replace(/^\/api/, '')),
title: '[[global:404.title]]',
bodyClass: helpers.buildBodyClass(req, res),
});
} }
await middleware.buildHeaderAsync(req, res); await middleware.buildHeaderAsync(req, res);
res.render('404', { path: validator.escape(path), title: '[[global:404.title]]' }); await res.render('404', {
path: validator.escape(path),
title: '[[global:404.title]]',
bodyClass: helpers.buildBodyClass(req, res),
});
}; };

View File

@@ -14,7 +14,7 @@ blocksController.getBlocks = async function (req, res, next) {
const start = Math.max(0, page - 1) * resultsPerPage; const start = Math.max(0, page - 1) * resultsPerPage;
const stop = start + resultsPerPage - 1; const stop = start + resultsPerPage - 1;
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid); const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query);
if (!userData) { if (!userData) {
return next(); return next();
} }

View File

@@ -8,7 +8,7 @@ const helpers = require('../helpers');
const categoriesController = module.exports; const categoriesController = module.exports;
categoriesController.get = async function (req, res, next) { categoriesController.get = async function (req, res, next) {
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid); const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query);
if (!userData) { if (!userData) {
return next(); return next();
} }

View File

@@ -12,7 +12,7 @@ consentController.get = async function (req, res, next) {
return next(); return next();
} }
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid); const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query);
if (!userData) { if (!userData) {
return next(); return next();
} }

View File

@@ -12,7 +12,7 @@ const editController = module.exports;
editController.get = async function (req, res, next) { editController.get = async function (req, res, next) {
const [userData, canUseSignature] = await Promise.all([ const [userData, canUseSignature] = await Promise.all([
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid), accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query),
privileges.global.can('signature', req.uid), privileges.global.can('signature', req.uid),
]); ]);
if (!userData) { if (!userData) {
@@ -114,7 +114,7 @@ async function renderRoute(name, req, res, next) {
} }
async function getUserData(req) { async function getUserData(req) {
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid); const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query);
if (!userData) { if (!userData) {
return null; return null;
} }

View File

@@ -16,7 +16,7 @@ followController.getFollowers = async function (req, res, next) {
}; };
async function getFollow(tpl, name, req, res, next) { async function getFollow(tpl, name, req, res, next) {
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid); const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query);
if (!userData) { if (!userData) {
return next(); return next();
} }

View File

@@ -7,7 +7,7 @@ const accountHelpers = require('./helpers');
const groupsController = module.exports; const groupsController = module.exports;
groupsController.get = async function (req, res, next) { groupsController.get = async function (req, res, next) {
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid); const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query);
if (!userData) { if (!userData) {
return next(); return next();
} }

View File

@@ -16,7 +16,7 @@ const categories = require('../../categories');
const helpers = module.exports; const helpers = module.exports;
helpers.getUserDataByUserSlug = async function (userslug, callerUID) { helpers.getUserDataByUserSlug = async function (userslug, callerUID, query = {}) {
const uid = await user.getUidByUserslug(userslug); const uid = await user.getUidByUserslug(userslug);
if (!uid) { if (!uid) {
return null; return null;
@@ -114,7 +114,11 @@ helpers.getUserDataByUserSlug = async function (userslug, callerUID) {
await getCounts(userData, callerUID); await getCounts(userData, callerUID);
const hookData = await plugins.hooks.fire('filter:helpers.getUserDataByUserSlug', { userData: userData, callerUID: callerUID }); const hookData = await plugins.hooks.fire('filter:helpers.getUserDataByUserSlug', {
userData: userData,
callerUID: callerUID,
query: query,
});
return hookData.userData; return hookData.userData;
}; };

View File

@@ -9,7 +9,7 @@ const pagination = require('../../pagination');
const infoController = module.exports; const infoController = module.exports;
infoController.get = async function (req, res, next) { infoController.get = async function (req, res, next) {
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid); const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query);
if (!userData) { if (!userData) {
return next(); return next();
} }

View File

@@ -51,11 +51,12 @@ notificationsController.get = async function (req, res, next) {
if (!selectedFilter) { if (!selectedFilter) {
return next(); return next();
} }
let nids = await user.notifications.getAll(req.uid, selectedFilter.filter);
const pageCount = Math.max(1, Math.ceil(nids.length / itemsPerPage));
nids = nids.slice(start, stop + 1);
const notifications = await user.notifications.getNotifications(nids, req.uid); const nids = await user.notifications.getAll(req.uid, selectedFilter.filter);
let notifications = await user.notifications.getNotifications(nids, req.uid);
const pageCount = Math.max(1, Math.ceil(notifications.length / itemsPerPage));
notifications = notifications.slice(start, stop + 1);
res.render('notifications', { res.render('notifications', {
notifications: notifications, notifications: notifications,

View File

@@ -8,6 +8,8 @@ const categories = require('../../categories');
const pagination = require('../../pagination'); const pagination = require('../../pagination');
const helpers = require('../helpers'); const helpers = require('../helpers');
const accountHelpers = require('./helpers'); const accountHelpers = require('./helpers');
const plugins = require('../../plugins');
const utils = require('../../utils');
const postsController = module.exports; const postsController = module.exports;
@@ -107,43 +109,43 @@ const templateToData = {
}; };
postsController.getBookmarks = async function (req, res, next) { postsController.getBookmarks = async function (req, res, next) {
await getFromUserSet('account/bookmarks', req, res, next); await getPostsFromUserSet('account/bookmarks', req, res, next);
}; };
postsController.getPosts = async function (req, res, next) { postsController.getPosts = async function (req, res, next) {
await getFromUserSet('account/posts', req, res, next); await getPostsFromUserSet('account/posts', req, res, next);
}; };
postsController.getUpVotedPosts = async function (req, res, next) { postsController.getUpVotedPosts = async function (req, res, next) {
await getFromUserSet('account/upvoted', req, res, next); await getPostsFromUserSet('account/upvoted', req, res, next);
}; };
postsController.getDownVotedPosts = async function (req, res, next) { postsController.getDownVotedPosts = async function (req, res, next) {
await getFromUserSet('account/downvoted', req, res, next); await getPostsFromUserSet('account/downvoted', req, res, next);
}; };
postsController.getBestPosts = async function (req, res, next) { postsController.getBestPosts = async function (req, res, next) {
await getFromUserSet('account/best', req, res, next); await getPostsFromUserSet('account/best', req, res, next);
}; };
postsController.getWatchedTopics = async function (req, res, next) { postsController.getWatchedTopics = async function (req, res, next) {
await getFromUserSet('account/watched', req, res, next); await getPostsFromUserSet('account/watched', req, res, next);
}; };
postsController.getIgnoredTopics = async function (req, res, next) { postsController.getIgnoredTopics = async function (req, res, next) {
await getFromUserSet('account/ignored', req, res, next); await getPostsFromUserSet('account/ignored', req, res, next);
}; };
postsController.getTopics = async function (req, res, next) { postsController.getTopics = async function (req, res, next) {
await getFromUserSet('account/topics', req, res, next); await getPostsFromUserSet('account/topics', req, res, next);
}; };
async function getFromUserSet(template, req, res, callback) { async function getPostsFromUserSet(template, req, res, callback) {
const data = templateToData[template]; const data = templateToData[template];
const page = Math.max(1, parseInt(req.query.page, 10) || 1); const page = Math.max(1, parseInt(req.query.page, 10) || 1);
const [userData, settings] = await Promise.all([ const [userData, settings] = await Promise.all([
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid), accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query),
user.getSettings(req.uid), user.getSettings(req.uid),
]); ]);
@@ -154,12 +156,26 @@ async function getFromUserSet(template, req, res, callback) {
const start = (page - 1) * itemsPerPage; const start = (page - 1) * itemsPerPage;
const stop = start + itemsPerPage - 1; const stop = start + itemsPerPage - 1;
const sets = await data.getSets(req.uid, userData); const sets = await data.getSets(req.uid, userData);
let result;
const [itemCount, itemData] = await Promise.all([ if (plugins.hooks.hasListeners('filter:account.getPostsFromUserSet')) {
settings.usePagination ? db.sortedSetsCardSum(sets) : 0, result = await plugins.hooks.fire('filter:account.getPostsFromUserSet', {
getItemData(sets, data, req, start, stop), req: req,
]); template: template,
userData: userData,
settings: settings,
data: data,
start: start,
stop: stop,
itemCount: 0,
itemData: [],
});
} else {
result = await utils.promiseParallel({
itemCount: settings.usePagination ? db.sortedSetsCardSum(sets) : 0,
itemData: getItemData(sets, data, req, start, stop),
});
}
const { itemCount, itemData } = result;
userData[data.type] = itemData[data.type]; userData[data.type] = itemData[data.type];
userData.nextStart = itemData.nextStart; userData.nextStart = itemData.nextStart;

View File

@@ -25,7 +25,7 @@ profileController.get = async function (req, res, next) {
} }
} }
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid); const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query);
if (!userData) { if (!userData) {
return next(); return next();
} }

View File

@@ -7,7 +7,7 @@ const accountHelpers = require('./helpers');
const sessionController = module.exports; const sessionController = module.exports;
sessionController.get = async function (req, res, next) { sessionController.get = async function (req, res, next) {
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid); const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query);
if (!userData) { if (!userData) {
return next(); return next();
} }

View File

@@ -18,7 +18,7 @@ const accountHelpers = require('./helpers');
const settingsController = module.exports; const settingsController = module.exports;
settingsController.get = async function (req, res, next) { settingsController.get = async function (req, res, next) {
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid); const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query);
if (!userData) { if (!userData) {
return next(); return next();
} }

View File

@@ -11,7 +11,7 @@ const accountHelpers = require('./helpers');
const uploadsController = module.exports; const uploadsController = module.exports;
uploadsController.get = async function (req, res, next) { uploadsController.get = async function (req, res, next) {
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid); const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query);
if (!userData) { if (!userData) {
return next(); return next();
} }

View File

@@ -3,6 +3,7 @@
const nconf = require('nconf'); const nconf = require('nconf');
const validator = require('validator'); const validator = require('validator');
const qs = require('querystring');
const db = require('../database'); const db = require('../database');
const privileges = require('../privileges'); const privileges = require('../privileges');
@@ -42,15 +43,15 @@ categoryController.get = async function (req, res, next) {
return next(); return next();
} }
if (topicIndex < 0) { if (topicIndex < 0) {
return helpers.redirect(res, '/category/' + categoryFields.slug); return helpers.redirect(res, `/category/${categoryFields.slug}?${qs.stringify(req.query)}`);
} }
if (!userPrivileges.read) { if (!userPrivileges.read) {
return helpers.notAllowed(req, res); return helpers.notAllowed(req, res);
} }
if (!res.locals.isAPI && (!req.params.slug || categoryFields.slug !== cid + '/' + req.params.slug) && (categoryFields.slug && categoryFields.slug !== cid + '/')) { if (!res.locals.isAPI && !req.params.slug && (categoryFields.slug && categoryFields.slug !== `${cid}/`)) {
return helpers.redirect(res, '/category/' + categoryFields.slug, true); return helpers.redirect(res, `/category/${categoryFields.slug}?${qs.stringify(req.query)}`, true);
} }
if (categoryFields.link) { if (categoryFields.link) {
@@ -86,7 +87,7 @@ categoryController.get = async function (req, res, next) {
} }
if (topicIndex > Math.max(categoryData.topic_count - 1, 0)) { if (topicIndex > Math.max(categoryData.topic_count - 1, 0)) {
return helpers.redirect(res, '/category/' + categoryData.slug + '/' + categoryData.topic_count); return helpers.redirect(res, `/category/${categoryData.slug}/${categoryData.topic_count}?${qs.stringify(req.query)}`);
} }
const pageCount = Math.max(1, Math.ceil(categoryData.topic_count / userSettings.topicsPerPage)); const pageCount = Math.max(1, Math.ceil(categoryData.topic_count / userSettings.topicsPerPage));
if (userSettings.usePagination && currentPage > pageCount) { if (userSettings.usePagination && currentPage > pageCount) {

View File

@@ -106,6 +106,7 @@ modsController.flags.list = async function (req, res, next) {
filters: filters, filters: filters,
sort: sort, sort: sort,
uid: req.uid, uid: req.uid,
query: req.query,
}), }),
analytics.getDailyStatsForSet('analytics:flags', Date.now(), 30), analytics.getDailyStatsForSet('analytics:flags', Date.now(), 30),
categories.buildForSelect(req.uid, 'read'), categories.buildForSelect(req.uid, 'read'),

View File

@@ -118,7 +118,7 @@ function calculateStartStop(page, postIndex, settings) {
let startSkip = 0; let startSkip = 0;
if (!settings.usePagination) { if (!settings.usePagination) {
if (postIndex !== 0) { if (postIndex > 1) {
page = 1; page = 1;
} }
startSkip = Math.max(0, postIndex - Math.ceil(settings.postsPerPage / 2)); startSkip = Math.max(0, postIndex - Math.ceil(settings.postsPerPage / 2));

View File

@@ -14,7 +14,7 @@ userController.getCurrentUser = async function (req, res) {
return res.status(401).json('not-authorized'); return res.status(401).json('not-authorized');
} }
const userslug = await user.getUserField(req.uid, 'userslug'); const userslug = await user.getUserField(req.uid, 'userslug');
const userData = await accountHelpers.getUserDataByUserSlug(userslug, req.uid); const userData = await accountHelpers.getUserDataByUserSlug(userslug, req.uid, req.query);
res.json(userData); res.json(userData);
}; };

View File

@@ -121,13 +121,13 @@ Flags.get = async function (flagId) {
return data.flag; return data.flag;
}; };
Flags.getCount = async function ({ uid, filters }) { Flags.getCount = async function ({ uid, filters, query }) {
filters = filters || {}; filters = filters || {};
const flagIds = await Flags.getFlagIdsWithFilters({ filters, uid }); const flagIds = await Flags.getFlagIdsWithFilters({ filters, uid, query });
return flagIds.length; return flagIds.length;
}; };
Flags.getFlagIdsWithFilters = async function ({ filters, uid }) { Flags.getFlagIdsWithFilters = async function ({ filters, uid, query }) {
let sets = []; let sets = [];
const orSets = []; const orSets = [];
@@ -164,7 +164,13 @@ Flags.getFlagIdsWithFilters = async function ({ filters, uid }) {
} }
} }
return flagIds; const result = await plugins.hooks.fire('filter:flags.getFlagIdsWithFilters', {
filters,
uid,
query,
flagIds,
});
return result.flagIds;
}; };
Flags.list = async function (data) { Flags.list = async function (data) {
@@ -172,6 +178,7 @@ Flags.list = async function (data) {
let flagIds = await Flags.getFlagIdsWithFilters({ let flagIds = await Flags.getFlagIdsWithFilters({
filters, filters,
uid: data.uid, uid: data.uid,
query: data.query,
}); });
flagIds = await Flags.sort(flagIds, data.sort); flagIds = await Flags.sort(flagIds, data.sort);

View File

@@ -3,7 +3,8 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const utils = require('./utils'); const utils = require('./utils');
const paths = require('./constants').paths; const { paths } = require('./constants');
const plugins = require('./plugins');
const Languages = module.exports; const Languages = module.exports;
const languagesPath = path.join(__dirname, '../build/public/language'); const languagesPath = path.join(__dirname, '../build/public/language');
@@ -12,8 +13,14 @@ const files = fs.readdirSync(path.join(paths.nodeModules, '/timeago/locales'));
Languages.timeagoCodes = files.filter(f => f.startsWith('jquery.timeago')).map(f => f.split('.')[2]); Languages.timeagoCodes = files.filter(f => f.startsWith('jquery.timeago')).map(f => f.split('.')[2]);
Languages.get = async function (language, namespace) { Languages.get = async function (language, namespace) {
const data = await fs.promises.readFile(path.join(languagesPath, language, namespace + '.json'), 'utf8'); const data = await fs.promises.readFile(path.join(languagesPath, language, `${namespace}.json`), 'utf8');
return JSON.parse(data) || {}; const parsed = JSON.parse(data) || {};
const result = await plugins.hooks.fire('filter:languages.get', {
language,
namespace,
data: parsed,
});
return result.data;
}; };
let codeCache = null; let codeCache = null;

View File

@@ -109,6 +109,7 @@ middleware.renderHeader = async function renderHeader(req, res, data) {
unreadCount: templateValues.unreadCount, unreadCount: templateValues.unreadCount,
} = await appendUnreadCounts({ } = await appendUnreadCounts({
uid: req.uid, uid: req.uid,
query: req.query,
navigation: results.navigation, navigation: results.navigation,
unreadData, unreadData,
})); }));
@@ -152,16 +153,17 @@ middleware.renderHeader = async function renderHeader(req, res, data) {
return await req.app.renderAsync('header', hookReturn.templateValues); return await req.app.renderAsync('header', hookReturn.templateValues);
}; };
async function appendUnreadCounts({ uid, navigation, unreadData }) { async function appendUnreadCounts({ uid, navigation, unreadData, query }) {
const originalRoutes = navigation.map(nav => nav.originalRoute); const originalRoutes = navigation.map(nav => nav.originalRoute);
const calls = { const calls = {
unreadData: topics.getUnreadData({ uid: uid }), unreadData: topics.getUnreadData({ uid: uid, query: query }),
unreadChatCount: messaging.getUnreadCount(uid), unreadChatCount: messaging.getUnreadCount(uid),
unreadNotificationCount: user.notifications.getUnreadCount(uid), unreadNotificationCount: user.notifications.getUnreadCount(uid),
unreadFlagCount: (async function () { unreadFlagCount: (async function () {
if (originalRoutes.includes('/flags') && await user.isPrivileged(uid)) { if (originalRoutes.includes('/flags') && await user.isPrivileged(uid)) {
return flags.getCount({ return flags.getCount({
uid, uid,
query,
filters: { filters: {
quick: 'unresolved', quick: 'unresolved',
cid: (await user.isAdminOrGlobalMod(uid)) ? [] : (await user.getModeratedCids(uid)), cid: (await user.isAdminOrGlobalMod(uid)) ? [] : (await user.getModeratedCids(uid)),

View File

@@ -7,6 +7,7 @@ const _ = require('lodash');
const meta = require('../meta'); const meta = require('../meta');
const languages = require('../languages'); const languages = require('../languages');
const helpers = require('./helpers'); const helpers = require('./helpers');
const plugins = require('../plugins');
module.exports = function (middleware) { module.exports = function (middleware) {
middleware.addHeaders = helpers.try(function addHeaders(req, res, next) { middleware.addHeaders = helpers.try(function addHeaders(req, res, next) {
@@ -75,10 +76,13 @@ module.exports = function (middleware) {
next(); next();
}); });
middleware.autoLocale = helpers.try(async function autoLocale(req, res, next) { middleware.autoLocale = helpers.try(async (req, res, next) => {
let langs; await plugins.hooks.fire('filter:middleware.autoLocale', {
req: req,
res: res,
});
if (req.query.lang) { if (req.query.lang) {
langs = await listCodes(); const langs = await listCodes();
if (!langs.includes(req.query.lang)) { if (!langs.includes(req.query.lang)) {
req.query.lang = meta.config.defaultLang; req.query.lang = meta.config.defaultLang;
} }
@@ -87,7 +91,7 @@ module.exports = function (middleware) {
if (parseInt(req.uid, 10) > 0 || !meta.config.autoDetectLang) { if (parseInt(req.uid, 10) > 0 || !meta.config.autoDetectLang) {
return next(); return next();
} }
langs = await listCodes(); const langs = await listCodes();
const lang = req.acceptsLanguages(langs); const lang = req.acceptsLanguages(langs);
if (!lang) { if (!lang) {
return next(); return next();

View File

@@ -1,5 +1,9 @@
'use strict'; 'use strict';
const winston = require('winston');
const validator = require('validator');
const slugify = require('../slugify');
const helpers = module.exports; const helpers = module.exports;
helpers.try = function (middleware) { helpers.try = function (middleware) {
@@ -20,3 +24,34 @@ helpers.try = function (middleware) {
} }
}; };
}; };
helpers.buildBodyClass = function (req, res, templateData = {}) {
const clean = req.path.replace(/^\/api/, '').replace(/^\/|\/$/g, '');
const parts = clean.split('/').slice(0, 3);
parts.forEach((p, index) => {
try {
p = slugify(decodeURIComponent(p));
} catch (err) {
winston.error(err.stack);
p = '';
}
p = validator.escape(String(p));
parts[index] = index ? `${parts[0]}-${p}` : `page-${p || 'home'}`;
});
if (templateData.template && templateData.template.topic) {
parts.push(`page-topic-category-${templateData.category.cid}`);
parts.push(`page-topic-category-${slugify(templateData.category.name)}`);
}
if (Array.isArray(templateData.breadcrumbs)) {
templateData.breadcrumbs.forEach((crumb) => {
if (crumb && crumb.hasOwnProperty('cid')) {
parts.push(`parent-category-${crumb.cid}`);
}
});
}
parts.push(`page-status-${res.statusCode}`);
return parts.join(' ');
};

View File

@@ -2,14 +2,14 @@
const nconf = require('nconf'); const nconf = require('nconf');
const validator = require('validator'); const validator = require('validator');
const winston = require('winston');
const plugins = require('../plugins'); const plugins = require('../plugins');
const meta = require('../meta'); const meta = require('../meta');
const translator = require('../translator'); const translator = require('../translator');
const widgets = require('../widgets'); const widgets = require('../widgets');
const utils = require('../utils'); const utils = require('../utils');
const slugify = require('../slugify'); const helpers = require('./helpers');
const relative_path = nconf.get('relative_path'); const relative_path = nconf.get('relative_path');
@@ -31,12 +31,18 @@ module.exports = function (middleware) {
options.relative_path = relative_path; options.relative_path = relative_path;
options.template = { name: template, [template]: true }; options.template = { name: template, [template]: true };
options.url = (req.baseUrl + req.path.replace(/^\/api/, '')); options.url = (req.baseUrl + req.path.replace(/^\/api/, ''));
options.bodyClass = buildBodyClass(req, res, options); options.bodyClass = helpers.buildBodyClass(req, res, options);
const buildResult = await plugins.hooks.fire('filter:' + template + '.build', { req: req, res: res, templateData: options }); const buildResult = await plugins.hooks.fire(`filter:${template}.build`, { req: req, res: res, templateData: options });
if (res.headersSent) {
return;
}
const templateToRender = buildResult.templateData.templateToRender || template; const templateToRender = buildResult.templateData.templateToRender || template;
const renderResult = await plugins.hooks.fire('filter:middleware.render', { req: req, res: res, templateData: buildResult.templateData }); const renderResult = await plugins.hooks.fire('filter:middleware.render', { req: req, res: res, templateData: buildResult.templateData });
if (res.headersSent) {
return;
}
options = renderResult.templateData; options = renderResult.templateData;
options._header = { options._header = {
tags: await meta.tags.parse(req, renderResult, res.locals.metaTags, res.locals.linkTags), tags: await meta.tags.parse(req, renderResult, res.locals.metaTags, res.locals.linkTags),
@@ -117,34 +123,4 @@ module.exports = function (middleware) {
const translated = await translator.translate(str, language); const translated = await translator.translate(str, language);
return translator.unescape(translated); return translator.unescape(translated);
} }
function buildBodyClass(req, res, templateData) {
const clean = req.path.replace(/^\/api/, '').replace(/^\/|\/$/g, '');
const parts = clean.split('/').slice(0, 3);
parts.forEach(function (p, index) {
try {
p = slugify(decodeURIComponent(p));
} catch (err) {
winston.error(err.stack);
p = '';
}
p = validator.escape(String(p));
parts[index] = index ? parts[0] + '-' + p : 'page-' + (p || 'home');
});
if (templateData.template.topic) {
parts.push('page-topic-category-' + templateData.category.cid);
parts.push('page-topic-category-' + slugify(templateData.category.name));
}
if (templateData.breadcrumbs) {
templateData.breadcrumbs.forEach(function (crumb) {
if (crumb.hasOwnProperty('cid')) {
parts.push('parent-category-' + crumb.cid);
}
});
}
parts.push('page-status-' + res.statusCode);
return parts.join(' ');
}
}; };

View File

@@ -185,9 +185,8 @@ module.exports = function (middleware) {
if (!userslug) { if (!userslug) {
return next(); return next();
} }
const path = req.path.replace(/^\/api/, '') const path = req.url.replace(/^\/api/, '')
.replace('uid', 'user') .replace(`/uid/${uid}`, () => `/user/${userslug}`);
.replace(uid, function () { return userslug; });
controllers.helpers.redirect(res, path); controllers.helpers.redirect(res, path);
}); });

View File

@@ -282,7 +282,7 @@ async function getWatchedCids(data) {
if (!data.categories.includes('watched')) { if (!data.categories.includes('watched')) {
return []; return [];
} }
return await user.getCategoriesByStates(data.uid, [categories.watchStates.watching]); return await user.getWatchedCategories(data.uid);
} }
async function getChildrenCids(data) { async function getChildrenCids(data) {

View File

@@ -6,7 +6,6 @@ const privileges = require('../privileges');
const plugins = require('../plugins'); const plugins = require('../plugins');
const meta = require('../meta'); const meta = require('../meta');
const topics = require('../topics'); const topics = require('../topics');
const categories = require('../categories');
const user = require('../user'); const user = require('../user');
const socketHelpers = require('./helpers'); const socketHelpers = require('./helpers');
const utils = require('../utils'); const utils = require('../utils');
@@ -100,41 +99,6 @@ SocketPosts.getPost = async function (socket, pid) {
return await api.posts.get(socket, { pid }); return await api.posts.get(socket, { pid });
}; };
SocketPosts.loadMoreBookmarks = async function (socket, data) {
return await loadMorePosts('uid:' + data.uid + ':bookmarks', socket.uid, data);
};
SocketPosts.loadMoreUserPosts = async function (socket, data) {
const cids = await categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read');
const keys = cids.map(c => 'cid:' + c + ':uid:' + data.uid + ':pids');
return await loadMorePosts(keys, socket.uid, data);
};
SocketPosts.loadMoreBestPosts = async function (socket, data) {
const cids = await categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read');
const keys = cids.map(c => 'cid:' + c + ':uid:' + data.uid + ':pids:votes');
return await loadMorePosts(keys, socket.uid, data);
};
SocketPosts.loadMoreUpVotedPosts = async function (socket, data) {
return await loadMorePosts('uid:' + data.uid + ':upvote', socket.uid, data);
};
SocketPosts.loadMoreDownVotedPosts = async function (socket, data) {
return await loadMorePosts('uid:' + data.uid + ':downvote', socket.uid, data);
};
async function loadMorePosts(set, uid, data) {
if (!data || !utils.isNumber(data.uid) || !utils.isNumber(data.after)) {
throw new Error('[[error:invalid-data]]');
}
const start = Math.max(0, parseInt(data.after, 10));
const stop = start + 9;
return await posts.getPostSummariesFromSet(set, uid, start, stop);
}
SocketPosts.getCategory = async function (socket, pid) { SocketPosts.getCategory = async function (socket, pid) {
return await posts.getCidByPid(pid); return await posts.getCidByPid(pid);
}; };

View File

@@ -1,7 +1,6 @@
'use strict'; 'use strict';
const topics = require('../../topics'); const topics = require('../../topics');
const categories = require('../../categories');
const privileges = require('../../privileges'); const privileges = require('../../privileges');
const meta = require('../../meta'); const meta = require('../../meta');
const utils = require('../../utils'); const utils = require('../../utils');
@@ -89,12 +88,6 @@ module.exports = function (SocketTopics) {
return await topics.getTopicsFromSet(data.set, socket.uid, start, stop); return await topics.getTopicsFromSet(data.set, socket.uid, start, stop);
}; };
SocketTopics.loadMoreUserTopics = async function (socket, data) {
const cids = await categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read');
data.set = cids.map(c => 'cid:' + c + ':uid:' + data.uid + ':tids');
return await SocketTopics.loadMoreFromSet(socket, data);
};
function calculateStartStop(data) { function calculateStartStop(data) {
const itemsPerPage = Math.min(meta.config.topicsPerPage || 20, parseInt(data.count, 10) || meta.config.topicsPerPage || 20); const itemsPerPage = Math.min(meta.config.topicsPerPage || 20, parseInt(data.count, 10) || meta.config.topicsPerPage || 20);
let start = Math.max(0, parseInt(data.after, 10)); let start = Math.max(0, parseInt(data.after, 10));

View File

@@ -48,6 +48,10 @@ module.exports = function (Topics) {
tids = await db.getSortedSetRevRange('uid:' + params.uid + ':followed_tids', 0, -1); tids = await db.getSortedSetRevRange('uid:' + params.uid + ':followed_tids', 0, -1);
} else if (params.cids) { } else if (params.cids) {
tids = await getCidTids(params); tids = await getCidTids(params);
} else if (params.tags.length) {
tids = await getTagTids(params);
} else if (params.sort === 'old') {
tids = await db.getSortedSetRange(`topics:recent`, 0, meta.config.recentMaxTopics - 1);
} else { } else {
tids = await db.getSortedSetRevRange('topics:' + params.sort, 0, meta.config.recentMaxTopics - 1); tids = await db.getSortedSetRevRange('topics:' + params.sort, 0, meta.config.recentMaxTopics - 1);
} }
@@ -55,12 +59,30 @@ module.exports = function (Topics) {
return tids; return tids;
} }
async function getTagTids(params) {
const sets = [
params.sort === 'old' ?
'topics:recent' :
`topics:${params.sort}`,
...params.tags.map(tag => `tag:${tag}:topics`),
];
const method = params.sort === 'old' ?
'getSortedSetIntersect' :
'getSortedSetRevIntersect';
return await db[method]({
sets: sets,
start: 0,
stop: meta.config.recentMaxTopics - 1,
weights: sets.map((s, index) => (index ? 0 : 1)),
});
}
async function getCidTids(params) { async function getCidTids(params) {
const sets = []; const sets = [];
const pinnedSets = []; const pinnedSets = [];
params.cids.forEach(function (cid) { params.cids.forEach((cid) => {
if (params.sort === 'recent') { if (params.sort === 'recent' || params.sort === 'old') {
sets.push('cid:' + cid + ':tids'); sets.push(`cid:${cid}:tids`);
} else { } else {
sets.push('cid:' + cid + ':tids' + (params.sort ? ':' + params.sort : '')); sets.push('cid:' + cid + ':tids' + (params.sort ? ':' + params.sort : ''));
} }
@@ -68,7 +90,10 @@ module.exports = function (Topics) {
}); });
let pinnedTids = await db.getSortedSetRevRange(pinnedSets, 0, -1); let pinnedTids = await db.getSortedSetRevRange(pinnedSets, 0, -1);
pinnedTids = await Topics.tools.checkPinExpiry(pinnedTids); pinnedTids = await Topics.tools.checkPinExpiry(pinnedTids);
const tids = await db.getSortedSetRevRange(sets, 0, meta.config.recentMaxTopics - 1); const method = params.sort === 'old' ?
'getSortedSetRange' :
'getSortedSetRevRange';
const tids = await db[method](sets, 0, meta.config.recentMaxTopics - 1);
return pinnedTids.concat(tids); return pinnedTids.concat(tids);
} }
@@ -77,12 +102,13 @@ module.exports = function (Topics) {
return tids; return tids;
} }
const topicData = await Topics.getTopicsFields(tids, ['tid', 'lastposttime', 'upvotes', 'downvotes', 'postcount', 'pinned']); const topicData = await Topics.getTopicsFields(tids, ['tid', 'lastposttime', 'upvotes', 'downvotes', 'postcount', 'pinned']);
let sortFn = sortRecent; const sortMap = {
if (params.sort === 'posts') { recent: sortRecent,
sortFn = sortPopular; old: sortOld,
} else if (params.sort === 'votes') { posts: sortPopular,
sortFn = sortVotes; votes: sortVotes,
} };
const sortFn = sortMap[params.sort] || sortRecent;
if (params.floatPinned) { if (params.floatPinned) {
floatPinned(topicData, sortFn); floatPinned(topicData, sortFn);
@@ -106,6 +132,10 @@ module.exports = function (Topics) {
return b.lastposttime - a.lastposttime; return b.lastposttime - a.lastposttime;
} }
function sortOld(a, b) {
return a.lastposttime - b.lastposttime;
}
function sortVotes(a, b) { function sortVotes(a, b) {
if (a.votes !== b.votes) { if (a.votes !== b.votes) {
return b.votes - a.votes; return b.votes - a.votes;

View File

@@ -4,6 +4,7 @@ const _ = require('lodash');
const db = require('../database'); const db = require('../database');
const categories = require('../categories'); const categories = require('../categories');
const plugins = require('../plugins');
module.exports = function (User) { module.exports = function (User) {
User.setCategoryWatchState = async function (uid, cids, state) { User.setCategoryWatchState = async function (uid, cids, state) {
@@ -36,14 +37,24 @@ module.exports = function (User) {
if (!(parseInt(uid, 10) > 0)) { if (!(parseInt(uid, 10) > 0)) {
return []; return [];
} }
return await User.getCategoriesByStates(uid, [categories.watchStates.ignoring]); const cids = await User.getCategoriesByStates(uid, [categories.watchStates.ignoring]);
const result = await plugins.hooks.fire('filter:user.getIgnoredCategories', {
uid: uid,
cids: cids,
});
return result.cids;
}; };
User.getWatchedCategories = async function (uid) { User.getWatchedCategories = async function (uid) {
if (!(parseInt(uid, 10) > 0)) { if (!(parseInt(uid, 10) > 0)) {
return []; return [];
} }
return await User.getCategoriesByStates(uid, [categories.watchStates.watching]); const cids = await User.getCategoriesByStates(uid, [categories.watchStates.watching]);
const result = await plugins.hooks.fire('filter:user.getWatchedCategories', {
uid: uid,
cids: cids,
});
return result.cids;
}; };
User.getCategoriesByStates = async function (uid, states) { User.getCategoriesByStates = async function (uid, states) {

View File

@@ -1050,8 +1050,18 @@ describe('Controllers', function () {
}); });
}); });
it('should 404 if user does not exist', function (done) { it('should redirect to userslug and keep query params', (done) => {
request(nconf.get('url') + '/api/uid/123123', { json: true }, function (err, res) { request(`${nconf.get('url')}/api/uid/${fooUid}/topics?foo=bar`, { json: true }, (err, res, body) => {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert.equal(res.headers['x-redirect'], '/user/foo/topics?foo=bar');
assert.equal(body, '/user/foo/topics?foo=bar');
done();
});
});
it('should 404 if user does not exist', (done) => {
request(`${nconf.get('url')}/api/uid/123123`, { json: true }, (err, res) => {
assert.ifError(err); assert.ifError(err);
assert.equal(res.statusCode, 404); assert.equal(res.statusCode, 404);
done(); done();

View File

@@ -901,55 +901,8 @@ describe('Post\'s', function () {
}); });
}); });
it('shold error with invalid data', function (done) { it('should get post category', (done) => {
socketPosts.loadMoreBookmarks({ uid: voterUid }, { uid: voterUid, after: null }, function (err) { socketPosts.getCategory({ uid: voterUid }, pid, (err, postCid) => {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
it('should load more bookmarks', function (done) {
socketPosts.loadMoreBookmarks({ uid: voterUid }, { uid: voterUid, after: 0 }, function (err, data) {
assert.ifError(err);
assert(data);
done();
});
});
it('should load more user posts', function (done) {
socketPosts.loadMoreUserPosts({ uid: voterUid }, { uid: voterUid, after: 0 }, function (err, data) {
assert.ifError(err);
assert(data);
done();
});
});
it('should load more best posts', function (done) {
socketPosts.loadMoreBestPosts({ uid: voterUid }, { uid: voterUid, after: 0 }, function (err, data) {
assert.ifError(err);
assert(data);
done();
});
});
it('should load more up voted posts', function (done) {
socketPosts.loadMoreUpVotedPosts({ uid: voterUid }, { uid: voterUid, after: 0 }, function (err, data) {
assert.ifError(err);
assert(data);
done();
});
});
it('should load more down voted posts', function (done) {
socketPosts.loadMoreDownVotedPosts({ uid: voterUid }, { uid: voterUid, after: 0 }, function (err, data) {
assert.ifError(err);
assert(data);
done();
});
});
it('should get post category', function (done) {
socketPosts.getCategory({ uid: voterUid }, pid, function (err, postCid) {
assert.ifError(err); assert.ifError(err);
assert.equal(cid, postCid); assert.equal(cid, postCid);
done(); done();

View File

@@ -2518,12 +2518,21 @@ describe('Topic\'s', function () {
}); });
}); });
describe('sorted topics', function () { describe('sorted topics', () => {
it('should get sorted topics in category', function (done) { let category;
var filters = ['', 'watched', 'unreplied', 'new']; before(async () => {
async.map(filters, function (filter, next) { category = await categories.create({ name: 'sorted' });
const topic1Result = await topics.post({ uid: topic.userId, cid: category.cid, title: 'old replied', content: 'topic 1 OP' });
const topic2Result = await topics.post({ uid: topic.userId, cid: category.cid, title: 'most recent replied', content: 'topic 2 OP' });
await topics.reply({ uid: topic.userId, content: 'topic 1 reply', tid: topic1Result.topicData.tid });
await topics.reply({ uid: topic.userId, content: 'topic 2 reply', tid: topic2Result.topicData.tid });
});
it('should get sorted topics in category', (done) => {
const filters = ['', 'watched', 'unreplied', 'new'];
async.map(filters, (filter, next) => {
topics.getSortedTopics({ topics.getSortedTopics({
cids: [topic.categoryId], cids: [category.cid],
uid: topic.userId, uid: topic.userId,
start: 0, start: 0,
stop: -1, stop: -1,
@@ -2539,5 +2548,28 @@ describe('Topic\'s', function () {
done(); done();
}); });
}); });
it('should get topics recent replied first', async () => {
const data = await topics.getSortedTopics({
cids: [category.cid],
uid: topic.userId,
start: 0,
stop: -1,
sort: 'recent',
});
assert.strictEqual(data.topics[0].title, 'most recent replied');
assert.strictEqual(data.topics[1].title, 'old replied');
});
it('should get topics recent replied last', async () => {
const data = await topics.getSortedTopics({
cids: [category.cid],
uid: topic.userId,
start: 0,
stop: -1,
sort: 'old',
});
assert.strictEqual(data.topics[0].title, 'old replied');
assert.strictEqual(data.topics[1].title, 'most recent replied');
});
}); });
}); });