feat: #7743 account/posts controller

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 9b3f4b98d1
commit e72f3e4ffe

@ -1,137 +1,38 @@
'use strict'; 'use strict';
const validator = require('validator');
var async = require('async'); const winston = require('winston');
var validator = require('validator'); const nconf = require('nconf');
var winston = require('winston');
var nconf = require('nconf'); const user = require('../../user');
const groups = require('../../groups');
var user = require('../../user'); const plugins = require('../../plugins');
var groups = require('../../groups'); const meta = require('../../meta');
var plugins = require('../../plugins'); const utils = require('../../utils');
var meta = require('../../meta'); const privileges = require('../../privileges');
var utils = require('../../utils');
var privileges = require('../../privileges');
const translator = require('../../translator'); const translator = require('../../translator');
var helpers = module.exports; const helpers = module.exports;
helpers.getUserDataByUserSlug = function (userslug, callerUID, callback) { helpers.getUserDataByUserSlug = async function (userslug, callerUID) {
let results; const uid = await user.getUidByUserslug(userslug);
async.waterfall([
function (next) {
user.getUidByUserslug(userslug, next);
},
function (uid, next) {
if (!uid) { if (!uid) {
return callback(null, null); return null;
} }
async.parallel({ const results = await getAllData(uid, callerUID);
userData: function (next) {
user.getUserData(uid, next);
},
isTargetAdmin: function (next) {
user.isAdministrator(uid, next);
},
userSettings: function (next) {
user.getSettings(uid, next);
},
isAdmin: function (next) {
user.isAdministrator(callerUID, next);
},
isGlobalModerator: function (next) {
user.isGlobalModerator(callerUID, next);
},
isModerator: function (next) {
user.isModeratorOfAnyCategory(callerUID, next);
},
isFollowing: function (next) {
user.isFollowing(callerUID, uid, next);
},
ips: function (next) {
user.getIPs(uid, 4, next);
},
profile_menu: function (next) {
const links = [{
id: 'info',
route: 'info',
name: '[[user:account_info]]',
visibility: {
self: false,
other: false,
moderator: true,
globalMod: true,
admin: true,
},
}, {
id: 'sessions',
route: 'sessions',
name: '[[pages:account/sessions]]',
visibility: {
self: true,
other: false,
moderator: false,
globalMod: false,
admin: false,
},
}];
if (meta.config.gdpr_enabled) {
links.push({
id: 'consent',
route: 'consent',
name: '[[user:consent.title]]',
visibility: {
self: true,
other: false,
moderator: false,
globalMod: false,
admin: false,
},
});
}
plugins.fireHook('filter:user.profileMenu', {
uid: uid,
callerUID: callerUID,
links: links,
}, next);
},
groups: function (next) {
groups.getUserGroups([uid], next);
},
sso: function (next) {
plugins.fireHook('filter:auth.list', { uid: uid, associations: [] }, next);
},
canEdit: function (next) {
privileges.users.canEdit(callerUID, uid, next);
},
canBanUser: function (next) {
privileges.users.canBanUser(callerUID, uid, next);
},
isBlocked: function (next) {
user.blocks.is(uid, callerUID, next);
},
}, next);
},
function (_results, next) {
results = _results;
if (!results.userData) { if (!results.userData) {
return callback(new Error('[[error:invalid-uid]]')); throw new Error('[[error:invalid-uid]]');
} }
parseAboutMe(results.userData, next); await parseAboutMe(results.userData);
},
function (next) { const userData = results.userData;
var userData = results.userData; const userSettings = results.userSettings;
var userSettings = results.userSettings; const isAdmin = results.isAdmin;
var isAdmin = results.isAdmin; const isGlobalModerator = results.isGlobalModerator;
var isGlobalModerator = results.isGlobalModerator; const isModerator = results.isModerator;
var isModerator = results.isModerator; const isSelf = parseInt(callerUID, 10) === parseInt(userData.uid, 10);
var isSelf = parseInt(callerUID, 10) === parseInt(userData.uid, 10);
userData.joindateISO = utils.toISOString(userData.joindate);
userData.lastonlineISO = utils.toISOString(userData.lastonline || userData.joindate);
userData.age = Math.max(0, userData.birthday ? Math.floor((new Date().getTime() - new Date(userData.birthday).getTime()) / 31536000000) : 0); userData.age = Math.max(0, userData.birthday ? Math.floor((new Date().getTime() - new Date(userData.birthday).getTime()) / 31536000000) : 0);
userData.emailClass = 'hide'; userData.emailClass = 'hide';
@ -188,7 +89,6 @@ helpers.getUserDataByUserSlug = function (userslug, callerUID, callback) {
}); });
userData.sso = results.sso.associations; userData.sso = results.sso.associations;
userData.status = user.getStatus(userData);
userData.banned = userData.banned === 1; userData.banned = userData.banned === 1;
userData.website = validator.escape(String(userData.website || '')); userData.website = validator.escape(String(userData.website || ''));
userData.websiteLink = !userData.website.startsWith('http') ? 'http://' + userData.website : userData.website; userData.websiteLink = !userData.website.startsWith('http') ? 'http://' + userData.website : userData.website;
@ -210,25 +110,82 @@ helpers.getUserDataByUserSlug = function (userslug, callerUID, callback) {
userData['username:disableEdit'] = !userData.isAdmin && meta.config['username:disableEdit']; userData['username:disableEdit'] = !userData.isAdmin && meta.config['username:disableEdit'];
userData['email:disableEdit'] = !userData.isAdmin && meta.config['email:disableEdit']; userData['email:disableEdit'] = !userData.isAdmin && meta.config['email:disableEdit'];
next(null, userData); return userData;
},
], callback);
}; };
function parseAboutMe(userData, callback) { async function getAllData(uid, callerUID) {
if (!userData.aboutme) { return await utils.promiseParallel({
return callback(); userData: user.getUserData(uid),
isTargetAdmin: user.isAdministrator(uid),
userSettings: user.getSettings(uid),
isAdmin: user.isAdministrator(callerUID),
isGlobalModerator: user.isGlobalModerator(callerUID),
isModerator: user.isModeratorOfAnyCategory(callerUID),
isFollowing: user.isFollowing(callerUID, uid),
ips: user.getIPs(uid, 4),
profile_menu: getProfileMenu(uid, callerUID),
groups: groups.getUserGroups([uid]),
sso: plugins.fireHook('filter:auth.list', { uid: uid, associations: [] }),
canEdit: privileges.users.canEdit(callerUID, uid),
canBanUser: privileges.users.canBanUser(callerUID, uid),
isBlocked: user.blocks.is(uid, callerUID),
});
} }
userData.aboutme = validator.escape(String(userData.aboutme || ''));
async.waterfall([ async function getProfileMenu(uid, callerUID) {
function (next) { const links = [{
plugins.fireHook('filter:parse.aboutme', userData.aboutme, next); id: 'info',
route: 'info',
name: '[[user:account_info]]',
visibility: {
self: false,
other: false,
moderator: true,
globalMod: true,
admin: true,
}, },
function (aboutme, next) { }, {
userData.aboutmeParsed = translator.escape(aboutme); id: 'sessions',
next(); route: 'sessions',
name: '[[pages:account/sessions]]',
visibility: {
self: true,
other: false,
moderator: false,
globalMod: false,
admin: false,
},
}];
if (meta.config.gdpr_enabled) {
links.push({
id: 'consent',
route: 'consent',
name: '[[user:consent.title]]',
visibility: {
self: true,
other: false,
moderator: false,
globalMod: false,
admin: false,
}, },
], callback); });
}
return await plugins.fireHook('filter:user.profileMenu', {
uid: uid,
callerUID: callerUID,
links: links,
});
}
async function parseAboutMe(userData) {
if (!userData.aboutme) {
return;
}
userData.aboutme = validator.escape(String(userData.aboutme || ''));
const parsed = await plugins.fireHook('filter:parse.aboutme', userData.aboutme);
userData.aboutmeParsed = translator.escape(parsed);
} }
function filterLinks(links, states) { function filterLinks(links, states) {
@ -256,3 +213,5 @@ function filterLinks(links, states) {
return permit; return permit;
}); });
} }
require('../../promisify')(helpers);

