You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.4 KiB
JavaScript

'use strict';
var async = require('async');
var posts = require('../posts');
var privileges = require('../privileges');
var meta = require('../meta');
var topics = require('../topics');
var user = require('../user');
var websockets = require('./index');
var socketHelpers = require('./helpers');
var utils = require('../../public/src/utils');
var apiController = require('../controllers/api');
var SocketPosts = {};
require('./posts/edit')(SocketPosts);
require('./posts/move')(SocketPosts);
require('./posts/votes')(SocketPosts);
require('./posts/bookmarks')(SocketPosts);
require('./posts/tools')(SocketPosts);
SocketPosts.reply = function (socket, data, callback) {
if (!data || !data.tid || !data.content) {
return callback(new Error('[[error:invalid-data]]'));
}
data.uid = socket.uid;
data.req = websockets.reqFromSocket(socket);
data.timestamp = Date.now();
async.waterfall([
function (next) {
topics.reply(data, next);
},
function (postData, next) {
var result = {
posts: [postData],
'reputation:disabled': parseInt(meta.config['reputation:disabled'], 10) === 1,
'downvote:disabled': parseInt(meta.config['downvote:disabled'], 10) === 1,
};
next(null, postData);
websockets.in('uid_' + socket.uid).emit('event:new_post', result);
user.updateOnlineUsers(socket.uid);
socketHelpers.notifyNew(socket.uid, 'newPost', result);
},
], callback);
};
SocketPosts.getRawPost = function (socket, pid, callback) {
async.waterfall([
function (next) {
privileges.posts.can('read', pid, socket.uid, next);
},
function (canRead, next) {
if (!canRead) {
return next(new Error('[[error:no-privileges]]'));
}
posts.getPostFields(pid, ['content', 'deleted'], next);
},
function (postData, next) {
if (parseInt(postData.deleted, 10) === 1) {
return next(new Error('[[error:no-post]]'));
}
next(null, postData.content);
},
], callback);
};
SocketPosts.getPost = function (socket, pid, callback) {
apiController.getPostData(pid, socket.uid, callback);
};
SocketPosts.loadMoreBookmarks = function (socket, data, callback) {
loadMorePosts('uid:' + data.uid + ':bookmarks', socket.uid, data, callback);
};
SocketPosts.loadMoreUserPosts = function (socket, data, callback) {
loadMorePosts('uid:' + data.uid + ':posts', socket.uid, data, callback);
};
SocketPosts.loadMoreBestPosts = function (socket, data, callback) {
loadMorePosts('uid:' + data.uid + ':posts:votes', socket.uid, data, callback);
};
SocketPosts.loadMoreUpVotedPosts = function (socket, data, callback) {
loadMorePosts('uid:' + data.uid + ':upvote', socket.uid, data, callback);
};
SocketPosts.loadMoreDownVotedPosts = function (socket, data, callback) {
loadMorePosts('uid:' + data.uid + ':downvote', socket.uid, data, callback);
};
function loadMorePosts(set, uid, data, callback) {
if (!data || !utils.isNumber(data.uid) || !utils.isNumber(data.after)) {
return callback(new Error('[[error:invalid-data]]'));
}
var start = Math.max(0, parseInt(data.after, 10));
var stop = start + 9;
posts.getPostSummariesFromSet(set, uid, start, stop, callback);
}
SocketPosts.getCategory = function (socket, pid, callback) {
posts.getCidByPid(pid, callback);
};
SocketPosts.getPidIndex = function (socket, data, callback) {
if (!data) {
return callback(new Error('[[error:invalid-data]]'));
}
posts.getPidIndex(data.pid, data.tid, data.topicPostSort, callback);
};
SocketPosts.getReplies = function (socket, pid, callback) {
if (!utils.isNumber(pid)) {
return callback(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;
results.posts = results.posts.filter(function (postData, index) {
return postData && postPrivileges[index].read;
});
topics.addPostData(results.posts, socket.uid, next);
},
function (postData, next) {
postData.forEach(function (postData) {
posts.modifyPostByPrivilege(postData, postPrivileges.isAdminOrMod);
});
next(null, postData);
},
], callback);
};
module.exports = SocketPosts;