From db6bbcc699f6206710c8660c47b523032ac474c2 Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Fri, 24 May 2013 12:32:28 -0400 Subject: [PATCH] post every x seconds added it x to config --- config.default.js | 3 +- src/posts.js | 115 ++++++++++++++++++++++++++-------------------- src/topics.js | 111 ++++++++++++++++++++++++-------------------- src/user.js | 3 +- 4 files changed, 131 insertions(+), 101 deletions(-) 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/src/posts.js b/src/posts.js index aa9a7a7f81..23d84c2696 100644 --- a/src/posts.js +++ b/src/posts.js @@ -127,60 +127,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'); - }); - - 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 + 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 + }); + } + }); }); }; @@ -193,11 +206,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); @@ -225,6 +239,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 bf40fc8931..f0573583e8 100644 --- a/src/topics.js +++ b/src/topics.js @@ -295,68 +295,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..4fefed224d 100644 --- a/src/user.js +++ b/src/user.js @@ -288,7 +288,8 @@ var config = require('../config.js'), 'gravatarpicture' : gravatar, 'uploadedpicture': '', 'reputation': 0, - 'postcount': 0 + 'postcount': 0, + 'lastposttime': 0 }); RDB.set('username:' + username + ':uid', uid);