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');
|
2019-06-24 15:21:43 -04:00
|
|
|
const categories = require('../categories');
|
2016-03-08 11:24:32 +02:00
|
|
|
var user = require('../user');
|
|
|
|
|
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) {
|
2018-10-21 16:47:51 -04:00
|
|
|
if (!data || !data.tid || (meta.config.minimumPostLength !== 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
|
|
|
}
|
|
|
|
|
|
2019-07-30 13:19:50 -04:00
|
|
|
socketHelpers.setDefaultPostData(data, socket);
|
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],
|
2018-10-21 16:47:51 -04:00
|
|
|
'reputation:disabled': meta.config['reputation:disabled'] === 1,
|
|
|
|
|
'downvote:disabled': meta.config['downvote:disabled'] === 1,
|
2017-02-23 17:02:54 +03:00
|
|
|
};
|
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
|
|
|
|
2019-08-07 17:38:23 -04:00
|
|
|
SocketPosts.getRawPost = async function (socket, pid) {
|
|
|
|
|
const canRead = await privileges.posts.can('topics:read', pid, socket.uid);
|
|
|
|
|
if (!canRead) {
|
|
|
|
|
throw new Error('[[error:no-privileges]]');
|
|
|
|
|
}
|
2019-08-07 14:10:19 -04:00
|
|
|
|
2019-08-07 17:38:23 -04:00
|
|
|
const postData = await posts.getPostFields(pid, ['content', 'deleted']);
|
|
|
|
|
if (postData.deleted) {
|
|
|
|
|
throw new Error('[[error:no-post]]');
|
|
|
|
|
}
|
|
|
|
|
postData.pid = pid;
|
|
|
|
|
const result = await plugins.fireHook('filter:post.getRawPost', { uid: socket.uid, postData: postData });
|
|
|
|
|
return result.postData.content;
|
2014-01-10 16:00:03 -05:00
|
|
|
};
|
|
|
|
|
|
2018-11-14 13:53:35 -05:00
|
|
|
SocketPosts.getTimestampByIndex = function (socket, data, callback) {
|
|
|
|
|
var pid;
|
|
|
|
|
var db = require('../database');
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
if (data.index < 0) {
|
|
|
|
|
data.index = 0;
|
|
|
|
|
}
|
|
|
|
|
if (data.index === 0) {
|
|
|
|
|
topics.getTopicField(data.tid, 'mainPid', next);
|
|
|
|
|
} else {
|
|
|
|
|
db.getSortedSetRange('tid:' + data.tid + ':posts', data.index - 1, data.index - 1, next);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
function (_pid, next) {
|
|
|
|
|
pid = Array.isArray(_pid) ? _pid[0] : _pid;
|
|
|
|
|
if (!pid) {
|
|
|
|
|
return callback(null, 0);
|
|
|
|
|
}
|
2019-03-16 14:51:46 -04:00
|
|
|
privileges.posts.can('topics:read', pid, socket.uid, next);
|
2018-11-14 13:53:35 -05:00
|
|
|
},
|
|
|
|
|
function (canRead, next) {
|
|
|
|
|
if (!canRead) {
|
|
|
|
|
return next(new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
|
|
|
|
posts.getPostFields(pid, ['timestamp'], next);
|
|
|
|
|
},
|
|
|
|
|
function (postData, next) {
|
|
|
|
|
next(null, postData.timestamp);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
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) {
|
2019-06-24 15:21:43 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
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');
|
|
|
|
|
loadMorePosts(keys, socket.uid, data, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
2015-03-31 23:40:58 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
SocketPosts.loadMoreBestPosts = function (socket, data, callback) {
|
2019-06-24 15:21:43 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
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');
|
|
|
|
|
loadMorePosts(keys, socket.uid, data, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
2016-01-14 13:47:26 +02:00
|
|
|
};
|
|
|
|
|
|
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:46:49 -04:00
|
|
|
|
|
|
|
|
topics.addPostData(results.posts, socket.uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (postData, next) {
|
|
|
|
|
postData.forEach(function (postData, index) {
|
2018-06-08 17:39:17 -04:00
|
|
|
posts.modifyPostByPrivilege(postData, postPrivileges[index]);
|
|
|
|
|
});
|
2018-06-08 17:46:49 -04:00
|
|
|
postData = postData.filter(function (postData, index) {
|
2016-11-10 13:37:34 +03:00
|
|
|
return postData && postPrivileges[index].read;
|
|
|
|
|
});
|
2018-06-08 17:46:49 -04:00
|
|
|
next(null, postData);
|
2016-11-02 13:21:51 +03:00
|
|
|
},
|
|
|
|
|
], 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);
|
|
|
|
|
}
|
2019-08-07 17:38:23 -04:00
|
|
|
|
|
|
|
|
require('../promisify')(SocketPosts);
|