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

149 lines
4.0 KiB
JavaScript
Raw Normal View History

2014-01-10 16:00:03 -05:00
var posts = require('../posts'),
meta = require('../meta'),
topics = require('../topics'),
favourites = require('../favourites'),
postTools = require('../postTools'),
SocketPosts = {};
2014-01-16 15:34:43 -05:00
SocketPosts.reply = function(socket, data, callback) {
if (socket.uid < 1 && parseInt(meta.config.allowGuestPosting, 10) === 0) {
socket.emit('event:alert', {
2014-01-10 16:00:03 -05:00
title: 'Reply Unsuccessful',
message: 'You don&apos;t seem to be logged in, so you cannot reply.',
type: 'danger',
timeout: 2000
});
return;
}
2014-01-16 15:34:43 -05:00
topics.reply(data.topic_id, socket.uid, data.content, function(err, postData) {
2014-01-10 16:00:03 -05:00
if(err) {
if (err.message === 'content-too-short') {
2014-01-16 15:34:43 -05:00
module.parent.exports.emitContentTooShortAlert(socket);
2014-01-10 16:00:03 -05:00
} else if (err.message === 'too-many-posts') {
2014-01-16 15:34:43 -05:00
module.parent.exports.emitTooManyPostsAlert(socket);
2014-01-10 16:00:03 -05:00
} else if (err.message === 'reply-error') {
2014-01-16 15:34:43 -05:00
socket.emit('event:alert', {
2014-01-10 16:00:03 -05:00
title: 'Reply Unsuccessful',
message: 'Your reply could not be posted at this time. Please try again later.',
type: 'warning',
timeout: 2000
});
} else if (err.message === 'no-privileges') {
2014-01-16 15:34:43 -05:00
socket.emit('event:alert', {
2014-01-10 16:00:03 -05:00
title: 'Unable to post',
message: 'You do not have posting privileges in this category.',
type: 'danger',
timeout: 7500
});
}
return;
}
if (postData) {
2014-01-10 16:00:03 -05:00
module.parent.exports.emitTopicPostStats();
2014-01-16 15:34:43 -05:00
socket.emit('event:alert', {
2014-01-10 16:00:03 -05:00
title: 'Reply Successful',
message: 'You have successfully replied. Click here to view your reply.',
type: 'success',
timeout: 2000
});
var socketData = {
posts: [postData]
};
2014-01-16 15:34:43 -05:00
socket.server.sockets.in('topic_' + postData.tid).emit('event:new_post', socketData);
socket.server.sockets.in('recent_posts').emit('event:new_post', socketData);
socket.server.sockets.in('user/' + postData.uid).emit('event:new_post', socketData);
callback();
2014-01-10 16:00:03 -05:00
}
});
};
2014-01-16 15:34:43 -05:00
SocketPosts.favourite = function(socket, data) {
favourites.favourite(data.pid, data.room_id, socket.uid, socket);
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:34:43 -05:00
SocketPosts.unfavourite = function(socket, data) {
favourites.unfavourite(data.pid, data.room_id, socket.uid, socket);
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:34:43 -05:00
SocketPosts.uploadImage = function(socket, data, callback) {
2014-01-10 16:00:03 -05:00
posts.uploadPostImage(data, callback);
};
2014-01-16 15:34:43 -05:00
SocketPosts.uploadFile = function(socket, data, callback) {
2014-01-10 16:00:03 -05:00
posts.uploadPostFile(data, callback);
};
2014-01-16 15:34:43 -05:00
SocketPosts.getRawPost = function(socket, data, callback) {
2014-01-10 16:00:03 -05:00
posts.getPostField(data.pid, 'content', function(err, raw) {
callback({
post: raw
});
});
};
2014-01-16 15:34:43 -05:00
SocketPosts.edit = function(socket, data, callback) {
if(!socket.uid) {
socket.emit('event:alert', {
2014-01-10 16:00:03 -05:00
title: 'Can&apos;t edit',
message: 'Guests can&apos;t edit posts!',
type: 'warning',
timeout: 2000
});
return;
} else if (!data.title || data.title.length < parseInt(meta.config.minimumTitleLength, 10)) {
2014-01-16 15:34:43 -05:00
topics.emitTitleTooShortAlert(socket);
2014-01-10 16:00:03 -05:00
return;
} else if (!data.content || data.content.length < parseInt(meta.config.minimumPostLength, 10)) {
2014-01-16 15:34:43 -05:00
module.parent.exports.emitContentTooShortAlert(socket);
2014-01-10 16:00:03 -05:00
return;
}
2014-01-16 15:34:43 -05:00
postTools.edit(socket.uid, data.pid, data.title, data.content, data.images);
callback();
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:34:43 -05:00
SocketPosts.delete = function(socket, data, callback) {
postTools.delete(socket.uid, data.pid, function(err) {
2014-01-10 16:00:03 -05:00
if(err) {
return callback(err);
}
module.parent.exports.emitTopicPostStats();
2014-01-16 15:34:43 -05:00
socket.server.sockets.in('topic_' + data.tid).emit('event:post_deleted', {
2014-01-10 16:00:03 -05:00
pid: data.pid
});
callback(null);
});
};
2014-01-16 15:34:43 -05:00
SocketPosts.restore = function(socket, data, callback) {
postTools.restore(socket.uid, data.pid, function(err) {
2014-01-10 16:00:03 -05:00
if(err) {
return callback(err);
}
module.parent.exports.emitTopicPostStats();
2014-01-16 15:34:43 -05:00
socket.server.sockets.in('topic_' + data.tid).emit('event:post_restored', {
2014-01-10 16:00:03 -05:00
pid: data.pid
});
callback(null);
});
};
2014-01-16 15:34:43 -05:00
SocketPosts.getPrivileges = function(socket, pid, callback) {
postTools.privileges(pid, socket.uid, function(privileges) {
2014-01-10 16:00:03 -05:00
privileges.pid = parseInt(pid);
callback(privileges);
});
};
module.exports = SocketPosts;