@ -1,82 +1,67 @@
'use strict'; 'use strict';
const db = require('../../database');
var async = require('async'); const user = require('../../user');
const posts = require('../../posts');
var db = require('../../database'); const topics = require('../../topics');
var user = require('../../user'); const categories = require('../../categories');
var posts = require('../../posts'); const pagination = require('../../pagination');
var topics = require('../../topics'); const helpers = require('../helpers');
var categories = require('../../categories'); const accountHelpers = require('./helpers');
var pagination = require('../../pagination');
var helpers = require('../helpers'); const postsController = module.exports;
var accountHelpers = require('./helpers');
const templateToData = {
var postsController = module.exports;
var templateToData = {
'account/bookmarks': { 'account/bookmarks': {
type: 'posts', type: 'posts',
noItemsFoundKey: '[[topic:bookmarks.has_no_bookmarks]]', noItemsFoundKey: '[[topic:bookmarks.has_no_bookmarks]]',
crumb: '[[user:bookmarks]]', crumb: '[[user:bookmarks]]',
getSets: function (callerUid, userData, calback) { getSets: function (callerUid, userData) {
setImmediate(calback, null, 'uid:' + userData.uid + ':bookmarks'); return 'uid:' + userData.uid + ':bookmarks';
}, },
}, },
'account/posts': { 'account/posts': {
type: 'posts', type: 'posts',
noItemsFoundKey: '[[user:has_no_posts]]', noItemsFoundKey: '[[user:has_no_posts]]',
crumb: '[[global:posts]]', crumb: '[[global:posts]]',
getSets: function (callerUid, userData, callback) { getSets: async function (callerUid, userData) {
async.waterfall([ const cids = await categories.getCidsByPrivilege('categories:cid', callerUid, 'topics:read');
function (next) { return cids.map(c => 'cid:' + c + ':uid:' + userData.uid + ':pids');
categories.getCidsByPrivilege('categories:cid', callerUid, 'topics:read', next);
},
function (cids, next) {
next(null, cids.map(c => 'cid:' + c + ':uid:' + userData.uid + ':pids'));
},
], callback);
}, },
}, },
'account/upvoted': { 'account/upvoted': {
type: 'posts', type: 'posts',
noItemsFoundKey: '[[user:has_no_upvoted_posts]]', noItemsFoundKey: '[[user:has_no_upvoted_posts]]',
crumb: '[[global:upvoted]]', crumb: '[[global:upvoted]]',
getSets: function (callerUid, userData, calback) { getSets: function (callerUid, userData) {
setImmediate(calback, null, 'uid:' + userData.uid + ':upvote'); return 'uid:' + userData.uid + ':upvote';
}, },
}, },
'account/downvoted': { 'account/downvoted': {
type: 'posts', type: 'posts',
noItemsFoundKey: '[[user:has_no_downvoted_posts]]', noItemsFoundKey: '[[user:has_no_downvoted_posts]]',
crumb: '[[global:downvoted]]', crumb: '[[global:downvoted]]',
getSets: function (callerUid, userData, calback) { getSets: function (callerUid, userData) {
setImmediate(calback, null, 'uid:' + userData.uid + ':downvote'); return 'uid:' + userData.uid + ':downvote';
}, },
}, },
'account/best': { 'account/best': {
type: 'posts', type: 'posts',
noItemsFoundKey: '[[user:has_no_voted_posts]]', noItemsFoundKey: '[[user:has_no_voted_posts]]',
crumb: '[[global:best]]', crumb: '[[global:best]]',
getSets: function (callerUid, userData, callback) { getSets: async function (callerUid, userData) {
async.waterfall([ const cids = await categories.getCidsByPrivilege('categories:cid', callerUid, 'topics:read');
function (next) { return cids.map(c => 'cid:' + c + ':uid:' + userData.uid + ':pids:votes');
categories.getCidsByPrivilege('categories:cid', callerUid, 'topics:read', next);
},
function (cids, next) {
next(null, cids.map(c => 'cid:' + c + ':uid:' + userData.uid + ':pids:votes'));
},
], callback);
}, },
}, },
'account/watched': { 'account/watched': {
type: 'topics', type: 'topics',
noItemsFoundKey: '[[user:has_no_watched_topics]]', noItemsFoundKey: '[[user:has_no_watched_topics]]',
crumb: '[[user:watched]]', crumb: '[[user:watched]]',
getSets: function (callerUid, userData, calback) { getSets: function (callerUid, userData) {
setImmediate(calback, null, 'uid:' + userData.uid + ':followed_tids'); return 'uid:' + userData.uid + ':followed_tids';
}, },
getTopics: function (set, req, start, stop, callback) { getTopics: async function (set, req, start, stop) {
const sort = req.query.sort; const sort = req.query.sort;
const map = { const map = {
votes: 'topics:votes', votes: 'topics:votes',
@ -87,144 +72,98 @@ var templateToData = {
}; };
if (!sort || !map[sort]) { if (!sort || !map[sort]) {
return topics.getTopicsFromSet(set, req.uid, start, stop, callback); return await topics.getTopicsFromSet(set, req.uid, start, stop);
} }
const sortSet = map[sort]; const sortSet = map[sort];
let tids; let tids = await db.getSortedSetRevRange(set, 0, -1);
async.waterfall([ const scores = await db.sortedSetScores(sortSet, tids);
function (next) {
db.getSortedSetRevRange(set, 0, -1, next);
},
function (_tids, next) {
tids = _tids;
db.sortedSetScores(sortSet, tids, next);
},
function (scores, next) {
tids = tids.map((tid, i) => ({ tid: tid, score: scores[i] })) tids = tids.map((tid, i) => ({ tid: tid, score: scores[i] }))
.sort((a, b) => b.score - a.score) .sort((a, b) => b.score - a.score)
.slice(start, stop + 1) .slice(start, stop + 1)
.map(t => t.tid); .map(t => t.tid);
topics.getTopics(tids, req.uid, next); const topicsData = await topics.getTopics(tids, req.uid);
},
function (topicsData, next) {
topics.calculateTopicIndices(topicsData, start); topics.calculateTopicIndices(topicsData, start);
next(null, { topics: topicsData, nextStart: stop + 1 }); return { topics: topicsData, nextStart: stop + 1 };
},
], callback);
}, },
}, },
'account/ignored': { 'account/ignored': {
type: 'topics', type: 'topics',
noItemsFoundKey: '[[user:has_no_ignored_topics]]', noItemsFoundKey: '[[user:has_no_ignored_topics]]',
crumb: '[[user:ignored]]', crumb: '[[user:ignored]]',
getSets: function (callerUid, userData, calback) { getSets: function (callerUid, userData) {
setImmediate(calback, null, 'uid:' + userData.uid + ':ignored_tids'); return 'uid:' + userData.uid + ':ignored_tids';
}, },
}, },
'account/topics': { 'account/topics': {
type: 'topics', type: 'topics',
noItemsFoundKey: '[[user:has_no_topics]]', noItemsFoundKey: '[[user:has_no_topics]]',
crumb: '[[global:topics]]', crumb: '[[global:topics]]',
getSets: function (callerUid, userData, callback) { getSets: async function (callerUid, userData) {
async.waterfall([ const cids = await categories.getCidsByPrivilege('categories:cid', callerUid, 'topics:read');
function (next) { return cids.map(c => 'cid:' + c + ':uid:' + userData.uid + ':tids');
categories.getCidsByPrivilege('categories:cid', callerUid, 'topics:read', next);
},
function (cids, next) {
next(null, cids.map(c => 'cid:' + c + ':uid:' + userData.uid + ':tids'));
},
], callback);
}, },
}, },
}; };
postsController.getBookmarks = function (req, res, next) { postsController.getBookmarks = async function (req, res, next) {
getFromUserSet('account/bookmarks', req, res, next); await getFromUserSet('account/bookmarks', req, res, next);
}; };
postsController.getPosts = function (req, res, next) { postsController.getPosts = async function (req, res, next) {
getFromUserSet('account/posts', req, res, next); await getFromUserSet('account/posts', req, res, next);
}; };
postsController.getUpVotedPosts = function (req, res, next) { postsController.getUpVotedPosts = async function (req, res, next) {
getFromUserSet('account/upvoted', req, res, next); await getFromUserSet('account/upvoted', req, res, next);
}; };
postsController.getDownVotedPosts = function (req, res, next) { postsController.getDownVotedPosts = async function (req, res, next) {
getFromUserSet('account/downvoted', req, res, next); await getFromUserSet('account/downvoted', req, res, next);
}; };
postsController.getBestPosts = function (req, res, next) { postsController.getBestPosts = async function (req, res, next) {
getFromUserSet('account/best', req, res, next); await getFromUserSet('account/best', req, res, next);
}; };
postsController.getWatchedTopics = function (req, res, next) { postsController.getWatchedTopics = async function (req, res, next) {
getFromUserSet('account/watched', req, res, next); await getFromUserSet('account/watched', req, res, next);
}; };
postsController.getIgnoredTopics = function (req, res, next) { postsController.getIgnoredTopics = async function (req, res, next) {
getFromUserSet('account/ignored', req, res, next); await getFromUserSet('account/ignored', req, res, next);
}; };
postsController.getTopics = function (req, res, next) { postsController.getTopics = async function (req, res, next) {
getFromUserSet('account/topics', req, res, next); await getFromUserSet('account/topics', req, res, next);
}; };
function getFromUserSet(template, req, res, callback) { async function getFromUserSet(template, req, res, callback) {
var data = templateToData[template]; const data = templateToData[template];
var userData; const page = Math.max(1, parseInt(req.query.page, 10) || 1);
var settings;
var itemsPerPage;
var page = Math.max(1, parseInt(req.query.page, 10) || 1);
async.waterfall([ const [userData, settings] = await Promise.all([
function (next) { accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid),
async.parallel({ user.getSettings(req.uid),
settings: function (next) { ]);
user.getSettings(req.uid, next);
}, if (!userData) {
userData: function (next) {
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next);
},
}, next);
},
function (results, next) {
if (!results.userData) {
return callback(); return callback();
} }
const itemsPerPage = data.type === 'topics' ? settings.topicsPerPage : settings.postsPerPage;
const start = (page - 1) * itemsPerPage;
const stop = start + itemsPerPage - 1;
const sets = await data.getSets(req.uid, userData);
userData = results.userData; const [itemCount, itemData] = await Promise.all([
settings = results.settings; settings.usePagination ? db.sortedSetsCardSum(sets) : 0,
itemsPerPage = data.type === 'topics' ? settings.topicsPerPage : settings.postsPerPage; getItemData(sets, data, req, start, stop),
]);
data.getSets(req.uid, userData, next); userData[data.type] = itemData[data.type];
}, userData.nextStart = itemData.nextStart;
function (sets, next) {
async.parallel({
itemCount: function (next) {
if (settings.usePagination) {
db.sortedSetsCardSum(sets, next);
} else {
next(null, 0);
}
},
data: function (next) {
var start = (page - 1) * itemsPerPage;
var stop = start + itemsPerPage - 1;
const method = data.type === 'topics' ? topics.getTopicsFromSet : posts.getPostSummariesFromSet;
if (data.getTopics) {
return data.getTopics(sets, req, start, stop, next);
}
method(sets, req.uid, start, stop, next);
},
}, next);
},
function (results) {
userData[data.type] = results.data[data.type];
userData.nextStart = results.data.nextStart;
var pageCount = Math.ceil(results.itemCount / itemsPerPage); const pageCount = Math.ceil(itemCount / itemsPerPage);
userData.pagination = pagination.create(page, pageCount, req.query); userData.pagination = pagination.create(page, pageCount, req.query);
userData.noItemsFoundKey = data.noItemsFoundKey; userData.noItemsFoundKey = data.noItemsFoundKey;
@ -244,6 +183,12 @@ function getFromUserSet(template, req, res, callback) {
}); });
res.render(template, userData); res.render(template, userData);
}, }
], callback);
async function getItemData(sets, data, req, start, stop) {
if (data.getTopics) {
return await data.getTopics(sets, req, start, stop);
}
const method = data.type === 'topics' ? topics.getTopicsFromSet : posts.getPostSummariesFromSet;
return await method(sets, req.uid, start, stop);
} }

Loading…
Cancel
Save