diff --git a/public/src/forum/admin/themes.js b/public/src/forum/admin/themes.js index 830e8ff025..2a613ffc96 100644 --- a/public/src/forum/admin/themes.js +++ b/public/src/forum/admin/themes.js @@ -23,7 +23,7 @@ define(function() { cssSrc = parentEl.attr('data-css'), themeId = parentEl.attr('data-theme'); - socket.emit('api:admin.theme.set', { + socket.emit('api:admin.themes.set', { type: themeType, id: themeId, src: cssSrc @@ -47,7 +47,7 @@ define(function() { revertEl.addEventListener('click', function() { bootbox.confirm('Are you sure you wish to remove the custom theme and restore the NodeBB default theme?', function(confirm) { if (confirm) { - socket.emit('api:admin.theme.set', { + socket.emit('api:admin.themes.set', { type: 'local', id: 'nodebb-theme-cerulean' }, function(err) { diff --git a/public/src/forum/admin/users.js b/public/src/forum/admin/users.js index 6d4f428ddb..a1083f9660 100644 --- a/public/src/forum/admin/users.js +++ b/public/src/forum/admin/users.js @@ -242,7 +242,7 @@ define(function() { if (set) { loadingMoreUsers = true; - socket.emit('api:users.loadMore', { + socket.emit('api:user.loadMore', { set: set, after: $('#users-container').children().length }, function(data) { diff --git a/public/src/forum/footer.js b/public/src/forum/footer.js index 71e8c54d1c..959c33592a 100644 --- a/public/src/forum/footer.js +++ b/public/src/forum/footer.js @@ -53,7 +53,7 @@ notifIcon.toggleClass('active', false); } - socket.emit('api:notifications.mark_all_read', null, function() { + socket.emit('api:modules.notifications.mark_all_read', null, function() { notifIcon.toggleClass('active', false); app.refreshTitle(); @@ -80,7 +80,7 @@ } if (target) { var nid = parseInt(target.getAttribute('data-nid')); - if (nid > 0) socket.emit('api:notifications.mark_read', nid); + if (nid > 0) socket.emit('api:modules.notifications.mark_read', nid); } }); @@ -137,7 +137,7 @@ return; } - socket.emit('api:chats.list', function(chats) { + socket.emit('api:modules.chats.list', function(chats) { var chatsFrag = document.createDocumentFragment(), chatEl = document.createElement('li'), numChats = chats.length, diff --git a/public/src/forum/notifications.js b/public/src/forum/notifications.js index de1e053871..1028f78cdf 100644 --- a/public/src/forum/notifications.js +++ b/public/src/forum/notifications.js @@ -14,7 +14,7 @@ define(function() { // Mark all as read button $(markAllReadEl).click(function() { - socket.emit('api:notifications.mark_all_read', {}, function() { + socket.emit('api:modules.notifications.mark_all_read', {}, function() { ajaxify.go('notifications'); app.alert({ alert_id: "notifications:mark_all_read", diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index 89f7641e2c..756a186485 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -723,10 +723,6 @@ define(['composer'], function(composer) { } }); - socket.on('api:post.privileges', function(privileges) { - toggle_mod_tools(privileges.pid, privileges.editable); - }); - function adjust_rep(value, pid, uid) { var post_rep = jQuery('.post_rep_' + pid), user_rep = jQuery('.user_rep_' + uid); @@ -861,11 +857,11 @@ define(['composer'], function(composer) { var postEl = $(document.querySelector('#post-container li[data-pid="' + pid + '"]')); if (postEl[0]) { - quoteEl = $(postEl[0].querySelector('.quote')), - favEl = $(postEl[0].querySelector('.favourite')), - replyEl = $(postEl[0].querySelector('.post_reply')); + quoteEl = postEl.find('.quote'), + favEl = postEl.find('.favourite'), + replyEl = postEl.find('.post_reply'); - socket.once('api:post.privileges', function(privileges) { + socket.emit('api:posts.getPrivileges', pid, function(privileges) { if (privileges.editable) { if (!postEl.hasClass('deleted')) { toggle_post_tools(pid, false); @@ -881,7 +877,6 @@ define(['composer'], function(composer) { } updatePostCount(); }); - socket.emit('api:post.privileges', pid); } } @@ -1073,7 +1068,9 @@ define(['composer'], function(composer) { .fadeIn('slow'); for (var x = 0, numPosts = data.posts.length; x < numPosts; x++) { - socket.emit('api:post.privileges', data.posts[x].pid); + socket.emit('api:posts.getPrivileges', data.posts[x].pid, function(privileges) { + toggle_mod_tools(privileges.pid, privileges.editable); + }); } infiniteLoaderActive = false; diff --git a/public/src/forum/users.js b/public/src/forum/users.js index 5c641a764f..23f2a2c27c 100644 --- a/public/src/forum/users.js +++ b/public/src/forum/users.js @@ -123,7 +123,7 @@ define(function() { function startLoading(set, after, emptyContainer) { loadingMoreUsers = true; - socket.emit('api:users.loadMore', { + socket.emit('api:user.loadMore', { set: set, after: after }, function(data) { diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js index 32d5e6ee8c..21475fe39d 100644 --- a/public/src/modules/chat.js +++ b/public/src/modules/chat.js @@ -123,7 +123,7 @@ define(['taskbar', 'string'], function(taskbar, S) { } function getChatMessages(chatModal, callback) { - socket.emit('api:chats.get', {touid:chatModal.touid}, function(messages) { + socket.emit('api:modules.chats.get', {touid:chatModal.touid}, function(messages) { for(var i = 0; i 0) { titleEl.val(postData.title); titleEl.prop('readOnly', true); - socket.emit('api:composer.editCheck', postData.pid, function(editCheck) { + socket.emit('api:modules.composer.editCheck', postData.pid, function(editCheck) { if (editCheck.titleEditable) { postContainer.find('input').prop('readonly', false); } diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js new file mode 100644 index 0000000000..90924d8b01 --- /dev/null +++ b/src/socket.io/admin.js @@ -0,0 +1,223 @@ +var groups = require('../groups'), + meta = require('../meta'), + plugins = require('../plugins'), + user = require('../user'), + topics = require('../topics'), + CategoryTools = require('../categoryTools'), + admin = { + user: require('../admin/user'), + categories: require('../admin/categories') + }, + + async = require('async'), + + SocketAdmin = {}; + +/* Topics */ + +SocketAdmin.topics = {}; + +SocketAdmin.topics.getMore = function(data, callback) { + topics.getAllTopics(data.limit, data.after, function(err, topics) { + callback(JSON.stringify(topics)); + }); +}; + +/* User */ + +SocketAdmin.user = {}; + +SocketAdmin.user.makeAdmin = function(theirid, sessionData) { + if (sessionData.uid && sessionData.uid > 0) { + admin.user.makeAdmin(sessionData.uid, theirid, sessionData.socket); + } +}; + +SocketAdmin.user.removeAdmin = function(theirid, sessionData) { + if (sessionData.uid && sessionData.uid > 0) { + admin.user.removeAdmin(sessionData.uid, theirid, sessionData.socket); + } +}; + +SocketAdmin.user.createUser = function(user, callback, sessionData) { + if (sessionData.uid && sessionData.uid > 0) { + admin.user.createUser(sessionData.uid, user, callback); + } +}; + +SocketAdmin.user.banUser = function(theirid, sessionData) { + if (sessionData.uid && sessionData.uid > 0) { + admin.user.banUser(sessionData.uid, theirid, sessionData.socket, function(isBanned) { + if(isBanned) { + if(sessionData.userSockets[theirid]) { + for(var i=0; i 0) { + admin.user.unbanUser(sessionData.uid, theirid, sessionData.socket); + } +}; + +SocketAdmin.user.search = function(username, callback, sessionData) { + if (!(sessionData.uid && sessionData.uid > 0)) { + return callback(); + } + + user.search(username, function(data) { + function isAdmin(userData, next) { + user.isAdministrator(userData.uid, function(err, isAdmin) { + if(err) { + return next(err); + } + + userData.administrator = isAdmin?'1':'0'; + next(); + }); + } + + async.each(data, isAdmin, function(err) { + if(err) { + return callback({message: err.message}); + } + + callback(null, data); + }); + }); +}; + +/* Categories */ + +SocketAdmin.categories = {}; + +SocketAdmin.categories.create = function(data, callback) { + categories.create(data, function(err, data) { + callback(err, data); + }); +}; + +SocketAdmin.categories.update = function(data, sessionData) { + admin.categories.update(data, sessionData.socket); +}; + +SocketAdmin.categories.search = function(username, cid, callback, sessionData) { + if (sessionData.uid && sessionData.uid > 0) { + user.search(username, function(data) { + async.map(data, function(userObj, next) { + CategoryTools.privileges(cid, userObj.uid, function(err, privileges) { + if (!err) { + userObj.privileges = privileges; + } else { + winston.error('[socket api:admin.categories.search] Could not retrieve permissions'); + } + + next(null, userObj); + }); + }, function(err, data) { + if (!callback) sessionData.socket.emit('api:admin.categories.search', data); + else callback(null, data); + }); + }); + } else { + if (!callback) sessionData.socket.emit('api:admin.user.search', null); + else callback(); + } +}; + +SocketAdmin.categories.setPrivilege = function(cid, uid, privilege, set, callback) { + var cb = function(err) { + CategoryTools.privileges(cid, uid, callback); + }; + + if (set) { + groups.joinByGroupName('cid:' + cid + ':privileges:' + privilege, uid, cb); + } else { + groups.leaveByGroupName('cid:' + cid + ':privileges:' + privilege, uid, cb); + } +}; + +SocketAdmin.categories.getPrivilegeSettings = function(cid, callback) { + async.parallel({ + "+r": function(next) { + groups.getByGroupName('cid:' + cid + ':privileges:+r', { expand: true }, function(err, groupObj) { + if (!err) { + next.apply(this, arguments); + } else { + next(null, { + members: [] + }); + } + }); + }, + "+w": function(next) { + groups.getByGroupName('cid:' + cid + ':privileges:+w', { expand: true }, function(err, groupObj) { + if (!err) { + next.apply(this, arguments); + } else { + next(null, { + members: [] + }); + } + }); + } + }, function(err, data) { + callback(null, { + "+r": data['+r'].members, + "+w": data['+w'].members + }); + }); +}; + +SocketAdmin.categories.setGroupPrivilege = function(cid, gid, privilege, set, callback) { + if (set) { + groups.joinByGroupName('cid:' + cid + ':privileges:' + privilege, gid, callback); + } else { + groups.leaveByGroupName('cid:' + cid + ':privileges:' + privilege, gid, callback); + } +}; + +SocketAdmin.categories.groupsList = function(cid, callback) { + groups.list({expand:false}, function(err, data){ + async.map(data, function(groupObj, next) { + CategoryTools.groupPrivileges(cid, groupObj.gid, function(err, privileges) { + if (!err) { + groupObj.privileges = privileges; + } else { + winston.error('[socket api:admin.categories.groupsList] Could not retrieve permissions'); + } + + next(null, groupObj); + }); + }, function(err, data) { + callback(null, data); + }); + }); +}; + +/* Themes & Plugins */ + +SocketAdmin.themes = {}; +SocketAdmin.plugins = {}; + +SocketAdmin.themes.getInstalled = function(callback) { + meta.themes.get(function(err, themeArr) { + callback(themeArr); + }); +}; + +SocketAdmin.themes.set = meta.themes.set; + +SocketAdmin.plugins.toggle = function(plugin_id, sessionData) { + plugins.toggleActive(plugin_id, function(status) { + sessionData.socket.emit('api:admin.plugins.toggle', status); + }); +}; + +module.exports = SocketAdmin; \ No newline at end of file diff --git a/src/socket.io/groups.js b/src/socket.io/groups.js new file mode 100644 index 0000000000..fed37f76ac --- /dev/null +++ b/src/socket.io/groups.js @@ -0,0 +1,39 @@ +var groups = require('../groups'), + + SocketGroups = {}; + +SocketGroups.create = function(data, callback) { + groups.create(data.name, data.description, function(err, groupObj) { + callback(err ? err.message : null, groupObj || undefined); + }); +}; + +SocketGroups.delete = function(gid, callback) { + groups.destroy(gid, function(err) { + callback(err ? err.message : null, err ? null : 'OK'); + }); +}; + +SocketGroups.get = function(gid, callback) { + groups.get(gid, { + expand: true + }, function(err, groupObj) { + callback(err ? err.message : null, groupObj || undefined); + }); +}; + +SocketGroups.join = function(data, callback) { + groups.join(data.gid, data.uid, callback); +}; + +SocketGroups.leave = function(data, callback) { + groups.leave(data.gid, data.uid, callback); +}; + +SocketGroups.update = function(data, callback) { + groups.update(data.gid, data.values, function(err) { + callback(err ? err.message : null); + }); +}; + +module.exports = SocketGroups; \ No newline at end of file diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 5723465c25..31be1962d4 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -299,6 +299,7 @@ function emitOnlineUserCount(callback) { } } +Sockets.emitAlert = emitAlert; function emitAlert(socket, title, message) { socket.emit('event:alert', { type: 'danger', @@ -309,6 +310,7 @@ function emitAlert(socket, title, message) { }); } +Sockets.emitContentTooShortAlert = emitContentTooShortAlert; function emitContentTooShortAlert(socket) { socket.emit('event:alert', { type: 'danger', @@ -319,6 +321,7 @@ function emitContentTooShortAlert(socket) { }); } +Sockets.emitTooManyPostsAlert = emitTooManyPostsAlert; function emitTooManyPostsAlert(socket) { socket.emit('event:alert', { title: 'Too many posts!', diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js new file mode 100644 index 0000000000..aa43c03b0d --- /dev/null +++ b/src/socket.io/modules.js @@ -0,0 +1,161 @@ +"use strict"; + +var posts = require('../posts'), + postTools = require('../postTools'), + topics = require('../topics'), + meta = require('../meta'), + Messaging = require('../messaging'), + user = require('../user'), + notifications = require('../notifications'), + + async = require('async'), + S = require('string'), + winston = require('winston'), + + SocketModules = {}; + +/* Posts Composer */ + +SocketModules.composer = {}; + +SocketModules.composer.push = function(data, callback, sessionData) { + if (parseInt(sessionData.uid, 10) > 0 || parseInt(meta.config.allowGuestPosting, 10) === 1) { + if (parseInt(data.pid, 10) > 0) { + + async.parallel([ + function(next) { + posts.getPostFields(data.pid, ['content'], next); + }, + function(next) { + topics.getTitleByPid(data.pid, function(title) { + next(null, title); + }); + } + ], function(err, results) { + callback({ + title: results[1], + pid: data.pid, + body: results[0].content + }); + }); + } + } else { + callback({ + error: 'no-uid' + }); + } +}; + +SocketModules.composer.editCheck = function(pid, callback) { + posts.getPostField(pid, 'tid', function(err, tid) { + postTools.isMain(pid, tid, function(err, isMain) { + callback({ + titleEditable: isMain + }); + }); + }); +}; + +/* Chat */ + +SocketModules.chats = {}; + +SocketModules.chats.get = function(data, callback, sessionData) { + var touid = data.touid; + Messaging.getMessages(sessionData.uid, touid, function(err, messages) { + if (err) { + return callback(null); + } + + callback(messages); + }); +}; + +SocketModules.chats.send = function(data, sessionData) { + + var touid = data.touid; + if (touid === sessionData.uid || sessionData.uid === 0) { + return; + } + + var msg = S(data.message).stripTags().s; + + user.getMultipleUserFields([sessionData.uid, touid], ['username'], function(err, usersData) { + if(err) { + return; + } + + var username = usersData[0].username, + toUsername = usersData[1].username, + finalMessage = username + ' : ' + msg, + notifText = 'New message from ' + username + ''; + + if (!module.parent.exports.isUserOnline(touid)) { + notifications.create(notifText, 'javascript:app.openChat('' + username + '', ' + sessionData.uid + ');', 'notification_' + sessionData.uid + '_' + touid, function(nid) { + notifications.push(nid, [touid], function(success) { + + }); + }); + } + + Messaging.addMessage(sessionData.uid, touid, msg, function(err, message) { + var numSockets = 0, + x; + + if (sessionData.userSockets[touid]) { + numSockets = sessionData.userSockets[touid].length; + + for (x = 0; x < numSockets; ++x) { + sessionData.userSockets[touid][x].emit('event:chats.receive', { + fromuid: sessionData.uid, + username: username, + message: finalMessage, + timestamp: Date.now() + }); + } + } + + if (sessionData.userSockets[sessionData.uid]) { + + numSockets = sessionData.userSockets[sessionData.uid].length; + + for (x = 0; x < numSockets; ++x) { + sessionData.userSockets[sessionData.uid][x].emit('event:chats.receive', { + fromuid: touid, + username: toUsername, + message: 'You : ' + msg, + timestamp: Date.now() + }); + } + } + }); + }); +}; + +SocketModules.chats.list = function(callback, sessionData) { + Messaging.getRecentChats(sessionData.uid, function(err, uids) { + if (err) { + winston.warn('[(socket) api:chats.list] Problem retrieving chats: ' + err.message); + } + + callback(uids || []); + }); +}; + +/* Notifications */ + +SocketModules.notifications = {}; + +SocketModules.notifications.mark_read = function(nid, sessionData) { + notifications.mark_read(nid, sessionData.uid); +}; + +SocketModules.notifications.mark_all_read = function(data, callback, sessionData) { + notifications.mark_all_read(sessionData.uid, function(err) { + if (!err) { + callback(); + } + }); +}; + +module.exports = SocketModules; \ No newline at end of file diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js new file mode 100644 index 0000000000..4e6b6f2914 --- /dev/null +++ b/src/socket.io/posts.js @@ -0,0 +1,154 @@ +var posts = require('../posts'), + meta = require('../meta'), + topics = require('../topics'), + favourites = require('../favourites'), + postTools = require('../postTools'), + + SocketPosts = {}; + +SocketPosts.reply = function(data, sessionData) { + if (sessionData.uid < 1 && parseInt(meta.config.allowGuestPosting, 10) === 0) { + sessionData.socket.emit('event:alert', { + title: 'Reply Unsuccessful', + message: 'You don't seem to be logged in, so you cannot reply.', + type: 'danger', + timeout: 2000 + }); + return; + } + + // FIXME: postDelay in sockets? I am disappoint. + // if (Date.now() - lastPostTime < meta.config.postDelay * 1000) { + // module.parent.exports.emitTooManyPostsAlert(sessionData.socket); + // return; + // } + + topics.reply(data.topic_id, sessionData.uid, data.content, function(err, postData) { + if(err) { + if (err.message === 'content-too-short') { + module.parent.exports.emitContentTooShortAlert(sessionData.socket); + } else if (err.message === 'too-many-posts') { + module.parent.exports.emitTooManyPostsAlert(sessionData.socket); + } else if (err.message === 'reply-error') { + sessionData.socket.emit('event:alert', { + 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') { + sessionData.socket.emit('event:alert', { + title: 'Unable to post', + message: 'You do not have posting privileges in this category.', + type: 'danger', + timeout: 7500 + }); + } + return; + } + + if (postData) { + lastPostTime = Date.now(); + module.parent.exports.emitTopicPostStats(); + + sessionData.socket.emit('event:alert', { + title: 'Reply Successful', + message: 'You have successfully replied. Click here to view your reply.', + type: 'success', + timeout: 2000 + }); + var socketData = { + posts: [postData] + }; + sessionData.server.sockets.in('topic_' + postData.tid).emit('event:new_post', socketData); + sessionData.server.sockets.in('recent_posts').emit('event:new_post', socketData); + sessionData.server.sockets.in('user/' + postData.uid).emit('event:new_post', socketData); + + } + + }); +}; + +SocketPosts.favourite = function(data, sessionData) { + favourites.favourite(data.pid, data.room_id, sessionData.uid, sessionData.socket); +}; + +SocketPosts.unfavourite = function(data, sessionData) { + favourites.unfavourite(data.pid, data.room_id, sessionData.uid, sessionData.socket); +}; + +SocketPosts.uploadImage = function(data, callback) { + posts.uploadPostImage(data, callback); +}; + +SocketPosts.uploadFile = function(data, callback) { + posts.uploadPostFile(data, callback); +}; + +SocketPosts.getRawPost = function(data, callback) { + posts.getPostField(data.pid, 'content', function(err, raw) { + callback({ + post: raw + }); + }); +}; + +SocketPosts.edit = function(data, sessionData) { + if(!sessionData.uid) { + sessionData.socket.emit('event:alert', { + title: 'Can't edit', + message: 'Guests can't edit posts!', + type: 'warning', + timeout: 2000 + }); + return; + } else if (!data.title || data.title.length < parseInt(meta.config.minimumTitleLength, 10)) { + topics.emitTitleTooShortAlert(sessionData.socket); + return; + } else if (!data.content || data.content.length < parseInt(meta.config.minimumPostLength, 10)) { + module.parent.exports.emitContentTooShortAlert(sessionData.socket); + return; + } + + postTools.edit(sessionData.uid, data.pid, data.title, data.content, data.images); +}; + +SocketPosts.delete = function(data, callback, sessionData) { + postTools.delete(sessionData.uid, data.pid, function(err) { + + if(err) { + return callback(err); + } + + module.parent.exports.emitTopicPostStats(); + + sessionData.server.sockets.in('topic_' + data.tid).emit('event:post_deleted', { + pid: data.pid + }); + callback(null); + }); +}; + +SocketPosts.restore = function(data, callback, sessionData) { + postTools.restore(sessionData.uid, data.pid, function(err) { + if(err) { + return callback(err); + } + + module.parent.exports.emitTopicPostStats(); + + sessionData.server.sockets.in('topic_' + data.tid).emit('event:post_restored', { + pid: data.pid + }); + callback(null); + }); +}; + +SocketPosts.getPrivileges = function(pid, callback, sessionData) { + postTools.privileges(pid, sessionData.uid, function(privileges) { + privileges.pid = parseInt(pid); + callback(privileges); + }); +}; + +module.exports = SocketPosts; \ No newline at end of file diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index 4cf436c279..8b95965344 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -17,13 +17,13 @@ SocketTopics.post = function(data, sessionData) { topics.post(sessionData.uid, data.title, data.content, data.category_id, function(err, result) { if(err) { if (err.message === 'title-too-short') { - emitAlert(socket, 'Title too short', 'Please enter a longer title. At least ' + meta.config.minimumTitleLength + ' characters.'); + module.parent.exports.emitAlert(socket, 'Title too short', 'Please enter a longer title. At least ' + meta.config.minimumTitleLength + ' characters.'); } else if (err.message === 'title-too-long') { - emitAlert(socket, 'Title too long', 'Please enter a shorter title. Titles can\'t be longer than ' + meta.config.maximumTitleLength + ' characters.'); + module.parent.exports.emitAlert(socket, 'Title too long', 'Please enter a shorter title. Titles can\'t be longer than ' + meta.config.maximumTitleLength + ' characters.'); } else if (err.message === 'content-too-short') { - emitContentTooShortAlert(socket); + module.parent.exports.emitContentTooShortAlert(socket); } else if (err.message === 'too-many-posts') { - emitTooManyPostsAlert(socket); + module.parent.exports.emitTooManyPostsAlert(socket); } else if (err.message === 'no-privileges') { socket.emit('event:alert', { title: 'Unable to post', diff --git a/src/socket.io/user.js b/src/socket.io/user.js index ef1475a061..e8790635bf 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -133,6 +133,21 @@ SocketUser.getActiveUsers = function(callback) { module.parent.exports.emitOnlineUserCount(callback); }; +SocketUser.loadMore = function(data, callback) { + var start = data.after, + end = start + 19; + + user.getUsers(data.set, start, end, function(err, data) { + if (err) { + winston.err(err); + } else { + callback({ + users: data + }); + } + }); +}; + /* Exports */ module.exports = SocketUser; \ No newline at end of file diff --git a/src/websockets.js b/src/websockets.js index a7f8e82025..27d66137f9 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -59,308 +59,26 @@ websockets.init = function(io) { - socket.on('api:posts.reply', function(data) { - if (uid < 1 && parseInt(meta.config.allowGuestPosting, 10) === 0) { - socket.emit('event:alert', { - title: 'Reply Unsuccessful', - message: 'You don't seem to be logged in, so you cannot reply.', - type: 'danger', - timeout: 2000 - }); - return; - } - - if (Date.now() - lastPostTime < meta.config.postDelay * 1000) { - emitTooManyPostsAlert(socket); - return; - } - - topics.reply(data.topic_id, uid, data.content, function(err, postData) { - if(err) { - if (err.message === 'content-too-short') { - emitContentTooShortAlert(socket); - } else if (err.message === 'too-many-posts') { - emitTooManyPostsAlert(socket); - } else if (err.message === 'reply-error') { - socket.emit('event:alert', { - 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') { - socket.emit('event:alert', { - title: 'Unable to post', - message: 'You do not have posting privileges in this category.', - type: 'danger', - timeout: 7500 - }); - } - return; - } - - if (postData) { - lastPostTime = Date.now(); - emitTopicPostStats(); - - socket.emit('event:alert', { - title: 'Reply Successful', - message: 'You have successfully replied. Click here to view your reply.', - type: 'success', - timeout: 2000 - }); - var socketData = { - posts: [postData] - }; - io.sockets.in('topic_' + postData.tid).emit('event:new_post', socketData); - io.sockets.in('recent_posts').emit('event:new_post', socketData); - io.sockets.in('user/' + postData.uid).emit('event:new_post', socketData); - - } - }); - }); - - - - socket.on('api:posts.favourite', function(data) { - favourites.favourite(data.pid, data.room_id, uid, socket); - }); - - socket.on('api:posts.unfavourite', function(data) { - favourites.unfavourite(data.pid, data.room_id, uid, socket); - }); - - - - - - socket.on('api:posts.uploadImage', function(data, callback) { - posts.uploadPostImage(data, callback); - }); - - socket.on('api:posts.uploadFile', function(data, callback) { - posts.uploadPostFile(data, callback); - }); - - socket.on('api:posts.getRawPost', function(data, callback) { - posts.getPostField(data.pid, 'content', function(err, raw) { - callback({ - post: raw - }); - }); - }); - - socket.on('api:posts.edit', function(data) { - if(!uid) { - socket.emit('event:alert', { - title: 'Can't edit', - message: 'Guests can't edit posts!', - type: 'warning', - timeout: 2000 - }); - return; - } else if (!data.title || data.title.length < parseInt(meta.config.minimumTitleLength, 10)) { - topics.emitTitleTooShortAlert(socket); - return; - } else if (!data.content || data.content.length < parseInt(meta.config.minimumPostLength, 10)) { - emitContentTooShortAlert(socket); - return; - } - - postTools.edit(uid, data.pid, data.title, data.content, data.images); - }); - - socket.on('api:posts.delete', function(data, callback) { - postTools.delete(uid, data.pid, function(err) { - - if(err) { - return callback(err); - } - - emitTopicPostStats(); - - io.sockets.in('topic_' + data.tid).emit('event:post_deleted', { - pid: data.pid - }); - callback(null); - }); - }); - - socket.on('api:posts.restore', function(data, callback) { - postTools.restore(uid, data.pid, function(err) { - if(err) { - return callback(err); - } - - emitTopicPostStats(); - - io.sockets.in('topic_' + data.tid).emit('event:post_restored', { - pid: data.pid - }); - callback(null); - }); - }); - socket.on('api:notifications.mark_read', function(nid) { - notifications.mark_read(nid, uid); - }); - socket.on('api:notifications.mark_all_read', function(data, callback) { - notifications.mark_all_read(uid, function(err) { - if (!err) { - callback(); - } - }); - }); - socket.on('api:chats.get', function(data, callback) { - var touid = data.touid; - Messaging.getMessages(uid, touid, function(err, messages) { - if (err) - return callback(null); - callback(messages); - }); - }); - socket.on('api:chats.send', function(data) { - - var touid = data.touid; - if (touid === uid || uid === 0) { - return; - } - - var msg = S(data.message).stripTags().s; - - user.getMultipleUserFields([uid, touid], ['username'], function(err, usersData) { - if(err) { - return; - } - - var username = usersData[0].username, - toUsername = usersData[1].username, - finalMessage = username + ' : ' + msg, - notifText = 'New message from ' + username + ''; - - if (!isUserOnline(touid)) { - notifications.create(notifText, 'javascript:app.openChat('' + username + '', ' + uid + ');', 'notification_' + uid + '_' + touid, function(nid) { - notifications.push(nid, [touid], function(success) { - - }); - }); - } - - Messaging.addMessage(uid, touid, msg, function(err, message) { - var numSockets = 0; - - if (userSockets[touid]) { - numSockets = userSockets[touid].length; - - for (var x = 0; x < numSockets; ++x) { - userSockets[touid][x].emit('event:chats.receive', { - fromuid: uid, - username: username, - message: finalMessage, - timestamp: Date.now() - }); - } - } - - if (userSockets[uid]) { - - numSockets = userSockets[uid].length; - - for (var x = 0; x < numSockets; ++x) { - userSockets[uid][x].emit('event:chats.receive', { - fromuid: touid, - username: toUsername, - message: 'You : ' + msg, - timestamp: Date.now() - }); - } - } - }); - }); - }); - socket.on('api:chats.list', function(callback) { - Messaging.getRecentChats(uid, function(err, uids) { - if (err) { - winston.warn('[(socket) api:chats.list] Problem retrieving chats: ' + err.message); - } - callback(uids || []); - }); - }); - socket.on('api:composer.push', function(data, callback) { - - if (parseInt(uid, 10) > 0 || parseInt(meta.config.allowGuestPosting, 10) === 1) { - if (parseInt(data.pid) > 0) { - - async.parallel([ - function(next) { - posts.getPostFields(data.pid, ['content'], next); - }, - function(next) { - topics.getTitleByPid(data.pid, function(title) { - next(null, title); - }); - } - ], function(err, results) { - callback({ - title: results[1], - pid: data.pid, - body: results[0].content - }); - }); - } - } else { - callback({ - error: 'no-uid' - }); - } - }); - socket.on('api:composer.editCheck', function(pid, callback) { - posts.getPostField(pid, 'tid', function(err, tid) { - postTools.isMain(pid, tid, function(err, isMain) { - callback({ - titleEditable: isMain - }); - }); - }); - }); - socket.on('api:post.privileges', function(pid) { - postTools.privileges(pid, uid, function(privileges) { - privileges.pid = parseInt(pid); - socket.emit('api:post.privileges', privileges); - }); - }); - socket.on('api:users.loadMore', function(data, callback) { - var start = data.after, - end = start + 19; - user.getUsers(data.set, start, end, function(err, data) { - if (err) { - winston.err(err); - } else { - callback({ - users: data - }); - } - }); - });