2017-02-18 01:56:23 -07:00
|
|
|
'use strict';
|
2014-03-11 14:48:35 -04:00
|
|
|
|
2017-02-23 17:12:46 +03:00
|
|
|
var async = require('async');
|
2015-09-25 15:09:25 -04:00
|
|
|
|
2016-03-08 11:24:32 +02:00
|
|
|
var posts = require('../posts');
|
|
|
|
|
var privileges = require('../privileges');
|
2018-05-30 14:21:03 -04:00
|
|
|
var plugins = require('../plugins');
|
2016-03-08 11:24:32 +02:00
|
|
|
var meta = require('../meta');
|
|
|
|
|
var topics = require('../topics');
|
|
|
|
|
var user = require('../user');
|
|
|
|
|
var websockets = require('./index');
|
|
|
|
|
var socketHelpers = require('./helpers');
|
2017-04-08 20:22:21 -06:00
|
|
|
var utils = require('../utils');
|
2014-01-10 16:00:03 -05:00
|
|
|
|
2016-03-08 11:24:32 +02:00
|
|
|
var apiController = require('../controllers/api');
|
|
|
|
|
|
2017-05-26 00:02:20 -04:00
|
|
|
var SocketPosts = module.exports;
|
2014-04-26 03:00:56 -04:00
|
|
|
|
2015-09-25 15:09:25 -04:00
|
|
|
require('./posts/edit')(SocketPosts);
|
|
|
|
|
require('./posts/move')(SocketPosts);
|
2016-10-08 19:09:48 +03:00
|
|
|
require('./posts/votes')(SocketPosts);
|
|
|
|
|
require('./posts/bookmarks')(SocketPosts);
|
2015-09-25 15:09:25 -04:00
|
|
|
require('./posts/tools')(SocketPosts);
|
2018-02-16 16:41:06 -05:00
|
|
|
require('./posts/diffs')(SocketPosts);
|
2015-09-25 15:09:25 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
SocketPosts.reply = function (socket, data, callback) {
|
2017-06-28 12:25:43 -04:00
|
|
|
if (!data || !data.tid || (parseInt(meta.config.minimumPostLength, 10) !== 0 && !data.content)) {
|
2014-04-09 14:12:46 -04:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
2014-01-16 22:06:23 -05:00
|
|
|
}
|
|
|
|
|
|
2014-02-22 17:56:13 -05:00
|
|
|
data.uid = socket.uid;
|
2014-04-27 00:47:08 -04:00
|
|
|
data.req = websockets.reqFromSocket(socket);
|
2016-08-16 22:27:21 +03:00
|
|
|
data.timestamp = Date.now();
|
2014-02-22 17:56:13 -05:00
|
|
|
|
2017-08-15 12:59:40 -04:00
|
|
|
async.waterfall([
|
2018-05-27 12:45:33 -04:00
|
|
|
function (next) {
|
|
|
|
|
meta.blacklist.test(data.req.ip, next);
|
|
|
|
|
},
|
2017-08-15 12:59:40 -04:00
|
|
|
function (next) {
|
|
|
|
|
posts.shouldQueue(socket.uid, data, next);
|
|
|
|
|
},
|
|
|
|
|
function (shouldQueue, next) {
|
|
|
|
|
if (shouldQueue) {
|
|
|
|
|
posts.addToQueue(data, next);
|
|
|
|
|
} else {
|
|
|
|
|
postReply(socket, data, next);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function postReply(socket, data, callback) {
|
2017-02-23 17:02:54 +03:00
|
|
|
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,
|
|
|
|
|
};
|
2014-09-14 14:19:36 -04:00
|
|
|
|
2017-02-23 17:02:54 +03:00
|
|
|
next(null, postData);
|
2014-09-14 14:19:36 -04:00
|
|
|
|
2018-04-25 12:45:25 -04:00
|
|
|
socket.emit('event:new_post', result);
|
2014-09-14 14:19:36 -04:00
|
|
|
|
2017-02-23 17:02:54 +03:00
|
|
|
user.updateOnlineUsers(socket.uid);
|
2015-03-25 15:51:11 -04:00
|
|
|
|
2017-02-23 17:02:54 +03:00
|
|
|
socketHelpers.notifyNew(socket.uid, 'newPost', result);
|
2017-02-23 18:31:49 -07:00
|
|
|
},
|
2017-02-23 17:02:54 +03:00
|
|
|
], callback);
|
2017-08-15 12:59:40 -04:00
|
|
|
}
|
2014-09-18 17:09:40 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
SocketPosts.getRawPost = function (socket, pid, callback) {
|
2014-05-08 13:24:34 -04:00
|
|
|
async.waterfall([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2014-05-17 18:59:34 -04:00
|
|
|
privileges.posts.can('read', pid, socket.uid, next);
|
2014-05-08 13:24:34 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (canRead, next) {
|
2014-05-14 17:53:23 -04:00
|
|
|
if (!canRead) {
|
2014-05-08 13:24:34 -04:00
|
|
|
return next(new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
|
|
|
|
posts.getPostFields(pid, ['content', 'deleted'], next);
|
2014-08-02 16:41:44 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (postData, next) {
|
2014-08-02 16:41:44 -04:00
|
|
|
if (parseInt(postData.deleted, 10) === 1) {
|
|
|
|
|
return next(new Error('[[error:no-post]]'));
|
|
|
|
|
}
|
|
|
|
|
next(null, postData.content);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2014-08-02 16:41:44 -04:00
|
|
|
], callback);
|
2014-01-10 16:00:03 -05:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
SocketPosts.getPost = function (socket, pid, callback) {
|
2016-07-01 13:01:09 +03:00
|
|
|
apiController.getPostData(pid, socket.uid, callback);
|
2016-03-08 11:24:32 +02:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
SocketPosts.loadMoreBookmarks = function (socket, data, callback) {
|
2016-10-08 19:09:48 +03:00
|
|
|
loadMorePosts('uid:' + data.uid + ':bookmarks', socket.uid, data, callback);
|
2014-02-03 19:24:27 -05:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
SocketPosts.loadMoreUserPosts = function (socket, data, callback) {
|
2015-03-31 23:40:58 -04:00
|
|
|
loadMorePosts('uid:' + data.uid + ':posts', socket.uid, data, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
SocketPosts.loadMoreBestPosts = function (socket, data, callback) {
|
2016-01-14 13:47:26 +02:00
|
|
|
loadMorePosts('uid:' + data.uid + ':posts:votes', socket.uid, data, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
SocketPosts.loadMoreUpVotedPosts = function (socket, data, callback) {
|
2016-01-14 13:47:26 +02:00
|
|
|
loadMorePosts('uid:' + data.uid + ':upvote', socket.uid, data, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
SocketPosts.loadMoreDownVotedPosts = function (socket, data, callback) {
|
2016-01-14 13:47:26 +02:00
|
|
|
loadMorePosts('uid:' + data.uid + ':downvote', socket.uid, data, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-31 23:40:58 -04:00
|
|
|
function loadMorePosts(set, uid, data, callback) {
|
|
|
|
|
if (!data || !utils.isNumber(data.uid) || !utils.isNumber(data.after)) {
|
2014-04-09 21:40:39 -04:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
2014-02-04 17:31:05 -05:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 20:35:49 +03:00
|
|
|
var start = Math.max(0, parseInt(data.after, 10));
|
|
|
|
|
var stop = start + 9;
|
2014-02-04 17:31:05 -05:00
|
|
|
|
2015-05-11 15:43:57 -04:00
|
|
|
posts.getPostSummariesFromSet(set, uid, start, stop, callback);
|
2015-03-31 23:40:58 -04:00
|
|
|
}
|
2014-03-11 21:52:22 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
SocketPosts.getCategory = function (socket, pid, callback) {
|
2014-03-13 20:24:04 -04:00
|
|
|
posts.getCidByPid(pid, callback);
|
2014-03-24 14:30:11 -04:00
|
|
|
};
|
2014-03-11 18:46:16 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
SocketPosts.getPidIndex = function (socket, data, callback) {
|
2015-09-14 21:04:56 -04:00
|
|
|
if (!data) {
|
|
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
posts.getPidIndex(data.pid, data.tid, data.topicPostSort, callback);
|
2014-08-27 15:03:36 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-27 18:45:21 -05:00
|
|
|
SocketPosts.getReplies = function (socket, pid, callback) {
|
|
|
|
|
if (!utils.isNumber(pid)) {
|
2017-02-23 17:02:54 +03:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
2016-10-27 18:45:21 -05:00
|
|
|
}
|
2016-11-02 13:21:51 +03:00
|
|
|
var postPrivileges;
|
2016-10-31 11:20:52 +03:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
posts.getPidsFromSet('pid:' + pid + ':replies', 0, -1, false, next);
|
|
|
|
|
},
|
|
|
|
|
function (pids, next) {
|
2016-11-02 13:21:51 +03:00
|
|
|
async.parallel({
|
|
|
|
|
posts: function (next) {
|
|
|
|
|
posts.getPostsByPids(pids, socket.uid, next);
|
|
|
|
|
},
|
|
|
|
|
privileges: function (next) {
|
|
|
|
|
privileges.posts.get(pids, socket.uid, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-11-02 13:21:51 +03:00
|
|
|
}, next);
|
2016-10-31 11:20:52 +03:00
|
|
|
},
|
2016-11-02 13:21:51 +03:00
|
|
|
function (results, next) {
|
|
|
|
|
postPrivileges = results.privileges;
|
2018-06-08 17:39:17 -04:00
|
|
|
results.posts.forEach(function (postData, index) {
|
|
|
|
|
posts.modifyPostByPrivilege(postData, postPrivileges[index]);
|
|
|
|
|
});
|
2016-11-10 13:37:34 +03:00
|
|
|
results.posts = results.posts.filter(function (postData, index) {
|
|
|
|
|
return postData && postPrivileges[index].read;
|
|
|
|
|
});
|
2016-11-02 13:21:51 +03:00
|
|
|
topics.addPostData(results.posts, socket.uid, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
2016-10-27 18:45:21 -05:00
|
|
|
};
|
2017-08-15 12:59:40 -04:00
|
|
|
|
|
|
|
|
SocketPosts.accept = function (socket, data, callback) {
|
|
|
|
|
acceptOrReject(posts.submitFromQueue, socket, data, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SocketPosts.reject = function (socket, data, callback) {
|
|
|
|
|
acceptOrReject(posts.removeFromQueue, socket, data, callback);
|
|
|
|
|
};
|
2017-10-31 16:04:25 -04:00
|
|
|
|
2017-10-31 10:53:28 -04:00
|
|
|
SocketPosts.editQueuedContent = function (socket, data, callback) {
|
|
|
|
|
if (!data || !data.id || !data.content) {
|
|
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
2018-05-30 14:21:03 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
posts.editQueuedContent(socket.uid, data.id, data.content, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
plugins.fireHook('filter:parse.post', { postData: data }, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
2017-10-31 10:53:28 -04:00
|
|
|
};
|
2017-08-15 12:59:40 -04:00
|
|
|
|
|
|
|
|
function acceptOrReject(method, socket, data, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
2017-10-31 16:04:25 -04:00
|
|
|
posts.canEditQueue(socket.uid, data.id, next);
|
2017-08-15 12:59:40 -04:00
|
|
|
},
|
2017-10-31 16:04:25 -04:00
|
|
|
function (canEditQueue, next) {
|
|
|
|
|
if (!canEditQueue) {
|
2017-08-15 12:59:40 -04:00
|
|
|
return callback(new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
method(data.id, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
}
|