mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-04-03 02:59:27 +02:00
Merge branch 'master' of https://github.com/designcreateplay/NodeBB
This commit is contained in:
114
src/templates.js
114
src/templates.js
@@ -1,114 +0,0 @@
|
||||
var fs = require('fs');
|
||||
|
||||
|
||||
// to be deprecated in favour of client-side only templates.
|
||||
|
||||
(function(Templates) {
|
||||
|
||||
global.templates = {};
|
||||
|
||||
function loadTemplates(templatesToLoad) {
|
||||
for (var t in templatesToLoad) {
|
||||
(function(file) {
|
||||
fs.readFile(global.configuration.ROOT_DIRECTORY + '/public/templates/' + file + '.tpl', function(err, html) {
|
||||
var template = function() {
|
||||
this.toString = function() {
|
||||
return this.html;
|
||||
};
|
||||
}
|
||||
|
||||
template.prototype.file = file;
|
||||
template.prototype.parse = parse;
|
||||
template.prototype.html = String(html);
|
||||
|
||||
global.templates[file] = new template;
|
||||
});
|
||||
}(templatesToLoad[t]));
|
||||
}
|
||||
}
|
||||
|
||||
Templates.init = function() {
|
||||
loadTemplates([
|
||||
'header', 'footer', 'register', 'home', 'topic', 'account', 'friends',
|
||||
'login', 'reset', 'reset_code',
|
||||
'403', 'logout',
|
||||
'admin/header', 'admin/footer', 'admin/index',
|
||||
'emails/header', 'emails/footer',
|
||||
'emails/reset', 'emails/reset_plaintext', 'emails/email_confirm', 'emails/email_confirm_plaintext'
|
||||
]);
|
||||
}
|
||||
|
||||
var parse = function(data) {
|
||||
function replace(key, value, template) {
|
||||
var searchRegex = new RegExp('{' + key + '}', 'g');
|
||||
return template.replace(searchRegex, value);
|
||||
}
|
||||
|
||||
function makeRegex(block) {
|
||||
return new RegExp("<!-- BEGIN " + block + " -->[^]*<!-- END " + block + " -->", 'g');
|
||||
}
|
||||
|
||||
function getBlock(regex, block, template) {
|
||||
data = template.match(regex);
|
||||
if (data == null) return;
|
||||
|
||||
data = data[0]
|
||||
.replace("<!-- BEGIN " + block + " -->", "")
|
||||
.replace("<!-- END " + block + " -->", "");
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
function setBlock(regex, block, template) {
|
||||
return template.replace(regex, block);
|
||||
}
|
||||
|
||||
var template = this.html, regex, block;
|
||||
|
||||
return (function parse(data, namespace, template) {
|
||||
console.log(this.file + ' is being called on server side. Templates will be deprecated soon');
|
||||
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) {
|
||||
continue;
|
||||
} else if (data[d].constructor == Array) {
|
||||
namespace += d;
|
||||
|
||||
regex = makeRegex(d),
|
||||
block = getBlock(regex, namespace, template)
|
||||
if (block == null) continue;
|
||||
|
||||
var numblocks = data[d].length - 1, i = 0, result = "";
|
||||
|
||||
do {
|
||||
result += parse(data[d][i], namespace + '.', block);
|
||||
} while (i++ < numblocks);
|
||||
|
||||
template = setBlock(regex, result, template);
|
||||
|
||||
} else if (data[d] instanceof Object) {
|
||||
namespace += d + '.';
|
||||
|
||||
regex = makeRegex(d),
|
||||
block = getBlock(regex, namespace, template)
|
||||
if (block == null) continue;
|
||||
|
||||
block = parse(data[d], namespace, block);
|
||||
template = setBlock(regex, block, template);
|
||||
} else {
|
||||
template = replace(namespace + d, data[d], template);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return template;
|
||||
|
||||
})(data, "", template);
|
||||
}
|
||||
|
||||
}(exports));
|
||||
120
src/topics.js
120
src/topics.js
@@ -35,7 +35,7 @@ marked.setOptions({
|
||||
postcount = [],
|
||||
locked = [],
|
||||
deleted = [],
|
||||
pinned = [],
|
||||
pinned = [];
|
||||
|
||||
for (var i=0, ii=tids.length; i<ii; i++) {
|
||||
title.push('tid:' + tids[i] + ':title');
|
||||
@@ -75,26 +75,25 @@ marked.setOptions({
|
||||
active_usernames = replies[1];
|
||||
var topics = [];
|
||||
|
||||
if (tids.length > 0) {
|
||||
title = replies[2];
|
||||
uid = replies[3];
|
||||
timestamp = replies[4];
|
||||
slug = replies[5];
|
||||
postcount = replies[6];
|
||||
locked = replies[7];
|
||||
deleted = replies[8];
|
||||
pinned = replies[9];
|
||||
title = replies[2];
|
||||
uid = replies[3];
|
||||
timestamp = replies[4];
|
||||
slug = replies[5];
|
||||
postcount = replies[6];
|
||||
locked = replies[7];
|
||||
deleted = replies[8];
|
||||
pinned = replies[9];
|
||||
|
||||
var usernames,
|
||||
has_read,
|
||||
moderators,
|
||||
teaser_info;
|
||||
var usernames,
|
||||
has_read,
|
||||
moderators,
|
||||
teaser_info;
|
||||
|
||||
function generate_topic() {
|
||||
if (!usernames || !has_read || !moderators || !teaser_info) return;
|
||||
function generate_topic() {
|
||||
if (!usernames || !has_read || !moderators || !teaser_info) return;
|
||||
|
||||
if (tids.length > 0) {
|
||||
for (var i=0, ii=title.length; i<ii; i++) {
|
||||
console.log(teaser_info[i]);
|
||||
topics.push({
|
||||
'title' : title[i],
|
||||
'uid' : uid[i],
|
||||
@@ -111,62 +110,61 @@ marked.setOptions({
|
||||
'teaser_text': teaser_info[i].text,
|
||||
'teaser_username': teaser_info[i].username
|
||||
});
|
||||
console.log(teaser_info[i].text, teaser_info[i].username);
|
||||
}
|
||||
|
||||
// Float pinned topics to the top
|
||||
topics = topics.sort(function(a, b) {
|
||||
if (a.pinned !== b.pinned) return b.pinned - a.pinned;
|
||||
else {
|
||||
// Sort by datetime descending
|
||||
return b.timestamp - a.timestamp;
|
||||
}
|
||||
});
|
||||
|
||||
var active_users = [];
|
||||
for (var username in active_usernames) {
|
||||
active_users.push({'username': active_usernames[username]});
|
||||
}
|
||||
|
||||
callback({
|
||||
'category_name' : category_id ? category_name : 'Recent',
|
||||
'show_topic_button' : category_id ? 'show' : 'hidden',
|
||||
'category_id': category_id || 0,
|
||||
'topics': topics,
|
||||
'active_users': active_users,
|
||||
'moderator_block_class': moderators.length > 0 ? '' : 'none',
|
||||
'moderators': moderators
|
||||
});
|
||||
}
|
||||
|
||||
user.get_usernames_by_uids(uid, function(userNames) {
|
||||
usernames = userNames;
|
||||
generate_topic();
|
||||
});
|
||||
|
||||
Topics.hasReadTopics(tids, current_user, function(hasRead) {
|
||||
has_read = hasRead;
|
||||
generate_topic();
|
||||
// Float pinned topics to the top
|
||||
topics = topics.sort(function(a, b) {
|
||||
if (a.pinned !== b.pinned) return b.pinned - a.pinned;
|
||||
else {
|
||||
// Sort by datetime descending
|
||||
return b.timestamp - a.timestamp;
|
||||
}
|
||||
});
|
||||
|
||||
categories.getModerators(category_id, function(mods) {
|
||||
moderators = mods;
|
||||
generate_topic();
|
||||
});
|
||||
var active_users = [];
|
||||
for (var username in active_usernames) {
|
||||
active_users.push({'username': active_usernames[username]});
|
||||
}
|
||||
|
||||
Topics.get_teasers(tids, function(teasers) {
|
||||
teaser_info = teasers;
|
||||
generate_topic();
|
||||
});
|
||||
}
|
||||
else {
|
||||
callback({
|
||||
'category_name' : category_id ? category_name : 'Recent',
|
||||
'show_topic_button' : category_id ? 'show' : 'hidden',
|
||||
'category_id': category_id || 0,
|
||||
'topics': []
|
||||
'topics': topics,
|
||||
'active_users': active_users,
|
||||
'moderator_block_class': moderators.length > 0 ? '' : 'none',
|
||||
'moderators': moderators
|
||||
});
|
||||
}
|
||||
|
||||
user.get_usernames_by_uids(uid, function(userNames) {
|
||||
usernames = userNames;
|
||||
generate_topic();
|
||||
});
|
||||
|
||||
Topics.hasReadTopics(tids, current_user, function(hasRead) {
|
||||
has_read = hasRead;
|
||||
generate_topic();
|
||||
});
|
||||
|
||||
categories.getModerators(category_id, function(mods) {
|
||||
moderators = mods;
|
||||
generate_topic();
|
||||
});
|
||||
|
||||
Topics.get_teasers(tids, function(teasers) {
|
||||
teaser_info = teasers;
|
||||
generate_topic();
|
||||
});
|
||||
// else {
|
||||
// callback({
|
||||
// 'category_name' : category_id ? category_name : 'Recent',
|
||||
// 'show_topic_button' : category_id ? 'show' : 'hidden',
|
||||
// 'category_id': category_id || 0,
|
||||
// 'topics': []
|
||||
// });
|
||||
// }
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -447,8 +447,9 @@ var config = require('../config.js'),
|
||||
};
|
||||
|
||||
User.get_usernames_by_uids = function(uids, callback) {
|
||||
|
||||
var usernames = [];
|
||||
|
||||
if (!Array.isArray(uids)) return callback([]);
|
||||
|
||||
for(var i=0, ii=uids.length; i<ii; ++i) {
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ var express = require('express'),
|
||||
|
||||
|
||||
(function(app) {
|
||||
var templates = global.templates;
|
||||
var templates = null;
|
||||
|
||||
// Middlewares
|
||||
app.use(express.favicon()); // 2 args: string path and object options (i.e. expire time etc)
|
||||
@@ -38,6 +38,11 @@ var express = require('express'),
|
||||
key: 'express.sid'
|
||||
}));
|
||||
|
||||
|
||||
module.exports.init = function() {
|
||||
templates = global.templates;
|
||||
}
|
||||
|
||||
auth.initialize(app);
|
||||
|
||||
app.use(function(req, res, next) {
|
||||
|
||||
@@ -6,8 +6,7 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),
|
||||
user = require('./user.js'),
|
||||
posts = require('./posts.js'),
|
||||
topics = require('./topics.js'),
|
||||
categories = require('./categories.js'),
|
||||
templates = require('./templates.js');
|
||||
categories = require('./categories.js');
|
||||
|
||||
(function(io) {
|
||||
var users = {},
|
||||
@@ -50,10 +49,6 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),
|
||||
|
||||
var uid = users[hs.sessionID];
|
||||
|
||||
if (DEVELOPMENT === true) {
|
||||
// refreshing templates
|
||||
templates.init();
|
||||
}
|
||||
|
||||
/*process.on('uncaughtException', function(err) {
|
||||
// handle the error safely
|
||||
|
||||
Reference in New Issue
Block a user