From db6bbcc699f6206710c8660c47b523032ac474c2 Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Fri, 24 May 2013 12:32:28 -0400 Subject: [PATCH 1/9] 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); From 448673b141273639bfc9f015c5d62e8f1e2b2c47 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 24 May 2013 12:46:19 -0400 Subject: [PATCH 2/9] recent replies part 2 --- public/templates/category.tpl | 8 +- src/categories.js | 6 +- src/posts.js | 134 +++++++++++++++++++--------------- src/redis.js | 25 +++++++ src/topics.js | 5 -- src/websockets.js | 7 +- 6 files changed, 113 insertions(+), 72 deletions(-) diff --git a/public/templates/category.tpl b/public/templates/category.tpl index 8064da84d7..ecec8a2688 100644 --- a/public/templates/category.tpl +++ b/public/templates/category.tpl @@ -48,7 +48,7 @@
Recent Replies
-
+
@@ -82,6 +82,7 @@ - + diff --git a/src/webserver.js b/src/webserver.js index f7b8028b4b..04a08dc03f 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -24,7 +24,7 @@ var express = require('express'), // Middlewares app.use(express.favicon()); // 2 args: string path and object options (i.e. expire time etc) - app.use(require('less-middleware')({ src: path.join(__dirname, '../', '/public') })); + app.use(require('less-middleware')({ src: path.join(__dirname, '../', 'public') })); app.use(express.static(path.join(__dirname, '../', 'public'))); app.use(express.bodyParser()); // Puts POST vars in request.body app.use(express.cookieParser()); // If you want to parse cookies (res.cookies) From d36a81966a53d58fd830322f8b3036c60fb08110 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 24 May 2013 23:02:13 -0400 Subject: [PATCH 7/9] use ajaxify to load 404 (prevents weird refreshing bug); part 1 of client side refactor: moving all template javascript vars into dom; templates.get, templates.set for dynamic variables populated via tpls --- public/src/templates.js | 22 ++++++++++++++++++---- public/templates/account.tpl | 8 ++++++-- public/templates/accountedit.tpl | 7 +++++-- public/templates/category.tpl | 7 +++++-- public/templates/friends.tpl | 11 +++++++---- public/templates/reset_code.tpl | 9 +++++++-- public/templates/topic.tpl | 31 ++++++++++++++++++++----------- public/templates/users.tpl | 4 ---- 8 files changed, 68 insertions(+), 31 deletions(-) diff --git a/public/src/templates.js b/public/src/templates.js index 34b8784be6..b3ea78105c 100644 --- a/public/src/templates.js +++ b/public/src/templates.js @@ -5,7 +5,8 @@ var config = {}, templates, fs = null, - available_templates = []; + available_templates = [], + parsed_variables = {}; module.exports = templates = {}; @@ -153,8 +154,8 @@ (function() { jQuery.get(API_URL + api_url, function(data) { - if(!data) { - window.location.href = '/404'; + if(data === false) { + ajaxify.go('404'); return; } @@ -168,12 +169,25 @@ if (!templates[tpl_url] || !template_data) return; document.getElementById('content').innerHTML = templates[tpl_url].parse(JSON.parse(template_data)); - if (callback) callback(true); + + jQuery('#content [template-variable]').each(function(index, element) { + templates.set(element.getAttribute('template-variable'), element.value); + }); + + if (callback) { + callback(true); + } } } + templates.get = function(key) { + return parsed_variables[key]; + } + templates.set = function(key, value) { + parsed_variables[key] = value; + } //modified from https://github.com/psychobunny/dcp.templates var parse = function(data) { diff --git a/public/templates/account.tpl b/public/templates/account.tpl index b04c8572f6..f269317b50 100644 --- a/public/templates/account.tpl +++ b/public/templates/account.tpl @@ -69,12 +69,16 @@
+ + + +