mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-03 06:10:38 +01:00
Merge branch 'master' of github.com:psychobunny/node-forum
Conflicts: public/templates/header.tpl src/webserver.js
This commit is contained in:
12
.project
Normal file
12
.project
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>node-forum</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>com.aptana.projects.webnature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
8
app.js
8
app.js
@@ -17,6 +17,12 @@ global.modules = modules;
|
||||
// global.uid = 1;
|
||||
|
||||
|
||||
process.on('uncaughtException', function(err) {
|
||||
// handle the error safely
|
||||
console.log("error message "+err);
|
||||
global.socket.emit('event:consolelog',{type:'uncaughtException',stack:err.stack,error:err.toString()});
|
||||
});
|
||||
|
||||
|
||||
(function(config) {
|
||||
config['ROOT_DIRECTORY'] = __dirname;
|
||||
@@ -25,6 +31,6 @@ global.modules = modules;
|
||||
// modules.webserver.init();
|
||||
modules.websockets.init();
|
||||
|
||||
|
||||
|
||||
|
||||
}(global.configuration));
|
||||
@@ -1,11 +1,18 @@
|
||||
var config = {
|
||||
"secret": 'nodebb-secret',
|
||||
"base_url": "http://localhost",
|
||||
"port": 4567,
|
||||
"url": undefined, // Leave this alone
|
||||
"mailer": {
|
||||
host: 'localhost',
|
||||
port: '25',
|
||||
from: 'mailer@localhost.lan'
|
||||
},
|
||||
"redis": {
|
||||
port: "6379",
|
||||
host: "127.0.0.1",
|
||||
options: {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,9 @@
|
||||
"cookie": "0.0.6",
|
||||
"connect-redis": "1.4.5",
|
||||
"path": "0.4.9",
|
||||
"crypto": "0.0.3"
|
||||
"crypto": "0.0.3",
|
||||
"passport": "0.1.16",
|
||||
"passport-local": "0.1.6"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"optionalDependencies": {},
|
||||
|
||||
@@ -2,5 +2,6 @@
|
||||
"socket" : {
|
||||
"address" : "localhost",
|
||||
"port" : "4567"
|
||||
}
|
||||
},
|
||||
"api_url" : "http://localhost:4567/api/"
|
||||
}
|
||||
@@ -22,8 +22,8 @@ var ajaxify = {};
|
||||
|
||||
ajaxify.go = function(url, callback) {
|
||||
var url = url.replace(/\/$/, "");
|
||||
var tpl_url = (url === '') ? 'home' : url;
|
||||
|
||||
var tpl_url = (url === '' || url === '/') ? 'home' : url.split('/')[0];
|
||||
|
||||
if (templates[tpl_url]) {
|
||||
window.history.pushState({}, url, "/" + url);
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
var socket,
|
||||
config,
|
||||
app = {};
|
||||
app = {},
|
||||
|
||||
API_URL = null;
|
||||
|
||||
// todo: cleanup,etc
|
||||
(function() {
|
||||
@@ -8,6 +10,8 @@ var socket,
|
||||
$.ajax({
|
||||
url: '/config.json?v=' + new Date().getTime(),
|
||||
success: function(data) {
|
||||
API_URL = data.api_url;
|
||||
|
||||
config = data;
|
||||
socket = io.connect('http://' + config.socket.address + config.socket.port? ':' + config.socket.port : '');
|
||||
|
||||
@@ -18,6 +22,10 @@ var socket,
|
||||
socket.on('event:alert', function(data) {
|
||||
app.alert(data);
|
||||
});
|
||||
|
||||
socket.on('event:consolelog', function(data) {
|
||||
console.log(data);
|
||||
});
|
||||
},
|
||||
async: false
|
||||
|
||||
@@ -74,14 +82,63 @@ var socket,
|
||||
}
|
||||
}
|
||||
|
||||
var post_window = null;
|
||||
app.open_post_window = function() {
|
||||
var post_window = null,
|
||||
submit_post_btn = null,
|
||||
post_title = null,
|
||||
reply_title = null,
|
||||
post_content = null;
|
||||
|
||||
|
||||
app.open_post_window = function(post_mode, id, title) {
|
||||
submit_post_btn = submit_post_btn || document.getElementById('submit_post_btn');
|
||||
post_title = post_title || document.getElementById('post_title');
|
||||
reply_title = reply_title || document.getElementById('reply_title');
|
||||
post_content = post_content || document.getElementById('post_content');
|
||||
|
||||
|
||||
post_window = post_window || document.getElementById('post_window');
|
||||
jQuery(post_window).slideToggle(250);
|
||||
document.getElementById('post_title').focus();
|
||||
|
||||
if (post_mode == null || post_mode == 'topic') {
|
||||
post_title.style.display = "block";
|
||||
reply_title.style.display = "none";
|
||||
post_title.focus();
|
||||
submit_post_btn.onclick = function() {
|
||||
app.post_topic();
|
||||
}
|
||||
} else {
|
||||
post_title.style.display = "none";
|
||||
reply_title.style.display = "block";
|
||||
reply_title.innerHTML = 'You are replying to "' + title + '"';
|
||||
post_content.focus();
|
||||
submit_post_btn.onclick = function() {
|
||||
app.post_reply(id)
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
app.post_reply = function(topic_id) {
|
||||
var content = document.getElementById('post_content').value;
|
||||
|
||||
if (content.length < 5) {
|
||||
app.alert({
|
||||
title: 'Reply Failure',
|
||||
message: 'You need to write more dude.',
|
||||
type: 'error',
|
||||
timeout: 2000
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
socket.emit('api:posts.reply', {
|
||||
'topic_id' : topic_id,
|
||||
'content' : content
|
||||
});
|
||||
jQuery(post_window).slideToggle(250);
|
||||
|
||||
};
|
||||
app.post_topic = function() {
|
||||
var title = document.getElementById('post_title').value,
|
||||
content = document.getElementById('post_content').value;
|
||||
@@ -91,10 +148,7 @@ var socket,
|
||||
title: 'Topic Post Failure',
|
||||
message: 'You need to write more dude.',
|
||||
type: 'error',
|
||||
timeout: 2000,
|
||||
clickfn: function() {
|
||||
ajaxify.go('register');
|
||||
}
|
||||
timeout: 2000
|
||||
});
|
||||
|
||||
return;
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
var templates = {};
|
||||
|
||||
(function() {
|
||||
var ready_callback;
|
||||
|
||||
templates.ready = function(callback) {
|
||||
//quick implementation because introducing a lib to handle several async callbacks
|
||||
if (callback == null) ready_callback();
|
||||
else ready_callback = callback;
|
||||
}
|
||||
|
||||
function loadTemplates(templatesToLoad) {
|
||||
var timestamp = new Date().getTime();
|
||||
var loaded = templatesToLoad.length;
|
||||
|
||||
for (var t in templatesToLoad) {
|
||||
(function(file) {
|
||||
@@ -18,6 +26,12 @@ var templates = {};
|
||||
template.prototype.html = String(html);
|
||||
|
||||
templates[file] = new template;
|
||||
|
||||
loaded--;
|
||||
if (loaded == 0) templates.ready();
|
||||
}).fail(function() {
|
||||
loaded--;
|
||||
if (loaded == 0) templates.ready();
|
||||
});
|
||||
}(templatesToLoad[t]));
|
||||
}
|
||||
@@ -26,7 +40,7 @@ var templates = {};
|
||||
|
||||
function init() {
|
||||
loadTemplates([
|
||||
'header', 'footer', 'register', 'home',
|
||||
'header', 'footer', 'register', 'home', 'topic',
|
||||
'login', 'reset', 'reset_code', 'account_settings',
|
||||
'emails/reset', 'emails/reset_plaintext'
|
||||
]);
|
||||
@@ -62,6 +76,10 @@ var templates = {};
|
||||
var template = this.html, regex, block;
|
||||
|
||||
return (function parse(data, namespace, template) {
|
||||
if (data.length == 0) {
|
||||
regex = makeRegex('[^]*');
|
||||
template = template.replace(regex, '');
|
||||
}
|
||||
|
||||
for (var d in data) {
|
||||
if (data.hasOwnProperty(d)) {
|
||||
@@ -112,10 +130,10 @@ function load_template(callback) {
|
||||
rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : '');
|
||||
|
||||
var url = location.href.replace(rootUrl +'/', '');
|
||||
if (url == '') url = 'home';
|
||||
jQuery.get('api/' + url, function(data) {
|
||||
|
||||
document.getElementById('content').innerHTML = templates[url].parse(JSON.parse(data));
|
||||
url = (url === '' || url === '/') ? 'home' : url;
|
||||
|
||||
jQuery.get(API_URL + url, function(data) {
|
||||
document.getElementById('content').innerHTML = templates[url.split('/')[0]].parse(JSON.parse(data));
|
||||
if (callback) callback();
|
||||
});
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
<h1>Account Settings</h1>
|
||||
<div class="well">
|
||||
<div class="alert" id="message" style="display:none">
|
||||
<button type="button" class="close" data-dismiss="message">×</button>
|
||||
<strong></strong>
|
||||
<p></p>
|
||||
</div>
|
||||
<!-- <label for="email">Email Address</label><input type="text" placeholder="Enter Email Address" id="email" /><br />
|
||||
<button class="btn btn-primary" id="reset" type="submit">Reset Password</button> -->
|
||||
<p>
|
||||
If you see this, you are logged in.
|
||||
</p>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
(function() {
|
||||
// ...
|
||||
}());
|
||||
</script>
|
||||
@@ -4,7 +4,7 @@
|
||||
<!-- START Forum Info -->
|
||||
<div id="footer" class="container" style="padding-top: 50px;">
|
||||
<div class="alert alert-info">
|
||||
<span id="active_users"></span><br />
|
||||
<span id="active_users"></span>; <span id="active_record"></span><br />
|
||||
<span id="number_of_users"></span><br />
|
||||
<span id="latest_user"></span>
|
||||
</div>
|
||||
@@ -16,7 +16,9 @@
|
||||
var num_users = document.getElementById('number_of_users'),
|
||||
latest_user = document.getElementById('latest_user'),
|
||||
active_users = document.getElementById('active_users'),
|
||||
user_label = document.getElementById('user_label');
|
||||
user_label = document.getElementById('user_label'),
|
||||
active_record = document.getElementById('active_record'),
|
||||
right_menu = document.getElementById('right-menu');
|
||||
|
||||
socket.emit('user.count', {});
|
||||
socket.on('user.count', function(data) {
|
||||
@@ -24,22 +26,49 @@
|
||||
});
|
||||
socket.emit('user.latest', {});
|
||||
socket.on('user.latest', function(data) {
|
||||
latest_user.innerHTML = "The most recent user to register is <b>" + data.username + "</b>.";
|
||||
if (data.username == '') {
|
||||
latest_user.innerHTML = '';
|
||||
} else {
|
||||
latest_user.innerHTML = "The most recent user to register is <b>" + data.username + "</b>.";
|
||||
}
|
||||
});
|
||||
socket.emit('api:user.active.get');
|
||||
socket.on('api:user.active.get', function(data) {
|
||||
active_users.innerHTML = 'There ' + (parseInt(data.users) !== 1 ? 'are' : 'is') + ' <strong>' + data.users + '</strong> user' + (parseInt(data.users) !== 1 ? 's' : '') + ' and <strong>' + data.anon + '</strong> guest' + (parseInt(data.anon) !== 1 ? 's' : '') + ' online';
|
||||
var plural_users = parseInt(data.users) !== 1,
|
||||
plural_anon = parseInt(data.anon) !== 1;
|
||||
|
||||
active_users.innerHTML = 'There ' + (plural_users ? 'are' : 'is') + ' <strong>' + data.users + '</strong> user' + (plural_users ? 's' : '') + ' and <strong>' + data.anon + '</strong> guest' + (plural_anon ? 's' : '') + ' online';
|
||||
});
|
||||
socket.emit('api:user.active.get_record');
|
||||
socket.on('api:user.active.get_record', function(data) {
|
||||
active_record.innerHTML = "most users ever online was <strong>" + data.record + "</strong> on <strong>" + (new Date(parseInt(data.timestamp,10))).toUTCString() + "</strong>";
|
||||
});
|
||||
socket.emit('api:user.get', { fields: ['username', 'picture'] });
|
||||
socket.on('api:user.get', function(data) {
|
||||
var gravatar = document.createElement('img'),
|
||||
name = document.createElement('span');
|
||||
if (data.uid > 0) {
|
||||
var gravatar = document.createElement('img'),
|
||||
name = document.createElement('span')
|
||||
logoutEl = document.createElement('li');
|
||||
|
||||
name.innerHTML = data['username'];
|
||||
gravatar.src = data['picture'];
|
||||
logoutEl.innerHTML = '<a href="/logout">Log out</a>';
|
||||
|
||||
user_label.appendChild(gravatar);
|
||||
user_label.appendChild(name);
|
||||
name.innerHTML = data['username'];
|
||||
gravatar.src = data['picture'];
|
||||
|
||||
user_label.innerHTML = '';
|
||||
user_label.appendChild(gravatar);
|
||||
user_label.appendChild(name);
|
||||
right_menu.appendChild(logoutEl);
|
||||
} else {
|
||||
var registerEl = document.createElement('li'),
|
||||
loginEl = document.createElement('li');
|
||||
|
||||
registerEl.innerHTML = '<a href="/register">Register</a>';
|
||||
loginEl.innerHTML = '<a href="/login">Login</a>';
|
||||
|
||||
right_menu.appendChild(registerEl);
|
||||
right_menu.appendChild(loginEl);
|
||||
}
|
||||
});
|
||||
}());
|
||||
</script>
|
||||
|
||||
@@ -90,33 +90,68 @@
|
||||
border: 1px solid #eee;
|
||||
margin-top: 50px;
|
||||
}
|
||||
.topic-container li.topic-row:nth-child(odd) {
|
||||
.topic-container a:nth-child(odd) li.topic-row {
|
||||
background-color:#fdfdfd;
|
||||
}
|
||||
.topic-container li.topic-row:nth-child(even) {
|
||||
.topic-container a:nth-child(even) li.topic-row {
|
||||
background-color:#fff;
|
||||
}
|
||||
.topic-container li.topic-row {
|
||||
cursor: pointer;
|
||||
border-bottom: 1px solid #eee;
|
||||
padding: 10px;
|
||||
|
||||
}
|
||||
.topic-container li:last-child {
|
||||
border-bottom: 0;
|
||||
}
|
||||
.topic-container li.topic-row:hover {
|
||||
background-color: #eee;
|
||||
}
|
||||
|
||||
|
||||
|
||||
.post-container {
|
||||
list-style-type: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
border: 1px solid #eee;
|
||||
|
||||
}
|
||||
.post-container li.post-row:nth-child(odd) {
|
||||
background-color:#fdfdfd;
|
||||
}
|
||||
.post-container li.post-row:nth-child(even) {
|
||||
background-color:#fff;
|
||||
}
|
||||
.post-container li.post-row {
|
||||
cursor: pointer;
|
||||
border-bottom: 1px solid #eee;
|
||||
padding: 10px;
|
||||
}
|
||||
.post-container li:last-child {
|
||||
border-bottom: 0;
|
||||
}
|
||||
.post-container li.post-row:hover {
|
||||
background-color: #eee;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#user_label img {
|
||||
border: 1px solid #999;
|
||||
margin-right: 8px;
|
||||
margin-top: -2px;
|
||||
float: left;
|
||||
}
|
||||
|
||||
#user_label span {
|
||||
font-size: 12px;
|
||||
font-weight: bold;
|
||||
}
|
||||
#reply_title {
|
||||
font-size: 17px;
|
||||
padding-top: 14px;
|
||||
font-weight: 600;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
@@ -132,13 +167,10 @@
|
||||
</button>
|
||||
<div class="nav-collapse collapse">
|
||||
<ul class="nav">
|
||||
<li class="active"><a href="/">Home</a></li>
|
||||
<li><a href="/register">Register</a></li>
|
||||
<li><a href="/login">Login</a></li>
|
||||
<li class="active"><a href="/">Forum</a></li>
|
||||
</ul>
|
||||
<ul class="nav pull-right">
|
||||
<li><a href="#" id="user_label"></a></li>
|
||||
<li><a href="/logout">Log out</a></li>
|
||||
<ul class="nav pull-right" id="right-menu">
|
||||
<li><p class="navbar-text" id="user_label"></p></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
@@ -148,6 +180,7 @@
|
||||
<div class="post-title-container">
|
||||
<div class="container">
|
||||
<input id="post_title" placeholder="Enter your topic title here." />
|
||||
<span id="reply_title"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="post-content-container">
|
||||
@@ -161,7 +194,7 @@
|
||||
<a class="btn btn-link" href="#" tabindex="-1"><i class="icon-list"></i></a>
|
||||
</div>
|
||||
<div class="btn-group" style="float: right; margin-right: -12px">
|
||||
<a class="btn" onclick="app.post_topic()"><i class="icon-ok"></i> Submit</a>
|
||||
<a id="submit_post_btn" class="btn" onclick="app.post_topic()"><i class="icon-ok"></i> Submit</a>
|
||||
<a class="btn" onclick="jQuery(post_window).slideToggle(250);"><i class="icon-remove"></i> Discard</a>
|
||||
</div>
|
||||
</div>
|
||||
@@ -172,4 +205,5 @@
|
||||
</div>
|
||||
</div>
|
||||
<div id="notification_window"></div>
|
||||
|
||||
<div class="container" id="content">
|
||||
@@ -1,9 +1,9 @@
|
||||
<button id="new_post" class="btn btn-primary btn-large">New Post</button>
|
||||
<ul class="topic-container">
|
||||
<!-- BEGIN topics -->
|
||||
<a href="topics/{topics.slug}"><li class="topic-row">
|
||||
<a href="topic/{topics.slug}"><li class="topic-row">
|
||||
<h4>{topics.title}</h4>
|
||||
<p>Posted on {topics.timestamp} by user {topics.uid}. {topics.post_count} posts.</p>
|
||||
<p>Posted {topics.relativeTime} by user {topics.uid}. {topics.post_count} posts.</p>
|
||||
</li></a>
|
||||
<!-- END topics -->
|
||||
</ul>
|
||||
|
||||
@@ -4,33 +4,10 @@
|
||||
<button type="button" class="close" data-dismiss="alert">×</button>
|
||||
<strong>Failed Login Attempt</strong> <p></p>
|
||||
</div>
|
||||
<label>Username</label><input type="text" placeholder="Enter Username" id="username" /><br />
|
||||
<label>Password</label><input type="password" placeholder="Enter Password" id="password" /><br />
|
||||
<button class="btn btn-primary" id="login" type="submit">Login</button>
|
||||
<form method="post" action="/login">
|
||||
<label>Username</label><input type="text" placeholder="Enter Username" name="username" id="username" /><br />
|
||||
<label>Password</label><input type="password" placeholder="Enter Password" name="password" id="password" /><br />
|
||||
<button class="btn btn-primary" id="login" type="submit">Login</button>
|
||||
</form>
|
||||
<a href="/reset">Forgot Password?</a>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
(function() {
|
||||
var username = document.getElementById('username'),
|
||||
password = document.getElementById('password'),
|
||||
login = document.getElementById('login'),
|
||||
error = document.getElementById('error');
|
||||
|
||||
login.onclick = function() {
|
||||
socket.emit('user.login', {
|
||||
username: username.value,
|
||||
password: password.value
|
||||
});
|
||||
};
|
||||
|
||||
ajaxify.register_events(['user.login']);
|
||||
socket.on('user.login', function(data) {
|
||||
if (data.status === 0) {
|
||||
jQuery('#error').show(50);
|
||||
jQuery('#error p').html(data.message);
|
||||
} else {
|
||||
document.location.href = '/';
|
||||
}
|
||||
});
|
||||
}());
|
||||
</script>
|
||||
</div>
|
||||
@@ -1,8 +1,23 @@
|
||||
<ul class="topic-container">
|
||||
<div class="container">
|
||||
<ul class="breadcrumb">
|
||||
<li><a href="/">Home</a> <span class="divider">/</span></li>
|
||||
<li class="active">{TOPIC_NAME}</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<ul class="post-container">
|
||||
<!-- BEGIN posts -->
|
||||
<li class="topic-row">
|
||||
<li class="post-row">
|
||||
<p>{posts.content}</p>
|
||||
<p>Posted on {posts.timestamp} by user {posts.uid}.</p>
|
||||
<p>Posted {posts.relativeTime} by user {posts.uid}.</p>
|
||||
</li>
|
||||
<!-- END posts -->
|
||||
</ul>
|
||||
</ul>
|
||||
<hr />
|
||||
<button id="post_reply" class="btn btn-primary btn-large">Reply</button>
|
||||
<script type="text/javascript">
|
||||
var post_reply = document.getElementById('post_reply');
|
||||
post_reply.onclick = function() {
|
||||
app.open_post_window('reply', "{TOPIC_ID}", "{TOPIC_NAME}");
|
||||
}
|
||||
</script>
|
||||
|
||||
91
src/posts.js
91
src/posts.js
@@ -1,4 +1,5 @@
|
||||
var RDB = require('./redis.js');
|
||||
var RDB = require('./redis.js'),
|
||||
utils = require('./utils.js');
|
||||
|
||||
(function(Posts) {
|
||||
//data structure
|
||||
@@ -15,60 +16,70 @@ var RDB = require('./redis.js');
|
||||
if (start == null) start = 0;
|
||||
if (end == null) end = start + 10;
|
||||
|
||||
RDB.lrange('tid:' + tid + ':posts', start, end, function(pids) {
|
||||
RDB.get('tid:' + tid + ':title', function(topic_name) { //do these asynch later
|
||||
RDB.lrange('tid:' + tid + ':posts', start, end, function(pids) {
|
||||
var content = [],
|
||||
uid = [],
|
||||
timestamp = [];
|
||||
|
||||
var content = [],
|
||||
uid = [],
|
||||
timestamp = [];
|
||||
for (var i=0, ii=pids.length; i<ii; i++) {
|
||||
content.push('pid:' + pids[i] + ':content');
|
||||
uid.push('pid:' + pids[i] + ':uid');
|
||||
timestamp.push('pid:' + pids[i] + ':timestamp');
|
||||
}
|
||||
|
||||
for (var i=0, ii=pids.length; i<ii; i++) {
|
||||
content.push('pid:' + pids[i] + ':content');
|
||||
uid.push('pid:' + pids[i] + ':uid');
|
||||
timestamp.push('pid:' + pids[i] + ':timestamp');
|
||||
}
|
||||
|
||||
if (pids.length > 0) {
|
||||
RDB.multi()
|
||||
.mget(content)
|
||||
.mget(uid)
|
||||
.mget(timestamp)
|
||||
.exec(function(err, replies) {
|
||||
content = replies[0];
|
||||
uid = replies[1];
|
||||
timestamp = replies[2];
|
||||
|
||||
var posts = [];
|
||||
for (var i=0, ii=content.length; i<ii; i++) {
|
||||
posts.push({
|
||||
'content' : content[i],
|
||||
'uid' : uid[i],
|
||||
'timestamp' : timestamp[i]
|
||||
});
|
||||
}
|
||||
|
||||
callback({'posts': posts});
|
||||
});
|
||||
} else {
|
||||
callback({});
|
||||
}
|
||||
if (pids.length > 0) {
|
||||
RDB.multi()
|
||||
.mget(content)
|
||||
.mget(uid)
|
||||
.mget(timestamp)
|
||||
.exec(function(err, replies) {
|
||||
content = replies[0];
|
||||
uid = replies[1];
|
||||
timestamp = replies[2];
|
||||
|
||||
var posts = [];
|
||||
for (var i=0, ii=content.length; i<ii; i++) {
|
||||
posts.push({
|
||||
'content' : content[i],
|
||||
'uid' : uid[i],
|
||||
'timestamp' : timestamp[i],
|
||||
'relativeTime': utils.relativeTime(timestamp[i])
|
||||
});
|
||||
}
|
||||
|
||||
callback({'TOPIC_NAME':topic_name, 'TOPIC_ID': tid, 'posts': posts});
|
||||
});
|
||||
} else {
|
||||
callback({});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Posts.reply = function() {
|
||||
Posts.reply = function(tid, uid, content) {
|
||||
Posts.create(uid, 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
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Posts.create = function(content, callback) {
|
||||
if (global.uid === null) return;
|
||||
|
||||
Posts.create = function(uid, content, callback) {
|
||||
if (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 + ':uid', uid);
|
||||
RDB.set('pid:' + pid + ':timestamp', new Date().getTime());
|
||||
|
||||
// User Details - move this out later
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
ERROR_LOGS = true,
|
||||
|
||||
redis = require('redis'),
|
||||
db = redis.createClient();
|
||||
config = require('../config.js'),
|
||||
db = redis.createClient(config.redis.port, config.redis.host, config.redis.options);
|
||||
|
||||
// todo (holy cow): append,auth,bgrewriteaof,bgsave,bitcount,bitop,blpop,brpop,brpoplpush,client kill,client list,client getname,client setname,config get,config set,config resetstat,dbsize,debug object,debug segfault,decrby,discard,dump,echo,eval,evalsha,exec,exists,expireat,flushall,flushdb,getbit,getrange,getset,hdel,hexists,hget,hgetall,hincrby,hincrbyfloat,hkeys,hlen,hmget,hmset,hset,hsetnx,hvals,incrby,incrbyfloat,info,lastsave,lindex,linsert,llen,lpop,lpushx,lrem,lset,ltrim,migrate,monitor,move,mset,msetnx,object,persist,pexpire,pexpireat,ping,psetex,psubscribe,pttl,publish,punsubscribe,quit,randomkey,rename,renamenx,restore,rpop,rpoplpush,rpush,rpushx,sadd,save,scard,script exists,script flush,script kill,script load,sdiff,sdiffstore,select,setbit,setex,setnx,setrange,shutdown,sinter,sinterstore,sismember,slaveof,slowlog,smembers,smove,sort,spop,srandmember,srem,strlen,subscribe,sunion,sunionstore,sync,time,ttl,type,unsubscribe,unwatch,watch,zadd,zcard,zcount,zincrby,zinterstore,zrange,zrangebyscore,zrank,zrem,zremrangebyrank,zremrangebyscore,zrevrange,zrevrangebyscore,zrevrank,zscore,zunionstore
|
||||
// done: get, set, incr, decr, del, mget, multi, expire, lpush, lrange, keys
|
||||
@@ -75,6 +76,10 @@
|
||||
db.lpush(key, item);
|
||||
}
|
||||
|
||||
RedisDB.rpush = function(key, item) {
|
||||
db.rpush(key, item);
|
||||
}
|
||||
|
||||
RedisDB.lrange = function(key, start, end, callback, error_handler) {
|
||||
db.lrange(key, start, end, function(error, data) {
|
||||
return_handler(error, data, callback, error_handler);
|
||||
|
||||
@@ -26,8 +26,8 @@ var fs = require('fs');
|
||||
Templates.init = function() {
|
||||
loadTemplates([
|
||||
'header', 'footer', 'register', 'home', 'topic',
|
||||
'login', 'reset', 'reset_code', 'account_settings',
|
||||
'logout', '403',
|
||||
'login', 'reset', 'reset_code', 'logout',
|
||||
'403',
|
||||
'emails/reset', 'emails/reset_plaintext'
|
||||
]);
|
||||
}
|
||||
@@ -60,7 +60,11 @@ var fs = require('fs');
|
||||
var template = this.html, regex, block;
|
||||
|
||||
return (function parse(data, namespace, template) {
|
||||
|
||||
if (Object.keys(data).length == 0) {
|
||||
regex = makeRegex('[^]*');
|
||||
template = template.replace(regex, '');
|
||||
}
|
||||
|
||||
for (var d in data) {
|
||||
if (data.hasOwnProperty(d)) {
|
||||
if (data[d] instanceof String || data[d] === null) {
|
||||
@@ -89,7 +93,7 @@ var fs = require('fs');
|
||||
|
||||
block = parse(data[d], namespace, block);
|
||||
template = setBlock(regex, block, template);
|
||||
} else {
|
||||
} else {
|
||||
template = replace(namespace + d, data[d], template);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
var RDB = require('./redis.js'),
|
||||
posts = require('./posts.js');
|
||||
|
||||
|
||||
|
||||
posts = require('./posts.js'),
|
||||
utils = require('./utils.js');
|
||||
|
||||
(function(Topics) {
|
||||
//data structure
|
||||
@@ -82,6 +80,7 @@ var RDB = require('./redis.js'),
|
||||
'title' : title[i],
|
||||
'uid' : uid[i],
|
||||
'timestamp' : timestamp[i],
|
||||
'relativeTime': utils.relativeTime(timestamp[i]),
|
||||
'slug' : slug[i],
|
||||
'post_count' : postcount[i]
|
||||
});
|
||||
@@ -97,7 +96,7 @@ var RDB = require('./redis.js'),
|
||||
|
||||
Topics.post = function(uid, title, content, category) {
|
||||
if (uid === 0) {
|
||||
global.socket.emit('event:alert', {
|
||||
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',
|
||||
@@ -137,7 +136,7 @@ var RDB = require('./redis.js'),
|
||||
RDB.set('topic:slug:' + slug + ':tid', tid);
|
||||
|
||||
// Posts
|
||||
posts.create(content, function(pid) {
|
||||
posts.create(uid, content, function(pid) {
|
||||
RDB.lpush('tid:' + tid + ':posts', pid);
|
||||
});
|
||||
|
||||
@@ -146,7 +145,7 @@ var RDB = require('./redis.js'),
|
||||
RDB.lpush('uid:' + uid + ':topics', tid);
|
||||
|
||||
|
||||
global.socket.emit('event:alert', {
|
||||
socket.emit('event:alert', {
|
||||
title: 'Thank you for posting',
|
||||
message: 'You have successfully posted. Click here to view your post.',
|
||||
type: 'notify',
|
||||
|
||||
112
src/user.js
112
src/user.js
@@ -1,5 +1,5 @@
|
||||
var config = require('../config.js'),
|
||||
utils = require('../utils.js'),
|
||||
utils = require('./utils.js'),
|
||||
RDB = require('./redis.js'),
|
||||
crypto = require('crypto'),
|
||||
emailjs = require('emailjs'),
|
||||
@@ -10,7 +10,9 @@ var config = require('../config.js'),
|
||||
User.get = function(uid, fields) {
|
||||
if (uid > 0) {
|
||||
var keys = [],
|
||||
returnData = {},
|
||||
returnData = {
|
||||
uid: uid
|
||||
},
|
||||
removeEmail = false;
|
||||
|
||||
if (!(fields instanceof Array)) fields = ['username', 'email'];
|
||||
@@ -27,7 +29,7 @@ var config = require('../config.js'),
|
||||
for(var x=0,numData=data.length;x<numData;x++) {
|
||||
returnData[fields[x]] = data[x];
|
||||
}
|
||||
console.log(returnData);
|
||||
|
||||
if (returnData.picture !== undefined) {
|
||||
var md5sum = crypto.createHash('md5');
|
||||
md5sum.update(returnData.email.toLowerCase());
|
||||
@@ -47,17 +49,17 @@ var config = require('../config.js'),
|
||||
|
||||
User.login = function(user) {
|
||||
if (user.username == null || user.password == null) {
|
||||
return global.socket.emit('user.login', {'status': 0, 'message': 'Missing fields'});
|
||||
return socket.emit('user.login', {'status': 0, 'message': 'Missing fields'});
|
||||
}
|
||||
|
||||
RDB.get('username:' + user.username + ':uid', function(uid) {
|
||||
if (uid == null) {
|
||||
return global.socket.emit('user.login', {'status': 0, 'message': 'Username does not exist.'});
|
||||
return socket.emit('user.login', {'status': 0, 'message': 'Username does not exist.'});
|
||||
}
|
||||
|
||||
RDB.get('uid:' + uid + ':password', function(password) {
|
||||
if (user.password != password) {
|
||||
return global.socket.emit('user.login', {'status': 0, 'message': 'Incorrect username / password combination.'});
|
||||
return socket.emit('user.login', {'status': 0, 'message': 'Incorrect username / password combination.'});
|
||||
} else {
|
||||
// Start, replace, or extend a session
|
||||
RDB.get('sess:' + user.sessionID, function(session) {
|
||||
@@ -76,6 +78,51 @@ var config = require('../config.js'),
|
||||
});
|
||||
};
|
||||
|
||||
User.loginViaLocal = function(username, password, next) {
|
||||
if (!username || !password) {
|
||||
return next({
|
||||
status: 'error',
|
||||
message: 'invalid-user'
|
||||
});
|
||||
} else {
|
||||
RDB.get('username:' + username + ':uid', function(uid) {
|
||||
if (uid == null) {
|
||||
return next({
|
||||
status: 'error',
|
||||
message: 'invalid-user'
|
||||
});
|
||||
}
|
||||
|
||||
RDB.get('uid:' + uid + ':password', function(user_password) {
|
||||
if (password == user_password) {
|
||||
// Start, replace, or extend a session
|
||||
// RDB.get('sess:' + user.sessionID, function(session) {
|
||||
// if (session !== user.sessionID) {
|
||||
// RDB.set('sess:' + user.sessionID + ':uid', uid, 60*60*24*14); // Login valid for two weeks
|
||||
// RDB.set('uid:' + uid + ':session', user.sessionID, 60*60*24*14);
|
||||
// } else {
|
||||
// RDB.expire('sess:' + user.sessionID + ':uid', 60*60*24*14); // Defer expiration to two weeks from now
|
||||
// RDB.expire('uid:' + uid + ':session', 60*60*24*14);
|
||||
// }
|
||||
// });
|
||||
|
||||
next({
|
||||
status: "ok",
|
||||
user: {
|
||||
uid: uid
|
||||
}
|
||||
});
|
||||
} else {
|
||||
next({
|
||||
status: 'error',
|
||||
message: 'invalid-password'
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
User.logout = function(sessionID, callback) {
|
||||
User.get_uid_by_session(sessionID, function(uid) {
|
||||
if (uid) {
|
||||
@@ -88,7 +135,7 @@ var config = require('../config.js'),
|
||||
|
||||
User.create = function(username, password, email) {
|
||||
if (username == null || password == null) {
|
||||
return; global.socket.emit('user.create', {'status': 0, 'message': 'Missing fields'});
|
||||
return; socket.emit('user.create', {'status': 0, 'message': 'Missing fields'});
|
||||
}
|
||||
|
||||
|
||||
@@ -111,9 +158,9 @@ var config = require('../config.js'),
|
||||
RDB.lpush('user:users', username);
|
||||
io.sockets.emit('user.latest', {username: username});
|
||||
|
||||
global.socket.emit('user.create', {'status': 1});
|
||||
socket.emit('user.create', {'status': 1});
|
||||
|
||||
global.socket.emit('event:alert', {
|
||||
socket.emit('event:alert', {
|
||||
title: 'Thank you for registering',
|
||||
message: 'You have successfully registered - welcome to nodebb!',
|
||||
type: 'notify',
|
||||
@@ -127,7 +174,7 @@ var config = require('../config.js'),
|
||||
User.exists = function(username, callback) {
|
||||
User.get_uid_by_username(username, function(exists) {
|
||||
exists = !!exists;
|
||||
global.socket.emit('user.exists', {exists: exists})
|
||||
socket.emit('user.exists', {exists: exists})
|
||||
|
||||
if (callback) {
|
||||
callback(exists);
|
||||
@@ -136,12 +183,12 @@ var config = require('../config.js'),
|
||||
};
|
||||
User.count = function() {
|
||||
RDB.get('user:count', function(count) {
|
||||
global.socket.emit('user.count', {count: (count === null) ? 0 : count});
|
||||
socket.emit('user.count', {count: (count === null) ? 0 : count});
|
||||
});
|
||||
};
|
||||
User.latest = function() {
|
||||
RDB.lrange('user:users', 0, 0, function(username) {
|
||||
global.socket.emit('user.latest', {username: username});
|
||||
socket.emit('user.latest', {username: username});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -157,6 +204,14 @@ var config = require('../config.js'),
|
||||
RDB.get('sess:' + session + ':uid', callback);
|
||||
};
|
||||
|
||||
User.session_ping = function(sessionID, uid) {
|
||||
// Start, replace, or extend a session
|
||||
RDB.get('sess:' + sessionID, function(session) {
|
||||
RDB.set('sess:' + sessionID + ':uid', uid, 60*60*24*14); // Login valid for two weeks
|
||||
RDB.set('uid:' + uid + ':session', sessionID, 60*60*24*14);
|
||||
});
|
||||
}
|
||||
|
||||
User.reset = {
|
||||
validate: function(code, callback) {
|
||||
if (typeof callback !== 'function') callback = undefined;
|
||||
@@ -165,18 +220,18 @@ var config = require('../config.js'),
|
||||
if (uid !== null) {
|
||||
RDB.get('reset:' + code + ':expiry', function(expiry) {
|
||||
if (expiry >= +new Date()/1000|0) {
|
||||
if (!callback) global.socket.emit('user:reset.valid', { valid: true });
|
||||
if (!callback) socket.emit('user:reset.valid', { valid: true });
|
||||
else callback(true);
|
||||
} else {
|
||||
// Expired, delete from db
|
||||
RDB.del('reset:' + code + ':uid');
|
||||
RDB.del('reset:' + code + ':expiry');
|
||||
if (!callback) global.socket.emit('user:reset.valid', { valid: false });
|
||||
if (!callback) socket.emit('user:reset.valid', { valid: false });
|
||||
else callback(false);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (!callback) global.socket.emit('user:reset.valid', { valid: false });
|
||||
if (!callback) socket.emit('user:reset.valid', { valid: false });
|
||||
else callback(false);
|
||||
}
|
||||
});
|
||||
@@ -208,13 +263,13 @@ var config = require('../config.js'),
|
||||
|
||||
emailjsServer.send(message, function(err, success) {
|
||||
if (err === null) {
|
||||
global.socket.emit('user.send_reset', {
|
||||
socket.emit('user.send_reset', {
|
||||
status: "ok",
|
||||
message: "code-sent",
|
||||
email: email
|
||||
});
|
||||
} else {
|
||||
global.socket.emit('user.send_reset', {
|
||||
socket.emit('user.send_reset', {
|
||||
status: "error",
|
||||
message: "send-failed"
|
||||
});
|
||||
@@ -222,7 +277,7 @@ var config = require('../config.js'),
|
||||
}
|
||||
});
|
||||
} else {
|
||||
global.socket.emit('user.send_reset', {
|
||||
socket.emit('user.send_reset', {
|
||||
status: "error",
|
||||
message: "invalid-email",
|
||||
email: email
|
||||
@@ -238,7 +293,7 @@ var config = require('../config.js'),
|
||||
RDB.del('reset:' + code + ':uid');
|
||||
RDB.del('reset:' + code + ':expiry');
|
||||
|
||||
global.socket.emit('user:reset.commit', { status: 'ok' });
|
||||
socket.emit('user:reset.commit', { status: 'ok' });
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -249,14 +304,29 @@ var config = require('../config.js'),
|
||||
exists: function(email, callback) {
|
||||
User.get_uid_by_email(email, function(exists) {
|
||||
exists = !!exists;
|
||||
if (typeof callback !== 'function') global.socket.emit('user.email.exists', { exists: exists });
|
||||
if (typeof callback !== 'function') socket.emit('user.email.exists', { exists: exists });
|
||||
else callback(exists);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
User.active = {
|
||||
get_record : function() {
|
||||
RDB.mget(['global:active_user_record', 'global:active_user_record_date'], function(data) {
|
||||
socket.emit('api:user.active.get_record', {record: data[0], timestamp: data[1]});
|
||||
});
|
||||
},
|
||||
|
||||
get: function(callback) {
|
||||
function user_record(total) {
|
||||
RDB.get('global:active_user_record', function(record) {
|
||||
if (total > record) {
|
||||
RDB.set('global:active_user_record', total);
|
||||
RDB.set('global:active_user_record_date', new Date().getTime());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
RDB.keys('active:*', function(active) {
|
||||
var returnObj = {
|
||||
users: 0,
|
||||
@@ -282,6 +352,8 @@ var config = require('../config.js'),
|
||||
}
|
||||
}
|
||||
|
||||
user_record(returnObj.anon + returnObj.users);
|
||||
|
||||
if (callback === undefined) {
|
||||
io.sockets.emit('api:user.active.get', returnObj)
|
||||
} else {
|
||||
|
||||
35
src/utils.js
Normal file
35
src/utils.js
Normal file
@@ -0,0 +1,35 @@
|
||||
var utils = {
|
||||
generateUUID: function() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
||||
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
},
|
||||
relativeTime: function(timestamp) {
|
||||
var now = +new Date(),
|
||||
difference = now - Math.floor(parseFloat(timestamp));
|
||||
|
||||
difference = Math.floor(difference / 1000);
|
||||
if (difference < 60) return difference + ' second' + (difference !== 1 ? 's' : '') + ' ago';
|
||||
|
||||
difference = Math.floor(difference / 60);
|
||||
if (difference < 60) return difference + ' minute' + (difference !== 1 ? 's' : '') + ' ago';
|
||||
|
||||
difference = Math.floor(difference / 60);
|
||||
if (difference < 24) return difference + ' hour' + (difference !== 1 ? 's' : '') + ' ago';
|
||||
|
||||
difference = Math.floor(difference / 24);
|
||||
if (difference < 3) return difference + ' day' + (difference !== 1 ? 's' : '') + ' ago';
|
||||
|
||||
// Lastly, just return a formatted date
|
||||
var date = new Date(timestamp);
|
||||
// hour = date.getHours(),
|
||||
// minute = date.getMinutes(),
|
||||
// day = date.getDate(),
|
||||
// month = date.getMonth(),
|
||||
// months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
||||
return date.toDateString();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = utils;
|
||||
120
src/webserver.js
120
src/webserver.js
@@ -3,7 +3,28 @@ var express = require('express'),
|
||||
server = require('http').createServer(WebServer),
|
||||
RedisStore = require('connect-redis')(express),
|
||||
path = require('path'),
|
||||
config = require('../config.js');
|
||||
config = require('../config.js'),
|
||||
redis = require('redis'),
|
||||
redisServer = redis.createClient(config.redis.port, config.redis.host, config.redis.options),
|
||||
passport = require('passport'),
|
||||
passportLocal = require('passport-local').Strategy;
|
||||
|
||||
passport.use(new passportLocal(function(user, password, next) {
|
||||
global.modules.user.loginViaLocal(user, password, function(login) {
|
||||
if (login.status === 'ok') next(null, login.user);
|
||||
else next(null, false, login);
|
||||
});
|
||||
}));
|
||||
|
||||
passport.serializeUser(function(user, done) {
|
||||
done(null, user.uid);
|
||||
});
|
||||
|
||||
passport.deserializeUser(function(uid, done) {
|
||||
done(null, {
|
||||
uid: uid
|
||||
});
|
||||
});
|
||||
|
||||
(function(app) {
|
||||
var templates = global.templates;
|
||||
@@ -24,31 +45,39 @@ var express = require('express'),
|
||||
app.use(express.compress());
|
||||
app.use(express.session({
|
||||
store: new RedisStore({
|
||||
client: redisServer,
|
||||
ttl: 60*60*24*14
|
||||
}),
|
||||
secret: 'nodebb',
|
||||
secret: config.secret,
|
||||
key: 'express.sid'
|
||||
}));
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
app.use(function(req, res, next) {
|
||||
// Don't bother with session handling for API requests
|
||||
if (/^\/api\//.test(req.url)) return next();
|
||||
|
||||
if (req.session.uid === undefined) {
|
||||
console.log('info: [Auth] First load, retrieving uid...');
|
||||
global.modules.user.get_uid_by_session(req.sessionID, function(uid) {
|
||||
if (uid !== null) {
|
||||
req.session.uid = uid;
|
||||
console.log('info: [Auth] uid ' + req.session.uid + ' found. Welcome back.');
|
||||
} else {
|
||||
req.session.uid = 0;
|
||||
console.log('info: [Auth] No login session found.');
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// console.log('SESSION: ' + req.sessionID);
|
||||
// console.log('info: [Auth] Ping from uid ' + req.session.uid);
|
||||
if (req.user && req.user.uid) {
|
||||
global.modules.user.session_ping(req.sessionID, req.user.uid);
|
||||
}
|
||||
|
||||
// if (req.session.uid === undefined) {
|
||||
// console.log('info: [Auth] First load, retrieving uid...');
|
||||
|
||||
// global.modules.user.get_uid_by_session(req.sessionID, function(uid) {
|
||||
// if (uid !== null) {
|
||||
// req.session.uid = uid;
|
||||
// console.log('info: [Auth] uid ' + req.session.uid + ' found. Welcome back.');
|
||||
// } else {
|
||||
// req.session.uid = 0;
|
||||
// console.log('info: [Auth] No login session found.');
|
||||
// }
|
||||
// });
|
||||
// } else {
|
||||
// // console.log('SESSION: ' + req.sessionID);
|
||||
// // console.log('info: [Auth] Ping from uid ' + req.session.uid);
|
||||
// }
|
||||
|
||||
// (Re-)register the session as active
|
||||
global.modules.user.active.register(req.sessionID);
|
||||
|
||||
@@ -59,58 +88,67 @@ var express = require('express'),
|
||||
// Useful if you want to use app.put and app.delete (instead of app.post all the time)
|
||||
// app.use(express.methodOverride());
|
||||
|
||||
app.get('/', function(req, res) {
|
||||
global.modules.topics.generate_forum_body(function(forum_body) {
|
||||
res.send(templates['header'] + forum_body + templates['footer']);
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/403', function(req, res) {
|
||||
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'];
|
||||
|
||||
// need a proper way to combine these two routes together
|
||||
app.get('/topics/:topic_id', function(req, res) {
|
||||
for (var i=0, ii=routes.length; i<ii; i++) {
|
||||
(function(route) {
|
||||
app.get('/' + route, function(req, res) {
|
||||
res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("' + route + '");});</script>' + templates['footer']);
|
||||
});
|
||||
}(routes[i]));
|
||||
}
|
||||
}());
|
||||
|
||||
|
||||
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('/topics/:topic_id/:slug', function(req, res) {
|
||||
global.modules.topics.generate_topic_body(function(topic_body) {
|
||||
res.send(templates['header'] + topic_body + templates['footer']);
|
||||
}, req.params.topic_id)
|
||||
});
|
||||
}, req.params.topic_id);
|
||||
}
|
||||
app.get('/topic/:topic_id', generate_topic_body);
|
||||
app.get('/topic/:topic_id*', generate_topic_body);
|
||||
|
||||
|
||||
|
||||
app.get('/api/:method', function(req, res) {
|
||||
function api_method(req, res) {
|
||||
switch(req.params.method) {
|
||||
case 'home' :
|
||||
global.modules.topics.get(function(data) {
|
||||
res.send(JSON.stringify(data));
|
||||
});
|
||||
break;
|
||||
case 'topic' :
|
||||
global.modules.posts.get(function(data) {
|
||||
res.send(JSON.stringify(data));
|
||||
}, req.params.id);
|
||||
break;
|
||||
default :
|
||||
res.send('{}');
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
app.get('/api/:method', api_method);
|
||||
app.get('/api/:method/:id', api_method);
|
||||
app.get('/api/:method/:id*', api_method);
|
||||
|
||||
app.get('/login', function(req, res) {
|
||||
res.send(templates['header'] + templates['login'] + templates['footer']);
|
||||
});
|
||||
app.post('/login', passport.authenticate('local', {
|
||||
successRedirect: '/',
|
||||
failureRedirect: '/login'
|
||||
}));
|
||||
|
||||
app.get('/logout', function(req, res) {
|
||||
console.log('info: [Auth] Session ' + res.sessionID + ' logout (uid: ' + global.uid + ')');
|
||||
global.modules.user.logout(req.sessionID, function(logout) {
|
||||
if (logout === true) {
|
||||
delete(req.session.uid);
|
||||
req.session.destroy();
|
||||
}
|
||||
req.logout();
|
||||
res.send(templates['header'] + templates['logout'] + templates['footer']);
|
||||
});
|
||||
|
||||
res.send(templates['header'] + templates['logout'] + templates['footer']);
|
||||
});
|
||||
|
||||
app.get('/reset/:code', function(req, res) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
var SocketIO = require('socket.io').listen(global.server),
|
||||
cookie = require('cookie'),
|
||||
connect = require('connect');
|
||||
connect = require('connect'),
|
||||
config = require('../config.js');
|
||||
|
||||
(function(io) {
|
||||
var modules = null,
|
||||
@@ -16,7 +17,7 @@ var SocketIO = require('socket.io').listen(global.server),
|
||||
io.set('authorization', function(handshakeData, accept) {
|
||||
if (handshakeData.headers.cookie) {
|
||||
handshakeData.cookie = cookie.parse(handshakeData.headers.cookie);
|
||||
handshakeData.sessionID = connect.utils.parseSignedCookie(handshakeData.cookie['express.sid'], 'nodebb');
|
||||
handshakeData.sessionID = connect.utils.parseSignedCookie(handshakeData.cookie['express.sid'], config.secret);
|
||||
|
||||
if (handshakeData.cookie['express.sid'] == handshakeData.sessionID) {
|
||||
return accept('Cookie is invalid.', false);
|
||||
@@ -93,9 +94,17 @@ var SocketIO = require('socket.io').listen(global.server),
|
||||
modules.topics.post(uid, data.title, data.content);
|
||||
});
|
||||
|
||||
socket.on('api:posts.reply', function(data) {
|
||||
modules.posts.reply(data.topic_id, uid, data.content);
|
||||
});
|
||||
|
||||
socket.on('api:user.active.get', function() {
|
||||
modules.user.active.get();
|
||||
});
|
||||
|
||||
socket.on('api:user.active.get_record', function() {
|
||||
modules.user.active.get_record();
|
||||
});
|
||||
});
|
||||
|
||||
}(SocketIO));
|
||||
|
||||
Reference in New Issue
Block a user