refactor: async/await socket.io/posts
parent
4652c68ba7
commit
e93ef0d7fd
@ -1,14 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
|
||||
var helpers = require('./helpers');
|
||||
const helpers = require('./helpers');
|
||||
|
||||
module.exports = function (SocketPosts) {
|
||||
SocketPosts.bookmark = function (socket, data, callback) {
|
||||
helpers.postCommand(socket, 'bookmark', 'bookmarked', '', data, callback);
|
||||
SocketPosts.bookmark = async function (socket, data) {
|
||||
return await helpers.postCommand(socket, 'bookmark', 'bookmarked', '', data);
|
||||
};
|
||||
|
||||
SocketPosts.unbookmark = function (socket, data, callback) {
|
||||
helpers.postCommand(socket, 'unbookmark', 'bookmarked', '', data, callback);
|
||||
SocketPosts.unbookmark = async function (socket, data) {
|
||||
return await helpers.postCommand(socket, 'unbookmark', 'bookmarked', '', data);
|
||||
};
|
||||
};
|
||||
|
@ -1,46 +1,30 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var posts = require('../../posts');
|
||||
var privileges = require('../../privileges');
|
||||
const posts = require('../../posts');
|
||||
const privileges = require('../../privileges');
|
||||
|
||||
module.exports = function (SocketPosts) {
|
||||
SocketPosts.getDiffs = function (socket, data, callback) {
|
||||
async.waterfall([
|
||||
async.apply(privilegeCheck, data.pid, socket.uid),
|
||||
function (next) {
|
||||
posts.diffs.list(data.pid, next);
|
||||
},
|
||||
function (timestamps, next) {
|
||||
timestamps.unshift(Date.now());
|
||||
next(null, timestamps);
|
||||
},
|
||||
], callback);
|
||||
SocketPosts.getDiffs = async function (socket, data) {
|
||||
await privilegeCheck(data.pid, socket.uid);
|
||||
const timestamps = await posts.diffs.list(data.pid);
|
||||
timestamps.unshift(Date.now());
|
||||
return timestamps;
|
||||
};
|
||||
|
||||
SocketPosts.showPostAt = function (socket, data, callback) {
|
||||
privilegeCheck(data.pid, socket.uid, function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
posts.diffs.load(data.pid, data.since, socket.uid, callback);
|
||||
});
|
||||
SocketPosts.showPostAt = async function (socket, data) {
|
||||
await privilegeCheck(data.pid, socket.uid);
|
||||
return await posts.diffs.load(data.pid, data.since, socket.uid);
|
||||
};
|
||||
|
||||
function privilegeCheck(pid, uid, callback) {
|
||||
async.parallel({
|
||||
deleted: async.apply(posts.getPostField, pid, 'deleted'),
|
||||
privileges: async.apply(privileges.posts.get, [pid], uid),
|
||||
}, function (err, payload) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
payload.privileges = payload.privileges[0];
|
||||
async function privilegeCheck(pid, uid) {
|
||||
const [deleted, privilegesData] = await Promise.all([
|
||||
posts.getPostField(pid, 'deleted'),
|
||||
privileges.posts.get([pid], uid),
|
||||
]);
|
||||
|
||||
const allowed = payload.privileges['posts:history'] && (payload.deleted ? payload.privileges['posts:view_deleted'] : true);
|
||||
callback(!allowed ? new Error('[[error:no-privileges]]') : null);
|
||||
});
|
||||
const allowed = privilegesData[0]['posts:history'] && (deleted ? privilegesData[0]['posts:view_deleted'] : true);
|
||||
if (!allowed) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -1,81 +1,69 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var validator = require('validator');
|
||||
var _ = require('lodash');
|
||||
const validator = require('validator');
|
||||
const _ = require('lodash');
|
||||
|
||||
var posts = require('../../posts');
|
||||
var groups = require('../../groups');
|
||||
var events = require('../../events');
|
||||
var meta = require('../../meta');
|
||||
var utils = require('../../utils');
|
||||
var websockets = require('../index');
|
||||
const posts = require('../../posts');
|
||||
const groups = require('../../groups');
|
||||
const events = require('../../events');
|
||||
const meta = require('../../meta');
|
||||
const utils = require('../../utils');
|
||||
const websockets = require('../index');
|
||||
|
||||
module.exports = function (SocketPosts) {
|
||||
SocketPosts.edit = function (socket, data, callback) {
|
||||
SocketPosts.edit = async function (socket, data) {
|
||||
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)) {
|
||||
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)
|
||||
var contentLen = utils.stripHTMLTags(data.content).trim().length;
|
||||
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
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.req = websockets.reqFromSocket(socket);
|
||||
|
||||
var editResult;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts.edit(data, next);
|
||||
},
|
||||
function (result, next) {
|
||||
editResult = result;
|
||||
if (result.topic.renamed) {
|
||||
events.log({
|
||||
type: 'topic-rename',
|
||||
uid: socket.uid,
|
||||
ip: socket.ip,
|
||||
tid: result.topic.tid,
|
||||
oldTitle: validator.escape(String(result.topic.oldTitle)),
|
||||
newTitle: validator.escape(String(result.topic.title)),
|
||||
});
|
||||
}
|
||||
const editResult = await posts.edit(data);
|
||||
if (editResult.topic.renamed) {
|
||||
await events.log({
|
||||
type: 'topic-rename',
|
||||
uid: socket.uid,
|
||||
ip: socket.ip,
|
||||
tid: editResult.topic.tid,
|
||||
oldTitle: validator.escape(String(editResult.topic.oldTitle)),
|
||||
newTitle: validator.escape(String(editResult.topic.title)),
|
||||
});
|
||||
}
|
||||
|
||||
if (!editResult.post.deleted) {
|
||||
websockets.in('topic_' + editResult.topic.tid).emit('event:post_edited', editResult);
|
||||
return editResult.post;
|
||||
}
|
||||
|
||||
if (!result.post.deleted) {
|
||||
websockets.in('topic_' + result.topic.tid).emit('event:post_edited', result);
|
||||
return callback(null, result.post);
|
||||
}
|
||||
const memberData = await groups.getMembersOfGroups([
|
||||
'administrators',
|
||||
'Global Moderators',
|
||||
'cid:' + editResult.topic.cid + ':privileges:moderate',
|
||||
'cid:' + editResult.topic.cid + ':privileges:groups:moderate',
|
||||
]);
|
||||
|
||||
groups.getMembersOfGroups([
|
||||
'administrators',
|
||||
'Global Moderators',
|
||||
'cid:' + result.topic.cid + ':privileges:moderate',
|
||||
'cid:' + result.topic.cid + ':privileges:groups:moderate',
|
||||
], next);
|
||||
},
|
||||
function (results, next) {
|
||||
var uids = _.uniq(_.flatten(results).concat(socket.uid.toString()));
|
||||
uids.forEach(function (uid) {
|
||||
websockets.in('uid_' + uid).emit('event:post_edited', editResult);
|
||||
});
|
||||
next(null, editResult.post);
|
||||
},
|
||||
], callback);
|
||||
const uids = _.uniq(_.flatten(memberData).concat(socket.uid.toString()));
|
||||
uids.forEach(uid => websockets.in('uid_' + uid).emit('event:post_edited', editResult));
|
||||
return editResult.post;
|
||||
};
|
||||
};
|
||||
|
@ -1,82 +1,62 @@
|
||||
'use strict';
|
||||
|
||||
|
||||
var async = require('async');
|
||||
var posts = require('../../posts');
|
||||
var plugins = require('../../plugins');
|
||||
var websockets = require('../index');
|
||||
var socketHelpers = require('../helpers');
|
||||
const posts = require('../../posts');
|
||||
const plugins = require('../../plugins');
|
||||
const websockets = require('../index');
|
||||
const 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) {
|
||||
return callback(new Error('[[error:not-logged-in]]'));
|
||||
throw new Error('[[error:not-logged-in]]');
|
||||
}
|
||||
|
||||
if (!data || !data.pid) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
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'),
|
||||
]);
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
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 (!exists) {
|
||||
throw new Error('[[error:invalid-pid]]');
|
||||
}
|
||||
|
||||
if (results.deleted) {
|
||||
return next(new Error('[[error:post-deleted]]'));
|
||||
}
|
||||
if (deleted) {
|
||||
throw new Error('[[error:post-deleted]]');
|
||||
}
|
||||
|
||||
/*
|
||||
hooks:
|
||||
filter:post.upvote
|
||||
filter:post.downvote
|
||||
filter:post.unvote
|
||||
filter:post.bookmark
|
||||
filter:post.unbookmark
|
||||
*/
|
||||
plugins.fireHook('filter:post.' + command, { data: data, uid: socket.uid }, next);
|
||||
},
|
||||
function (filteredData, next) {
|
||||
executeCommand(socket, command, eventName, notification, filteredData.data, next);
|
||||
},
|
||||
], callback);
|
||||
/*
|
||||
hooks:
|
||||
filter:post.upvote
|
||||
filter:post.downvote
|
||||
filter:post.unvote
|
||||
filter:post.bookmark
|
||||
filter:post.unbookmark
|
||||
*/
|
||||
const filteredData = await plugins.fireHook('filter:post.' + command, { data: data, uid: socket.uid });
|
||||
return await executeCommand(socket, command, eventName, notification, filteredData.data);
|
||||
};
|
||||
|
||||
function executeCommand(socket, command, eventName, notification, data, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts[command](data.pid, socket.uid, next);
|
||||
},
|
||||
function (result, next) {
|
||||
if (result && eventName) {
|
||||
websockets.in('uid_' + socket.uid).emit('posts.' + command, result);
|
||||
websockets.in(data.room_id).emit('event:' + eventName, result);
|
||||
}
|
||||
|
||||
if (result && command === 'upvote') {
|
||||
socketHelpers.upvote(result, notification);
|
||||
} else if (result && notification) {
|
||||
socketHelpers.sendNotificationToPostOwner(data.pid, socket.uid, command, notification);
|
||||
} else if (result && command === 'unvote') {
|
||||
socketHelpers.rescindUpvoteNotification(data.pid, socket.uid);
|
||||
}
|
||||
next(null, result);
|
||||
},
|
||||
], callback);
|
||||
async function executeCommand(socket, command, eventName, notification, data) {
|
||||
const result = await posts[command](data.pid, socket.uid);
|
||||
if (result && eventName) {
|
||||
websockets.in('uid_' + socket.uid).emit('posts.' + command, result);
|
||||
websockets.in(data.room_id).emit('event:' + eventName, result);
|
||||
}
|
||||
if (result && command === 'upvote') {
|
||||
socketHelpers.upvote(result, notification);
|
||||
} else if (result && notification) {
|
||||
socketHelpers.sendNotificationToPostOwner(data.pid, socket.uid, command, notification);
|
||||
} else if (result && command === 'unvote') {
|
||||
socketHelpers.rescindUpvoteNotification(data.pid, socket.uid);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -1,40 +1,31 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var privileges = require('../../privileges');
|
||||
var topics = require('../../topics');
|
||||
var socketHelpers = require('../helpers');
|
||||
const privileges = require('../../privileges');
|
||||
const topics = require('../../topics');
|
||||
const socketHelpers = require('../helpers');
|
||||
|
||||
module.exports = function (SocketPosts) {
|
||||
SocketPosts.movePost = function (socket, data, callback) {
|
||||
SocketPosts.movePosts(socket, { pids: [data.pid], tid: data.tid }, callback);
|
||||
SocketPosts.movePost = async function (socket, data) {
|
||||
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) {
|
||||
return callback(new Error('[[error:not-logged-in]]'));
|
||||
throw new Error('[[error:not-logged-in]]');
|
||||
}
|
||||
|
||||
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);
|
||||
},
|
||||
function (next) {
|
||||
socketHelpers.sendNotificationToPostOwner(pid, socket.uid, 'move', 'notifications:moved_your_post');
|
||||
next();
|
||||
},
|
||||
], next);
|
||||
}, callback);
|
||||
for (const pid of data.pids) {
|
||||
/* eslint-disable no-await-in-loop */
|
||||
const canMove = await privileges.posts.canMove(pid, socket.uid);
|
||||
if (!canMove) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
await topics.movePostToTopic(socket.uid, pid, data.tid);
|
||||
socketHelpers.sendNotificationToPostOwner(pid, socket.uid, 'move', 'notifications:moved_your_post');
|
||||
}
|
||||
};
|
||||
};
|
||||
|
@ -1,103 +1,74 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
|
||||
var db = require('../../database');
|
||||
var user = require('../../user');
|
||||
var posts = require('../../posts');
|
||||
var privileges = require('../../privileges');
|
||||
var meta = require('../../meta');
|
||||
var helpers = require('./helpers');
|
||||
const db = require('../../database');
|
||||
const user = require('../../user');
|
||||
const posts = require('../../posts');
|
||||
const privileges = require('../../privileges');
|
||||
const meta = require('../../meta');
|
||||
const helpers = require('./helpers');
|
||||
|
||||
module.exports = function (SocketPosts) {
|
||||
SocketPosts.getVoters = function (socket, data, callback) {
|
||||
SocketPosts.getVoters = async function (socket, data) {
|
||||
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'];
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (meta.config.votesArePublic) {
|
||||
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 canSeeVotes = meta.config.votesArePublic || await privileges.categories.isAdminOrMod(data.cid, socket.uid);
|
||||
if (!canSeeVotes) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
const [upvoteUids, downvoteUids] = await Promise.all([
|
||||
db.getSetMembers('pid:' + data.pid + ':upvote'),
|
||||
showDownvotes ? db.getSetMembers('pid:' + data.pid + ':downvote') : [],
|
||||
]);
|
||||
|
||||
const [upvoters, downvoters] = await Promise.all([
|
||||
user.getUsersFields(upvoteUids, ['username', 'userslug', 'picture']),
|
||||
user.getUsersFields(downvoteUids, ['username', 'userslug', 'picture']),
|
||||
]);
|
||||
|
||||
async.parallel({
|
||||
upvoteUids: function (next) {
|
||||
db.getSetMembers('pid:' + data.pid + ':upvote', next);
|
||||
},
|
||||
downvoteUids: function (next) {
|
||||
if (!showDownvotes) {
|
||||
return setImmediate(next, null, []);
|
||||
}
|
||||
db.getSetMembers('pid:' + data.pid + ':downvote', next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
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);
|
||||
return {
|
||||
upvoteCount: upvoters.length,
|
||||
downvoteCount: downvoters.length,
|
||||
showDownvotes: showDownvotes,
|
||||
upvoters: upvoters,
|
||||
downvoters: downvoters,
|
||||
};
|
||||
};
|
||||
|
||||
SocketPosts.getUpvoters = function (socket, pids, callback) {
|
||||
SocketPosts.getUpvoters = async function (socket, pids) {
|
||||
if (!Array.isArray(pids)) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
const data = await posts.getUpvotedUidsByPids(pids);
|
||||
if (!data.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts.getUpvotedUidsByPids(pids, next);
|
||||
},
|
||||
function (data, next) {
|
||||
if (!data.length) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
async.map(data, function (uids, next) {
|
||||
var otherCount = 0;
|
||||
if (uids.length > 6) {
|
||||
otherCount = uids.length - 5;
|
||||
uids = uids.slice(0, 5);
|
||||
}
|
||||
user.getUsernamesByUids(uids, function (err, usernames) {
|
||||
next(err, {
|
||||
otherCount: otherCount,
|
||||
usernames: usernames,
|
||||
});
|
||||
});
|
||||
}, next);
|
||||
},
|
||||
], callback);
|
||||
const result = await Promise.all(data.map(async function (uids) {
|
||||
let otherCount = 0;
|
||||
if (uids.length > 6) {
|
||||
otherCount = uids.length - 5;
|
||||
uids = uids.slice(0, 5);
|
||||
}
|
||||
const usernames = await user.getUsernamesByUids(uids);
|
||||
return {
|
||||
otherCount: otherCount,
|
||||
usernames: usernames,
|
||||
};
|
||||
}));
|
||||
return result;
|
||||
};
|
||||
|
||||
SocketPosts.upvote = function (socket, data, callback) {
|
||||
helpers.postCommand(socket, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data, callback);
|
||||
SocketPosts.upvote = async function (socket, data) {
|
||||
return await helpers.postCommand(socket, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data);
|
||||
};
|
||||
|
||||
SocketPosts.downvote = function (socket, data, callback) {
|
||||
helpers.postCommand(socket, 'downvote', 'voted', '', data, callback);
|
||||
SocketPosts.downvote = async function (socket, data) {
|
||||
return await helpers.postCommand(socket, 'downvote', 'voted', '', data);
|
||||
};
|
||||
|
||||
SocketPosts.unvote = function (socket, data, callback) {
|
||||
helpers.postCommand(socket, 'unvote', 'voted', '', data, callback);
|
||||
SocketPosts.unvote = async function (socket, data) {
|
||||
return await helpers.postCommand(socket, 'unvote', 'voted', '', data);
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue