Compare commits

..

21 Commits

Author SHA1 Message Date
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
65 changed files with 419 additions and 569 deletions

View File

@@ -2,7 +2,7 @@
"name": "nodebb",
"license": "GPL-3.0",
"description": "NodeBB Forum",
"version": "1.16.2",
"version": "1.16.2-beta.2",
"homepage": "http://www.nodebb.org",
"repository": {
"type": "git",
@@ -43,7 +43,7 @@
"bcryptjs": "2.4.3",
"benchpressjs": "2.4.0",
"body-parser": "^1.19.0",
"bootbox": "5.5.2",
"bootbox": "4.4.0",
"bootstrap": "^3.4.1",
"chart.js": "^2.9.3",
"cli-graph": "^3.2.2",
@@ -101,7 +101,7 @@
"nodebb-plugin-spam-be-gone": "0.7.7",
"nodebb-rewards-essentials": "0.1.4",
"nodebb-theme-lavender": "5.0.17",
"nodebb-theme-persona": "10.3.19",
"nodebb-theme-persona": "10.3.18",
"nodebb-theme-slick": "1.3.8",
"nodebb-theme-vanilla": "11.3.10",
"nodebb-widget-essentials": "5.0.2",
@@ -154,12 +154,12 @@
"@commitlint/cli": "11.0.0",
"@commitlint/config-angular": "11.0.0",
"coveralls": "3.1.0",
"eslint": "7.18.0",
"eslint": "7.17.0",
"eslint-config-airbnb-base": "14.2.1",
"eslint-plugin-import": "2.22.1",
"grunt": "1.3.0",
"grunt-contrib-watch": "1.1.0",
"husky": "4.3.8",
"husky": "4.3.7",
"jsdom": "16.4.0",
"lint-staged": "10.5.3",
"mocha": "8.2.1",
@@ -190,4 +190,4 @@
"url": "https://github.com/barisusakli"
}
]
}
}

View File

@@ -13,7 +13,7 @@
"filter-active": "Ci sono uno o più filtri attivi in questa lista di segnalazioni",
"filter-reset": "Rimuovi Filtri",
"filters": "Opzioni Filtri",
"filter-reporterId": "UID segnalatore",
"filter-reporterId": "Segnalatore UID",
"filter-targetUid": "UID segnalato",
"filter-type": "Tipo Segnalazione",
"filter-type-all": "Tutto il Contenuto",

View File

@@ -104,16 +104,10 @@ paths:
$ref: 'write/posts/pid.yaml'
/posts/{pid}/state:
$ref: 'write/posts/pid/state.yaml'
/posts/{pid}/move:
$ref: 'write/posts/pid/move.yaml'
/posts/{pid}/vote:
$ref: 'write/posts/pid/vote.yaml'
/posts/{pid}/bookmark:
$ref: 'write/posts/pid/bookmark.yaml'
/posts/{pid}/diffs:
$ref: 'write/posts/pid/diffs.yaml'
/posts/{pid}/diffs/{since}:
$ref: 'write/posts/pid/diffs/since.yaml'
/admin/settings/{setting}:
$ref: 'write/admin/settings/setting.yaml'
/files/:

View File

@@ -1,41 +0,0 @@
get:
tags:
- posts
summary: get post edit history
description: This operation retrieves a post's edit history
parameters:
- in: path
name: pid
schema:
type: string
required: true
description: a valid post id
example: 2
responses:
'200':
description: Post history successfully retrieved.
content:
application/json:
schema:
type: object
properties:
status:
$ref: ../../../components/schemas/Status.yaml#/Status
response:
type: object
properties:
timestamps:
type: array
items:
type: number
revisions:
type: array
items:
type: object
properties:
timestamp:
type: number
username:
type: string
editable:
type: boolean

View File

@@ -1,65 +0,0 @@
get:
tags:
- posts
summary: get single post edit history
description: This operation retrieves a post's edit history
parameters:
- in: path
name: pid
schema:
type: string
required: true
description: a valid post id
example: 2
- in: path
name: since
schema:
type: number
required: true
description: a valid UNIX timestamp
example: 0
responses:
'200':
description: Post history successfully retrieved.
content:
application/json:
schema:
type: object
properties:
status:
$ref: ../../../../components/schemas/Status.yaml#/Status
response:
$ref: ../../../../components/schemas/PostObject.yaml#/PostObject
put:
tags:
- posts
summary: revert a post
description: This operation reverts a post to an earlier version. The revert process will append a new history item to the post's edit history.
parameters:
- in: path
name: pid
schema:
type: string
required: true
description: a valid post id
example: 2
- in: path
name: since
schema:
type: number
required: true
description: a valid UNIX timestamp
example: 0
responses:
'200':
description: Post successfully reverted
content:
application/json:
schema:
type: object
properties:
status:
$ref: ../../../../components/schemas/Status.yaml#/Status
response:
type: object
properties: {}

View File

@@ -1,36 +0,0 @@
put:
tags:
- posts
summary: move a post
description: This operation moves a post to a different topic.
parameters:
- in: path
name: pid
schema:
type: number
required: true
description: a valid post id
example: 5
requestBody:
content:
application/json:
schema:
type: object
properties:
tid:
type: number
description: a valid topic id
example: 4
responses:
'200':
description: Post successfully moved
content:
application/json:
schema:
type: object
properties:
status:
$ref: ../../../components/schemas/Status.yaml#/Status
response:
type: object
properties: {}

