Merge remote-tracking branch 'origin/master' into user-icons

This commit is contained in:
Julian Lam
2015-09-30 13:45:33 -04:00
39 changed files with 322 additions and 294 deletions

View File

@@ -10,45 +10,52 @@ module.exports = function(Categories) {
Categories.update = function(modified, callback) {
function updateCategory(cid, next) {
Categories.exists(cid, function(err, exists) {
if (err || !exists) {
return next(err);
}
var modifiedFields = modified[cid];
if (modifiedFields.hasOwnProperty('name')) {
modifiedFields.slug = cid + '/' + utils.slugify(modifiedFields.name);
}
plugins.fireHook('filter:category.update', {category: modifiedFields}, function(err, categoryData) {
if (err) {
return next(err);
}
var category = categoryData.category;
var fields = Object.keys(category);
async.each(fields, function(key, next) {
updateCategoryField(cid, key, category[key], next);
}, function(err) {
if (err) {
return next(err);
}
plugins.fireHook('action:category.update', {cid: cid, modified: category});
next();
});
});
});
}
var cids = Object.keys(modified);
async.each(cids, updateCategory, function(err) {
async.each(cids, function(cid, next) {
updateCategory(cid, modified[cid], next);
}, function(err) {
callback(err, cids);
});
};
function updateCategory(cid, modifiedFields, callback) {
Categories.exists(cid, function(err, exists) {
if (err || !exists) {
return callback(err);
}
if (modifiedFields.hasOwnProperty('name')) {
modifiedFields.slug = cid + '/' + utils.slugify(modifiedFields.name);
}
plugins.fireHook('filter:category.update', {category: modifiedFields}, function(err, categoryData) {
if (err) {
return callback(err);
}
var category = categoryData.category;
var fields = Object.keys(category);
// move parent to front, so its updated first
var parentCidIndex = fields.indexOf('parentCid');
if (parentCidIndex !== -1 && fields.length > 1) {
fields.splice(0, 0, fields.splice(parentCidIndex, 1)[0]);
}
async.eachSeries(fields, function(key, next) {
updateCategoryField(cid, key, category[key], next);
}, function(err) {
if (err) {
return callback(err);
}
plugins.fireHook('action:category.update', {cid: cid, modified: category});
callback();
});
});
});
}
function updateCategoryField(cid, key, value, callback) {
if (key === 'parentCid') {
return updateParent(cid, value, callback);

View File

@@ -1,24 +1,24 @@
'use strict';
var path = require('path');
var fs = require('fs');
var file = require('../../file');
var themesController = {};
themesController.get = function(req, res, next) {
var themeDir = path.join(__dirname, '../../../node_modules/' + req.params.theme);
fs.exists(themeDir, function(exists) {
if (exists) {
var themeConfig = require(path.join(themeDir, 'theme.json')),
screenshotPath = path.join(themeDir, themeConfig.screenshot);
if (themeConfig.screenshot && fs.existsSync(screenshotPath)) {
res.sendFile(screenshotPath);
} else {
res.sendFile(path.join(__dirname, '../../../public/images/themes/default.png'));
}
} else {
file.exists(themeDir, function(exists) {
if (!exists) {
return next();
}
var themeConfig = require(path.join(themeDir, 'theme.json')),
screenshotPath = path.join(themeDir, themeConfig.screenshot);
if (themeConfig.screenshot && file.existsSync(screenshotPath)) {
res.sendFile(screenshotPath);
} else {
res.sendFile(path.join(__dirname, '../../../public/images/themes/default.png'));
}
});
};

View File

@@ -253,9 +253,10 @@ categoriesController.get = function(req, res, callback) {
data.pageCount = pageCount;
data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1;
data.rssFeedUrl = nconf.get('relative_path') + '/category/' + data.cid + '.rss';
data.pagination = pagination.create(data.currentPage, data.pageCount);
data.title = data.name;
data.pagination = pagination.create(data.currentPage, data.pageCount);
data.pagination.rel.forEach(function(rel) {
rel.href = nconf.get('url') + '/category/' + data.slug + rel.href;
res.locals.linkTags.push(rel);
});

View File

@@ -263,6 +263,7 @@ topicsController.get = function(req, res, callback) {
data.rssFeedUrl = nconf.get('relative_path') + '/topic/' + data.tid + '.rss';
data.pagination = pagination.create(data.currentPage, data.pageCount);
data.pagination.rel.forEach(function(rel) {
rel.href = nconf.get('url') + '/topic/' + data.slug + rel.href;
res.locals.linkTags.push(rel);
});

View File

@@ -68,7 +68,7 @@ module.exports = function(db, module) {
return callback(err, null);
}
callback(null, item[field] || null);
callback(null, item.hasOwnProperty(field) ? item[field] : null);
});
};

View File

@@ -8,7 +8,6 @@ var fs = require('fs'),
Magic = mmmagic.Magic,
mime = require('mime'),
meta = require('./meta'),
utils = require('../public/src/utils');
var file = {};
@@ -63,6 +62,7 @@ file.isFileTypeAllowed = function(path, allowedExtensions, callback) {
};
file.allowedExtensions = function() {
var meta = require('./meta');
var allowedExtensions = (meta.config.allowedFileExtensions || '').trim();
if (!allowedExtensions) {
return [];
@@ -80,4 +80,21 @@ file.allowedExtensions = function() {
return allowedExtensions;
};
file.exists = function(path, callback) {
fs.stat(path, function(err, stat) {
callback(!err && stat);
});
};
file.existsSync = function(path) {
var exists = false;
try {
exists = fs.statSync(path);
} catch(err) {
exists = false;
}
return !!exists;
};
module.exports = file;

View File

@@ -10,6 +10,7 @@ var fs = require('fs'),
winston = require('winston'),
util = require('util'),
socketio = require('socket.io'),
file = require('./file'),
meta = require('./meta'),
morgan = require('morgan');
@@ -76,7 +77,7 @@ var opts = {
/* Open the streams to log to: either a path or stdout */
var stream;
if(value) {
if(fs.existsSync(value)) {
if(file.existsSync(value)) {
var stats = fs.statSync(value);
if(stats) {
if(stats.isDirectory()) {

View File

@@ -11,6 +11,7 @@ var winston = require('winston'),
plugins = require('../plugins'),
emitter = require('../emitter'),
db = require('../database'),
file = require('../file'),
utils = require('../../public/src/utils');
module.exports = function(Meta) {
@@ -149,24 +150,25 @@ module.exports = function(Meta) {
Meta.css.getFromFile = function(callback) {
var cachePath = path.join(__dirname, '../../public/stylesheet.css'),
acpCachePath = path.join(__dirname, '../../public/admin.css');
fs.exists(cachePath, function(exists) {
if (exists) {
if (nconf.get('isPrimary') === 'true') {
winston.verbose('[meta/css] Reading stylesheets from file');
async.map([cachePath, acpCachePath], fs.readFile, function(err, files) {
Meta.css.cache = files[0];
Meta.css.acpCache = files[1];
emitter.emit('meta:css.compiled');
callback();
});
} else {
callback();
}
} else {
file.exists(cachePath, function(exists) {
if (!exists) {
winston.warn('[meta/css] No stylesheets found on disk, re-minifying');
Meta.css.minify.apply(Meta.css, arguments);
Meta.css.minify(callback);
return;
}
if (nconf.get('isPrimary') !== 'true') {
return callback();
}
winston.verbose('[meta/css] Reading stylesheets from file');
async.map([cachePath, acpCachePath], fs.readFile, function(err, files) {
Meta.css.cache = files[0];
Meta.css.acpCache = files[1];
emitter.emit('meta:css.compiled');
callback();
});
});
};
@@ -197,10 +199,10 @@ module.exports = function(Meta) {
}
function filterMissingFiles(files) {
return files.filter(function(file) {
var exists = fs.existsSync(path.join(__dirname, '../../node_modules', file));
return files.filter(function(filePath) {
var exists = file.existsSync(path.join(__dirname, '../../node_modules', filePath));
if (!exists) {
winston.warn('[meta/css] File not found! ' + file);
winston.warn('[meta/css] File not found! ' + filePath);
}
return exists;
});

View File

@@ -8,7 +8,7 @@ var winston = require('winston'),
os = require('os'),
nconf = require('nconf'),
fs = require('fs'),
file = require('../file'),
plugins = require('../plugins'),
emitter = require('../emitter'),
utils = require('../../public/src/utils');
@@ -208,30 +208,31 @@ module.exports = function(Meta) {
var scriptPath = path.join(__dirname, '../../public/nodebb.min.js'),
mapPath = path.join(__dirname, '../../public/nodebb.min.js.map'),
paths = [scriptPath];
fs.exists(scriptPath, function(exists) {
if (exists) {
if (nconf.get('isPrimary') === 'true') {
fs.exists(mapPath, function(exists) {
if (exists) {
paths.push(mapPath);
}
winston.verbose('[meta/js] Reading client-side scripts from file');
async.map(paths, fs.readFile, function(err, files) {
Meta.js.cache = files[0];
Meta.js.map = files[1] || '';
emitter.emit('meta:js.compiled');
callback();
});
});
} else {
callback();
}
} else {
file.exists(scriptPath, function(exists) {
if (!exists) {
winston.warn('[meta/js] No script file found on disk, re-minifying');
Meta.js.minify.apply(Meta.js, arguments);
Meta.js.minify(minify, callback);
return;
}
if (nconf.get('isPrimary') !== 'true') {
return callback();
}
file.exists(mapPath, function(exists) {
if (exists) {
paths.push(mapPath);
}
winston.verbose('[meta/js] Reading client-side scripts from file');
async.map(paths, fs.readFile, function(err, files) {
Meta.js.cache = files[0];
Meta.js.map = files[1] || '';
emitter.emit('meta:js.compiled');
callback();
});
});
});
};

View File

@@ -6,6 +6,8 @@ var nconf = require('nconf'),
fs = require('fs'),
path = require('path'),
async = require('async'),
file = require('../file'),
db = require('../database');
module.exports = function(Meta) {
@@ -34,27 +36,24 @@ module.exports = function(Meta) {
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());
fs.readFile(config, function (err, file) {
if (err) {
return next();
}
// Minor adjustments for API output
configObj.type = 'local';
if (configObj.screenshot) {
configObj.screenshot_url = nconf.get('relative_path') + '/css/previews/' + configObj.id;
} else {
configObj.screenshot_url = nconf.get('relative_path') + '/images/themes/default.png';
}
var configObj = JSON.parse(file.toString());
// Minor adjustments for API output
configObj.type = 'local';
if (configObj.screenshot) {
configObj.screenshot_url = nconf.get('relative_path') + '/css/previews/' + configObj.id;
} else {
configObj.screenshot_url = nconf.get('relative_path') + '/images/themes/default.png';
}
next(null, configObj);
});
next(err, configObj);
}
});
} else {
next();
}
}, function (err, themes) {
themes = themes.filter(function (theme) {
return (theme !== undefined);
@@ -145,7 +144,7 @@ module.exports = function(Meta) {
if (themeObj.templates) {
themePath = path.join(nconf.get('themes_path'), themeObj.id, themeObj.templates);
} else if (fs.existsSync(fallback)) {
} else if (file.existsSync(fallback)) {
themePath = fallback;
}

View File

@@ -2,6 +2,7 @@
var meta = require('../meta'),
db = require('../database'),
file = require('../file'),
auth = require('../routes/authentication'),
path = require('path'),
@@ -21,7 +22,7 @@ var middleware = {};
function setupFavicon(app) {
var faviconPath = path.join(__dirname, '../../', 'public', meta.config['brand:favicon'] ? meta.config['brand:favicon'] : 'favicon.ico');
if (fs.existsSync(faviconPath)) {
if (file.existsSync(faviconPath)) {
app.use(nconf.get('relative_path'), favicon(faviconPath));
}
}

View File

@@ -14,6 +14,7 @@ var fs = require('fs'),
translator = require('../public/src/modules/translator'),
utils = require('../public/src/utils'),
hotswap = require('./hotswap'),
file = require('./file'),
controllers = require('./controllers'),
app, middleware;
@@ -103,7 +104,7 @@ var fs = require('fs'),
return path.join(__dirname, '../node_modules/', plugin);
});
async.filter(plugins, fs.exists, function(plugins){
async.filter(plugins, file.exists, function(plugins) {
async.eachSeries(plugins, Plugins.loadPlugin, next);
});
},

View File

@@ -6,6 +6,7 @@ var fs = require('fs'),
async = require('async'),
winston = require('winston'),
nconf = require('nconf'),
file = require('../file'),
utils = require('../../public/src/utils');
@@ -107,7 +108,7 @@ module.exports = function(Plugins) {
var realPath = pluginData.staticDirs[mappedPath];
var staticDir = path.join(pluginPath, realPath);
fs.exists(staticDir, function(exists) {
file.exists(staticDir, function(exists) {
if (exists) {
Plugins.staticDirs[pluginData.id + '/' + mappedPath] = staticDir;
} else {

View File

@@ -51,6 +51,7 @@ module.exports = function(privileges) {
editable: editable,
deletable: deletable,
view_deleted: isAdminOrMod || results.isOwner,
isAdminOrMod: isAdminOrMod,
disabled: disabled,
tid: tid,
uid: uid

View File

@@ -22,31 +22,18 @@ module.exports = function(app, middleware, controllers) {
} else {
return null;
}
}).filter(function(a) { return a; });
}).filter(Boolean);
if (matches) {
async.map(matches, function(mappedPath, next) {
var filePath = path.join(plugins.staticDirs[mappedPath], decodeURIComponent(relPath.slice(mappedPath.length)));
if (!matches) {
return next();
}
fs.exists(filePath, function(exists) {
if (exists) {
next(null, filePath);
} else {
next();
}
});
}, function(err, matches) {
if (err) {
return next(err);
}
matches = matches.filter(Boolean);
matches = matches.map(function(mappedPath) {
return path.join(plugins.staticDirs[mappedPath], decodeURIComponent(relPath.slice(mappedPath.length)));
});
if (matches.length) {
res.sendFile(matches[0]);
} else {
next();
}
});
if (matches.length) {
res.sendFile(matches[0]);
} else {
next();
}

View File

@@ -145,21 +145,6 @@ SocketPosts.getRawPost = function(socket, pid, callback) {
], callback);
};
SocketPosts.getPrivileges = function(socket, pids, callback) {
privileges.posts.get(pids, socket.uid, function(err, privileges) {
if (err) {
return callback(err);
}
if (!Array.isArray(privileges) || !privileges.length) {
return callback(new Error('[[error:invalid-data]]'));
}
callback(null, privileges);
});
};
SocketPosts.loadMoreFavourites = function(socket, data, callback) {
loadMorePosts('uid:' + data.uid + ':favourites', socket.uid, data, callback);
};

View File

@@ -283,6 +283,9 @@ module.exports = function(Topics) {
topicInfo: function(next) {
Topics.getTopicFields(tid, ['tid', 'title', 'slug', 'cid', 'postcount'], next);
},
parents: function(next) {
Topics.addParentPosts([postData], next);
},
content: function(next) {
posts.parsePost(postData, next);
}

View File

@@ -112,6 +112,9 @@ module.exports = function(Topics) {
},
privileges: function(next) {
privileges.posts.get(pids, uid, next);
},
parents: function(next) {
Topics.addParentPosts(postData, next);
}
}, function(err, results) {
if (err) {
@@ -129,9 +132,9 @@ module.exports = function(Topics) {
postObj.votes = postObj.votes || 0;
postObj.display_moderator_tools = results.privileges[i].editable;
postObj.display_move_tools = results.privileges[i].move && postObj.index !== 0;
postObj.selfPost = parseInt(uid, 10) === parseInt(postObj.uid, 10);
postObj.selfPost = !!parseInt(uid, 10) && parseInt(uid, 10) === parseInt(postObj.uid, 10);
if(postObj.deleted && !results.privileges[i].view_deleted) {
if (postObj.deleted && !results.privileges[i].view_deleted) {
postObj.content = '[[topic:post_is_deleted]]';
}
@@ -146,6 +149,44 @@ module.exports = function(Topics) {
});
};
Topics.addParentPosts = function(postData, callback) {
var parentPids = postData.map(function(postObj) {
return postObj && postObj.hasOwnProperty('toPid') ? parseInt(postObj.toPid, 10) : null;
}).filter(Boolean);
if (!parentPids.length) {
return callback();
}
var parentPosts;
async.waterfall([
async.apply(posts.getPostsFields, parentPids, ['uid']),
function(_parentPosts, next) {
parentPosts = _parentPosts;
var parentUids = parentPosts.map(function(postObj) { return parseInt(postObj.uid, 10); }).filter(function(uid, idx, users) {
return users.indexOf(uid) === idx;
});
user.getUsersFields(parentUids, ['username'], next);
},
function (userData, next) {
var usersMap = {};
userData.forEach(function(user) {
usersMap[user.uid] = user.username;
});
var parents = {};
parentPosts.forEach(function(post, i) {
parents[parentPids[i]] = {username: usersMap[post.uid]};
});
postData.forEach(function(post) {
post.parent = parents[post.toPid];
});
next();
}
], callback);
};
Topics.calculatePostIndices = function(posts, start, stop, postCount, reverse) {
posts.forEach(function(post, index) {
if (reverse) {

View File

@@ -1,3 +1,4 @@
<script type="text/tpl" data-template="500">
<div class="alert alert-danger">
<strong>[[global:500.title]]</strong>
<p>[[global:500.message]]</p>
@@ -5,3 +6,4 @@
<!-- IF error --><p>{error}</p><!-- ENDIF error -->
</div>
</script>

View File

@@ -26,4 +26,7 @@
</div>
</li>
<!-- END categories -->
<li class="children-placeholder"></li>
</ul>

View File

@@ -186,9 +186,10 @@ module.exports.testSocket = function(socketPath, callback) {
return callback(new Error('invalid socket path : ' + socketPath));
}
var net = require('net');
var file = require('./file');
async.series([
function(next) {
fs.exists(socketPath, function(exists) {
file.exists(socketPath, function(exists) {
if (exists) {
next();
} else {