Skip to content

Node.js based forum software built for the modern web

streaming posts. also moved topics to be entirely client side parsed. made some changes. added block level caching to templates. added a prepare method to templates

psychobunny3e96f641912c
diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js
index e61aa5264e..36f4e233e8 100644
--- a/public/src/ajaxify.js
+++ b/public/src/ajaxify.js
@@ -26,7 +26,6 @@ var ajaxify = {};
socket.emit('event:enter_room', 'global');
current_room = 'global';
}
-
var url = url.replace(/\/$/, "");
var tpl_url = (url === '' || url === '/') ? 'home' : url.split('/')[0];
diff --git a/public/src/templates.js b/public/src/templates.js
index 7fe0734437..e476f429ca 100644
--- a/public/src/templates.js
+++ b/public/src/templates.js
@@ -7,7 +7,15 @@ var templates = {};
//quick implementation because introducing a lib to handle several async callbacks
if (callback == null && ready_callback) ready_callback();
else ready_callback = callback;
- }
+ };
+
+ templates.prepare = function(raw_tpl, data) {
+ var template = {};
+ template.html = raw_tpl;
+ template.parse = parse;
+ template.blocks = {};
+ return template;
+ };
function loadTemplates(templatesToLoad) {
var timestamp = new Date().getTime();
@@ -24,9 +32,10 @@ var templates = {};
template.prototype.parse = parse;
template.prototype.html = String(html);
-
- templates[file] = new template;
+ template.prototype.blocks = {};
+ templates[file] = new template;
+
loaded--;
if (loaded == 0) templates.ready();
}).fail(function() {
@@ -49,6 +58,8 @@ var templates = {};
//modified from https://github.com/psychobunny/dcp.templates
var parse = function(data) {
+ var self = this;
+
function replace(key, value, template) {
var searchRegex = new RegExp('{' + key + '}', 'g');
return template.replace(searchRegex, value);
@@ -62,6 +73,8 @@ var templates = {};
data = template.match(regex);
if (data == null) return;
+ if (block !== undefined) self.blocks[block] = data[0];
+
data = data[0]
.replace("<!-- BEGIN " + block + " -->", "")
.replace("<!-- END " + block + " -->", "");
diff --git a/public/templates/topic.tpl b/public/templates/topic.tpl
index ba0ada6fe0..adf3092cc8 100644
--- a/public/templates/topic.tpl
+++ b/public/templates/topic.tpl
@@ -5,7 +5,7 @@
</ul>
</div>
-<ul class="post-container container">
+<ul id="post-container" class="post-container container">
<!-- BEGIN posts -->
<li class="row">
<div class="span1 profile-image-block">
@@ -63,6 +63,28 @@ socket.on('event:rep_down', function(data) {
adjust_rep(-1, data.pid, data.uid);
});
+socket.on('event:new_post', function(data) {
+ var html = templates.prepare(templates['topic'].blocks['posts']).parse(data);
+
+ jQuery('<div></div>').appendTo("#post-container").hide().append(html).fadeIn('slow');
+});
+/*
+jQuery('document').ready(function() {
+ setTimeout(function() {
+ //console.log(JSON.stringify(templates['topic'].blocks));
+ var html = templates.prepare(templates['topic'].blocks['posts']).parse({
+ 'posts' : [
+ {
+ 'username' : 'derp'
+ }
+ ]
+ });
+
+ jQuery('<div></div>').appendTo("#post-container").hide().append(html).fadeIn('slow');
+
+ }, 1500);
+});
+*/
function adjust_rep(value, pid, uid) {
var post_rep = document.getElementById('post_rep_' + pid),
user_rep = document.getElementById('user_rep_' + uid);
diff --git a/src/posts.js b/src/posts.js
index 635c199cd1..45cfd0e291 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -92,12 +92,38 @@ var RDB = require('./redis.js'),
Posts.create(uid, tid, content, function(pid) {
RDB.rpush('tid:' + tid + ':posts', pid);
+
socket.emit('event:alert', {
title: 'Reply Successful',
message: 'You have successfully replied. Click here to view your reply.',
type: 'notify',
timeout: 2000
});
+
+ user.get_user_postdetails([uid], function(user_details) {
+ user.get_gravatars_by_uids([uid], 80, function(gravatars) {
+ var timestamp = new Date().getTime();
+
+ socket.broadcast.to('topic_' + tid).emit('event:new_post', {
+ 'posts' : [
+ {
+ 'pid' : pid,
+ 'content' : marked(content || ''),
+ 'uid' : uid,
+ 'username' : user_details.username[0] || 'anonymous',
+ 'user_rep' : user_details.rep[0] || 0,
+ 'post_rep' : 0,
+ 'gravatar' : gravatars[0],
+ 'timestamp' : timestamp,
+ 'relativeTime': utils.relativeTime(timestamp),
+ 'fav_star_class' :'icon-star-empty'
+ }
+ ]
+ });
+ });
+ });
+
+
});
};
@@ -117,7 +143,6 @@ var RDB = require('./redis.js'),
if (callback) callback(pid);
});
-
}
diff --git a/src/webserver.js b/src/webserver.js
index 978d8ff308..c46be8d058 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -118,6 +118,8 @@ passport.deserializeUser(function(uid, done) {
res.send(templates['header'] + templates['403'] + templates['footer']);
});
+
+
// Basic Routes (entirely client-side parsed, goal is to move the rest of the crap in this file into this one section)
(function() {
var routes = ['', 'login', 'register'];
@@ -131,17 +133,14 @@ passport.deserializeUser(function(uid, done) {
}
}());
-
- function generate_topic_body(req, res) {
- global.modules.topics.generate_topic_body(function(topic_body) {
- res.send(templates['header'] + topic_body + templates['footer']);
- }, req.params.topic_id);
- }
- app.get('/topic/:topic_id', generate_topic_body);
- app.get('/topic/:topic_id*', generate_topic_body);
+ // Complex Routes
+ app.get('/topic/:topic_id/:slug?', function(req, res) {
+ res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("' + 'topic/' + req.params.topic_id + '");});</script>' + templates['footer']);
+ });
+ // These functions are called via ajax once the initial page is loaded to populate templates with data
function api_method(req, res) {
switch(req.params.method) {
case 'home' :
@@ -173,7 +172,7 @@ passport.deserializeUser(function(uid, done) {
case 'topic' :
global.modules.posts.get(function(data) {
res.send(JSON.stringify(data));
- }, req.params.id, req.user.uid);
+ }, req.params.id, req.user.uid || 0);
break;
default :
res.send('{}');