Files
NodeBB/src/socket.io/categories.js

223 lines
5.8 KiB
JavaScript
Raw Normal View History

'use strict';
2016-08-22 17:16:52 -05:00
var async = require('async');
2015-12-28 10:42:15 +02:00
var db = require('../database');
var categories = require('../categories');
var privileges = require('../privileges');
var user = require('../user');
var topics = require('../topics');
2016-03-08 11:24:32 +02:00
var apiController = require('../controllers/api');
2015-12-28 10:42:15 +02:00
2017-05-26 00:02:20 -04:00
var SocketCategories = module.exports;
SocketCategories.getRecentReplies = function (socket, cid, callback) {
categories.getRecentReplies(cid, socket.uid, 4, callback);
};
SocketCategories.get = function (socket, data, callback) {
2017-05-26 00:02:20 -04:00
async.waterfall([
function (next) {
async.parallel({
isAdmin: async.apply(user.isAdministrator, socket.uid),
categories: function (next) {
async.waterfall([
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
async.apply(categories.getCategoriesData),
], next);
},
}, next);
2017-02-17 19:31:21 -07:00
},
2017-05-26 00:02:20 -04:00
function (results, next) {
results.categories = results.categories.filter(function (category) {
return category && (!category.disabled || results.isAdmin);
});
2015-10-13 13:58:25 -04:00
2017-05-26 00:02:20 -04:00
next(null, results.categories);
},
], callback);
};
SocketCategories.getWatchedCategories = function (socket, data, callback) {
2017-05-26 00:02:20 -04:00
async.waterfall([
function (next) {
async.parallel({
categories: async.apply(categories.getCategoriesByPrivilege, 'cid:0:children', socket.uid, 'find'),
ignoredCids: async.apply(user.getIgnoredCategories, socket.uid),
}, next);
},
function (results, next) {
var watchedCategories = results.categories.filter(function (category) {
return category && results.ignoredCids.indexOf(category.cid.toString()) === -1;
});
next(null, watchedCategories);
},
], callback);
2015-05-26 15:05:46 -04:00
};
SocketCategories.loadMore = function (socket, data, callback) {
2015-01-08 13:47:15 -05:00
if (!data) {
2014-04-09 21:22:37 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-01-17 12:49:21 -05:00
}
data.query = data.query || {};
2017-05-26 00:02:20 -04:00
var userPrivileges;
async.waterfall([
function (next) {
async.parallel({
privileges: function (next) {
privileges.categories.get(data.cid, socket.uid, next);
},
settings: function (next) {
user.getSettings(socket.uid, next);
},
targetUid: function (next) {
if (data.query.author) {
user.getUidByUserslug(data.query.author, next);
2017-05-26 00:02:20 -04:00
} else {
next();
}
},
}, next);
},
2017-05-26 00:02:20 -04:00
function (results, next) {
userPrivileges = results.privileges;
if (!userPrivileges.read) {
return callback(new Error('[[error:no-privileges]]'));
}
2017-05-26 00:02:20 -04:00
var infScrollTopicsPerPage = 20;
var sort = data.sort || data.categoryTopicSort;
2017-05-26 00:02:20 -04:00
var start = Math.max(0, parseInt(data.after, 10));
if (data.direction === -1) {
start -= infScrollTopicsPerPage;
2017-05-26 00:02:20 -04:00
}
var stop = start + infScrollTopicsPerPage - 1;
start = Math.max(0, start);
stop = Math.max(0, stop);
categories.getCategoryTopics({
uid: socket.uid,
2017-05-26 00:02:20 -04:00
cid: data.cid,
start: start,
stop: stop,
sort: sort,
2017-05-26 00:02:20 -04:00
settings: results.settings,
query: data.query,
tag: data.query.tag,
targetUid: results.targetUid,
2017-05-26 00:02:20 -04:00
}, next);
},
function (data, next) {
categories.modifyTopicsByPrivilege(data.topics, userPrivileges);
2016-01-27 20:36:40 +02:00
2017-05-26 00:02:20 -04:00
data.privileges = userPrivileges;
data.template = {
category: true,
2017-02-17 19:31:21 -07:00
name: 'category',
};
2017-05-26 00:02:20 -04:00
next(null, data);
},
], callback);
};
SocketCategories.getPageCount = function (socket, cid, callback) {
2014-02-10 14:15:54 -05:00
categories.getPageCount(cid, socket.uid, callback);
};
2014-01-24 23:05:59 -05:00
SocketCategories.getTopicCount = function (socket, cid, callback) {
2014-04-04 12:42:41 -04:00
categories.getCategoryField(cid, 'topic_count', callback);
};
SocketCategories.getCategoriesByPrivilege = function (socket, privilege, callback) {
categories.getCategoriesByPrivilege('categories:cid', socket.uid, privilege, callback);
2014-08-15 15:45:01 -04:00
};
SocketCategories.getMoveCategories = function (socket, data, callback) {
2017-05-26 00:02:20 -04:00
async.waterfall([
function (next) {
async.parallel({
isAdmin: async.apply(user.isAdministrator, socket.uid),
categories: function (next) {
async.waterfall([
function (next) {
db.getSortedSetRange('cid:0:children', 0, -1, next);
},
function (cids, next) {
categories.getCategories(cids, socket.uid, next);
},
2017-05-30 14:10:12 -04:00
function (categoriesData, next) {
categories.buildForSelectCategories(categoriesData, next);
},
2017-05-26 00:02:20 -04:00
], next);
2017-02-17 19:31:21 -07:00
},
2017-05-26 00:02:20 -04:00
}, next);
2017-02-17 19:31:21 -07:00
},
2017-05-26 00:02:20 -04:00
function (results, next) {
results.categories = results.categories.filter(function (category) {
return category && (!category.disabled || results.isAdmin) && !category.link;
});
2015-12-28 10:42:15 +02:00
2017-05-26 00:02:20 -04:00
next(null, results.categories);
},
], callback);
2015-12-28 10:42:15 +02:00
};
SocketCategories.watch = function (socket, cid, callback) {
2016-08-22 17:16:52 -05:00
ignoreOrWatch(user.watchCategory, socket, cid, callback);
2014-08-29 15:57:20 -04:00
};
SocketCategories.ignore = function (socket, cid, callback) {
2016-08-22 17:16:52 -05:00
ignoreOrWatch(user.ignoreCategory, socket, cid, callback);
2014-08-29 15:57:20 -04:00
};
2016-08-22 17:16:52 -05:00
function ignoreOrWatch(fn, socket, cid, callback) {
var cids = [parseInt(cid, 10)];
2016-08-22 17:16:52 -05:00
async.waterfall([
function (next) {
2016-08-22 17:16:52 -05:00
db.getSortedSetRange('categories:cid', 0, -1, next);
},
function (cids, next) {
2016-08-22 17:16:52 -05:00
categories.getCategoriesFields(cids, ['cid', 'parentCid'], next);
},
function (categoryData, next) {
categoryData.forEach(function (c) {
2016-08-22 17:16:52 -05:00
c.cid = parseInt(c.cid, 10);
c.parentCid = parseInt(c.parentCid, 10);
});
// filter to subcategories of cid
2017-02-18 14:00:29 -07:00
var cat;
do {
cat = categoryData.find(function (c) {
return cids.indexOf(c.cid) === -1 && cids.indexOf(c.parentCid) !== -1;
2016-08-22 17:16:52 -05:00
});
2017-02-18 14:00:29 -07:00
if (cat) {
cids.push(cat.cid);
}
} while (cat);
2016-08-22 17:16:52 -05:00
async.each(cids, function (cid, next) {
2016-08-22 17:16:52 -05:00
fn(socket.uid, cid, next);
}, next);
},
function (next) {
2016-08-22 17:16:52 -05:00
topics.pushUnreadCount(socket.uid, next);
2017-02-17 19:31:21 -07:00
},
function (next) {
next(null, cids);
},
2016-08-22 17:16:52 -05:00
], callback);
}
SocketCategories.isModerator = function (socket, cid, callback) {
2015-03-18 17:50:47 -04:00
user.isModerator(socket.uid, cid, callback);
};
SocketCategories.getCategory = function (socket, cid, callback) {
2016-07-01 13:01:09 +03:00
apiController.getCategoryData(cid, socket.uid, callback);
2016-03-08 11:24:32 +02:00
};