Merge remote-tracking branch 'origin/master' into flagging-refactor

This commit is contained in:
Julian Lam
2016-12-05 11:28:10 -05:00
36 changed files with 1269 additions and 155 deletions

View File

@@ -1,8 +1,7 @@
'use strict';
var user = require('../../user'),
helpers = require('../helpers');
var user = require('../../user');
var helpers = require('../helpers');
var notificationsController = {};

View File

@@ -8,7 +8,7 @@ var db = require('./database');
var Reset = {};
Reset.reset = function () {
Reset.reset = function (callback) {
db.init(function (err) {
if (err) {
winston.error(err.message);
@@ -17,20 +17,20 @@ Reset.reset = function () {
if (nconf.get('t')) {
if(nconf.get('t') === true) {
resetThemes();
resetThemes(callback);
} else {
resetTheme(nconf.get('t'));
resetTheme(nconf.get('t'), callback);
}
} else if (nconf.get('p')) {
if (nconf.get('p') === true) {
resetPlugins();
resetPlugins(callback);
} else {
resetPlugin(nconf.get('p'));
resetPlugin(nconf.get('p'), callback);
}
} else if (nconf.get('w')) {
resetWidgets();
resetWidgets(callback);
} else if (nconf.get('s')) {
resetSettings();
resetSettings(callback);
} else if (nconf.get('a')) {
require('async').series([resetWidgets, resetThemes, resetPlugins, resetSettings], function (err) {
if (!err) {
@@ -38,7 +38,12 @@ Reset.reset = function () {
} else {
winston.error('[reset] Errors were encountered while resetting your forum settings: %s', err.message);
}
process.exit();
if (typeof callback === 'function') {
callback();
} else {
process.exit(0);
}
});
} else {
process.stdout.write('\nNodeBB Reset\n'.bold);
@@ -52,6 +57,7 @@ Reset.reset = function () {
process.stdout.write('\nPlugin and theme reset flags (-p & -t) can take a single argument\n');
process.stdout.write(' e.g. ./nodebb reset -p nodebb-plugin-mentions, ./nodebb reset -t nodebb-theme-persona\n');
process.exit();
}
});
@@ -69,7 +75,7 @@ function resetSettings(callback) {
});
}
function resetTheme(themeId) {
function resetTheme(themeId, callback) {
var meta = require('./meta');
var fs = require('fs');
@@ -88,7 +94,11 @@ function resetTheme(themeId) {
winston.info('[reset] Theme reset to ' + themeId);
}
process.exit();
if (typeof callback === 'function') {
callback();
} else {
process.exit(0);
}
});
}
});
@@ -105,12 +115,12 @@ function resetThemes(callback) {
if (typeof callback === 'function') {
callback(err);
} else {
process.exit();
process.exit(0);
}
});
}
function resetPlugin(pluginId) {
function resetPlugin(pluginId, callback) {
var active = false;
async.waterfall([
@@ -136,7 +146,11 @@ function resetPlugin(pluginId) {
}
}
process.exit();
if (typeof callback === 'function') {
callback();
} else {
process.exit(0);
}
});
}
@@ -146,7 +160,7 @@ function resetPlugins(callback) {
if (typeof callback === 'function') {
callback(err);
} else {
process.exit();
process.exit(0);
}
});
}

View File

@@ -1,5 +1,6 @@
'use strict';
var async = require('async');
var topics = require('../../topics');
var utils = require('../../../public/src/utils');
@@ -13,9 +14,6 @@ module.exports = function (SocketTopics) {
};
SocketTopics.searchAndLoadTags = function (socket, data, callback) {
if (!data) {
return callback(new Error('[[error:invalid-data]]'));
}
topics.searchAndLoadTags(data, callback);
};
@@ -26,13 +24,14 @@ module.exports = function (SocketTopics) {
var start = parseInt(data.after, 10);
var stop = start + 99;
topics.getTags(start, stop, function (err, tags) {
if (err) {
return callback(err);
async.waterfall([
function (next) {
topics.getTags(start, stop, next);
},
function (tags, next) {
tags = tags.filter(Boolean);
next(null, {tags: tags, nextStart: stop + 1});
}
tags = tags.filter(Boolean);
callback(null, {tags: tags, nextStart: stop + 1});
});
], callback);
};
};

View File

