feat: #7743, finish post module

v1.18.x
Barış Soner Uşaklı 6 years ago
parent c4bb467ea5
commit c0c6c652be

@ -1,6 +1,5 @@
'use strict'; 'use strict';
var async = require('async');
var nconf = require('nconf'); var nconf = require('nconf');
var url = require('url'); var url = require('url');
var winston = require('winston'); var winston = require('winston');
@ -21,37 +20,31 @@ module.exports = function (Posts) {
length: 5, length: 5,
}; };
Posts.parsePost = function (postData, callback) { Posts.parsePost = async function (postData) {
if (!postData) { if (!postData) {
return setImmediate(callback, null, postData); return postData;
} }
postData.content = String(postData.content || ''); postData.content = String(postData.content || '');
var cache = require('./cache'); const cache = require('./cache');
if (postData.pid && cache.has(String(postData.pid))) { const pid = String(postData.pid);
postData.content = cache.get(String(postData.pid)); const cachedContent = cache.get(pid);
if (postData.pid && cachedContent !== undefined) {
postData.content = cachedContent;
cache.hits += 1; cache.hits += 1;
return callback(null, postData); return postData;
} }
cache.misses += 1; cache.misses += 1;
const data = await plugins.fireHook('filter:parse.post', { postData: postData });
async.waterfall([ data.postData.content = translator.escape(data.postData.content);
function (next) { if (global.env === 'production' && data.postData.pid) {
plugins.fireHook('filter:parse.post', { postData: postData }, next); cache.set(pid, data.postData.content);
}, }
function (data, next) { return data.postData;
data.postData.content = translator.escape(data.postData.content);
if (global.env === 'production' && data.postData.pid) {
cache.set(String(data.postData.pid), data.postData.content);
}
next(null, data.postData);
},
], callback);
}; };
Posts.parseSignature = function (userData, uid, callback) { Posts.parseSignature = async function (userData, uid) {
userData.signature = sanitizeSignature(userData.signature || ''); userData.signature = sanitizeSignature(userData.signature || '');
plugins.fireHook('filter:parse.signature', { userData: userData, uid: uid }, callback); return await plugins.fireHook('filter:parse.signature', { userData: userData, uid: uid });
}; };
Posts.relativeToAbsolute = function (content, regex) { Posts.relativeToAbsolute = function (content, regex) {

@ -1,6 +1,5 @@
'use strict'; 'use strict';
var async = require('async');
const _ = require('lodash'); const _ = require('lodash');
var db = require('../database'); var db = require('../database');
@ -15,296 +14,169 @@ var plugins = require('../plugins');
var socketHelpers = require('../socket.io/helpers'); var socketHelpers = require('../socket.io/helpers');
module.exports = function (Posts) { module.exports = function (Posts) {
Posts.shouldQueue = function (uid, data, callback) { Posts.shouldQueue = async function (uid, data) {
async.waterfall([ const userData = await user.getUserFields(uid, ['uid', 'reputation', 'postcount']);
function (next) { const shouldQueue = meta.config.postQueue && (!userData.uid || userData.reputation < meta.config.newbiePostDelayThreshold || userData.postcount <= 0);
user.getUserFields(uid, ['uid', 'reputation', 'postcount'], next); const result = await plugins.fireHook('filter:post.shouldQueue', {
}, shouldQueue: !!shouldQueue,
function (userData, next) { uid: uid,
const shouldQueue = meta.config.postQueue && (!userData.uid || userData.reputation < meta.config.newbiePostDelayThreshold || userData.postcount <= 0); data: data,
plugins.fireHook('filter:post.shouldQueue', { });
shouldQueue: shouldQueue, return result.shouldQueue;
uid: uid,
data: data,
}, next);
},
function (result, next) {
next(null, result.shouldQueue);
},
], callback);
}; };
function removeQueueNotification(id, callback) { async function removeQueueNotification(id) {
async.waterfall([ await notifications.rescind('post-queue-' + id);
function (next) { const data = await getParsedObject(id);
notifications.rescind('post-queue-' + id, next); if (!data) {
}, return;
function (next) { }
getParsedObject(id, next); const cid = await getCid(data.type, data);
}, const uids = await getNotificationUids(cid);
function (data, next) { uids.forEach(uid => user.notifications.pushCount(uid));
if (!data) {
return callback();
}
getCid(data.type, data, next);
},
function (cid, next) {
getNotificationUids(cid, next);
},
function (uids, next) {
uids.forEach(uid => user.notifications.pushCount(uid));
next();
},
], callback);
} }
function getNotificationUids(cid, callback) { async function getNotificationUids(cid) {
async.waterfall([ const results = await Promise.all([
function (next) { groups.getMembersOfGroups(['administrators', 'Global Moderators']),
async.parallel([ categories.getModeratorUids([cid]),
async.apply(groups.getMembersOfGroups, ['administrators', 'Global Moderators']), ]);
async.apply(categories.getModeratorUids, [cid]), return _.uniq(_.flattenDeep(results));
], next);
},
function (results, next) {
next(null, _.uniq(_.flattenDeep(results)));
},
], callback);
} }
Posts.addToQueue = function (data, callback) { Posts.addToQueue = async function (data) {
var type = data.title ? 'topic' : 'reply'; const type = data.title ? 'topic' : 'reply';
var id = type + '-' + Date.now(); const now = Date.now();
async.waterfall([ const id = type + '-' + now;
function (next) { await canPost(type, data);
canPost(type, data, next); await db.sortedSetAdd('post:queue', now, id);
}, await db.setObject('post:queue:' + id, {
function (next) { id: id,
db.sortedSetAdd('post:queue', Date.now(), id, next); uid: data.uid,
}, type: type,
function (next) { data: JSON.stringify(data),
db.setObject('post:queue:' + id, { });
id: id, await user.setUserField(data.uid, 'lastqueuetime', now);
uid: data.uid,
type: type, const cid = await getCid(type, data);
data: JSON.stringify(data), const uids = await getNotificationUids(cid);
}, next); const notifObj = await notifications.create({
}, type: 'post-queue',
function (next) { nid: 'post-queue-' + id,
user.setUserField(data.uid, 'lastqueuetime', Date.now(), next); mergeId: 'post-queue',
}, bodyShort: '[[notifications:post_awaiting_review]]',
function (next) { bodyLong: data.content,
async.parallel({ path: '/post-queue',
notification: function (next) { });
notifications.create({ await notifications.push(notifObj, uids);
type: 'post-queue', return {
nid: 'post-queue-' + id, id: id,
mergeId: 'post-queue', type: type,
bodyShort: '[[notifications:post_awaiting_review]]', queued: true,
bodyLong: data.content, message: '[[success:post-queued]]',
path: '/post-queue', };
}, next);
},
uids: function (next) {
async.waterfall([
function (next) {
getCid(type, data, next);
},
function (cid, next) {
getNotificationUids(cid, next);
},
], next);
},
}, next);
},
function (results, next) {
if (results.notification) {
notifications.push(results.notification, results.uids, next);
} else {
next();
}
},
function (next) {
next(null, {
id: id,
type: type,
queued: true,
message: '[[success:post-queued]]',
});
},
], callback);
}; };
function getCid(type, data, callback) { async function getCid(type, data) {
if (type === 'topic') { if (type === 'topic') {
return setImmediate(callback, null, data.cid); return data.cid;
} else if (type === 'reply') { } else if (type === 'reply') {
topics.getTopicField(data.tid, 'cid', callback); return await topics.getTopicField(data.tid, 'cid');
} else {
return setImmediate(callback, null, null);
} }
return null;
} }
function canPost(type, data, callback) { async function canPost(type, data) {
async.waterfall([ const cid = await getCid(type, data);
function (next) { const typeToPrivilege = {
getCid(type, data, next); topic: 'topics:create',
}, reply: 'topics:reply',
function (cid, next) { };
async.parallel({
canPost: function (next) { const [canPost] = await Promise.all([
if (type === 'topic') { privileges.categories.can(typeToPrivilege[type], cid, data.uid),
privileges.categories.can('topics:create', cid, data.uid, next); user.isReadyToQueue(data.uid, cid),
} else if (type === 'reply') { ]);
privileges.categories.can('topics:reply', cid, data.uid, next); if (!canPost) {
} throw new Error('[[error:no-privileges]]');
}, }
isReadyToQueue: function (next) {
user.isReadyToQueue(data.uid, cid, next);
},
}, next);
},
function (results, next) {
if (!results.canPost) {
return next(new Error('[[error:no-privileges]]'));
}
next();
},
], callback);
} }
Posts.removeFromQueue = function (id, callback) { Posts.removeFromQueue = async function (id) {
async.waterfall([ await removeQueueNotification(id);
function (next) { await db.sortedSetRemove('post:queue', id);
removeQueueNotification(id, next); await db.delete('post:queue:' + id);
},
function (next) {
db.sortedSetRemove('post:queue', id, next);
},
function (next) {
db.delete('post:queue:' + id, next);
},
], callback);
}; };
Posts.submitFromQueue = function (id, callback) { Posts.submitFromQueue = async function (id) {
async.waterfall([ const data = await getParsedObject(id);
function (next) { if (!data) {
getParsedObject(id, next); return;
}, }
function (data, next) { if (data.type === 'topic') {
if (!data) { await createTopic(data.data);
return callback(); } else if (data.type === 'reply') {
} await createReply(data.data);
if (data.type === 'topic') { }
createTopic(data.data, next); await Posts.removeFromQueue(id);
} else if (data.type === 'reply') {
createReply(data.data, next);
}
},
function (next) {
Posts.removeFromQueue(id, next);
},
], callback);
}; };
function getParsedObject(id, callback) { async function getParsedObject(id) {
async.waterfall([ const data = await db.getObject('post:queue:' + id);
function (next) { if (!data) {
db.getObject('post:queue:' + id, next); return null;
}, }
function (data, next) { data.data = JSON.parse(data.data);
if (!data) { return data;
return callback(null, null);
}
try {
data.data = JSON.parse(data.data);
} catch (err) {
return next(err);
}
next(null, data);
},
], callback);
} }
function createTopic(data, callback) { async function createTopic(data) {
async.waterfall([ const result = await topics.post(data);
function (next) { socketHelpers.notifyNew(data.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
topics.post(data, next);
},
function (result, next) {
socketHelpers.notifyNew(data.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
next();
},
], callback);
} }
function createReply(data, callback) { async function createReply(data) {
async.waterfall([ const postData = await topics.reply(data);
function (next) { const result = {
topics.reply(data, next); posts: [postData],
}, 'reputation:disabled': !!meta.config['reputation:disabled'],
function (postData, next) { 'downvote:disabled': !!meta.config['downvote:disabled'],
var result = { };
posts: [postData], socketHelpers.notifyNew(data.uid, 'newPost', result);
'reputation:disabled': !!meta.config['reputation:disabled'],
'downvote:disabled': !!meta.config['downvote:disabled'],
};
socketHelpers.notifyNew(data.uid, 'newPost', result);
next();
},
], callback);
} }
Posts.editQueuedContent = function (uid, id, content, callback) { Posts.editQueuedContent = async function (uid, id, content) {
async.waterfall([ const canEditQueue = await Posts.canEditQueue(uid, id);
function (next) { if (!canEditQueue) {
Posts.canEditQueue(uid, id, next); throw new Error('[[error:no-privileges]]');
}, }
function (canEditQueue, next) { const data = await getParsedObject(id);
if (!canEditQueue) { if (!data) {
return callback(new Error('[[error:no-privileges]]')); return;
} }
getParsedObject(id, next); data.data.content = content;
}, await db.setObjectField('post:queue:' + id, 'data', JSON.stringify(data.data));
function (data, next) {
if (!data) {
return callback();
}
data.data.content = content;
db.setObjectField('post:queue:' + id, 'data', JSON.stringify(data.data), next);
},
], callback);
}; };
Posts.canEditQueue = function (uid, id, callback) { Posts.canEditQueue = async function (uid, id) {
async.waterfall([ const [isAdminOrGlobalMod, data] = await Promise.all([
function (next) { user.isAdminOrGlobalMod(uid),
async.parallel({ getParsedObject(id),
isAdminOrGlobalMod: function (next) { ]);
user.isAdminOrGlobalMod(uid, next); if (!data) {
}, return false;
data: function (next) { }
getParsedObject(id, next);
}, if (isAdminOrGlobalMod) {
}, next); return true;
}, }
function (results, next) {
if (results.isAdminOrGlobalMod) { let cid;
return callback(null, true); if (data.type === 'topic') {
} cid = data.data.cid;
if (!results.data) { } else if (data.type === 'reply') {
return callback(null, false); cid = await topics.getTopicField(data.data.tid, 'cid');
} }
if (results.data.type === 'topic') { return await user.isModerator(uid, cid);
next(null, results.data.data.cid);
} else if (results.data.type === 'reply') {
topics.getTopicField(results.data.data.tid, 'cid', next);
}
},
function (cid, next) {
user.isModerator(uid, cid, next);
},
], callback);
}; };
}; };

@ -1,53 +1,33 @@
'use strict'; 'use strict';
var async = require('async'); const _ = require('lodash');
var _ = require('lodash');
var db = require('../database'); const db = require('../database');
var privileges = require('../privileges'); const privileges = require('../privileges');
module.exports = function (Posts) { module.exports = function (Posts) {
var terms = { const terms = {
day: 86400000, day: 86400000,
week: 604800000, week: 604800000,
month: 2592000000, month: 2592000000,
}; };
Posts.getRecentPosts = function (uid, start, stop, term, callback) { Posts.getRecentPosts = async function (uid, start, stop, term) {
var min = 0; let min = 0;
if (terms[term]) { if (terms[term]) {
min = Date.now() - terms[term]; min = Date.now() - terms[term];
} }
var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1; const count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
let pids = await db.getSortedSetRevRangeByScore('posts:pid', start, count, '+inf', min);
async.waterfall([ pids = await privileges.posts.filter('topics:read', pids, uid);
function (next) { return await Posts.getPostSummaryByPids(pids, uid, { stripTags: true });
db.getSortedSetRevRangeByScore('posts:pid', start, count, '+inf', min, next);
},
function (pids, next) {
privileges.posts.filter('topics:read', pids, uid, next);
},
function (pids, next) {
Posts.getPostSummaryByPids(pids, uid, { stripTags: true }, next);
},
], callback);
}; };
Posts.getRecentPosterUids = function (start, stop, callback) { Posts.getRecentPosterUids = async function (start, stop) {
async.waterfall([ const pids = await db.getSortedSetRevRange('posts:pid', start, stop);
function (next) { const postData = await Posts.getPostsFields(pids, ['uid']);
db.getSortedSetRevRange('posts:pid', start, stop, next); return _.uniq(postData.map(p => p && p.uid).filter(uid => parseInt(uid, 10)));
},
function (pids, next) {
Posts.getPostsFields(pids, ['uid'], next);
},
function (postData, next) {
var uids = _.uniq(postData.map(post => post && post.uid).filter(uid => parseInt(uid, 10)));
next(null, uids);
},
], callback);
}; };
}; };

@ -1,123 +1,83 @@
'use strict'; 'use strict';
var async = require('async'); const validator = require('validator');
var validator = require('validator'); const _ = require('lodash');
var _ = require('lodash');
var topics = require('../topics'); const topics = require('../topics');
var user = require('../user'); const user = require('../user');
var plugins = require('../plugins'); const plugins = require('../plugins');
var categories = require('../categories'); const categories = require('../categories');
var utils = require('../utils'); const utils = require('../utils');
module.exports = function (Posts) { module.exports = function (Posts) {
Posts.getPostSummaryByPids = function (pids, uid, options, callback) { Posts.getPostSummaryByPids = async function (pids, uid, options) {
if (!Array.isArray(pids) || !pids.length) { if (!Array.isArray(pids) || !pids.length) {
return callback(null, []); return [];
} }
options.stripTags = options.hasOwnProperty('stripTags') ? options.stripTags : false; options.stripTags = options.hasOwnProperty('stripTags') ? options.stripTags : false;
options.parse = options.hasOwnProperty('parse') ? options.parse : true; options.parse = options.hasOwnProperty('parse') ? options.parse : true;
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : []; options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];
var fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes'].concat(options.extraFields); const fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes'].concat(options.extraFields);
var posts; let posts = await Posts.getPostsFields(pids, fields);
async.waterfall([ posts = posts.filter(Boolean);
function (next) { posts = await user.blocks.filter(uid, posts);
Posts.getPostsFields(pids, fields, next);
}, const uids = _.uniq(posts.map(p => p && p.uid));
function (_posts, next) { const tids = _.uniq(posts.map(p => p && p.tid));
posts = _posts.filter(Boolean);
user.blocks.filter(uid, posts, next); const [users, topicsAndCategories] = await Promise.all([
}, user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status']),
function (_posts, next) { getTopicAndCategories(tids),
posts = _posts; ]);
var uids = {};
var topicKeys = {}; const uidToUser = toObject('uid', users);
const tidToTopic = toObject('tid', topicsAndCategories.topics);
posts.forEach(function (post) { const cidToCategory = toObject('cid', topicsAndCategories.categories);
uids[post.uid] = 1;
topicKeys[post.tid] = 1; posts.forEach(function (post) {
}); // If the post author isn't represented in the retrieved users' data, then it means they were deleted, assume guest.
async.parallel({ if (!uidToUser.hasOwnProperty(post.uid)) {
users: function (next) { post.uid = 0;
user.getUsersFields(Object.keys(uids), ['uid', 'username', 'userslug', 'picture', 'status'], next); }
}, post.user = uidToUser[post.uid];
topicsAndCategories: function (next) { post.topic = tidToTopic[post.tid];
getTopicAndCategories(Object.keys(topicKeys), next); post.category = post.topic && cidToCategory[post.topic.cid];
}, post.isMainPost = post.topic && post.pid === post.topic.mainPid;
}, next); post.deleted = post.deleted === 1;
}, post.timestampISO = utils.toISOString(post.timestamp);
function (results, next) { });
results.users = toObject('uid', results.users);
results.topics = toObject('tid', results.topicsAndCategories.topics); posts = posts.filter(post => tidToTopic[post.tid]);
results.categories = toObject('cid', results.topicsAndCategories.categories);
posts = await parsePosts(posts, options);
posts.forEach(function (post) { const result = await plugins.fireHook('filter:post.getPostSummaryByPids', { posts: posts, uid: uid });
// If the post author isn't represented in the retrieved users' data, then it means they were deleted, assume guest. return result.posts;
if (!results.users.hasOwnProperty(post.uid)) {
post.uid = 0;
}
post.user = results.users[post.uid];
post.topic = results.topics[post.tid];
post.category = post.topic && results.categories[post.topic.cid];
post.isMainPost = post.topic && post.pid === post.topic.mainPid;
post.deleted = post.deleted === 1;
post.timestampISO = utils.toISOString(post.timestamp);
});
posts = posts.filter(function (post) {
return results.topics[post.tid];
});
parsePosts(posts, options, next);
},
function (posts, next) {
plugins.fireHook('filter:post.getPostSummaryByPids', { posts: posts, uid: uid }, next);
},
function (data, next) {
next(null, data.posts);
},
], callback);
}; };
function parsePosts(posts, options, callback) { async function parsePosts(posts, options) {
async.map(posts, function (post, next) { async function parse(post) {
async.waterfall([ if (!post.content || !options.parse) {
function (next) { post.content = post.content ? validator.escape(String(post.content)) : post.content;
if (!post.content || !options.parse) { return post;
post.content = post.content ? validator.escape(String(post.content)) : post.content; }
return next(null, post); post = await Posts.parsePost(post);
} if (options.stripTags) {
Posts.parsePost(post, next); post.content = stripTags(post.content);
}, }
function (post, next) { return post;
if (options.stripTags) { }
post.content = stripTags(post.content); return await Promise.all(posts.map(p => parse(p)));
}
next(null, post);
},
], next);
}, callback);
} }
function getTopicAndCategories(tids, callback) { async function getTopicAndCategories(tids) {
var topicsData; const topicsData = await topics.getTopicsFields(tids, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted', 'postcount', 'mainPid']);
async.waterfall([ const cids = _.uniq(topicsData.map(topic => topic && topic.cid));
function (next) { const categoriesData = await categories.getCategoriesFields(cids, ['cid', 'name', 'icon', 'slug', 'parentCid', 'bgColor', 'color']);
topics.getTopicsFields(tids, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted', 'postcount', 'mainPid'], next); return { topics: topicsData, categories: categoriesData };
},
function (_topicsData, next) {
topicsData = _topicsData;
var cids = _.uniq(topicsData.map(topic => topic && topic.cid));
categories.getCategoriesFields(cids, ['cid', 'name', 'icon', 'slug', 'parentCid', 'bgColor', 'color'], next);
},
function (categoriesData, next) {
next(null, { topics: topicsData, categories: categoriesData });
},
], callback);
} }
function toObject(key, data) { function toObject(key, data) {

@ -1,72 +1,53 @@
'use strict'; 'use strict';
var async = require('async'); const privileges = require('../privileges');
var privileges = require('../privileges');
module.exports = function (Posts) { module.exports = function (Posts) {
Posts.tools = {}; Posts.tools = {};
Posts.tools.delete = function (uid, pid, callback) { Posts.tools.delete = async function (uid, pid) {
togglePostDelete(uid, pid, true, callback); return await togglePostDelete(uid, pid, true);
}; };
Posts.tools.restore = function (uid, pid, callback) { Posts.tools.restore = async function (uid, pid) {
togglePostDelete(uid, pid, false, callback); return await togglePostDelete(uid, pid, false);
}; };
function togglePostDelete(uid, pid, isDelete, callback) { async function togglePostDelete(uid, pid, isDelete) {
async.waterfall([ const [postData, canDelete] = await Promise.all([
function (next) { Posts.getPostData(pid),
Posts.exists(pid, next); privileges.posts.canDelete(pid, uid),
}, ]);
function (exists, next) { if (!postData) {
if (!exists) { throw new Error('[[error:no-post]]');
return next(new Error('[[error:no-post]]')); }
}
Posts.getPostField(pid, 'deleted', next); if (postData.deleted && isDelete) {
}, throw new Error('[[error:post-already-deleted]]');
function (deleted, next) { } else if (!postData.deleted && !isDelete) {
if (deleted && isDelete) { throw new Error('[[error:post-already-restored]]');
return next(new Error('[[error:post-already-deleted]]')); }
} else if (!deleted && !isDelete) {
return next(new Error('[[error:post-already-restored]]')); if (!canDelete.flag) {
} throw new Error(canDelete.message);
}
privileges.posts.canDelete(pid, uid, next); let post;
}, if (isDelete) {
function (canDelete, next) { require('./cache').del(pid);
if (!canDelete.flag) { post = await Posts.delete(pid, uid);
return next(new Error(canDelete.message)); } else {
} post = await Posts.restore(pid, uid);
post = await Posts.parsePost(post);
if (isDelete) { }
require('./cache').del(pid); return post;
Posts.delete(pid, uid, next);
} else {
Posts.restore(pid, uid, function (err, postData) {
if (err) {
return next(err);
}
Posts.parsePost(postData, next);
});
}
},
], callback);
} }
Posts.tools.purge = function (uid, pid, callback) { Posts.tools.purge = async function (uid, pid) {
async.waterfall([ const canPurge = await privileges.posts.canPurge(pid, uid);
function (next) { if (!canPurge) {
privileges.posts.canPurge(pid, uid, next); throw new Error('[[error:no-privileges]]');
}, }
function (canPurge, next) { require('./cache').del(pid);
if (!canPurge) { await Posts.purge(pid, uid);
return next(new Error('[[error:no-privileges]]'));
}
require('./cache').del(pid);
Posts.purge(pid, uid, next);
},
], callback);
}; };
}; };

@ -5,7 +5,6 @@ var nconf = require('nconf');
var crypto = require('crypto'); var crypto = require('crypto');
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var util = require('util');
var winston = require('winston'); var winston = require('winston');
var db = require('../database'); var db = require('../database');
@ -18,42 +17,33 @@ module.exports = function (Posts) {
const pathPrefix = path.join(nconf.get('upload_path'), 'files'); const pathPrefix = path.join(nconf.get('upload_path'), 'files');
const searchRegex = /\/assets\/uploads\/files\/([^\s")]+\.?[\w]*)/g; const searchRegex = /\/assets\/uploads\/files\/([^\s")]+\.?[\w]*)/g;
Posts.uploads.sync = function (pid, callback) { Posts.uploads.sync = async function (pid) {
// Scans a post and updates sorted set of uploads // Scans a post's content and updates sorted set of uploads
async.parallel({ const [content, currentUploads] = await Promise.all([
content: async.apply(Posts.getPostField, pid, 'content'), Posts.getPostField(pid, 'content'),
uploads: async.apply(Posts.uploads.list, pid), Posts.uploads.list(pid),
}, function (err, data) { ]);
if (err) {
return callback(err);
}
// Extract upload file paths from post content
let match = searchRegex.exec(data.content);
const uploads = [];
while (match) {
uploads.push(match[1].replace('-resized', ''));
match = searchRegex.exec(data.content);
}
// Create add/remove sets // Extract upload file paths from post content
const add = uploads.filter(path => !data.uploads.includes(path)); let match = searchRegex.exec(content);
const remove = data.uploads.filter(path => !uploads.includes(path)); const uploads = [];
while (match) {
uploads.push(match[1].replace('-resized', ''));
match = searchRegex.exec(content);
}
async.parallel([ // Create add/remove sets
async.apply(Posts.uploads.associate, pid, add), const add = uploads.filter(path => !currentUploads.includes(path));
async.apply(Posts.uploads.dissociate, pid, remove), const remove = currentUploads.filter(path => !uploads.includes(path));
], function (err) { await Promise.all([
// Strictly return only err Posts.uploads.associate(pid, add),
callback(err); Posts.uploads.dissociate(pid, remove),
}); ]);
});
}; };
Posts.uploads.list = function (pid, callback) { Posts.uploads.list = async function (pid) {
// Returns array of this post's uploads return await db.getSortedSetRange('post:' + pid + ':uploads', 0, -1);
db.getSortedSetRange('post:' + pid + ':uploads', 0, -1, callback);
}; };
Posts.uploads.listWithSizes = async function (pid) { Posts.uploads.listWithSizes = async function (pid) {
@ -66,98 +56,70 @@ module.exports = function (Posts) {
})); }));
}; };
Posts.uploads.isOrphan = function (filePath, callback) { Posts.uploads.isOrphan = async function (filePath) {
// Returns bool indicating whether a file is still CURRENTLY included in any posts const length = await db.sortedSetCard('upload:' + md5(filePath) + ':pids');
db.sortedSetCard('upload:' + md5(filePath) + ':pids', function (err, length) { return length === 0;
callback(err, length === 0);
});
}; };
Posts.uploads.getUsage = function (filePaths, callback) { Posts.uploads.getUsage = async function (filePaths) {
// Given an array of file names, determines which pids they are used in // Given an array of file names, determines which pids they are used in
if (!Array.isArray(filePaths)) { if (!Array.isArray(filePaths)) {
filePaths = [filePaths]; filePaths = [filePaths];
} }
const keys = filePaths.map(fileObj => 'upload:' + md5(fileObj.name.replace('-resized', '')) + ':pids'); const keys = filePaths.map(fileObj => 'upload:' + md5(fileObj.name.replace('-resized', '')) + ':pids');
async.map(keys, function (key, next) { return await Promise.all(keys.map(k => db.getSortedSetRange(k, 0, -1)));
db.getSortedSetRange(key, 0, -1, next);
}, callback);
}; };
Posts.uploads.associate = function (pid, filePaths, callback) { Posts.uploads.associate = async function (pid, filePaths) {
// Adds an upload to a post's sorted set of uploads // Adds an upload to a post's sorted set of uploads
filePaths = !Array.isArray(filePaths) ? [filePaths] : filePaths; filePaths = !Array.isArray(filePaths) ? [filePaths] : filePaths;
if (!filePaths.length) { if (!filePaths.length) {
return setImmediate(callback); return;
} }
async.filter(filePaths, function (filePath, next) { filePaths = await async.filter(filePaths, function (filePath, next) {
// Only process files that exist // Only process files that exist
fs.access(path.join(pathPrefix, filePath), fs.constants.F_OK | fs.constants.R_OK, function (err) { fs.access(path.join(pathPrefix, filePath), fs.constants.F_OK | fs.constants.R_OK, function (err) {
next(null, !err); next(null, !err);
}); });
}, function (err, filePaths) {
if (err) {
return callback(err);
}
const now = Date.now();
const scores = filePaths.map(() => now);
let methods = [async.apply(db.sortedSetAdd.bind(db), 'post:' + pid + ':uploads', scores, filePaths)];
methods = methods.concat(filePaths.map(path => async.apply(db.sortedSetAdd.bind(db), 'upload:' + md5(path) + ':pids', now, pid)));
methods = methods.concat(async function () {
await Posts.uploads.saveSize(filePaths);
});
async.parallel(methods, function (err) {
// Strictly return only err
callback(err);
});
}); });
const now = Date.now();
const scores = filePaths.map(() => now);
const bulkAdd = filePaths.map(path => ['upload:' + md5(path) + ':pids', now, pid]);
await Promise.all([
db.sortedSetAdd('post:' + pid + ':uploads', scores, filePaths),
db.sortedSetAddBulk(bulkAdd),
Posts.uploads.saveSize(filePaths),
]);
}; };
Posts.uploads.dissociate = function (pid, filePaths, callback) { Posts.uploads.dissociate = async function (pid, filePaths) {
// Removes an upload from a post's sorted set of uploads // Removes an upload from a post's sorted set of uploads
filePaths = !Array.isArray(filePaths) ? [filePaths] : filePaths; filePaths = !Array.isArray(filePaths) ? [filePaths] : filePaths;
if (!filePaths.length) { if (!filePaths.length) {
return setImmediate(callback); return;
} }
let methods = [async.apply(db.sortedSetRemove.bind(db), 'post:' + pid + ':uploads', filePaths)];
methods = methods.concat(filePaths.map(path => async.apply(db.sortedSetRemove.bind(db), 'upload:' + md5(path) + ':pids', pid)));
async.parallel(methods, function (err) { const bulkRemove = filePaths.map(path => ['upload:' + md5(path) + ':pids', pid]);
// Strictly return only err await Promise.all([
callback(err); db.sortedSetRemove('post:' + pid + ':uploads', filePaths),
}); db.sortedSetRemoveBulk(bulkRemove),
]);
}; };
Posts.uploads.saveSize = async (filePaths) => { Posts.uploads.saveSize = async (filePaths) => {
const getSize = util.promisify(image.size); await Promise.all(filePaths.map(async function (fileName) {
const sizes = await Promise.all(filePaths.map(async function (fileName) {
try { try {
return await getSize(path.join(pathPrefix, fileName)); const size = await image.size(path.join(pathPrefix, fileName));
} catch (e) { winston.verbose('[posts/uploads/' + fileName + '] Saving size');
// Error returned by getSize, do not save size in database await db.setObject('upload:' + md5(fileName), {
return null; width: size.width,
height: size.height,
});
} catch (err) {
winston.error('[posts/uploads] Error while saving post upload sizes (' + fileName + '): ' + err.message);
} }
})); }));
const methods = filePaths.map((filePath, idx) => {
if (!sizes[idx]) {
return null;
}
winston.verbose('[posts/uploads/' + filePath + '] Saving size');
return async.apply(db.setObject, 'upload:' + md5(filePath), {
width: sizes[idx].width,
height: sizes[idx].height,
});
}).filter(Boolean);
async.parallel(methods, function (err) {
if (err) {
winston.error('[posts/uploads] Error while saving post upload sizes: ', err.message);
} else {
winston.verbose('[posts/uploads] Finished saving post upload sizes.');
}
});
}; };
}; };

