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

155 lines
4.3 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');
var meta = require('../meta');
var topics = require('../topics');
var user = require('../user');
var websockets = require('./index');
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);
SocketPosts.reply = function (socket, data, callback) {
2015-10-02 18:55:23 -04:00
if (!data || !data.tid || !data.content) {
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;
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-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,
};
2017-02-23 17:02:54 +03:00
next(null, postData);
2017-02-23 17:02:54 +03:00
websockets.in('uid_' + socket.uid).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);
2015-01-31 15:42:10 -05:00
};
2014-09-18 17:09:40 -04:00
SocketPosts.getRawPost = function (socket, pid, callback) {
2014-05-08 13:24:34 -04:00
async.waterfall([
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
},
function (canRead, next) {
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
},
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
};
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) {
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)) {
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;
2016-11-10 13:37:34 +03:00
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);
2017-02-17 19:31:21 -07:00
},
], callback);
};