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.

124 lines
3.3 KiB
JavaScript

11 years ago
"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 socketTopics = require('./topics');
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/favourites')(SocketPosts);
require('./posts/tools')(SocketPosts);
require('./posts/flag')(SocketPosts);
SocketPosts.reply = function(socket, data, callback) {
10 years ago
if (!data || !data.tid || !data.content) {
return callback(new Error('[[error:invalid-data]]'));
11 years ago
}
data.uid = socket.uid;
data.req = websockets.reqFromSocket(socket);
topics.reply(data, function(err, postData) {
if (err) {
11 years ago
return callback(err);
}
var result = {
posts: [postData],
'reputation:disabled': parseInt(meta.config['reputation:disabled'], 10) === 1,
'downvote:disabled': parseInt(meta.config['downvote:disabled'], 10) === 1,
};
callback(null, postData);
socket.emit('event:new_post', result);
user.updateOnlineUsers(socket.uid);
socketHelpers.notifyNew(socket.uid, 'newPost', result);
});
};
11 years ago
SocketPosts.getRawPost = function(socket, pid, callback) {
11 years ago
async.waterfall([
function(next) {
11 years ago
privileges.posts.can('read', pid, socket.uid, next);
11 years ago
},
function(canRead, next) {
if (!canRead) {
11 years ago
return next(new Error('[[error:no-privileges]]'));
}
posts.getPostFields(pid, ['content', 'deleted'], next);
11 years ago
},
function(postData, next) {
if (parseInt(postData.deleted, 10) === 1) {
return next(new Error('[[error:no-post]]'));
}
next(null, postData.content);
11 years ago
}
11 years ago
], callback);
};
SocketPosts.getPost = function(socket, pid, callback) {
9 years ago
apiController.getPostData(pid, socket.uid, callback);
};
SocketPosts.loadMoreFavourites = function(socket, data, callback) {
loadMorePosts('uid:' + data.uid + ':favourites', 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]]'));
}
9 years ago
var start = Math.max(0, parseInt(data.after, 10));
var stop = start + 9;
posts.getPostSummariesFromSet(set, uid, start, stop, callback);
}
11 years ago
SocketPosts.getCategory = function(socket, pid, callback) {
posts.getCidByPid(pid, callback);
11 years ago
};
10 years ago
SocketPosts.getPidIndex = function(socket, data, callback) {
if (!data) {
return callback(new Error('[[error:invalid-data]]'));
}
posts.getPidIndex(data.pid, data.tid, data.topicPostSort, callback);
};
module.exports = SocketPosts;