View File

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

View File

@@ -119,9 +119,9 @@ define('admin/manage/category', [
return app.alertError(err.message);
}
Benchpress.render('admin/partials/categories/copy-settings', {
app.parseAndTranslate('admin/partials/categories/copy-settings', {
categories: allCategories,
}).then(function (html) {
}, function (html) {
var selectedCid;
var modal = bootbox.dialog({
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');
posts.handleInfiniteScroll('posts.loadMoreBestPosts', 'account/best');
posts.handleInfiniteScroll('account/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');
posts.handleInfiniteScroll('posts.loadMoreBookmarks', 'account/bookmarks');
posts.handleInfiniteScroll('account/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');
posts.handleInfiniteScroll('posts.loadMoreDownVotedPosts', 'account/downvoted');
posts.handleInfiniteScroll('account/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) {
var AccountPosts = {};
var method;
var template;
var page = 1;
AccountPosts.init = function () {
header.init();
$('[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) {
method = _method;
AccountPosts.handleInfiniteScroll = function (_template) {
template = _template;
page = ajaxify.data.pagination.currentPage;
if (!config.usePagination) {
infinitescroll.init(loadMore);
}
@@ -26,17 +27,16 @@ define('forum/account/posts', ['forum/account/header', 'forum/infinitescroll'],
if (direction < 0) {
return;
}
var params = utils.params();
page += 1;
params.page = page;
infinitescroll.loadMore(method, {
uid: ajaxify.data.theirid,
after: $('[component="posts"]').attr('data-nextstart'),
}, function (data, done) {
infinitescroll.loadMoreXhr(params, function (data, done) {
if (data.posts && data.posts.length) {
onPostsLoaded(data.posts, done);
} else {
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) {
var AccountTopics = {};
var method;
var template;
var set;
var page = 1;
AccountTopics.init = function () {
header.init();
AccountTopics.handleInfiniteScroll('topics.loadMoreUserTopics', 'account/topics');
AccountTopics.handleInfiniteScroll('account/topics');
};
AccountTopics.handleInfiniteScroll = function (_method, _template, _set) {
method = _method;
AccountTopics.handleInfiniteScroll = function (_template) {
template = _template;
set = _set;
page = ajaxify.data.pagination.currentPage;
if (!config.usePagination) {
infinitescroll.init(loadMore);
}
@@ -27,20 +25,16 @@ define('forum/account/topics', ['forum/account/header', 'forum/infinitescroll'],
if (direction < 0) {
return;
}
var params = utils.params();
page += 1;
params.page = page;
infinitescroll.loadMore(method, {
set: set,
uid: ajaxify.data.theirid,
after: $('[component="category"]').attr('data-nextstart'),
count: config.topicsPerPage,
}, function (data, done) {
infinitescroll.loadMoreXhr(params, function (data, done) {
if (data.topics && data.topics.length) {
onTopicsLoaded(data.topics, done);
} else {
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');
posts.handleInfiniteScroll('posts.loadMoreUpVotedPosts', 'account/upvoted');
posts.handleInfiniteScroll('account/upvoted');
};
return Upvoted;

View File

@@ -7,7 +7,7 @@ define('forum/account/watched', ['forum/account/header', 'forum/account/topics']
AccountWatched.init = function () {
header.init();
topics.handleInfiniteScroll('topics.loadMoreFromSet', 'account/watched', 'uid:' + ajaxify.data.theirid + ':followed_tids');
topics.handleInfiniteScroll('account/watched');
};
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) {
if (els.length <= count) {
return;

View File

@@ -1,6 +1,6 @@
'use strict';
define('forum/topic/diffs', ['api', 'forum/topic/images'], function (api) {
define('forum/topic/diffs', ['forum/topic/images'], function () {
var Diffs = {};
Diffs.open = function (pid) {
@@ -10,7 +10,11 @@ define('forum/topic/diffs', ['api', 'forum/topic/images'], function (api) {
var localeStringOpts = { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric' };
api.get(`/posts/${pid}/diffs`, {}).then((data) => {
socket.emit('posts.getDiffs', { pid: pid }, function (err, data) {
if (err) {
return app.alertError(err.message);
}
app.parseAndTranslate('partials/modals/post_history', {
diffs: data.revisions.map(function (revision) {
var timestamp = parseInt(revision.timestamp, 10);
@@ -52,7 +56,7 @@ define('forum/topic/diffs', ['api', 'forum/topic/images'], function (api) {
revertEl.prop('disabled', true);
});
});
}).catch(app.alertError);
});
};
Diffs.load = function (pid, since, postContainer) {
@@ -60,7 +64,11 @@ define('forum/topic/diffs', ['api', 'forum/topic/images'], function (api) {
return;
}
api.get(`/posts/${pid}/diffs/${since}`, {}).then((data) => {
socket.emit('posts.showPostAt', { pid: pid, since: since }, function (err, data) {
if (err) {
return app.alertError(err.message);
}
data.deleted = !!parseInt(data.deleted, 10);
app.parseAndTranslate('partials/posts_list', 'posts', {
@@ -68,7 +76,7 @@ define('forum/topic/diffs', ['api', 'forum/topic/images'], function (api) {
}, function (html) {
postContainer.empty().append(html);
});
}).catch(app.alertError);
});
};
Diffs.restore = function (pid, since, modal) {
@@ -76,10 +84,14 @@ define('forum/topic/diffs', ['api', 'forum/topic/images'], function (api) {
return;
}
api.put(`/posts/${pid}/diffs/${since}`, {}).then(() => {
socket.emit('posts.restoreDiff', { pid: pid, since: since }, function (err) {
if (err) {
return app.alertError(err);
}
modal.modal('hide');
app.alertSuccess('[[topic:diffs.post-restored]]');
}).catch(app.alertError);
});
};
return Diffs;

View File

@@ -2,8 +2,8 @@
define('forum/topic/move-post', [
'components', 'postSelect', 'translator', 'alerts', 'api',
], function (components, postSelect, translator, alerts, api) {
'components', 'postSelect', 'translator', 'alerts',
], function (components, postSelect, translator, alerts) {
var MovePost = {};
var moveModal;
@@ -100,10 +100,10 @@ define('forum/topic/move-post', [
if (!ajaxify.data.template.topic || !data.tid) {
return;
}
Promise.all(data.pids.map(pid => api.put(`/posts/${pid}/move`, {
tid: data.tid,
}))).then(() => {
socket.emit('posts.movePosts', { pids: data.pids, tid: data.tid }, function (err) {
if (err) {
return app.alertError(err.message);
}
data.pids.forEach(function (pid) {
components.get('post', 'pid', pid).fadeOut(500, function () {
$(this).remove();
@@ -111,7 +111,7 @@ define('forum/topic/move-post', [
});
closeMoveModal();
}).catch(app.alertError);
});
}
function closeMoveModal() {

View File

@@ -210,9 +210,7 @@ define('forum/topic/posts', [
html.insertBefore(before);
// Now restore the relative position the user was on prior to new post insertion
if (scrollTop > 0) {
$(window).scrollTop(scrollTop + ($(document).height() - height));
}
$(window).scrollTop(scrollTop + ($(document).height() - height));
} else {
components.get('topic').append(html);
}

View File

@@ -2,7 +2,19 @@
(function (factory) {
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); };
if (typeof define === 'function' && define.amd) {
@@ -286,9 +298,6 @@
var out = translated;
translatedArgs.forEach(function (arg, i) {
var escaped = arg.replace(/%(?=\d)/g, '&#37;').replace(/\\,/g, '&#44;');
// fix double escaped translation keys, see https://github.com/NodeBB/NodeBB/issues/9206
escaped = escaped.replace(/&amp;lsqb;/g, '&lsqb;')
.replace(/&amp;rsqb;/g, '&rsqb;');
out = out.replace(new RegExp('%' + (i + 1), 'g'), escaped);
});
return out;
@@ -548,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
*/

View File

@@ -4,7 +4,6 @@ const validator = require('validator');
const _ = require('lodash');
const utils = require('../utils');
const user = require('../user');
const posts = require('../posts');
const topics = require('../topics');
const groups = require('../groups');
@@ -13,7 +12,6 @@ const events = require('../events');
const privileges = require('../privileges');
const apiHelpers = require('./helpers');
const websockets = require('../socket.io');
const socketHelpers = require('../socket.io/helpers');
const postsAPI = module.exports;
@@ -196,27 +194,6 @@ async function isMainAndLastPost(pid) {
};
}
postsAPI.move = async function (caller, data) {
const canMove = await Promise.all([
privileges.topics.isAdminOrMod(data.tid, caller.uid),
privileges.posts.canMove(data.pid, caller.uid),
]);
if (!canMove.every(Boolean)) {
throw new Error('[[error:no-privileges]]');
}
await topics.movePostToTopic(caller.uid, data.pid, data.tid);
const [postDeleted, topicDeleted] = await Promise.all([
posts.getPostField(data.pid, 'deleted'),
topics.getTopicField(data.tid, 'deleted'),
]);
if (!postDeleted && !topicDeleted) {
socketHelpers.sendNotificationToPostOwner(data.pid, caller.uid, 'move', 'notifications:moved_your_post');
}
};
postsAPI.upvote = async function (caller, data) {
return await apiHelpers.postCommand(caller, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data);
};
@@ -236,61 +213,3 @@ postsAPI.bookmark = async function (caller, data) {
postsAPI.unbookmark = async function (caller, data) {
return await apiHelpers.postCommand(caller, 'unbookmark', 'bookmarked', '', data);
};
async function diffsPrivilegeCheck(pid, uid) {
const [deleted, privilegesData] = await Promise.all([
posts.getPostField(pid, 'deleted'),
privileges.posts.get([pid], uid),
]);
const allowed = privilegesData[0]['posts:history'] && (deleted ? privilegesData[0]['posts:view_deleted'] : true);
if (!allowed) {
throw new Error('[[error:no-privileges]]');
}
}
postsAPI.getDiffs = async (caller, data) => {
await diffsPrivilegeCheck(data.pid, caller.uid);
const timestamps = await posts.diffs.list(data.pid);
const post = await posts.getPostFields(data.pid, ['timestamp', 'uid']);
const diffs = await posts.diffs.get(data.pid);
const uids = diffs.map(diff => diff.uid || null);
uids.push(post.uid);
let usernames = await user.getUsersFields(uids, ['username']);
usernames = usernames.map(userObj => (userObj.uid ? userObj.username : null));
let canEdit = true;
try {
await user.isPrivilegedOrSelf(caller.uid, post.uid);
} catch (e) {
canEdit = false;
}
timestamps.push(post.timestamp);
return {
timestamps: timestamps,
revisions: timestamps.map((timestamp, idx) => ({
timestamp: timestamp,
username: usernames[idx],
})),
editable: canEdit,
};
};
postsAPI.loadDiff = async (caller, data) => {
await diffsPrivilegeCheck(data.pid, caller.uid);
return await posts.diffs.load(data.pid, data.since, caller.uid);
};
postsAPI.restoreDiff = async (caller, data) => {
const cid = await posts.getCidByPid(data.pid);
const canEdit = await privileges.categories.can('edit', cid, caller.uid);
if (!canEdit) {
throw new Error('[[error:no-privileges]]');
}
const edit = await posts.diffs.restore(data.pid, data.since, caller.uid, apiHelpers.buildReqObject(caller));
websockets.in('topic_' + edit.topic.tid).emit('event:post_edited', edit);
};

View File

@@ -150,6 +150,11 @@ module.exports = function (Categories) {
if (copyParent) {
destination.parentCid = source.parentCid || 0;
}
await plugins.hooks.fire('filter:categories.copySettingsFrom', {
source: source,
destination: destination,
copyParent: copyParent,
});
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.parent = parent;
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;
};

View File

@@ -14,7 +14,7 @@ blocksController.getBlocks = async function (req, res, next) {
const start = Math.max(0, page - 1) * resultsPerPage;
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) {
return next();
}

View File

@@ -8,7 +8,7 @@ const helpers = require('../helpers');
const categoriesController = module.exports;
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) {
return next();
}

View File

@@ -12,7 +12,7 @@ consentController.get = async function (req, res, 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) {
return next();
}

View File

@@ -12,7 +12,7 @@ const editController = module.exports;
editController.get = async function (req, res, next) {
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),
]);
if (!userData) {
@@ -114,7 +114,7 @@ async function renderRoute(name, req, res, next) {
}
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) {
return null;
}

View File

@@ -16,7 +16,7 @@ followController.getFollowers = async function (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) {
return next();
}

View File

@@ -7,7 +7,7 @@ const accountHelpers = require('./helpers');
const groupsController = module.exports;
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) {
return next();
}

View File

@@ -16,7 +16,7 @@ const categories = require('../../categories');
const helpers = module.exports;
helpers.getUserDataByUserSlug = async function (userslug, callerUID) {
helpers.getUserDataByUserSlug = async function (userslug, callerUID, query = {}) {
const uid = await user.getUidByUserslug(userslug);
if (!uid) {
return null;
@@ -114,7 +114,11 @@ helpers.getUserDataByUserSlug = async function (userslug, 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;
};

View File

@@ -9,7 +9,7 @@ const pagination = require('../../pagination');
const infoController = module.exports;
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) {
return next();
}

View File

@@ -51,11 +51,12 @@ notificationsController.get = async function (req, res, next) {
if (!selectedFilter) {
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', {
notifications: notifications,

View File

@@ -8,6 +8,8 @@ const categories = require('../../categories');
const pagination = require('../../pagination');
const helpers = require('../helpers');
const accountHelpers = require('./helpers');
const plugins = require('../../plugins');
const utils = require('../../utils');
const postsController = module.exports;
@@ -107,43 +109,43 @@ const templateToData = {
};
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) {
await getFromUserSet('account/posts', req, res, next);
await getPostsFromUserSet('account/posts', 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) {
await getFromUserSet('account/downvoted', req, res, next);
await getPostsFromUserSet('account/downvoted', 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) {
await getFromUserSet('account/watched', req, res, next);
await getPostsFromUserSet('account/watched', 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) {
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 page = Math.max(1, parseInt(req.query.page, 10) || 1);
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),
]);
@@ -154,12 +156,26 @@ async function getFromUserSet(template, req, res, callback) {
const start = (page - 1) * itemsPerPage;
const stop = start + itemsPerPage - 1;
const sets = await data.getSets(req.uid, userData);
const [itemCount, itemData] = await Promise.all([
settings.usePagination ? db.sortedSetsCardSum(sets) : 0,
getItemData(sets, data, req, start, stop),
]);
let result;
if (plugins.hooks.hasListeners('filter:account.getPostsFromUserSet')) {
result = await plugins.hooks.fire('filter:account.getPostsFromUserSet', {
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.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) {
return next();
}

View File

@@ -7,7 +7,7 @@ const accountHelpers = require('./helpers');
const sessionController = module.exports;
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) {
return next();
}

View File

@@ -18,7 +18,7 @@ const accountHelpers = require('./helpers');
const settingsController = module.exports;
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) {
return next();
}

View File

@@ -11,7 +11,7 @@ const accountHelpers = require('./helpers');
const uploadsController = module.exports;
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) {
return next();
}

View File

@@ -49,8 +49,8 @@ categoryController.get = async function (req, res, next) {
return helpers.notAllowed(req, res);
}
if (!res.locals.isAPI && (!req.params.slug || categoryFields.slug !== cid + '/' + req.params.slug) && (categoryFields.slug && categoryFields.slug !== cid + '/')) {
return helpers.redirect(res, '/category/' + categoryFields.slug, true);
if (!res.locals.isAPI && !req.params.slug && (categoryFields.slug && categoryFields.slug !== `${cid}/`)) {
return helpers.redirect(res, `/category/${categoryFields.slug}`, true);
}
if (categoryFields.link) {

View File

@@ -118,7 +118,7 @@ function calculateStartStop(page, postIndex, settings) {
let startSkip = 0;
if (!settings.usePagination) {
if (postIndex !== 0) {
if (postIndex > 1) {
page = 1;
}
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');
}
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);
};

View File

@@ -38,14 +38,6 @@ Posts.delete = async (req, res) => {
helpers.formatApiResponse(200, res);
};
Posts.move = async (req, res) => {
await api.posts.move(req, {
pid: req.params.pid,
tid: req.body.tid,
});
helpers.formatApiResponse(200, res);
};
async function mock(req) {
const tid = await posts.getPostField(req.params.pid, 'tid');
return { pid: req.params.pid, room_id: `topic_${tid}` };
@@ -81,16 +73,3 @@ Posts.unbookmark = async (req, res) => {
await api.posts.unbookmark(req, data);
helpers.formatApiResponse(200, res);
};
Posts.getDiffs = async (req, res) => {
helpers.formatApiResponse(200, res, await api.posts.getDiffs(req, { ...req.params }));
};
Posts.loadDiff = async (req, res) => {
helpers.formatApiResponse(200, res, await api.posts.loadDiff(req, { ...req.params }));
};
Posts.restoreDiff = async (req, res) => {
helpers.formatApiResponse(200, res, await api.posts.restoreDiff(req, { ...req.params }));
};

View File

@@ -104,14 +104,8 @@ Topics.deleteTags = async (req, res) => {
};
Topics.getThumbs = async (req, res) => {
if (isFinite(req.params.tid)) { // post_uuids can be passed in occasionally, in that case no checks are necessary
const [exists, canRead] = await Promise.all([
topics.exists(req.params.tid),
privileges.topics.can('topics:read', req.params.tid, req.uid),
]);
if (!exists || !canRead) {
return helpers.formatApiResponse(403, res);
}
if (!await privileges.topics.can('topics:read', req.params.tid, req.uid)) {
return helpers.formatApiResponse(403, res);
}
helpers.formatApiResponse(200, res, await topics.thumbs.get(req.params.tid));

View File

@@ -230,16 +230,6 @@ Emailer.send = async (template, uid, params) => {
params.uid = uid;
params.username = userData.username;
params.rtl = await translator.translate('[[language:dir]]', userSettings.userLang) === 'rtl';
const result = await Plugins.hooks.fire('filter:email.cancel', {
cancel: false, // set to true in plugin to cancel sending email
template: template,
params: params,
});
if (result.cancel) {
return;
}
await Emailer.sendToEmail(template, userData.email, userSettings.userLang, params);
};

View File

@@ -3,7 +3,8 @@
const fs = require('fs');
const path = require('path');
const utils = require('./utils');
const paths = require('./constants').paths;
const { paths } = require('./constants');
const plugins = require('./plugins');
const Languages = module.exports;
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.get = async function (language, namespace) {
const data = await fs.promises.readFile(path.join(languagesPath, language, namespace + '.json'), 'utf8');
return JSON.parse(data) || {};
const data = await fs.promises.readFile(path.join(languagesPath, language, `${namespace}.json`), 'utf8');
const parsed = JSON.parse(data) || {};
const result = await plugins.hooks.fire('filter:languages.get', {
language,
namespace,
data: parsed,
});
return result.data;
};
let codeCache = null;

View File

@@ -46,7 +46,7 @@ Assert.topic = helpers.try(async (req, res, next) => {
Assert.post = helpers.try(async (req, res, next) => {
if (!await posts.exists(req.params.pid)) {
return controllerHelpers.formatApiResponse(404, res, new Error('[[error:no-post]]'));
return controllerHelpers.formatApiResponse(404, res, new Error('[[error:no-topic]]'));
}
next();

View File

@@ -1,26 +1,26 @@
'use strict';
const nconf = require('nconf');
const jsesc = require('jsesc');
const _ = require('lodash');
var nconf = require('nconf');
var jsesc = require('jsesc');
var _ = require('lodash');
const validator = require('validator');
const util = require('util');
var util = require('util');
const db = require('../database');
const user = require('../user');
const topics = require('../topics');
const messaging = require('../messaging');
const flags = require('../flags');
const meta = require('../meta');
const plugins = require('../plugins');
const navigation = require('../navigation');
const translator = require('../translator');
const privileges = require('../privileges');
const languages = require('../languages');
const utils = require('../utils');
const helpers = require('./helpers');
var db = require('../database');
var user = require('../user');
var topics = require('../topics');
var messaging = require('../messaging');
var flags = require('../flags');
var meta = require('../meta');
var plugins = require('../plugins');
var navigation = require('../navigation');
var translator = require('../translator');
var privileges = require('../privileges');
var languages = require('../languages');
var utils = require('../utils');
var helpers = require('./helpers');
const controllers = {
var controllers = {
api: require('../controllers/api'),
helpers: require('../controllers/helpers'),
};
@@ -49,9 +49,9 @@ middleware.buildHeader = helpers.try(async function buildHeader(req, res, next)
middleware.buildHeaderAsync = util.promisify(middleware.buildHeader);
middleware.renderHeader = async function renderHeader(req, res, data) {
const registrationType = meta.config.registrationType || 'normal';
var registrationType = meta.config.registrationType || 'normal';
res.locals.config = res.locals.config || {};
const templateValues = {
var templateValues = {
title: meta.config.title || '',
'title:url': meta.config['title:url'] || '',
description: meta.config.description || '',
@@ -240,7 +240,7 @@ middleware.renderFooter = async function renderFooter(req, res, templateValues)
};
function modifyTitle(obj) {
const title = controllers.helpers.buildTitle(meta.config.homePageTitle || '[[pages:home]]');
var title = controllers.helpers.buildTitle(meta.config.homePageTitle || '[[pages:home]]');
obj.browserTitle = title;
if (obj.metaTags) {

View File

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

View File

@@ -33,10 +33,16 @@ module.exports = function (middleware) {
options.url = (req.baseUrl + req.path.replace(/^\/api/, ''));
options.bodyClass = 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 renderResult = await plugins.hooks.fire('filter:middleware.render', { req: req, res: res, templateData: buildResult.templateData });
if (res.headersSent) {
return;
}
options = renderResult.templateData;
options._header = {
tags: await meta.tags.parse(req, renderResult, res.locals.metaTags, res.locals.linkTags),

View File

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

View File

@@ -327,9 +327,10 @@ Notifications.prune = async function () {
]);
await batch.processSortedSet('users:joindate', async function (uids) {
const unread = uids.map(uid => 'uid:' + uid + ':notifications:unread');
const read = uids.map(uid => 'uid:' + uid + ':notifications:read');
await db.sortedSetsRemoveRangeByScore(unread.concat(read), '-inf', cutoffTime);
await Promise.all([
db.sortedSetsRemoveRangeByScore(uids.map(uid => 'uid:' + uid + ':notifications:unread'), '-inf', cutoffTime),
db.sortedSetsRemoveRangeByScore(uids.map(uid => 'uid:' + uid + ':notifications:read'), '-inf', cutoffTime),
]);
}, { batch: 500, interval: 100 });
} catch (err) {
if (err) {

View File

@@ -92,7 +92,7 @@ Hooks.unregister = function (id, hook, method) {
Hooks.fire = async function (hook, params) {
const hookList = plugins.loadedHooks[hook];
const hookType = hook.split(':')[0];
if (global.env === 'development' && hook !== 'action:plugins.firehook') {
if (global.env === 'development' && hook !== 'action:plugins.fireHook') {
winston.verbose('[plugins/fireHook] ' + hook);
}
@@ -102,8 +102,8 @@ Hooks.fire = async function (hook, params) {
}
const result = await hookTypeToMethod[hookType](hook, hookList, params);
if (hook !== 'action:plugins.firehook') {
Hooks.fire('action:plugins.firehook', { hook: hook, params: params });
if (hook !== 'action:plugins.fireHook') {
Hooks.fire('action:plugins.fireHook', { hook: hook, params: params });
}
if (result !== undefined) {
return result;

View File

@@ -18,17 +18,11 @@ module.exports = function () {
setupApiRoute(router, 'put', '/:pid/state', [...middlewares, middleware.assert.post], controllers.write.posts.restore);
setupApiRoute(router, 'delete', '/:pid/state', [...middlewares, middleware.assert.post], controllers.write.posts.delete);
setupApiRoute(router, 'put', '/:pid/move', [...middlewares, middleware.assert.post, middleware.checkRequired.bind(null, ['tid'])], controllers.write.posts.move);
setupApiRoute(router, 'put', '/:pid/vote', [...middlewares, middleware.checkRequired.bind(null, ['delta']), middleware.assert.post], controllers.write.posts.vote);
setupApiRoute(router, 'delete', '/:pid/vote', [...middlewares, middleware.assert.post], controllers.write.posts.unvote);
setupApiRoute(router, 'put', '/:pid/bookmark', [...middlewares, middleware.assert.post], controllers.write.posts.bookmark);
setupApiRoute(router, 'delete', '/:pid/bookmark', [...middlewares, middleware.assert.post], controllers.write.posts.unbookmark);
setupApiRoute(router, 'get', '/:pid/diffs', [middleware.authenticateOrGuest, middleware.assert.post], controllers.write.posts.getDiffs);
setupApiRoute(router, 'get', '/:pid/diffs/:since', [middleware.authenticateOrGuest, middleware.assert.post], controllers.write.posts.loadDiff);
setupApiRoute(router, 'put', '/:pid/diffs/:since', [...middlewares, middleware.assert.post], controllers.write.posts.restoreDiff);
return router;
};

View File

@@ -6,7 +6,6 @@ const privileges = require('../privileges');
const plugins = require('../plugins');
const meta = require('../meta');
const topics = require('../topics');
const categories = require('../categories');
const user = require('../user');
const socketHelpers = require('./helpers');
const utils = require('../utils');
@@ -100,41 +99,6 @@ SocketPosts.getPost = async function (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) {
return await posts.getCidByPid(pid);
};

View File

@@ -1,21 +1,67 @@
'use strict';
const api = require('../../api');
const posts = require('../../posts');
const user = require('../../user');
const privileges = require('../../privileges');
const apiHelpers = require('../../api/helpers');
const websockets = require('..');
module.exports = function (SocketPosts) {
SocketPosts.getDiffs = async function (socket, data) {
websockets.warnDeprecated(socket, 'GET /api/v3/posts/:pid/diffs');
return await api.posts.getDiffs(socket, data);
await privilegeCheck(data.pid, socket.uid);
const timestamps = await posts.diffs.list(data.pid);
const post = await posts.getPostFields(data.pid, ['timestamp', 'uid']);
const diffs = await posts.diffs.get(data.pid);
const uids = diffs.map(diff => diff.uid || null);
uids.push(post.uid);
let usernames = await user.getUsersFields(uids, ['username']);
usernames = usernames.map(userObj => (userObj.uid ? userObj.username : null));
let canEdit = true;
try {
await user.isPrivilegedOrSelf(socket.uid, post.uid);
} catch (e) {
canEdit = false;
}
timestamps.push(post.timestamp);
return {
timestamps: timestamps,
revisions: timestamps.map((timestamp, idx) => ({
timestamp: timestamp,
username: usernames[idx],
})),
editable: canEdit,
};
};
SocketPosts.showPostAt = async function (socket, data) {
websockets.warnDeprecated(socket, 'GET /api/v3/posts/:pid/diffs/:since');
return await api.posts.loadDiff(socket, data);
await privilegeCheck(data.pid, socket.uid);
return await posts.diffs.load(data.pid, data.since, socket.uid);
};
async function privilegeCheck(pid, uid) {
const [deleted, privilegesData] = await Promise.all([
posts.getPostField(pid, 'deleted'),
privileges.posts.get([pid], uid),
]);
const allowed = privilegesData[0]['posts:history'] && (deleted ? privilegesData[0]['posts:view_deleted'] : true);
if (!allowed) {
throw new Error('[[error:no-privileges]]');
}
}
SocketPosts.restoreDiff = async function (socket, data) {
websockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/diffs/:since');
return await api.posts.restoreDiff(socket, data);
const cid = await posts.getCidByPid(data.pid);
const canEdit = await privileges.categories.can('edit', cid, socket.uid);
if (!canEdit) {
throw new Error('[[error:no-privileges]]');
}
const edit = await posts.diffs.restore(data.pid, data.since, socket.uid, apiHelpers.buildReqObject(socket));
websockets.in('topic_' + edit.topic.tid).emit('event:post_edited', edit);
};
};

View File

@@ -1,33 +1,45 @@
'use strict';
const api = require('../../api');
const sockets = require('..');
const privileges = require('../../privileges');
const topics = require('../../topics');
const posts = require('../../posts');
const socketHelpers = require('../helpers');
module.exports = function (SocketPosts) {
function moveChecks(socket, typeCheck, data) {
SocketPosts.movePost = async function (socket, data) {
await SocketPosts.movePosts(socket, { pids: [data.pid], tid: data.tid });
};
SocketPosts.movePosts = async function (socket, data) {
if (!socket.uid) {
throw new Error('[[error:not-logged-in]]');
}
if (!data || !typeCheck || !data.tid) {
if (!data || !Array.isArray(data.pids) || !data.tid) {
throw new Error('[[error:invalid-data]]');
}
}
SocketPosts.movePost = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/move');
const canMove = await privileges.topics.isAdminOrMod(data.tid, socket.uid);
if (!canMove) {
throw new Error('[[error:no-privileges]]');
}
moveChecks(socket, isFinite(data.pid), data);
await api.posts.move(socket, data);
};
for (const pid of data.pids) {
/* eslint-disable no-await-in-loop */
const canMove = await privileges.posts.canMove(pid, socket.uid);
if (!canMove) {
throw new Error('[[error:no-privileges]]');
}
await topics.movePostToTopic(socket.uid, pid, data.tid);
SocketPosts.movePosts = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/move');
const [postDeleted, topicDeleted] = await Promise.all([
posts.getPostField(pid, 'deleted'),
topics.getTopicField(data.tid, 'deleted'),
]);
moveChecks(socket, !Array.isArray(data.pids), data);
await Promise.all(data.pids.map(async pid => api.posts.move(socket, {
tid: data.tid,
pid,
})));
if (!postDeleted && !topicDeleted) {
socketHelpers.sendNotificationToPostOwner(pid, socket.uid, 'move', 'notifications:moved_your_post');
}
}
};
};

View File

@@ -1,7 +1,6 @@
'use strict';
const topics = require('../../topics');
const categories = require('../../categories');
const privileges = require('../../privileges');
const meta = require('../../meta');
const utils = require('../../utils');
@@ -89,12 +88,6 @@ module.exports = function (SocketTopics) {
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) {
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));

View File

@@ -48,6 +48,10 @@ module.exports = function (Topics) {
tids = await db.getSortedSetRevRange('uid:' + params.uid + ':followed_tids', 0, -1);
} else if (params.cids) {
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 {
tids = await db.getSortedSetRevRange('topics:' + params.sort, 0, meta.config.recentMaxTopics - 1);
}
@@ -55,12 +59,30 @@ module.exports = function (Topics) {
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) {
const sets = [];
const pinnedSets = [];
params.cids.forEach(function (cid) {
if (params.sort === 'recent') {
sets.push('cid:' + cid + ':tids');
params.cids.forEach((cid) => {
if (params.sort === 'recent' || params.sort === 'old') {
sets.push(`cid:${cid}:tids`);
} else {
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);
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);
}
@@ -77,12 +102,13 @@ module.exports = function (Topics) {
return tids;
}
const topicData = await Topics.getTopicsFields(tids, ['tid', 'lastposttime', 'upvotes', 'downvotes', 'postcount', 'pinned']);
let sortFn = sortRecent;
if (params.sort === 'posts') {
sortFn = sortPopular;
} else if (params.sort === 'votes') {
sortFn = sortVotes;
}
const sortMap = {
recent: sortRecent,
old: sortOld,
posts: sortPopular,
votes: sortVotes,
};
const sortFn = sortMap[params.sort] || sortRecent;
if (params.floatPinned) {
floatPinned(topicData, sortFn);
@@ -106,6 +132,10 @@ module.exports = function (Topics) {
return b.lastposttime - a.lastposttime;
}
function sortOld(a, b) {
return a.lastposttime - b.lastposttime;
}
function sortVotes(a, b) {
if (a.votes !== b.votes) {
return b.votes - a.votes;

View File

@@ -16,7 +16,7 @@ module.exports = {
const userData = await user.getUsersFields(uids, ['uid', 'fullname']);
const bulkAdd = userData
.filter(u => u.uid && u.fullname)
.map(u => ['fullname:sorted', 0, String(u.fullname).substr(0, 255).toLowerCase() + ':' + u.uid]);
.map(u => ['fullname:sorted', 0, u.fullname.substr(0, 255).toLowerCase() + ':' + u.uid]);
await db.sortedSetAddBulk(bulkAdd);
}, {
batch: 500,

View File

@@ -13,9 +13,6 @@ module.exports = {
timestamp: Date.UTC(2020, 9, 13),
method: async function () {
const progress = this.progress;
const maxGroupLength = meta.config.maximumGroupNameLength;
meta.config.maximumGroupNameLength = 30;
const timestamp = await db.getObjectField('group:administrators', 'timestamp');
const verifiedExists = await groups.exists('verified-users');
if (!verifiedExists) {
@@ -41,8 +38,7 @@ module.exports = {
timestamp: timestamp + 1,
});
}
// restore setting
meta.config.maximumGroupNameLength = maxGroupLength;
await batch.processSortedSet('users:joindate', async function (uids) {
progress.incr(uids.length);
const userData = await user.getUsersFields(uids, ['uid', 'email:confirmed']);

View File

@@ -135,12 +135,6 @@ describe('API', async () => {
title: 'Test Topic 2',
content: 'Test topic 2 content',
});
await topics.post({
uid: unprivUid,
cid: testCategory.cid,
title: 'Test Topic 3',
content: 'Test topic 3 content',
});
// Create a sample flag
await flags.create('post', 1, unprivUid, 'sample reasons', Date.now());

View File

@@ -1050,8 +1050,18 @@ describe('Controllers', function () {
});
});
it('should 404 if user does not exist', function (done) {
request(nconf.get('url') + '/api/uid/123123', { json: true }, function (err, res) {
it('should redirect to userslug and keep query params', (done) => {
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.equal(res.statusCode, 404);
done();

View File

@@ -901,55 +901,8 @@ describe('Post\'s', function () {
});
});
it('shold error with invalid data', function (done) {
socketPosts.loadMoreBookmarks({ uid: voterUid }, { uid: voterUid, after: null }, function (err) {
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) {
it('should get post category', (done) => {
socketPosts.getCategory({ uid: voterUid }, pid, (err, postCid) => {
assert.ifError(err);
assert.equal(cid, postCid);
done();

View File

@@ -2518,12 +2518,21 @@ describe('Topic\'s', function () {
});
});
describe('sorted topics', function () {
it('should get sorted topics in category', function (done) {
var filters = ['', 'watched', 'unreplied', 'new'];
async.map(filters, function (filter, next) {
describe('sorted topics', () => {
let category;
before(async () => {
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({
cids: [topic.categoryId],
cids: [category.cid],
uid: topic.userId,
start: 0,
stop: -1,
@@ -2539,5 +2548,28 @@ describe('Topic\'s', function () {
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');
});
});
});

View File

@@ -135,16 +135,6 @@ describe('new Translator(language)', function () {
});
});
it('should translate escaped translation arguments properly', function () {
// https://github.com/NodeBB/NodeBB/issues/9206
var translator = Translator.create('en-GB');
var key = '[[notifications:upvoted_your_post_in, test1, error: Error: &lsqb;&lsqb;error:group-name-too-long&rsqb;&rsqb; on NodeBB Upgrade]]';
return translator.translate(key).then(function (translated) {
assert.strictEqual(translated, '<strong>test1</strong> has upvoted your post in <strong>error: Error: &lsqb;&lsqb;error:group-name-too-long&rsqb;&rsqb; on NodeBB Upgrade</strong>.');
});
});
it('should properly escape and ignore % and \\, in arguments', function () {
var translator = Translator.create('en-GB');