@@ -178,30 +178,30 @@ module.exports = function (Topics) {
var sets = tids.map(function (tid) {
return 'topic:' + tid + ':tags';
});
var uniqueTopicTags;
var topicTags;
async.waterfall([
function (next) {
db.getSetsMembers(sets, next);
},
function (_topicTags, next) {
topicTags = _topicTags;
uniqueTopicTags = _.uniq(_.flatten(topicTags));
db.getSetsMembers(sets, function (err, topicTags) {
if (err) {
return callback(err);
}
var uniqueTopicTags = _.uniq(_.flatten(topicTags));
var tags = uniqueTopicTags.map(function (tag) {
return {value: tag};
});
async.parallel({
tagData: function (next) {
Topics.getTagData(tags, next);
},
counts: function (next) {
db.sortedSetScores('tags:topic:count', uniqueTopicTags, next);
}
}, function (err, results) {
if (err) {
return callback(err);
}
var tags = uniqueTopicTags.map(function (tag) {
return {value: tag};
});
async.parallel({
tagData: function (next) {
Topics.getTagData(tags, next);
},
counts: function (next) {
db.sortedSetScores('tags:topic:count', uniqueTopicTags, next);
}
}, next);
},
function (results, next) {
results.tagData.forEach(function (tag, index) {
tag.score = results.counts[index] ? results.counts[index] : 0;
});
@@ -214,9 +214,9 @@ module.exports = function (Topics) {
}
});
callback(null, topicTags);
});
});
next(null, topicTags);
}
], callback);
};
Topics.updateTags = function (tid, tags, callback) {
@@ -235,60 +235,54 @@ module.exports = function (Topics) {
};
Topics.deleteTopicTags = function (tid, callback) {
Topics.getTopicTags(tid, function (err, tags) {
if (err) {
return callback(err);
async.waterfall([
function (next) {
Topics.getTopicTags(tid, next);
},
function (tags, next) {
async.series([
function (next) {
db.delete('topic:' + tid + ':tags', next);
},
function (next) {
var sets = tags.map(function (tag) {
return 'tag:' + tag + ':topics';
});
db.sortedSetsRemove(sets, tid, next);
},
function (next) {
async.each(tags, function (tag, next) {
updateTagCount(tag, next);
}, next);
}
], next);
}
async.series([
function (next) {
db.delete('topic:' + tid + ':tags', next);
},
function (next) {
var sets = tags.map(function (tag) {
return 'tag:' + tag + ':topics';
});
db.sortedSetsRemove(sets, tid, next);
},
function (next) {
async.each(tags, function (tag, next) {
updateTagCount(tag, next);
}, next);
}
], function (err) {
callback(err);
});
], function (err) {
callback(err);
});
};
Topics.searchTags = function (data, callback) {
function done(matches) {
plugins.fireHook('filter:tags.search', {data: data, matches: matches}, function (err, data) {
callback(err, data ? data.matches : []);
});
}
if (!data || !data.query) {
return callback(null, []);
}
if (plugins.hasListeners('filter:topics.searchTags')) {
return plugins.fireHook('filter:topics.searchTags', {data: data}, function (err, data) {
if (err) {
return callback(err);
async.waterfall([
function (next) {
if (plugins.hasListeners('filter:topics.searchTags')) {
plugins.fireHook('filter:topics.searchTags', {data: data}, next);
} else {
findMatches(data.query, next);
}
done(data.matches);
});
}
findMatches(data.query, function (err, matches) {
if (err) {
return callback(err);
},
function (result, next) {
plugins.fireHook('filter:tags.search', {data: data, matches: result.matches}, next);
},
function (result, next) {
next(null, result.matches);
}
done(matches);
});
], callback);
};
Topics.autocompleteTags = function (data, callback) {
@@ -296,41 +290,44 @@ module.exports = function (Topics) {
return callback(null, []);
}
if (plugins.hasListeners('filter:topics.autocompleteTags')) {
return plugins.fireHook('filter:topics.autocompleteTags', {data: data}, function (err, data) {
if (err) {
return callback(err);
async.waterfall([
function (next) {
if (plugins.hasListeners('filter:topics.autocompleteTags')) {
plugins.fireHook('filter:topics.autocompleteTags', {data: data}, next);
} else {
findMatches(data.query, next);
}
callback(null, data.matches);
});
}
findMatches(data.query, callback);
},
function (result, next) {
next(null, result.matches);
}
], callback);
};
function findMatches(query, callback) {
db.getSortedSetRevRange('tags:topic:count', 0, -1, function (err, tags) {
if (err) {
return callback(err);
}
async.waterfall([
function (next) {
db.getSortedSetRevRange('tags:topic:count', 0, -1, next);
},
function (tags, next) {
query = query.toLowerCase();
query = query.toLowerCase();
var matches = [];
for(var i = 0; i < tags.length; ++i) {
if (tags[i].toLowerCase().startsWith(query)) {
matches.push(tags[i]);
if (matches.length > 19) {
break;
var matches = [];
for(var i = 0; i < tags.length; ++i) {
if (tags[i].toLowerCase().startsWith(query)) {
matches.push(tags[i]);
if (matches.length > 19) {
break;
}
}
}
}
matches = matches.sort(function (a, b) {
return a > b;
});
callback(null, matches);
});
matches = matches.sort(function (a, b) {
return a > b;
});
next(null, {matches: matches});
}
], callback);
}
Topics.searchAndLoadTags = function (data, callback) {
@@ -340,28 +337,28 @@ module.exports = function (Topics) {
pageCount: 1
};
if (!data.query || !data.query.length) {
if (!data || !data.query || !data.query.length) {
return callback(null, searchResult);
}
Topics.searchTags(data, function (err, tags) {
if (err) {
return callback(err);
}
async.parallel({
counts: function (next) {
db.sortedSetScores('tags:topic:count', tags, next);
},
tagData: function (next) {
tags = tags.map(function (tag) {
return {value: tag};
});
async.waterfall([
function (next) {
Topics.searchTags(data, next);
},
function (tags, next) {
async.parallel({
counts: function (next) {
db.sortedSetScores('tags:topic:count', tags, next);
},
tagData: function (next) {
tags = tags.map(function (tag) {
return {value: tag};
});
Topics.getTagData(tags, next);
}
}, function (err, results) {
if (err) {
return callback(err);
}
Topics.getTagData(tags, next);
}
}, next);
},
function (results, next) {
results.tagData.forEach(function (tag, index) {
tag.score = results.counts[index];
});
@@ -371,9 +368,9 @@ module.exports = function (Topics) {
searchResult.tags = results.tagData;
searchResult.matchCount = results.tagData.length;
searchResult.pageCount = 1;
callback(null, searchResult);
});
});
next(null, searchResult);
}
], callback);
};
Topics.getRelatedTopics = function (topicData, uid, callback) {