mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: async/await socket.io/posts
This commit is contained in:
@@ -1,20 +1,19 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const db = require('../database');
|
||||||
|
const posts = require('../posts');
|
||||||
var posts = require('../posts');
|
const privileges = require('../privileges');
|
||||||
var privileges = require('../privileges');
|
const plugins = require('../plugins');
|
||||||
var plugins = require('../plugins');
|
const meta = require('../meta');
|
||||||
var meta = require('../meta');
|
const topics = require('../topics');
|
||||||
var topics = require('../topics');
|
|
||||||
const categories = require('../categories');
|
const categories = require('../categories');
|
||||||
var user = require('../user');
|
const user = require('../user');
|
||||||
var socketHelpers = require('./helpers');
|
const socketHelpers = require('./helpers');
|
||||||
var utils = require('../utils');
|
const utils = require('../utils');
|
||||||
|
|
||||||
var apiController = require('../controllers/api');
|
const apiController = require('../controllers/api');
|
||||||
|
|
||||||
var SocketPosts = module.exports;
|
const SocketPosts = module.exports;
|
||||||
|
|
||||||
require('./posts/edit')(SocketPosts);
|
require('./posts/edit')(SocketPosts);
|
||||||
require('./posts/move')(SocketPosts);
|
require('./posts/move')(SocketPosts);
|
||||||
@@ -23,51 +22,35 @@ require('./posts/bookmarks')(SocketPosts);
|
|||||||
require('./posts/tools')(SocketPosts);
|
require('./posts/tools')(SocketPosts);
|
||||||
require('./posts/diffs')(SocketPosts);
|
require('./posts/diffs')(SocketPosts);
|
||||||
|
|
||||||
SocketPosts.reply = function (socket, data, callback) {
|
SocketPosts.reply = async function (socket, data) {
|
||||||
if (!data || !data.tid || (meta.config.minimumPostLength !== 0 && !data.content)) {
|
if (!data || !data.tid || (meta.config.minimumPostLength !== 0 && !data.content)) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
socketHelpers.setDefaultPostData(data, socket);
|
socketHelpers.setDefaultPostData(data, socket);
|
||||||
|
await meta.blacklist.test(data.req.ip);
|
||||||
async.waterfall([
|
const shouldQueue = await posts.shouldQueue(socket.uid, data);
|
||||||
function (next) {
|
|
||||||
meta.blacklist.test(data.req.ip, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
posts.shouldQueue(socket.uid, data, next);
|
|
||||||
},
|
|
||||||
function (shouldQueue, next) {
|
|
||||||
if (shouldQueue) {
|
if (shouldQueue) {
|
||||||
posts.addToQueue(data, next);
|
return await posts.addToQueue(data);
|
||||||
} else {
|
|
||||||
postReply(socket, data, next);
|
|
||||||
}
|
}
|
||||||
},
|
return await postReply(socket, data);
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function postReply(socket, data, callback) {
|
async function postReply(socket, data) {
|
||||||
async.waterfall([
|
const postData = await topics.reply(data);
|
||||||
function (next) {
|
const result = {
|
||||||
topics.reply(data, next);
|
|
||||||
},
|
|
||||||
function (postData, next) {
|
|
||||||
var result = {
|
|
||||||
posts: [postData],
|
posts: [postData],
|
||||||
'reputation:disabled': meta.config['reputation:disabled'] === 1,
|
'reputation:disabled': meta.config['reputation:disabled'] === 1,
|
||||||
'downvote:disabled': meta.config['downvote:disabled'] === 1,
|
'downvote:disabled': meta.config['downvote:disabled'] === 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
next(null, postData);
|
|
||||||
|
|
||||||
socket.emit('event:new_post', result);
|
socket.emit('event:new_post', result);
|
||||||
|
|
||||||
user.updateOnlineUsers(socket.uid);
|
user.updateOnlineUsers(socket.uid);
|
||||||
|
|
||||||
socketHelpers.notifyNew(socket.uid, 'newPost', result);
|
socketHelpers.notifyNew(socket.uid, 'newPost', result);
|
||||||
},
|
|
||||||
], callback);
|
return postData;
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketPosts.getRawPost = async function (socket, pid) {
|
SocketPosts.getRawPost = async function (socket, pid) {
|
||||||
@@ -85,173 +68,117 @@ SocketPosts.getRawPost = async function (socket, pid) {
|
|||||||
return result.postData.content;
|
return result.postData.content;
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.getTimestampByIndex = function (socket, data, callback) {
|
SocketPosts.getTimestampByIndex = async function (socket, data) {
|
||||||
var pid;
|
|
||||||
var db = require('../database');
|
|
||||||
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
if (data.index < 0) {
|
if (data.index < 0) {
|
||||||
data.index = 0;
|
data.index = 0;
|
||||||
}
|
}
|
||||||
|
let pid;
|
||||||
if (data.index === 0) {
|
if (data.index === 0) {
|
||||||
topics.getTopicField(data.tid, 'mainPid', next);
|
pid = topics.getTopicField(data.tid, 'mainPid');
|
||||||
} else {
|
} else {
|
||||||
db.getSortedSetRange('tid:' + data.tid + ':posts', data.index - 1, data.index - 1, next);
|
pid = db.getSortedSetRange('tid:' + data.tid + ':posts', data.index - 1, data.index - 1);
|
||||||
}
|
}
|
||||||
},
|
pid = Array.isArray(pid) ? pid[0] : pid;
|
||||||
function (_pid, next) {
|
|
||||||
pid = Array.isArray(_pid) ? _pid[0] : _pid;
|
|
||||||
if (!pid) {
|
if (!pid) {
|
||||||
return callback(null, 0);
|
return 0;
|
||||||
}
|
}
|
||||||
privileges.posts.can('topics:read', pid, socket.uid, next);
|
|
||||||
},
|
const canRead = await privileges.posts.can('topics:read', pid, socket.uid);
|
||||||
function (canRead, next) {
|
|
||||||
if (!canRead) {
|
if (!canRead) {
|
||||||
return next(new Error('[[error:no-privileges]]'));
|
throw new Error('[[error:no-privileges]]');
|
||||||
}
|
}
|
||||||
posts.getPostFields(pid, ['timestamp'], next);
|
return await posts.getPostField(pid, 'timestamp');
|
||||||
},
|
|
||||||
function (postData, next) {
|
|
||||||
next(null, postData.timestamp);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.getPost = function (socket, pid, callback) {
|
SocketPosts.getPost = async function (socket, pid) {
|
||||||
apiController.getPostData(pid, socket.uid, callback);
|
return await apiController.getPostData(pid, socket.uid);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.loadMoreBookmarks = function (socket, data, callback) {
|
SocketPosts.loadMoreBookmarks = async function (socket, data) {
|
||||||
loadMorePosts('uid:' + data.uid + ':bookmarks', socket.uid, data, callback);
|
return await loadMorePosts('uid:' + data.uid + ':bookmarks', socket.uid, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.loadMoreUserPosts = function (socket, data, callback) {
|
SocketPosts.loadMoreUserPosts = async function (socket, data) {
|
||||||
async.waterfall([
|
const cids = await categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read');
|
||||||
function (next) {
|
|
||||||
categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read', next);
|
|
||||||
},
|
|
||||||
function (cids, next) {
|
|
||||||
const keys = cids.map(c => 'cid:' + c + ':uid:' + data.uid + ':pids');
|
const keys = cids.map(c => 'cid:' + c + ':uid:' + data.uid + ':pids');
|
||||||
loadMorePosts(keys, socket.uid, data, next);
|
return await loadMorePosts(keys, socket.uid, data);
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.loadMoreBestPosts = function (socket, data, callback) {
|
SocketPosts.loadMoreBestPosts = async function (socket, data) {
|
||||||
async.waterfall([
|
const cids = await categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read');
|
||||||
function (next) {
|
|
||||||
categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read', next);
|
|
||||||
},
|
|
||||||
function (cids, next) {
|
|
||||||
const keys = cids.map(c => 'cid:' + c + ':uid:' + data.uid + ':pids:votes');
|
const keys = cids.map(c => 'cid:' + c + ':uid:' + data.uid + ':pids:votes');
|
||||||
loadMorePosts(keys, socket.uid, data, next);
|
return await loadMorePosts(keys, socket.uid, data);
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.loadMoreUpVotedPosts = function (socket, data, callback) {
|
SocketPosts.loadMoreUpVotedPosts = async function (socket, data) {
|
||||||
loadMorePosts('uid:' + data.uid + ':upvote', socket.uid, data, callback);
|
return await loadMorePosts('uid:' + data.uid + ':upvote', socket.uid, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.loadMoreDownVotedPosts = function (socket, data, callback) {
|
SocketPosts.loadMoreDownVotedPosts = async function (socket, data) {
|
||||||
loadMorePosts('uid:' + data.uid + ':downvote', socket.uid, data, callback);
|
return await loadMorePosts('uid:' + data.uid + ':downvote', socket.uid, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
function loadMorePosts(set, uid, data, callback) {
|
async function loadMorePosts(set, uid, data) {
|
||||||
if (!data || !utils.isNumber(data.uid) || !utils.isNumber(data.after)) {
|
if (!data || !utils.isNumber(data.uid) || !utils.isNumber(data.after)) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
var start = Math.max(0, parseInt(data.after, 10));
|
const start = Math.max(0, parseInt(data.after, 10));
|
||||||
var stop = start + 9;
|
const stop = start + 9;
|
||||||
|
|
||||||
posts.getPostSummariesFromSet(set, uid, start, stop, callback);
|
return await posts.getPostSummariesFromSet(set, uid, start, stop);
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketPosts.getCategory = function (socket, pid, callback) {
|
SocketPosts.getCategory = async function (socket, pid) {
|
||||||
posts.getCidByPid(pid, callback);
|
return await posts.getCidByPid(pid);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.getPidIndex = function (socket, data, callback) {
|
SocketPosts.getPidIndex = async function (socket, data) {
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
posts.getPidIndex(data.pid, data.tid, data.topicPostSort, callback);
|
return await posts.getPidIndex(data.pid, data.tid, data.topicPostSort);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.getReplies = function (socket, pid, callback) {
|
SocketPosts.getReplies = async function (socket, pid) {
|
||||||
if (!utils.isNumber(pid)) {
|
if (!utils.isNumber(pid)) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
var postPrivileges;
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
posts.getPidsFromSet('pid:' + pid + ':replies', 0, -1, false, next);
|
|
||||||
},
|
|
||||||
function (pids, next) {
|
|
||||||
async.parallel({
|
|
||||||
posts: function (next) {
|
|
||||||
posts.getPostsByPids(pids, socket.uid, next);
|
|
||||||
},
|
|
||||||
privileges: function (next) {
|
|
||||||
privileges.posts.get(pids, socket.uid, next);
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
postPrivileges = results.privileges;
|
|
||||||
|
|
||||||
topics.addPostData(results.posts, socket.uid, next);
|
const pids = await posts.getPidsFromSet('pid:' + pid + ':replies', 0, -1, false);
|
||||||
},
|
|
||||||
function (postData, next) {
|
var [postData, postPrivileges] = await Promise.all([
|
||||||
postData.forEach(function (postData, index) {
|
posts.getPostsByPids(pids, socket.uid),
|
||||||
posts.modifyPostByPrivilege(postData, postPrivileges[index]);
|
privileges.posts.get(pids, socket.uid),
|
||||||
});
|
]);
|
||||||
postData = postData.filter(function (postData, index) {
|
postData = await topics.addPostData(postData, socket.uid);
|
||||||
return postData && postPrivileges[index].read;
|
postData.forEach((postData, index) => posts.modifyPostByPrivilege(postData, postPrivileges[index]));
|
||||||
});
|
postData = postData.filter((postData, index) => postData && postPrivileges[index].read);
|
||||||
next(null, postData);
|
return postData;
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.accept = function (socket, data, callback) {
|
SocketPosts.accept = async function (socket, data) {
|
||||||
acceptOrReject(posts.submitFromQueue, socket, data, callback);
|
await acceptOrReject(posts.submitFromQueue, socket, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.reject = function (socket, data, callback) {
|
SocketPosts.reject = async function (socket, data) {
|
||||||
acceptOrReject(posts.removeFromQueue, socket, data, callback);
|
await acceptOrReject(posts.removeFromQueue, socket, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.editQueuedContent = function (socket, data, callback) {
|
async function acceptOrReject(method, socket, data) {
|
||||||
if (!data || !data.id || !data.content) {
|
const canEditQueue = await posts.canEditQueue(socket.uid, data.id);
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
|
||||||
}
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
posts.editQueuedContent(socket.uid, data.id, data.content, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
plugins.fireHook('filter:parse.post', { postData: data }, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
function acceptOrReject(method, socket, data, callback) {
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
posts.canEditQueue(socket.uid, data.id, next);
|
|
||||||
},
|
|
||||||
function (canEditQueue, next) {
|
|
||||||
if (!canEditQueue) {
|
if (!canEditQueue) {
|
||||||
return callback(new Error('[[error:no-privileges]]'));
|
throw new Error('[[error:no-privileges]]');
|
||||||
}
|
}
|
||||||
|
await method(data.id);
|
||||||
method(data.id, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SocketPosts.editQueuedContent = async function (socket, data) {
|
||||||
|
if (!data || !data.id || !data.content) {
|
||||||
|
throw new Error('[[error:invalid-data]]');
|
||||||
|
}
|
||||||
|
await posts.editQueuedContent(socket.uid, data.id, data.content);
|
||||||
|
return await plugins.fireHook('filter:parse.post', { postData: data });
|
||||||
|
};
|
||||||
|
|
||||||
require('../promisify')(SocketPosts);
|
require('../promisify')(SocketPosts);
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
|
||||||
var helpers = require('./helpers');
|
const helpers = require('./helpers');
|
||||||
|
|
||||||
module.exports = function (SocketPosts) {
|
module.exports = function (SocketPosts) {
|
||||||
SocketPosts.bookmark = function (socket, data, callback) {
|
SocketPosts.bookmark = async function (socket, data) {
|
||||||
helpers.postCommand(socket, 'bookmark', 'bookmarked', '', data, callback);
|
return await helpers.postCommand(socket, 'bookmark', 'bookmarked', '', data);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.unbookmark = function (socket, data, callback) {
|
SocketPosts.unbookmark = async function (socket, data) {
|
||||||
helpers.postCommand(socket, 'unbookmark', 'bookmarked', '', data, callback);
|
return await helpers.postCommand(socket, 'unbookmark', 'bookmarked', '', data);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,46 +1,30 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const posts = require('../../posts');
|
||||||
var posts = require('../../posts');
|
const privileges = require('../../privileges');
|
||||||
var privileges = require('../../privileges');
|
|
||||||
|
|
||||||
module.exports = function (SocketPosts) {
|
module.exports = function (SocketPosts) {
|
||||||
SocketPosts.getDiffs = function (socket, data, callback) {
|
SocketPosts.getDiffs = async function (socket, data) {
|
||||||
async.waterfall([
|
await privilegeCheck(data.pid, socket.uid);
|
||||||
async.apply(privilegeCheck, data.pid, socket.uid),
|
const timestamps = await posts.diffs.list(data.pid);
|
||||||
function (next) {
|
|
||||||
posts.diffs.list(data.pid, next);
|
|
||||||
},
|
|
||||||
function (timestamps, next) {
|
|
||||||
timestamps.unshift(Date.now());
|
timestamps.unshift(Date.now());
|
||||||
next(null, timestamps);
|
return timestamps;
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.showPostAt = function (socket, data, callback) {
|
SocketPosts.showPostAt = async function (socket, data) {
|
||||||
privilegeCheck(data.pid, socket.uid, function (err) {
|
await privilegeCheck(data.pid, socket.uid);
|
||||||
if (err) {
|
return await posts.diffs.load(data.pid, data.since, socket.uid);
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
posts.diffs.load(data.pid, data.since, socket.uid, callback);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function privilegeCheck(pid, uid, callback) {
|
async function privilegeCheck(pid, uid) {
|
||||||
async.parallel({
|
const [deleted, privilegesData] = await Promise.all([
|
||||||
deleted: async.apply(posts.getPostField, pid, 'deleted'),
|
posts.getPostField(pid, 'deleted'),
|
||||||
privileges: async.apply(privileges.posts.get, [pid], uid),
|
privileges.posts.get([pid], uid),
|
||||||
}, function (err, payload) {
|
]);
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
const allowed = privilegesData[0]['posts:history'] && (deleted ? privilegesData[0]['posts:view_deleted'] : true);
|
||||||
|
if (!allowed) {
|
||||||
|
throw new Error('[[error:no-privileges]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
payload.privileges = payload.privileges[0];
|
|
||||||
|
|
||||||
const allowed = payload.privileges['posts:history'] && (payload.deleted ? payload.privileges['posts:view_deleted'] : true);
|
|
||||||
callback(!allowed ? new Error('[[error:no-privileges]]') : null);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,81 +1,69 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const validator = require('validator');
|
||||||
var validator = require('validator');
|
const _ = require('lodash');
|
||||||
var _ = require('lodash');
|
|
||||||
|
|
||||||
var posts = require('../../posts');
|
const posts = require('../../posts');
|
||||||
var groups = require('../../groups');
|
const groups = require('../../groups');
|
||||||
var events = require('../../events');
|
const events = require('../../events');
|
||||||
var meta = require('../../meta');
|
const meta = require('../../meta');
|
||||||
var utils = require('../../utils');
|
const utils = require('../../utils');
|
||||||
var websockets = require('../index');
|
const websockets = require('../index');
|
||||||
|
|
||||||
module.exports = function (SocketPosts) {
|
module.exports = function (SocketPosts) {
|
||||||
SocketPosts.edit = function (socket, data, callback) {
|
SocketPosts.edit = async function (socket, data) {
|
||||||
if (!socket.uid) {
|
if (!socket.uid) {
|
||||||
return callback(new Error('[[error:not-logged-in]]'));
|
throw new Error('[[error:not-logged-in]]');
|
||||||
} else if (!data || !data.pid || (meta.config.minimumPostLength !== 0 && !data.content)) {
|
} else if (!data || !data.pid || (meta.config.minimumPostLength !== 0 && !data.content)) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trim and remove HTML (latter for composers that send in HTML, like redactor)
|
// Trim and remove HTML (latter for composers that send in HTML, like redactor)
|
||||||
var contentLen = utils.stripHTMLTags(data.content).trim().length;
|
var contentLen = utils.stripHTMLTags(data.content).trim().length;
|
||||||
|
|
||||||
if (data.title && data.title.length < meta.config.minimumTitleLength) {
|
if (data.title && data.title.length < meta.config.minimumTitleLength) {
|
||||||
return callback(new Error('[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]'));
|
throw new Error('[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]');
|
||||||
} else if (data.title && data.title.length > meta.config.maximumTitleLength) {
|
} else if (data.title && data.title.length > meta.config.maximumTitleLength) {
|
||||||
return callback(new Error('[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]'));
|
throw new Error('[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]');
|
||||||
} else if (data.tags && data.tags.length < meta.config.minimumTagsPerTopic) {
|
} else if (data.tags && data.tags.length < meta.config.minimumTagsPerTopic) {
|
||||||
return callback(new Error('[[error:not-enough-tags, ' + meta.config.minimumTagsPerTopic + ']]'));
|
throw new Error('[[error:not-enough-tags, ' + meta.config.minimumTagsPerTopic + ']]');
|
||||||
} else if (data.tags && data.tags.length > meta.config.maximumTagsPerTopic) {
|
} else if (data.tags && data.tags.length > meta.config.maximumTagsPerTopic) {
|
||||||
return callback(new Error('[[error:too-many-tags, ' + meta.config.maximumTagsPerTopic + ']]'));
|
throw new Error('[[error:too-many-tags, ' + meta.config.maximumTagsPerTopic + ']]');
|
||||||
} else if (meta.config.minimumPostLength !== 0 && contentLen < meta.config.minimumPostLength) {
|
} else if (meta.config.minimumPostLength !== 0 && contentLen < meta.config.minimumPostLength) {
|
||||||
return callback(new Error('[[error:content-too-short, ' + meta.config.minimumPostLength + ']]'));
|
throw new Error('[[error:content-too-short, ' + meta.config.minimumPostLength + ']]');
|
||||||
} else if (contentLen > meta.config.maximumPostLength) {
|
} else if (contentLen > meta.config.maximumPostLength) {
|
||||||
return callback(new Error('[[error:content-too-long, ' + meta.config.maximumPostLength + ']]'));
|
throw new Error('[[error:content-too-long, ' + meta.config.maximumPostLength + ']]');
|
||||||
}
|
}
|
||||||
|
|
||||||
data.uid = socket.uid;
|
data.uid = socket.uid;
|
||||||
data.req = websockets.reqFromSocket(socket);
|
data.req = websockets.reqFromSocket(socket);
|
||||||
|
|
||||||
var editResult;
|
const editResult = await posts.edit(data);
|
||||||
async.waterfall([
|
if (editResult.topic.renamed) {
|
||||||
function (next) {
|
await events.log({
|
||||||
posts.edit(data, next);
|
|
||||||
},
|
|
||||||
function (result, next) {
|
|
||||||
editResult = result;
|
|
||||||
if (result.topic.renamed) {
|
|
||||||
events.log({
|
|
||||||
type: 'topic-rename',
|
type: 'topic-rename',
|
||||||
uid: socket.uid,
|
uid: socket.uid,
|
||||||
ip: socket.ip,
|
ip: socket.ip,
|
||||||
tid: result.topic.tid,
|
tid: editResult.topic.tid,
|
||||||
oldTitle: validator.escape(String(result.topic.oldTitle)),
|
oldTitle: validator.escape(String(editResult.topic.oldTitle)),
|
||||||
newTitle: validator.escape(String(result.topic.title)),
|
newTitle: validator.escape(String(editResult.topic.title)),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!result.post.deleted) {
|
if (!editResult.post.deleted) {
|
||||||
websockets.in('topic_' + result.topic.tid).emit('event:post_edited', result);
|
websockets.in('topic_' + editResult.topic.tid).emit('event:post_edited', editResult);
|
||||||
return callback(null, result.post);
|
return editResult.post;
|
||||||
}
|
}
|
||||||
|
|
||||||
groups.getMembersOfGroups([
|
const memberData = await groups.getMembersOfGroups([
|
||||||
'administrators',
|
'administrators',
|
||||||
'Global Moderators',
|
'Global Moderators',
|
||||||
'cid:' + result.topic.cid + ':privileges:moderate',
|
'cid:' + editResult.topic.cid + ':privileges:moderate',
|
||||||
'cid:' + result.topic.cid + ':privileges:groups:moderate',
|
'cid:' + editResult.topic.cid + ':privileges:groups:moderate',
|
||||||
], next);
|
]);
|
||||||
},
|
|
||||||
function (results, next) {
|
const uids = _.uniq(_.flatten(memberData).concat(socket.uid.toString()));
|
||||||
var uids = _.uniq(_.flatten(results).concat(socket.uid.toString()));
|
uids.forEach(uid => websockets.in('uid_' + uid).emit('event:post_edited', editResult));
|
||||||
uids.forEach(function (uid) {
|
return editResult.post;
|
||||||
websockets.in('uid_' + uid).emit('event:post_edited', editResult);
|
|
||||||
});
|
|
||||||
next(null, editResult.post);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,45 +1,36 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
|
||||||
var async = require('async');
|
const posts = require('../../posts');
|
||||||
var posts = require('../../posts');
|
const plugins = require('../../plugins');
|
||||||
var plugins = require('../../plugins');
|
const websockets = require('../index');
|
||||||
var websockets = require('../index');
|
const socketHelpers = require('../helpers');
|
||||||
var socketHelpers = require('../helpers');
|
|
||||||
|
|
||||||
var helpers = module.exports;
|
const helpers = module.exports;
|
||||||
|
|
||||||
helpers.postCommand = function (socket, command, eventName, notification, data, callback) {
|
helpers.postCommand = async function (socket, command, eventName, notification, data) {
|
||||||
if (!socket.uid) {
|
if (!socket.uid) {
|
||||||
return callback(new Error('[[error:not-logged-in]]'));
|
throw new Error('[[error:not-logged-in]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data || !data.pid) {
|
if (!data || !data.pid) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.room_id) {
|
if (!data.room_id) {
|
||||||
return callback(new Error('[[error:invalid-room-id, ' + data.room_id + ' ]]'));
|
throw new Error('[[error:invalid-room-id, ' + data.room_id + ' ]]');
|
||||||
|
}
|
||||||
|
const [exists, deleted] = await Promise.all([
|
||||||
|
posts.exists(data.pid),
|
||||||
|
posts.getPostField(data.pid, 'deleted'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!exists) {
|
||||||
|
throw new Error('[[error:invalid-pid]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
async.waterfall([
|
if (deleted) {
|
||||||
function (next) {
|
throw new Error('[[error:post-deleted]]');
|
||||||
async.parallel({
|
|
||||||
exists: function (next) {
|
|
||||||
posts.exists(data.pid, next);
|
|
||||||
},
|
|
||||||
deleted: function (next) {
|
|
||||||
posts.getPostField(data.pid, 'deleted', next);
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
if (!results.exists) {
|
|
||||||
return next(new Error('[[error:invalid-pid]]'));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (results.deleted) {
|
|
||||||
return next(new Error('[[error:post-deleted]]'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -50,25 +41,16 @@ helpers.postCommand = function (socket, command, eventName, notification, data,
|
|||||||
filter:post.bookmark
|
filter:post.bookmark
|
||||||
filter:post.unbookmark
|
filter:post.unbookmark
|
||||||
*/
|
*/
|
||||||
plugins.fireHook('filter:post.' + command, { data: data, uid: socket.uid }, next);
|
const filteredData = await plugins.fireHook('filter:post.' + command, { data: data, uid: socket.uid });
|
||||||
},
|
return await executeCommand(socket, command, eventName, notification, filteredData.data);
|
||||||
function (filteredData, next) {
|
|
||||||
executeCommand(socket, command, eventName, notification, filteredData.data, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function executeCommand(socket, command, eventName, notification, data, callback) {
|
async function executeCommand(socket, command, eventName, notification, data) {
|
||||||
async.waterfall([
|
const result = await posts[command](data.pid, socket.uid);
|
||||||
function (next) {
|
|
||||||
posts[command](data.pid, socket.uid, next);
|
|
||||||
},
|
|
||||||
function (result, next) {
|
|
||||||
if (result && eventName) {
|
if (result && eventName) {
|
||||||
websockets.in('uid_' + socket.uid).emit('posts.' + command, result);
|
websockets.in('uid_' + socket.uid).emit('posts.' + command, result);
|
||||||
websockets.in(data.room_id).emit('event:' + eventName, result);
|
websockets.in(data.room_id).emit('event:' + eventName, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result && command === 'upvote') {
|
if (result && command === 'upvote') {
|
||||||
socketHelpers.upvote(result, notification);
|
socketHelpers.upvote(result, notification);
|
||||||
} else if (result && notification) {
|
} else if (result && notification) {
|
||||||
@@ -76,7 +58,5 @@ function executeCommand(socket, command, eventName, notification, data, callback
|
|||||||
} else if (result && command === 'unvote') {
|
} else if (result && command === 'unvote') {
|
||||||
socketHelpers.rescindUpvoteNotification(data.pid, socket.uid);
|
socketHelpers.rescindUpvoteNotification(data.pid, socket.uid);
|
||||||
}
|
}
|
||||||
next(null, result);
|
return result;
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +1,31 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const privileges = require('../../privileges');
|
||||||
var privileges = require('../../privileges');
|
const topics = require('../../topics');
|
||||||
var topics = require('../../topics');
|
const socketHelpers = require('../helpers');
|
||||||
var socketHelpers = require('../helpers');
|
|
||||||
|
|
||||||
module.exports = function (SocketPosts) {
|
module.exports = function (SocketPosts) {
|
||||||
SocketPosts.movePost = function (socket, data, callback) {
|
SocketPosts.movePost = async function (socket, data) {
|
||||||
SocketPosts.movePosts(socket, { pids: [data.pid], tid: data.tid }, callback);
|
await SocketPosts.movePosts(socket, { pids: [data.pid], tid: data.tid });
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.movePosts = function (socket, data, callback) {
|
SocketPosts.movePosts = async function (socket, data) {
|
||||||
if (!socket.uid) {
|
if (!socket.uid) {
|
||||||
return callback(new Error('[[error:not-logged-in]]'));
|
throw new Error('[[error:not-logged-in]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data || !Array.isArray(data.pids) || !data.tid) {
|
if (!data || !Array.isArray(data.pids) || !data.tid) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
|
||||||
async.eachSeries(data.pids, function (pid, next) {
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
privileges.posts.canMove(pid, socket.uid, next);
|
|
||||||
},
|
|
||||||
function (canMove, next) {
|
|
||||||
if (!canMove) {
|
|
||||||
return next(new Error('[[error:no-privileges]]'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
topics.movePostToTopic(socket.uid, pid, data.tid, next);
|
for (const pid of data.pids) {
|
||||||
},
|
/* eslint-disable no-await-in-loop */
|
||||||
function (next) {
|
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);
|
||||||
socketHelpers.sendNotificationToPostOwner(pid, socket.uid, 'move', 'notifications:moved_your_post');
|
socketHelpers.sendNotificationToPostOwner(pid, socket.uid, 'move', 'notifications:moved_your_post');
|
||||||
next();
|
}
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}, callback);
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,253 +1,172 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const posts = require('../../posts');
|
||||||
|
const topics = require('../../topics');
|
||||||
var posts = require('../../posts');
|
const events = require('../../events');
|
||||||
var topics = require('../../topics');
|
const websockets = require('../index');
|
||||||
var events = require('../../events');
|
const socketTopics = require('../topics');
|
||||||
var websockets = require('../index');
|
const privileges = require('../../privileges');
|
||||||
var socketTopics = require('../topics');
|
const plugins = require('../../plugins');
|
||||||
var privileges = require('../../privileges');
|
const social = require('../../social');
|
||||||
var plugins = require('../../plugins');
|
const user = require('../../user');
|
||||||
var social = require('../../social');
|
const utils = require('../../utils');
|
||||||
var user = require('../../user');
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = function (SocketPosts) {
|
module.exports = function (SocketPosts) {
|
||||||
SocketPosts.loadPostTools = function (socket, data, callback) {
|
SocketPosts.loadPostTools = async function (socket, data) {
|
||||||
if (!data || !data.pid || !data.cid) {
|
if (!data || !data.pid || !data.cid) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
const results = await utils.promiseParallel({
|
||||||
async.parallel({
|
posts: posts.getPostFields(data.pid, ['deleted', 'bookmarks', 'uid', 'ip']),
|
||||||
posts: function (next) {
|
isAdmin: user.isAdministrator(socket.uid),
|
||||||
posts.getPostFields(data.pid, ['deleted', 'bookmarks', 'uid', 'ip'], next);
|
isGlobalMod: user.isGlobalModerator(socket.uid),
|
||||||
},
|
isModerator: user.isModerator(socket.uid, data.cid),
|
||||||
isAdmin: function (next) {
|
canEdit: privileges.posts.canEdit(data.pid, socket.uid),
|
||||||
user.isAdministrator(socket.uid, next);
|
canDelete: privileges.posts.canDelete(data.pid, socket.uid),
|
||||||
},
|
canPurge: privileges.posts.canPurge(data.pid, socket.uid),
|
||||||
isGlobalMod: function (next) {
|
canFlag: privileges.posts.canFlag(data.pid, socket.uid),
|
||||||
user.isGlobalModerator(socket.uid, next);
|
bookmarked: posts.hasBookmarked(data.pid, socket.uid),
|
||||||
},
|
tools: plugins.fireHook('filter:post.tools', { pid: data.pid, uid: socket.uid, tools: [] }),
|
||||||
isModerator: function (next) {
|
postSharing: social.getActivePostSharing(),
|
||||||
user.isModerator(socket.uid, data.cid, next);
|
history: posts.diffs.exists(data.pid),
|
||||||
},
|
});
|
||||||
canEdit: function (next) {
|
|
||||||
privileges.posts.canEdit(data.pid, socket.uid, next);
|
const postData = results.posts;
|
||||||
},
|
postData.tools = results.tools.tools;
|
||||||
canDelete: function (next) {
|
postData.bookmarked = results.bookmarked;
|
||||||
privileges.posts.canDelete(data.pid, socket.uid, next);
|
postData.selfPost = socket.uid && socket.uid === postData.uid;
|
||||||
},
|
postData.display_edit_tools = results.canEdit.flag;
|
||||||
canPurge: function (next) {
|
postData.display_delete_tools = results.canDelete.flag;
|
||||||
privileges.posts.canPurge(data.pid, socket.uid, next);
|
postData.display_purge_tools = results.canPurge;
|
||||||
},
|
postData.display_flag_tools = socket.uid && !postData.selfPost && results.canFlag.flag;
|
||||||
canFlag: function (next) {
|
postData.display_moderator_tools = postData.display_edit_tools || postData.display_delete_tools;
|
||||||
privileges.posts.canFlag(data.pid, socket.uid, next);
|
postData.display_move_tools = results.isAdmin || results.isModerator;
|
||||||
},
|
postData.display_change_owner_tools = results.isAdmin || results.isModerator;
|
||||||
bookmarked: function (next) {
|
postData.display_ip_ban = (results.isAdmin || results.isGlobalMod) && !postData.selfPost;
|
||||||
posts.hasBookmarked(data.pid, socket.uid, next);
|
postData.display_history = results.history;
|
||||||
},
|
postData.toolsVisible = postData.tools.length || postData.display_moderator_tools;
|
||||||
tools: function (next) {
|
|
||||||
plugins.fireHook('filter:post.tools', { pid: data.pid, uid: socket.uid, tools: [] }, next);
|
|
||||||
},
|
|
||||||
postSharing: function (next) {
|
|
||||||
social.getActivePostSharing(next);
|
|
||||||
},
|
|
||||||
history: async.apply(posts.diffs.exists, data.pid),
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
var posts = results.posts;
|
|
||||||
posts.tools = results.tools.tools;
|
|
||||||
posts.bookmarked = results.bookmarked;
|
|
||||||
posts.selfPost = socket.uid && socket.uid === posts.uid;
|
|
||||||
posts.display_edit_tools = results.canEdit.flag;
|
|
||||||
posts.display_delete_tools = results.canDelete.flag;
|
|
||||||
posts.display_purge_tools = results.canPurge;
|
|
||||||
posts.display_flag_tools = socket.uid && !posts.selfPost && results.canFlag.flag;
|
|
||||||
posts.display_moderator_tools = posts.display_edit_tools || posts.display_delete_tools;
|
|
||||||
posts.display_move_tools = results.isAdmin || results.isModerator;
|
|
||||||
posts.display_change_owner_tools = results.isAdmin || results.isModerator;
|
|
||||||
posts.display_ip_ban = (results.isAdmin || results.isGlobalMod) && !posts.selfPost;
|
|
||||||
posts.display_history = results.history;
|
|
||||||
posts.toolsVisible = posts.tools.length || posts.display_moderator_tools;
|
|
||||||
|
|
||||||
if (!results.isAdmin && !results.isGlobalMod && !results.isModerator) {
|
if (!results.isAdmin && !results.isGlobalMod && !results.isModerator) {
|
||||||
posts.ip = undefined;
|
postData.ip = undefined;
|
||||||
}
|
}
|
||||||
next(null, results);
|
return results;
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.delete = function (socket, data, callback) {
|
SocketPosts.delete = async function (socket, data) {
|
||||||
if (!data || !data.pid) {
|
if (!data || !data.pid) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
var postData;
|
const postData = await posts.tools.delete(socket.uid, data.pid);
|
||||||
async.waterfall([
|
const results = await isMainAndLastPost(data.pid);
|
||||||
function (next) {
|
|
||||||
posts.tools.delete(socket.uid, data.pid, next);
|
|
||||||
},
|
|
||||||
function (_postData, next) {
|
|
||||||
postData = _postData;
|
|
||||||
isMainAndLastPost(data.pid, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
if (results.isMain && results.isLast) {
|
if (results.isMain && results.isLast) {
|
||||||
deleteOrRestoreTopicOf('delete', data.pid, socket, next);
|
await deleteOrRestoreTopicOf('delete', data.pid, socket);
|
||||||
} else {
|
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
websockets.in('topic_' + data.tid).emit('event:post_deleted', postData);
|
websockets.in('topic_' + data.tid).emit('event:post_deleted', postData);
|
||||||
|
|
||||||
events.log({
|
await events.log({
|
||||||
type: 'post-delete',
|
type: 'post-delete',
|
||||||
uid: socket.uid,
|
uid: socket.uid,
|
||||||
pid: data.pid,
|
pid: data.pid,
|
||||||
tid: postData.tid,
|
tid: postData.tid,
|
||||||
ip: socket.ip,
|
ip: socket.ip,
|
||||||
});
|
});
|
||||||
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.restore = function (socket, data, callback) {
|
SocketPosts.restore = async function (socket, data) {
|
||||||
if (!data || !data.pid) {
|
if (!data || !data.pid) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
var postData;
|
const postData = await posts.tools.restore(socket.uid, data.pid);
|
||||||
async.waterfall([
|
const results = await isMainAndLastPost(data.pid);
|
||||||
function (next) {
|
|
||||||
posts.tools.restore(socket.uid, data.pid, next);
|
|
||||||
},
|
|
||||||
function (_postData, next) {
|
|
||||||
postData = _postData;
|
|
||||||
isMainAndLastPost(data.pid, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
if (results.isMain && results.isLast) {
|
if (results.isMain && results.isLast) {
|
||||||
deleteOrRestoreTopicOf('restore', data.pid, socket, next);
|
await deleteOrRestoreTopicOf('restore', data.pid, socket);
|
||||||
} else {
|
|
||||||
setImmediate(next);
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
websockets.in('topic_' + data.tid).emit('event:post_restored', postData);
|
websockets.in('topic_' + data.tid).emit('event:post_restored', postData);
|
||||||
|
|
||||||
events.log({
|
await events.log({
|
||||||
type: 'post-restore',
|
type: 'post-restore',
|
||||||
uid: socket.uid,
|
uid: socket.uid,
|
||||||
pid: data.pid,
|
pid: data.pid,
|
||||||
tid: postData.tid,
|
tid: postData.tid,
|
||||||
ip: socket.ip,
|
ip: socket.ip,
|
||||||
});
|
});
|
||||||
|
|
||||||
setImmediate(next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.deletePosts = function (socket, data, callback) {
|
SocketPosts.deletePosts = async function (socket, data) {
|
||||||
if (!data || !Array.isArray(data.pids)) {
|
if (!data || !Array.isArray(data.pids)) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
|
}
|
||||||
|
for (const pid of data.pids) {
|
||||||
|
/* eslint-disable no-await-in-loop */
|
||||||
|
await SocketPosts.delete(socket, { pid: pid, tid: data.tid });
|
||||||
}
|
}
|
||||||
async.eachSeries(data.pids, function (pid, next) {
|
|
||||||
SocketPosts.delete(socket, { pid: pid, tid: data.tid }, next);
|
|
||||||
}, callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.purgePosts = function (socket, data, callback) {
|
SocketPosts.purgePosts = async function (socket, data) {
|
||||||
if (!data || !Array.isArray(data.pids)) {
|
if (!data || !Array.isArray(data.pids)) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
|
}
|
||||||
|
for (const pid of data.pids) {
|
||||||
|
/* eslint-disable no-await-in-loop */
|
||||||
|
await SocketPosts.purge(socket, { pid: pid, tid: data.tid });
|
||||||
}
|
}
|
||||||
async.eachSeries(data.pids, function (pid, next) {
|
|
||||||
SocketPosts.purge(socket, { pid: pid, tid: data.tid }, next);
|
|
||||||
}, callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.purge = function (socket, data, callback) {
|
SocketPosts.purge = async function (socket, data) {
|
||||||
if (!data || !parseInt(data.pid, 10)) {
|
if (!data || !parseInt(data.pid, 10)) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
var postData;
|
|
||||||
var topicData;
|
|
||||||
var isMainAndLast = false;
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
isMainAndLastPost(data.pid, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
if (results.isMain && !results.isLast) {
|
|
||||||
return next(new Error('[[error:cant-purge-main-post]]'));
|
|
||||||
}
|
|
||||||
isMainAndLast = results.isMain && results.isLast;
|
|
||||||
|
|
||||||
posts.getPostFields(data.pid, ['toPid', 'tid'], next);
|
|
||||||
},
|
const results = await isMainAndLastPost(data.pid);
|
||||||
function (_postData, next) {
|
if (results.isMain && !results.isLast) {
|
||||||
postData = _postData;
|
throw new Error('[[error:cant-purge-main-post]]');
|
||||||
|
}
|
||||||
|
|
||||||
|
const isMainAndLast = results.isMain && results.isLast;
|
||||||
|
const postData = await posts.getPostFields(data.pid, ['toPid', 'tid']);
|
||||||
postData.pid = data.pid;
|
postData.pid = data.pid;
|
||||||
posts.tools.purge(socket.uid, data.pid, next);
|
|
||||||
},
|
await posts.tools.purge(socket.uid, data.pid);
|
||||||
function (next) {
|
|
||||||
websockets.in('topic_' + data.tid).emit('event:post_purged', postData);
|
websockets.in('topic_' + data.tid).emit('event:post_purged', postData);
|
||||||
topics.getTopicFields(data.tid, ['title', 'cid'], next);
|
const topicData = await topics.getTopicFields(data.tid, ['title', 'cid']);
|
||||||
},
|
|
||||||
function (_topicData, next) {
|
await events.log({
|
||||||
topicData = _topicData;
|
|
||||||
events.log({
|
|
||||||
type: 'post-purge',
|
type: 'post-purge',
|
||||||
uid: socket.uid,
|
uid: socket.uid,
|
||||||
pid: data.pid,
|
pid: data.pid,
|
||||||
ip: socket.ip,
|
ip: socket.ip,
|
||||||
tid: postData.tid,
|
tid: postData.tid,
|
||||||
title: String(topicData.title),
|
title: String(topicData.title),
|
||||||
}, next);
|
});
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
if (isMainAndLast) {
|
if (isMainAndLast) {
|
||||||
socketTopics.doTopicAction('purge', 'event:topic_purged', socket, { tids: [postData.tid], cid: topicData.cid }, next);
|
await socketTopics.doTopicAction('purge', 'event:topic_purged', socket, { tids: [postData.tid], cid: topicData.cid });
|
||||||
} else {
|
|
||||||
setImmediate(next);
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function deleteOrRestoreTopicOf(command, pid, socket, callback) {
|
async function deleteOrRestoreTopicOf(command, pid, socket) {
|
||||||
async.waterfall([
|
const topic = await posts.getTopicFields(pid, ['tid', 'cid', 'deleted']);
|
||||||
function (next) {
|
|
||||||
posts.getTopicFields(pid, ['tid', 'cid', 'deleted'], next);
|
|
||||||
},
|
|
||||||
function (topic, next) {
|
|
||||||
if (command === 'delete' && !topic.deleted) {
|
if (command === 'delete' && !topic.deleted) {
|
||||||
socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, { tids: [topic.tid], cid: topic.cid }, next);
|
await socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, { tids: [topic.tid], cid: topic.cid });
|
||||||
} else if (command === 'restore' && topic.deleted) {
|
} else if (command === 'restore' && topic.deleted) {
|
||||||
socketTopics.doTopicAction('restore', 'event:topic_restored', socket, { tids: [topic.tid], cid: topic.cid }, next);
|
await socketTopics.doTopicAction('restore', 'event:topic_restored', socket, { tids: [topic.tid], cid: topic.cid });
|
||||||
} else {
|
|
||||||
setImmediate(next);
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isMainAndLastPost(pid, callback) {
|
async function isMainAndLastPost(pid) {
|
||||||
async.parallel({
|
const [isMain, topicData] = await Promise.all([
|
||||||
isMain: function (next) {
|
posts.isMain(pid),
|
||||||
posts.isMain(pid, next);
|
posts.getTopicFields(pid, ['postcount']),
|
||||||
},
|
]);
|
||||||
isLast: function (next) {
|
return {
|
||||||
posts.getTopicFields(pid, ['postcount'], function (err, topic) {
|
isMain: isMain,
|
||||||
next(err, topic ? topic.postcount === 1 : false);
|
isLast: topicData && topicData.postcount === 1,
|
||||||
});
|
};
|
||||||
},
|
|
||||||
}, callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketPosts.changeOwner = async function (socket, data) {
|
SocketPosts.changeOwner = async function (socket, data) {
|
||||||
|
|||||||
@@ -1,103 +1,74 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const db = require('../../database');
|
||||||
|
const user = require('../../user');
|
||||||
var db = require('../../database');
|
const posts = require('../../posts');
|
||||||
var user = require('../../user');
|
const privileges = require('../../privileges');
|
||||||
var posts = require('../../posts');
|
const meta = require('../../meta');
|
||||||
var privileges = require('../../privileges');
|
const helpers = require('./helpers');
|
||||||
var meta = require('../../meta');
|
|
||||||
var helpers = require('./helpers');
|
|
||||||
|
|
||||||
module.exports = function (SocketPosts) {
|
module.exports = function (SocketPosts) {
|
||||||
SocketPosts.getVoters = function (socket, data, callback) {
|
SocketPosts.getVoters = async function (socket, data) {
|
||||||
if (!data || !data.pid || !data.cid) {
|
if (!data || !data.pid || !data.cid) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
const showDownvotes = !meta.config['downvote:disabled'];
|
const showDownvotes = !meta.config['downvote:disabled'];
|
||||||
async.waterfall([
|
const canSeeVotes = meta.config.votesArePublic || await privileges.categories.isAdminOrMod(data.cid, socket.uid);
|
||||||
function (next) {
|
if (!canSeeVotes) {
|
||||||
if (meta.config.votesArePublic) {
|
throw new Error('[[error:no-privileges]]');
|
||||||
return next(null, true);
|
|
||||||
}
|
|
||||||
privileges.categories.isAdminOrMod(data.cid, socket.uid, next);
|
|
||||||
},
|
|
||||||
function (isAdminOrMod, next) {
|
|
||||||
if (!isAdminOrMod) {
|
|
||||||
return next(new Error('[[error:no-privileges]]'));
|
|
||||||
}
|
}
|
||||||
|
const [upvoteUids, downvoteUids] = await Promise.all([
|
||||||
|
db.getSetMembers('pid:' + data.pid + ':upvote'),
|
||||||
|
showDownvotes ? db.getSetMembers('pid:' + data.pid + ':downvote') : [],
|
||||||
|
]);
|
||||||
|
|
||||||
async.parallel({
|
const [upvoters, downvoters] = await Promise.all([
|
||||||
upvoteUids: function (next) {
|
user.getUsersFields(upvoteUids, ['username', 'userslug', 'picture']),
|
||||||
db.getSetMembers('pid:' + data.pid + ':upvote', next);
|
user.getUsersFields(downvoteUids, ['username', 'userslug', 'picture']),
|
||||||
},
|
]);
|
||||||
downvoteUids: function (next) {
|
|
||||||
if (!showDownvotes) {
|
return {
|
||||||
return setImmediate(next, null, []);
|
upvoteCount: upvoters.length,
|
||||||
}
|
downvoteCount: downvoters.length,
|
||||||
db.getSetMembers('pid:' + data.pid + ':downvote', next);
|
showDownvotes: showDownvotes,
|
||||||
},
|
upvoters: upvoters,
|
||||||
}, next);
|
downvoters: downvoters,
|
||||||
},
|
};
|
||||||
function (results, next) {
|
|
||||||
async.parallel({
|
|
||||||
upvoters: function (next) {
|
|
||||||
user.getUsersFields(results.upvoteUids, ['username', 'userslug', 'picture'], next);
|
|
||||||
},
|
|
||||||
downvoters: function (next) {
|
|
||||||
user.getUsersFields(results.downvoteUids, ['username', 'userslug', 'picture'], next);
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
results.upvoteCount = results.upvoters.length;
|
|
||||||
results.downvoteCount = results.downvoters.length;
|
|
||||||
results.showDownvotes = showDownvotes;
|
|
||||||
next(null, results);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.getUpvoters = function (socket, pids, callback) {
|
SocketPosts.getUpvoters = async function (socket, pids) {
|
||||||
if (!Array.isArray(pids)) {
|
if (!Array.isArray(pids)) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
const data = await posts.getUpvotedUidsByPids(pids);
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
posts.getUpvotedUidsByPids(pids, next);
|
|
||||||
},
|
|
||||||
function (data, next) {
|
|
||||||
if (!data.length) {
|
if (!data.length) {
|
||||||
return callback(null, []);
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
async.map(data, function (uids, next) {
|
const result = await Promise.all(data.map(async function (uids) {
|
||||||
var otherCount = 0;
|
let otherCount = 0;
|
||||||
if (uids.length > 6) {
|
if (uids.length > 6) {
|
||||||
otherCount = uids.length - 5;
|
otherCount = uids.length - 5;
|
||||||
uids = uids.slice(0, 5);
|
uids = uids.slice(0, 5);
|
||||||
}
|
}
|
||||||
user.getUsernamesByUids(uids, function (err, usernames) {
|
const usernames = await user.getUsernamesByUids(uids);
|
||||||
next(err, {
|
return {
|
||||||
otherCount: otherCount,
|
otherCount: otherCount,
|
||||||
usernames: usernames,
|
usernames: usernames,
|
||||||
});
|
};
|
||||||
});
|
}));
|
||||||
}, next);
|
return result;
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.upvote = function (socket, data, callback) {
|
SocketPosts.upvote = async function (socket, data) {
|
||||||
helpers.postCommand(socket, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data, callback);
|
return await helpers.postCommand(socket, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.downvote = function (socket, data, callback) {
|
SocketPosts.downvote = async function (socket, data) {
|
||||||
helpers.postCommand(socket, 'downvote', 'voted', '', data, callback);
|
return await helpers.postCommand(socket, 'downvote', 'voted', '', data);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketPosts.unvote = function (socket, data, callback) {
|
SocketPosts.unvote = async function (socket, data) {
|
||||||
helpers.postCommand(socket, 'unvote', 'voted', '', data, callback);
|
return await helpers.postCommand(socket, 'unvote', 'voted', '', data);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -143,3 +143,5 @@ SocketTopics.isModerator = function (socket, tid, callback) {
|
|||||||
SocketTopics.getTopic = function (socket, tid, callback) {
|
SocketTopics.getTopic = function (socket, tid, callback) {
|
||||||
apiController.getTopicData(tid, socket.uid, callback);
|
apiController.getTopicData(tid, socket.uid, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
require('../promisify')(SocketTopics);
|
||||||
|
|||||||
Reference in New Issue
Block a user