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/socket.io/categories.js

223 lines
5.8 KiB
JavaScript

'use strict';
var async = require('async');
9 years ago
var categories = require('../categories');
var privileges = require('../privileges');
var user = require('../user');
var topics = require('../topics');
var apiController = require('../controllers/api');
9 years ago
8 years ago
var SocketCategories = module.exports;
SocketCategories.getRecentReplies = function (socket, cid, callback) {
categories.getRecentReplies(cid, socket.uid, 4, callback);
};
SocketCategories.get = function (socket, data, callback) {
8 years ago
async.waterfall([
function (next) {
async.parallel({
isAdmin: async.apply(user.isAdministrator, socket.uid),
categories: function (next) {
async.waterfall([
async.apply(categories.getAllCidsFromSet, 'categories:cid'),
8 years ago
async.apply(categories.getCategoriesData),
], next);
},
}, next);
},
8 years ago
function (results, next) {
results.categories = results.categories.filter(function (category) {
return category && (!category.disabled || results.isAdmin);
});
10 years ago
8 years ago
next(null, results.categories);
},
], callback);
};
SocketCategories.getWatchedCategories = function (socket, data, callback) {
8 years ago
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.includes(String(category.cid));
8 years ago
});
next(null, watchedCategories);
},
], callback);
10 years ago
};
SocketCategories.loadMore = function (socket, data, callback) {
10 years ago
if (!data) {
11 years ago
return callback(new Error('[[error:invalid-data]]'));
}
data.query = data.query || {};
8 years ago
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);
8 years ago
} else {
next();
}
},
}, next);
},
8 years ago
function (results, next) {
userPrivileges = results.privileges;
if (!userPrivileges.read) {
return callback(new Error('[[error:no-privileges]]'));
}
8 years ago
var infScrollTopicsPerPage = 20;
var sort = data.sort || data.categoryTopicSort;
8 years ago
var start = Math.max(0, parseInt(data.after, 10));
if (data.direction === -1) {
start -= infScrollTopicsPerPage;
8 years ago
}
var stop = start + infScrollTopicsPerPage - 1;
start = Math.max(0, start);
stop = Math.max(0, stop);
categories.getCategoryTopics({
uid: socket.uid,
8 years ago
cid: data.cid,
start: start,
stop: stop,
sort: sort,
8 years ago
settings: results.settings,
query: data.query,
tag: data.query.tag,
targetUid: results.targetUid,
8 years ago
}, next);
},
function (data, next) {
categories.modifyTopicsByPrivilege(data.topics, userPrivileges);
8 years ago
data.privileges = userPrivileges;
data.template = {
category: true,
name: 'category',
};
8 years ago
next(null, data);
},
], callback);
};
SocketCategories.getTopicCount = function (socket, cid, callback) {
11 years ago
categories.getCategoryField(cid, 'topic_count', callback);
};
SocketCategories.getCategoriesByPrivilege = function (socket, privilege, callback) {
categories.getCategoriesByPrivilege('categories:cid', socket.uid, privilege, callback);
11 years ago
};
SocketCategories.getMoveCategories = function (socket, data, callback) {
8 years ago
async.waterfall([
function (next) {
async.parallel({
isAdmin: async.apply(user.isAdministrator, socket.uid),
categories: function (next) {
async.waterfall([
function (next) {
categories.getAllCidsFromSet('categories:cid', next);
8 years ago
},
function (cids, next) {
categories.getCategories(cids, socket.uid, next);
},
8 years ago
function (categoriesData, next) {
categoriesData = categories.getTree(categoriesData);
8 years ago
categories.buildForSelectCategories(categoriesData, next);
},
8 years ago
], next);
},
8 years ago
}, next);
},
8 years ago
function (results, next) {
results.categories = results.categories.filter(function (category) {
return category && (!category.disabled || results.isAdmin) && !category.link;
});
9 years ago
8 years ago
next(null, results.categories);
},
], callback);
9 years ago
};
SocketCategories.watch = function (socket, cid, callback) {
ignoreOrWatch(user.watchCategory, socket, cid, callback);
11 years ago
};
SocketCategories.ignore = function (socket, cid, callback) {
ignoreOrWatch(user.ignoreCategory, socket, cid, callback);
11 years ago
};
function ignoreOrWatch(fn, socket, cid, callback) {
var targetUid = socket.uid;
var cids = [parseInt(cid, 10)];
if (typeof cid === 'object') {
targetUid = cid.uid;
cids = [parseInt(cid.cid, 10)];
}
async.waterfall([
function (next) {
user.isAdminOrGlobalModOrSelf(socket.uid, targetUid, next);
},
function (next) {
categories.getAllCidsFromSet('categories:cid', next);
},
function (cids, next) {
categories.getCategoriesFields(cids, ['cid', 'parentCid'], next);
},
function (categoryData, next) {
// filter to subcategories of cid
var cat;
do {
cat = categoryData.find(function (c) {
return !cids.includes(c.cid) && cids.includes(c.parentCid);
});
if (cat) {
cids.push(cat.cid);
}
} while (cat);
async.each(cids, function (cid, next) {
fn(targetUid, cid, next);
}, next);
},
function (next) {
topics.pushUnreadCount(targetUid, next);
},
function (next) {
next(null, cids);
},
], callback);
}
SocketCategories.isModerator = function (socket, cid, callback) {
10 years ago
user.isModerator(socket.uid, cid, callback);
};
SocketCategories.getCategory = function (socket, cid, callback) {
9 years ago
apiController.getCategoryData(cid, socket.uid, callback);
};