Compare commits

...

8 Commits

Author SHA1 Message Date
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
10 changed files with 128 additions and 32 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

@@ -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

@@ -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

@@ -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

@@ -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

@@ -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

@@ -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

@@ -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');
});
}); });
}); });