Files
NodeBB/src/socket.io/posts.js

261 lines
6.9 KiB
JavaScript
Raw Normal View History

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');
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');
const categories = require('../categories');
2016-03-08 11:24:32 +02:00
var user = require('../user');
var socketHelpers = require('./helpers');
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;
require('./posts/edit')(SocketPosts);
require('./posts/move')(SocketPosts);
2016-10-08 19:09:48 +03:00
require('./posts/votes')(SocketPosts);
require('./posts/bookmarks')(SocketPosts);
require('./posts/tools')(SocketPosts);
2018-02-16 16:41:06 -05:00
require('./posts/diffs')(SocketPosts);
SocketPosts.reply = function (socket, data, callback) {
if (!data || !data.tid || (meta.config.minimumPostLength !== 0 && !data.content)) {
return callback(new Error('[[error:invalid-data]]'));
2014-01-16 22:06:23 -05:00
}
socketHelpers.setDefaultPostData(data, socket);
2014-02-22 17:56:13 -05:00
async.waterfall([
2018-05-27 12:45:33 -04:00
function (next) {
meta.blacklist.test(data.req.ip, next);
},
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': meta.config['reputation:disabled'] === 1,
'downvote:disabled': meta.config['downvote:disabled'] === 1,
2017-02-23 17:02:54 +03:00
};
2017-02-23 17:02:54 +03:00
next(null, postData);
2018-04-25 12:45:25 -04:00
socket.emit('event:new_post', result);
2017-02-23 17:02:54 +03:00
user.updateOnlineUsers(socket.uid);
2017-02-23 17:02:54 +03:00
socketHelpers.notifyNew(socket.uid, 'newPost', result);
},
2017-02-23 17:02:54 +03:00
], callback);
}
2014-09-18 17:09:40 -04:00
SocketPosts.getRawPost = function (socket, pid, callback) {
async.waterfall([
function (next) {
privileges.posts.can('topics: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 (postData.deleted) {
return next(new Error('[[error:no-post]]'));
}
next(null, postData.content);
},
], callback);
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);
}
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);
};
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
};
SocketPosts.loadMoreBookmarks = function (socket, data, callback) {
2016-10-08 19:09:48 +03:00
loadMorePosts('uid:' + data.uid + ':bookmarks', socket.uid, data, callback);
};
SocketPosts.loadMoreUserPosts = function (socket, data, callback) {
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);
};
SocketPosts.loadMoreBestPosts = function (socket, data, callback) {
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);
};
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)) {
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
posts.getPostSummariesFromSet(set, uid, start, stop, callback);
}
2014-03-11 21:52:22 -04: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
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
};
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]]'));
}
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) {
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
},
}, next);
2016-10-31 11:20:52 +03:00
},
function (results, next) {
postPrivileges = results.privileges;
topics.addPostData(results.posts, socket.uid, next);
},
function (postData, next) {
postData.forEach(function (postData, index) {
posts.modifyPostByPrivilege(postData, postPrivileges[index]);
});
postData = postData.filter(function (postData, index) {
2016-11-10 13:37:34 +03:00
return postData && postPrivileges[index].read;
});
next(null, postData);
},
], callback);
};
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
};
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-10-31 16:04:25 -04:00
function (canEditQueue, next) {
if (!canEditQueue) {
return callback(new Error('[[error:no-privileges]]'));
}
method(data.id, next);
},
], callback);
}