@ -20,31 +20,9 @@ module.exports = function (Posts) {
privileges.global.can('signature', uid), privileges.global.can('signature', uid),
]); ]);
var groupTitles = _.uniq(_.flatten(userData.map(u => u && u.groupTitleArray))); const groupsMap = await getGroupsMap(userData);
const groupsMap = {};
const groupsData = await groups.getGroupsData(groupTitles);
groupsData.forEach(function (group) {
if (group && group.userTitleEnabled && !group.hidden) {
groupsMap[group.name] = {
name: group.name,
slug: group.slug,
labelColor: group.labelColor,
textColor: group.textColor,
icon: group.icon,
userTitle: group.userTitle,
};
}
});
userData.forEach(function (userData, index) { userData.forEach(function (userData, index) {
userData.uid = userData.uid || 0;
userData.username = userData.username || '[[global:guest]]';
userData.userslug = userData.userslug || '';
userData.reputation = userData.reputation || 0;
userData.postcount = userData.postcount || 0;
userData.banned = userData.banned === 1;
userData.picture = userData.picture || '';
userData.status = user.getStatus(userData);
userData.signature = validator.escape(String(userData.signature || '')); userData.signature = validator.escape(String(userData.signature || ''));
userData.fullname = userSettings[index].showfullname ? validator.escape(String(userData.fullname || '')) : undefined; userData.fullname = userSettings[index].showfullname ? validator.escape(String(userData.fullname || '')) : undefined;
userData.selectedGroups = []; userData.selectedGroups = [];
@ -54,40 +32,60 @@ module.exports = function (Posts) {
} }
}); });
return await async.map(userData, async function (userData) { return await Promise.all(userData.map(async function (userData) {
const results = await async.parallel({ const [isMemberOfGroups, signature, customProfileInfo] = await Promise.all([
isMemberOfGroups: function (next) { checkGroupMembership(userData.uid, userData.groupTitleArray),
if (!Array.isArray(userData.groupTitleArray) || !userData.groupTitleArray.length) { parseSignature(userData, uid, canUseSignature),
return next(); plugins.fireHook('filter:posts.custom_profile_info', { profile: [], uid: userData.uid }),
} ]);
groups.isMemberOfGroups(userData.uid, userData.groupTitleArray, next);
},
signature: function (next) {
if (!userData.signature || !canUseSignature || meta.config.disableSignatures) {
userData.signature = '';
return next();
}
Posts.parseSignature(userData, uid, next);
},
customProfileInfo: function (next) {
plugins.fireHook('filter:posts.custom_profile_info', { profile: [], uid: userData.uid }, next);
},
});
if (results.isMemberOfGroups && userData.groupTitleArray) { if (isMemberOfGroups && userData.groupTitleArray) {
userData.groupTitleArray.forEach(function (userGroup, index) { userData.groupTitleArray.forEach(function (userGroup, index) {
if (results.isMemberOfGroups[index] && groupsMap[userGroup]) { if (isMemberOfGroups[index] && groupsMap[userGroup]) {
userData.selectedGroups.push(groupsMap[userGroup]); userData.selectedGroups.push(groupsMap[userGroup]);
} }
}); });
} }
userData.signature = signature;
userData.custom_profile_info = customProfileInfo.profile;
return await plugins.fireHook('filter:posts.modifyUserInfo', userData);
}));
};
userData.custom_profile_info = results.customProfileInfo.profile; async function checkGroupMembership(uid, groupTitleArray) {
if (!Array.isArray(groupTitleArray) || !groupTitleArray.length) {
return null;
}
return await groups.isMemberOfGroups(uid, groupTitleArray);
}
async function parseSignature(userData, uid, canUseSignature) {
if (!userData.signature || !canUseSignature || meta.config.disableSignatures) {
return '';
}
const result = await Posts.parseSignature(userData, uid);
return result.userData.signature;
}
const result = await plugins.fireHook('filter:posts.modifyUserInfo', userData); async function getGroupsMap(userData) {
return result; const groupTitles = _.uniq(_.flatten(userData.map(u => u && u.groupTitleArray)));
const groupsMap = {};
const groupsData = await groups.getGroupsData(groupTitles);
groupsData.forEach(function (group) {
if (group && group.userTitleEnabled && !group.hidden) {
groupsMap[group.name] = {
name: group.name,
slug: group.slug,
labelColor: group.labelColor,
textColor: group.textColor,
icon: group.icon,
userTitle: group.userTitle,
};
}
}); });
}; return groupsMap;
}
async function getUserData(uids, uid) { async function getUserData(uids, uid) {
const fields = [ const fields = [

@ -1022,7 +1022,7 @@ describe('Post\'s', function () {
it('should not crash if id does not exist', function (done) { it('should not crash if id does not exist', function (done) {
socketPosts.reject({ uid: globalModUid }, { id: '123123123' }, function (err) { socketPosts.reject({ uid: globalModUid }, { id: '123123123' }, function (err) {
assert.ifError(err); assert.equal(err.message, '[[error:no-privileges]]');
done(); done();
}); });
}); });

Loading…
Cancel
Save