You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/meta.js

348 lines
9.2 KiB
JavaScript

11 years ago
var fs = require('fs'),
path = require('path'),
11 years ago
async = require('async'),
winston = require('winston'),
11 years ago
nconf = require('nconf'),
utils = require('./../public/src/utils'),
translator = require('./../public/src/translator'),
11 years ago
db = require('./database'),
plugins = require('./plugins'),
User = require('./user');
11 years ago
(function (Meta) {
Meta.config = {};
Meta.configs = {
init: function (callback) {
delete Meta.config;
Meta.configs.list(function (err, config) {
11 years ago
if(err) {
winston.error(err);
11 years ago
return callback(err);
}
11 years ago
Meta.config = config;
callback();
});
},
list: function (callback) {
11 years ago
db.getObject('config', function (err, config) {
if(err) {
11 years ago
return callback(err);
}
11 years ago
config = config || {};
config.status = 'ok';
callback(err, config);
});
},
get: function (field, callback) {
11 years ago
db.getObjectField('config', field, callback);
},
getFields: function (fields, callback) {
11 years ago
db.getObjectFields('config', fields, callback);
},
set: function (field, value, callback) {
if(!field) {
return callback(new Error('invalid config field'));
}
11 years ago
db.setObjectField('config', field, value, function(err, res) {
if (callback) {
if(!err && Meta.config) {
11 years ago
Meta.config[field] = value;
}
callback(err, res);
}
// this might be a good spot to add a hook
if (field === 'defaultLang') {
translator.loadServer();
}
});
},
setOnEmpty: function (field, value, callback) {
11 years ago
Meta.configs.get(field, function (err, curValue) {
if(err) {
return callback(err);
}
if (!curValue) {
Meta.configs.set(field, value, callback);
} else {
callback();
}
});
},
remove: function (field) {
11 years ago
db.deleteObjectField('config', field);
}
};
Meta.themes = {
get: function (callback) {
var themePath = nconf.get('themes_path');
fs.readdir(themePath, function (err, files) {
async.filter(files, function (file, next) {
fs.stat(path.join(themePath, file), function (err, fileStat) {
if (err) {
return next(false);
}
next((fileStat.isDirectory() && file.slice(0, 13) === 'nodebb-theme-'));
});
}, function (themes) {
async.map(themes, function (theme, next) {
var config = path.join(themePath, theme, 'theme.json');
if (fs.existsSync(config)) {
fs.readFile(config, function (err, file) {
if (err) return next();
else {
var configObj = JSON.parse(file.toString());
next(err, configObj);
}
});
} else {
next();
}
}, function (err, themes) {
themes = themes.filter(function (theme) {
return (theme !== undefined);
});
callback(null, themes);
});
});
});
},
set: function(data, callback) {
var themeData = {
'theme:type': data.type,
'theme:id': data.id,
11 years ago
'theme:staticDir': '',
'theme:templates': '',
'theme:src': ''
};
switch(data.type) {
case 'local':
async.waterfall([
function(next) {
fs.readFile(path.join(nconf.get('themes_path'), data.id, 'theme.json'), function(err, config) {
if (!err) {
config = JSON.parse(config.toString());
next(null, config);
} else {
next(err);
}
});
},
function(config, next) {
themeData['theme:staticDir'] = config.staticDir ? config.staticDir : '';
themeData['theme:templates'] = config.templates ? config.templates : '';
themeData['theme:src'] = config.frameworkCSS ? config.frameworkCSS : '';
11 years ago
db.setObject('config', themeData, next);
}
], function(err) {
callback(err);
});
break;
case 'bootswatch':
themeData['theme:src'] = data.src;
11 years ago
db.setObject('config', themeData, callback);
break;
}
}
};
Meta.title = {
tests: {
isCategory: /^category\/\d+\/?/,
isTopic: /^topic\/\d+\/?/,
isUserPage: /^user\/[^\/]+(\/[\w]+)?/
},
build: function (urlFragment, callback) {
var user = require('./user');
Meta.title.parseFragment(decodeURIComponent(urlFragment), function(err, title) {
if (err) {
11 years ago
title = Meta.config.browserTitle || 'NodeBB';
} else {
11 years ago
title = (title ? title + ' | ' : '') + (Meta.config.browserTitle || 'NodeBB');
}
callback(null, title);
});
},
parseFragment: function (urlFragment, callback) {
var translated = ['', 'recent', 'unread', 'users', 'notifications'];
if (translated.indexOf(urlFragment) !== -1) {
if (!urlFragment.length) {
urlFragment = 'home';
}
translator.translate('[[pages:' + urlFragment + ']]', function(translated) {
callback(null, translated);
});
} else if (this.tests.isCategory.test(urlFragment)) {
var cid = urlFragment.match(/category\/(\d+)/)[1];
require('./categories').getCategoryField(cid, 'name', function (err, name) {
callback(null, name);
});
} else if (this.tests.isTopic.test(urlFragment)) {
var tid = urlFragment.match(/topic\/(\d+)/)[1];
require('./topics').getTopicField(tid, 'title', function (err, title) {
callback(null, title);
});
} else if (this.tests.isUserPage.test(urlFragment)) {
var matches = urlFragment.match(/user\/([^\/]+)\/?([\w]+)?/),
userslug = matches[1],
subpage = matches[2];
User.getUsernameByUserslug(userslug, function(err, username) {
if (subpage) {
translator.translate('[[pages:user.' + subpage + ', ' + username + ']]', function(translated) {
callback(null, translated);
})
} else {
callback(null, username);
}
});
} else {
callback(null);
}
}
};
Meta.js = {
scripts: [
'vendor/jquery/js/jquery.js',
'vendor/jquery/js/jquery-ui-1.10.4.custom.js',
'vendor/jquery/timeago/jquery.timeago.js',
'vendor/jquery/js/jquery.form.js',
'vendor/bootstrap/js/bootstrap.min.js',
'vendor/requirejs/require.js',
'vendor/bootbox/bootbox.min.js',
'vendor/tinycon/tinycon.js',
'vendor/xregexp/xregexp.js',
'vendor/xregexp/unicode/unicode-base.js',
'src/app.js',
'src/templates.js',
'src/ajaxify.js',
'src/translator.js',
'src/utils.js'
],
minFile: path.join(__dirname, '..', 'public/src/nodebb.min.js'),
get: function (callback) {
plugins.fireHook('filter:scripts.get', this.scripts, function(err, scripts) {
11 years ago
var ctime,
jsPaths = scripts.map(function (jsPath) {
jsPath = path.normalize(jsPath);
if (jsPath.substring(0, 7) === 'plugins') {
var paths = jsPath.split(path.sep),
mappedPath = paths[1];
11 years ago
if (plugins.staticDirs[mappedPath]) {
jsPath = jsPath.replace(path.join('plugins', mappedPath), '');
return path.join(plugins.staticDirs[mappedPath], jsPath);
} else {
winston.warn('[meta.scripts.get] Could not resolve mapped path: ' + mappedPath + '. Are you sure it is defined by a plugin?');
return null;
}
} else {
return path.join(__dirname, '..', '/public', jsPath);
}
});
11 years ago
Meta.js.scripts = jsPaths.filter(function(path) { return path !== null });
if (process.env.NODE_ENV !== 'development') {
async.parallel({
11 years ago
ctime: function (next) {
async.map(jsPaths, fs.stat, function (err, stats) {
async.reduce(stats, 0, function (memo, item, next) {
if(item) {
11 years ago
ctime = +new Date(item.ctime);
next(null, ctime > memo ? ctime : memo);
} else {
next(null, memo);
}
}, next);
});
},
minFile: function (next) {
if (!fs.existsSync(Meta.js.minFile)) {
winston.info('No minified client-side library found');
return next(null, 0);
}
fs.stat(Meta.js.minFile, function (err, stat) {
11 years ago
next(err, +new Date(stat.ctime));
});
}
}, function (err, results) {
11 years ago
if (results.minFile > results.ctime) {
winston.info('No changes to client-side libraries -- skipping minification');
callback(null, [path.relative(path.join(__dirname, '../public'), Meta.js.minFile)]);
} else {
11 years ago
winston.info('Minifying client-side libraries -- please wait');
Meta.js.minify(function () {
callback(null, [
path.relative(path.join(__dirname, '../public'), Meta.js.minFile)
]);
});
}
});
} else {
callback(null, scripts);
}
});
},
minify: function (callback) {
var uglifyjs = require('uglify-js'),
jsPaths = this.scripts,
minified;
11 years ago
if (process.env.NODE_ENV === 'development') {
winston.info('Minifying client-side libraries');
}
minified = uglifyjs.minify(jsPaths);
fs.writeFile(Meta.js.minFile, minified.code, function (err) {
if (!err) {
11 years ago
if (process.env.NODE_ENV === 'development') {
winston.info('Minified client-side libraries');
}
callback();
} else {
winston.error('Problem minifying client-side libraries, exiting.');
process.exit();
}
});
}
};
Meta.db = {
getFile: function (callback) {
11 years ago
db.getFileName(callback);
}
};
Meta.restart = function() {
if (process.send) {
process.send('nodebb:restart');
} else {
winston.error('[meta.restart] Could not restart, are you sure NodeBB was started with `./nodebb start`?');
}
};
}(exports));