added post button, post window, added click handler to toaster alert, started on posts and topics schema and code

This commit is contained in:
psychobunny
2013-04-24 20:40:34 +00:00
parent dfd5c9960b
commit d711a4e334
10 changed files with 290 additions and 89 deletions

40
src/posts.js Normal file
View File

@@ -0,0 +1,40 @@
var RDB = require('./redis.js');
(function(Posts) {
//data structure
//*global:next_post_id
// *pid:1:content
// *pid:1:uid
// *pid:1:timestamp
// ***pid:1:replies
// *uid:1:posts
Posts.get = function(topic) {
}
Posts.reply = function() {
};
Posts.create = function(content, callback) {
if (global.uid === null) return;
RDB.incr('global:next_post_id', function(pid) {
// Posts Info
RDB.set('pid:' + pid + ':content', content);
RDB.set('pid:' + pid + ':uid', global.uid);
RDB.set('pid:' + pid + ':timestamp', new Date().getTime());
// User Details - move this out later
RDB.lpush('uid:' + uid + ':posts', pid);
if (callback) callback(pid);
});
}
}(exports));

111
src/topics.js Normal file
View File

@@ -0,0 +1,111 @@
var RDB = require('./redis.js'),
posts = require('./posts.js');
(function(Topics) {
//data structure
//*global:next_topic_id
// *tid:1:title
// *tid:1:uid
// *tid:1:posts (array of pid)
// *tid:1:timestamp
// *uid:1:topics
// *topic:slug:how-to-eat-chicken:tid
Topics.get_by_category = function(category, start, end) {
}
Topics.get = function(start, end) {
if (start == null) start = 0;
if (end == null) end = start + 10;
RDB.lrange('topics:tid', start, end, function() {
global.socket.emit
});
}
Topics.post = function(title, content, category) {
if (global.uid === null) {
global.socket.emit('event:alert', {
title: 'Thank you for posting',
message: 'Since you are unregistered, your post is awaiting approval. Click here to register now.',
type: 'warning',
timeout: 7500,
clickfn: function() {
ajaxify.go('register');
}
});
return; // for now, until anon code is written.
}
RDB.incr('global:next_topic_id', function(tid) {
// Global Topics
if (global.uid !== null) {
RDB.lpush('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);
}
if (category) {
RDB.lpush('topics:' + category + ':tid', tid);
}
// Topic Info
RDB.set('tid:' + tid + ':title', title);
RDB.set('tid:' + tid + ':uid', global.uid);
RDB.set('tid:' + tid + ':timestamp', new Date().getTime());
RDB.set('topic:slug:' + slugify(title) + ':tid', tid);
// Posts
posts.create(content, function(pid) {
RDB.lpush('tid:' + tid + ':posts', pid);
});
// User Details - move this out later
RDB.lpush('uid:' + uid + ':topics', tid);
global.socket.emit('event:alert', {
title: 'Thank you for registering',
message: 'You have successfully registered - welcome to nodebb!',
type: 'notify',
timeout: 2000
});
});
};
}(exports));
//http://dense13.com/blog/2009/05/03/converting-string-to-slug-javascript/
function slugify(str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
var to = "aaaaeeeeiiiioooouuuunc------";
for (var i=0, l=from.length ; i<l ; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
return str;
}

View File

@@ -7,35 +7,23 @@ var express = require('express'),
(function(app) {
var templates = global.templates;
function refreshTemplates() {
//need a better solution than copying this code on every call. is there an "onconnect" event?
if (DEVELOPMENT === true) {
// refreshing templates
modules.templates.init();
}
}
app.get('/', function(req, res) {
refreshTemplates();
res.send(templates['header'] + templates['home'] + templates['footer']);
});
app.get('/login', function(req, res) {
refreshTemplates();
res.send(templates['header'] + templates['login'] + templates['footer']);
});
app.get('/reset/:code', function(req, res) {
refreshTemplates();
res.send(templates['header'] + templates['reset_code'].parse({ reset_code: req.params.code }) + templates['footer']);
});
app.get('/reset', function(req, res) {
refreshTemplates();
res.send(templates['header'] + templates['reset'] + templates['footer']);
});
app.get('/register', function(req, res) {
refreshTemplates();
res.send(templates['header'] + templates['register'] + templates['footer']);
});

View File

@@ -56,6 +56,10 @@ var SocketIO = require('socket.io').listen(global.server);
socket.on('user:reset.commit', function(data) {
modules.user.reset.commit(data.code, data.password);
});
socket.on('api:topics.post', function(data) {
modules.topics.post(data.title, data.content);
});
});
}(SocketIO));