diff --git a/config.default.js b/config.default.js index 7228221310..01ba1d9af7 100644 --- a/config.default.js +++ b/config.default.js @@ -59,7 +59,8 @@ var config = { }, "show_motd": true, - "motd": undefined + "motd": undefined, + "post_delay" : 10000 } config.url = config.base_url + (config.use_port ? ':' + config.port : '') + '/'; diff --git a/public/src/app.js b/public/src/app.js index 1f9d04c719..c04a423114 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -38,27 +38,21 @@ var socket, socket.on('connect', function(data){ if(reconnecting) { - app.alert({ - alert_id: 'connection_alert', - title: 'Connected', - message: 'Connection successful', - type: 'success', - timeout: 5000 - }); + setTimeout(function(){ + app.alert({ + alert_id: 'connection_alert', + title: 'Connected', + message: 'Connection successful.', + type: 'success', + timeout: 5000 + }); + }, 1000); reconnecting = false; } }); socket.on('disconnect', function(data){ - setTimeout(function() { - app.alert({ - alert_id: 'connection_alert', - title: 'Disconnect', - message: 'You have disconnected from NodeBB, we will try to reconnect!', - type: 'error', - timeout: 5000 - }); - }, 500); + }); socket.on('reconnecting', function(data) { diff --git a/public/templates/footer.tpl b/public/templates/footer.tpl index 515f511a30..133771bda6 100644 --- a/public/templates/footer.tpl +++ b/public/templates/footer.tpl @@ -125,8 +125,10 @@ }); socket.on('api:notifications.counts', function(counts) { var notifIcon = document.querySelector('.notifications a i'); - if (counts.unread > 0) notifIcon.className = 'icon-circle active'; - else notifIcon.className = 'icon-circle-blank'; + if(notifIcon) { + if (counts.unread > 0) notifIcon.className = 'icon-circle active'; + else notifIcon.className = 'icon-circle-blank'; + } }); socket.on('event:new_notification', function() { console.log('WOOT'); diff --git a/src/posts.js b/src/posts.js index 6b93fda730..07c9e1884c 100644 --- a/src/posts.js +++ b/src/posts.js @@ -140,63 +140,73 @@ marked.setOptions({ return; } - Posts.create(uid, tid, content, function(pid) { - if (pid > 0) { - RDB.rpush('tid:' + tid + ':posts', pid); - - RDB.del('tid:' + tid + ':read_by_uid'); // let everybody know there is an unread post - Posts.get_cid_by_pid(pid, function(cid) { - RDB.del('cid:' + cid + ':read_by_uid'); - }); - - topics.get_cid_by_tid(tid, function(cid) { - RDB.zadd('categories:recent_posts:cid:' + cid, (new Date()).getTime(), pid); - }); - - - // Re-add the poster, so he/she does not get an "unread" flag on this topic - topics.markAsRead(tid, uid); - // this will duplicate once we enter the thread, which is where we should be going + user.getUserField(uid, 'lastposttime', function(lastposttime) { + if(new Date().getTime() - lastposttime < config.post_delay) { socket.emit('event:alert', { - title: 'Reply Successful', - message: 'You have successfully replied. Click here to view your reply.', - type: 'notify', + title: 'Too many posts!', + message: 'You can only post every '+ (config.post_delay / 1000) + ' seconds.', + type: 'error', timeout: 2000 }); + return; + } - user.getUserFields(uid, ['username','reputation','picture','signature'], function(data) { - - var timestamp = new Date().getTime(); - - io.sockets.in('topic_' + tid).emit('event:new_post', { - 'posts' : [ - { - 'pid' : pid, - 'content' : marked(content || ''), - 'uid' : uid, - 'username' : data.username || 'anonymous', - 'user_rep' : data.reputation || 0, - 'post_rep' : 0, - 'gravatar' : data.picture, - 'signature' : marked(data.signature || ''), - 'timestamp' : timestamp, - 'relativeTime': utils.relativeTime(timestamp), - 'fav_star_class' :'icon-star-empty', - 'edited-class': 'none', - 'editor': '', - } - ] + Posts.create(uid, tid, content, function(pid) { + if (pid > 0) { + RDB.rpush('tid:' + tid + ':posts', pid); + + RDB.del('tid:' + tid + ':read_by_uid'); // let everybody know there is an unread post + Posts.get_cid_by_pid(pid, function(cid) { + RDB.del('cid:' + cid + ':read_by_uid'); }); - }); - } else { - socket.emit('event:alert', { - title: 'Reply Unsuccessful', - message: 'Your reply could not be posted at this time. Please try again later.', - type: 'notify', - timeout: 2000 - }); - } + + RDB.zadd('topics:recent_posts:tid:' + tid, (new Date()).getTime(), pid); + + // Re-add the poster, so he/she does not get an "unread" flag on this topic + topics.markAsRead(tid, uid); + // this will duplicate once we enter the thread, which is where we should be going + + socket.emit('event:alert', { + title: 'Reply Successful', + message: 'You have successfully replied. Click here to view your reply.', + type: 'notify', + timeout: 2000 + }); + + user.getUserFields(uid, ['username','reputation','picture','signature'], function(data) { + + var timestamp = new Date().getTime(); + + io.sockets.in('topic_' + tid).emit('event:new_post', { + 'posts' : [ + { + 'pid' : pid, + 'content' : marked(content || ''), + 'uid' : uid, + 'username' : data.username || 'anonymous', + 'user_rep' : data.reputation || 0, + 'post_rep' : 0, + 'gravatar' : data.picture, + 'signature' : marked(data.signature || ''), + 'timestamp' : timestamp, + 'relativeTime': utils.relativeTime(timestamp), + 'fav_star_class' :'icon-star-empty', + 'edited-class': 'none', + 'editor': '', + } + ] + }); + }); + } else { + socket.emit('event:alert', { + title: 'Reply Unsuccessful', + message: 'Your reply could not be posted at this time. Please try again later.', + type: 'notify', + timeout: 2000 + }); + } + }); }); }; @@ -209,11 +219,12 @@ marked.setOptions({ if (!locked || locked === '0') { RDB.incr('global:next_post_id', function(err, pid) { RDB.handle(err); - + + var timestamp = new Date().getTime(); // Posts Info RDB.set('pid:' + pid + ':content', content); RDB.set('pid:' + pid + ':uid', uid); - RDB.set('pid:' + pid + ':timestamp', new Date().getTime()); + RDB.set('pid:' + pid + ':timestamp', timestamp); RDB.set('pid:' + pid + ':rep', 0); RDB.set('pid:' + pid + ':tid', tid); @@ -241,6 +252,7 @@ marked.setOptions({ RDB.lpush('uid:' + uid + ':posts', pid); user.incrementUserFieldBy(uid, 'postcount', 1); + user.setUserField(uid, 'lastposttime', timestamp); if (callback) callback(pid); diff --git a/src/topics.js b/src/topics.js index ec60397afb..fe56df07dc 100644 --- a/src/topics.js +++ b/src/topics.js @@ -290,68 +290,81 @@ marked.setOptions({ return; // for now, until anon code is written. } - RDB.incr('global:next_topic_id', function(err, tid) { - RDB.handle(err); - - // Global Topics - if (uid == null) uid = 0; - if (uid !== null) { - RDB.sadd('topics:tid', tid); - } else { - // need to add some unique key sent by client so we can update this with the real uid later - RDB.lpush('topics:queued:tid', tid); + user.getUserField(uid, 'lastposttime', function(lastposttime) { + + if(new Date().getTime() - lastposttime < config.post_delay) { + socket.emit('event:alert', { + title: 'Too many posts!', + message: 'You can only post every '+ (config.post_delay / 1000) + ' seconds.', + type: 'error', + timeout: 2000 + }); + return; } - var slug = tid + '/' + utils.slugify(title); + RDB.incr('global:next_topic_id', function(err, tid) { + RDB.handle(err); - // Topic Info - RDB.set('tid:' + tid + ':title', title); - RDB.set('tid:' + tid + ':uid', uid); - RDB.set('tid:' + tid + ':slug', slug); - RDB.set('tid:' + tid + ':timestamp', new Date().getTime()); - + // Global Topics + if (uid == null) uid = 0; + if (uid !== null) { + RDB.sadd('topics:tid', tid); + } else { + // need to add some unique key sent by client so we can update this with the real uid later + RDB.lpush('topics:queued:tid', tid); + } + + var slug = tid + '/' + utils.slugify(title); + + // Topic Info + RDB.set('tid:' + tid + ':title', title); + RDB.set('tid:' + tid + ':uid', uid); + RDB.set('tid:' + tid + ':slug', slug); + RDB.set('tid:' + tid + ':timestamp', new Date().getTime()); - RDB.set('topic:slug:' + slug + ':tid', tid); + + RDB.set('topic:slug:' + slug + ':tid', tid); - // Posts - posts.create(uid, tid, content, function(pid) { - if (pid > 0) { - RDB.lpush('tid:' + tid + ':posts', pid); + // Posts + posts.create(uid, tid, content, function(pid) { + if (pid > 0) { + RDB.lpush('tid:' + tid + ':posts', pid); - // Notify any users looking at the category that a new topic has arrived - Topics.get_topic(tid, uid, function(topicData) { - io.sockets.in('category_' + category_id).emit('event:new_topic', topicData); - }); - } - }); + // Notify any users looking at the category that a new topic has arrived + Topics.get_topic(tid, uid, function(topicData) { + io.sockets.in('category_' + category_id).emit('event:new_topic', topicData); + }); + } + }); - Topics.markAsRead(tid, uid); + Topics.markAsRead(tid, uid); - // User Details - move this out later - RDB.lpush('uid:' + uid + ':topics', tid); + // User Details - move this out later + RDB.lpush('uid:' + uid + ':topics', tid); - socket.emit('event:alert', { - title: 'Thank you for posting', - message: 'You have successfully posted. Click here to view your post.', - type: 'notify', - timeout: 2000 - }); + socket.emit('event:alert', { + title: 'Thank you for posting', + message: 'You have successfully posted. Click here to view your post.', + type: 'notify', + timeout: 2000 + }); - // let everyone know that there is an unread topic in this category - RDB.del('cid:' + category_id + ':read_by_uid'); + // let everyone know that there is an unread topic in this category + RDB.del('cid:' + category_id + ':read_by_uid'); - RDB.zadd('topics:recent', (new Date()).getTime(), tid); - //RDB.zadd('topics:active', tid); + RDB.zadd('topics:recent', (new Date()).getTime(), tid); + //RDB.zadd('topics:active', tid); - // in future it may be possible to add topics to several categories, so leaving the door open here. - RDB.sadd('categories:' + category_id + ':tid', tid); - RDB.set('tid:' + tid + ':cid', category_id); - categories.getCategories([category_id], function(data) { - RDB.set('tid:' + tid + ':category_name', data.categories[0].name); - RDB.set('tid:' + tid + ':category_slug', data.categories[0].slug); - }); + // in future it may be possible to add topics to several categories, so leaving the door open here. + RDB.sadd('categories:' + category_id + ':tid', tid); + RDB.set('tid:' + tid + ':cid', category_id); + categories.getCategories([category_id], function(data) { + RDB.set('tid:' + tid + ':category_name', data.categories[0].name); + RDB.set('tid:' + tid + ':category_slug', data.categories[0].slug); + }); - RDB.incr('cid:' + category_id + ':topiccount'); + RDB.incr('cid:' + category_id + ':topiccount'); + }); }); }; diff --git a/src/user.js b/src/user.js index a600fbf9e9..aa59326810 100644 --- a/src/user.js +++ b/src/user.js @@ -196,8 +196,8 @@ var config = require('../config.js'), callback(err); } else { // Save twitter-specific information to the user - RDB.set('uid:' + uid + ':twid', twid); - RDB.set('twid:' + twid + ':uid', uid); + User.setUserField(uid, 'twid', twid); + RDB.hset('twid:uid', twid, uid); callback(null, { uid: uid }); @@ -216,17 +216,23 @@ var config = require('../config.js'), }); } else { // New User - User.create(handle, null, email, function(err, uid) { - if (err !== null) { - callback(err); - } else { - // Save twitter-specific information to the user - RDB.set('uid:' + uid + ':gplusid', gplusid); - RDB.set('gplusid:' + gplusid + ':uid', uid); - callback(null, { - uid: uid + var success = function(uid) { + // Save google-specific information to the user + User.setUserField(uid, 'gplusid', gplusid); + RDB.hset('gplusid:uid', gplusid, uid); + callback(null, { + uid: uid + }); + } + + User.get_uid_by_email(email, function(uid) { + if (!uid) { + User.create(handle, null, email, function(err, uid) { + if (err !== null) { + callback(err); + } else success(uid); }); - } + } else success(uid); // Existing account -- merge }); } }); @@ -241,17 +247,23 @@ var config = require('../config.js'), }); } else { // New User - User.create(name, null, email, function(err, uid) { - if (err !== null) { - callback(err); - } else { - // Save twitter-specific information to the user - RDB.set('uid:' + uid + ':fbid', fbid); - RDB.set('fbid:' + fbid + ':uid', uid); + var success = function(uid) { + // Save facebook-specific information to the user + User.setUserField(uid, 'fbid', fbid); + RDB.hset('fbid:uid', fbid, uid); callback(null, { uid: uid }); - } + } + + User.get_uid_by_email(email, function(uid) { + if (!uid) { + User.create(name, null, email, function(err, uid) { + if (err !== null) { + callback(err); + } else success(uid); + }); + } else success(uid); // Existing account -- merge }); } }); @@ -288,7 +300,8 @@ var config = require('../config.js'), 'gravatarpicture' : gravatar, 'uploadedpicture': '', 'reputation': 0, - 'postcount': 0 + 'postcount': 0, + 'lastposttime': 0 }); RDB.set('username:' + username + ':uid', uid); @@ -493,21 +506,21 @@ var config = require('../config.js'), }; User.get_uid_by_twitter_id = function(twid, callback) { - RDB.get('twid:' + twid + ':uid', function(err, uid) { + RDB.hget('twid:uid', twid, function(err, uid) { RDB.handle(err); callback(uid); }); } User.get_uid_by_google_id = function(gplusid, callback) { - RDB.get('gplusid:' + gplusid + ':uid', function(err, uid) { + RDB.hget('gplusid:uid', gplusid, function(err, uid) { RDB.handle(err); callback(uid); }); } User.get_uid_by_fbid = function(fbid, callback) { - RDB.get('fbid:' + fbid + ':uid', function(err, uid) { + RDB.hget('fbid:uid', fbid, function(err, uid) { RDB.handle(err); callback(uid); });