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

237 lines
5.9 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
var SocketCategories = {};
SocketCategories.getRecentReplies = function (socket, cid, callback) {
categories.getRecentReplies(cid, socket.uid, 4, callback);
};
SocketCategories.get = function (socket, data, callback) {
2015-10-13 13:58:25 -04:00
async.parallel({
isAdmin: async.apply(user.isAdministrator, socket.uid),
categories: function (next) {
2015-10-13 13:58:25 -04:00
async.waterfall([
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
async.apply(categories.getCategoriesData),
], next);
2017-02-17 19:31:21 -07:00
},
}, function (err, results) {
2015-10-13 13:58:25 -04:00
if (err) {
return callback(err);
}
results.categories = results.categories.filter(function (category) {
2015-10-13 13:58:25 -04:00
return category && (!category.disabled || results.isAdmin);
});
callback(null, results.categories);
});
};
SocketCategories.getWatchedCategories = function (socket, data, callback) {
2015-05-26 15:05:46 -04:00
async.parallel({
2016-11-21 13:47:34 +03:00
categories: async.apply(categories.getCategoriesByPrivilege, 'cid:0:children', socket.uid, 'find'),
2017-02-17 19:31:21 -07:00
ignoredCids: async.apply(user.getIgnoredCategories, socket.uid),
}, function (err, results) {
2015-05-26 15:05:46 -04:00
if (err) {
return callback(err);
}
var watchedCategories = results.categories.filter(function (category) {
2015-05-26 15:05:46 -04:00
return category && results.ignoredCids.indexOf(category.cid.toString()) === -1;
});
2016-11-21 13:47:34 +03:00
2015-05-26 15:05:46 -04:00
callback(null, watchedCategories);
});
};
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
}
2014-04-16 14:30:36 -04:00
async.parallel({
privileges: function (next) {
2014-05-15 20:49:47 -04:00
privileges.categories.get(data.cid, socket.uid, next);
2014-04-16 14:30:36 -04:00
},
settings: function (next) {
2014-04-16 14:30:36 -04:00
user.getSettings(socket.uid, next);
},
targetUid: function (next) {
if (data.author) {
user.getUidByUserslug(data.author, next);
} else {
next();
}
2017-02-17 19:31:21 -07:00
},
}, function (err, results) {
2014-04-16 14:30:36 -04:00
if (err) {
return callback(err);
}
if (!results.privileges.read) {
return callback(new Error('[[error:no-privileges]]'));
}
var infScrollTopicsPerPage = 20;
2016-08-31 22:50:48 +03:00
var set = 'cid:' + data.cid + ':tids';
var reverse = false;
2015-01-08 13:47:15 -05:00
2016-08-31 22:50:48 +03:00
if (data.categoryTopicSort === 'newest_to_oldest') {
2015-01-08 13:47:15 -05:00
reverse = true;
2016-08-31 22:50:48 +03:00
} else if (data.categoryTopicSort === 'most_posts') {
2015-01-08 13:47:15 -05:00
reverse = true;
set = 'cid:' + data.cid + ':tids:posts';
}
2016-11-21 13:47:34 +03:00
var start = Math.max(0, parseInt(data.after, 10));
if (data.direction === -1) {
start = start - (reverse ? infScrollTopicsPerPage : -infScrollTopicsPerPage);
}
var stop = start + infScrollTopicsPerPage - 1;
start = Math.max(0, start);
stop = Math.max(0, stop);
2014-04-16 14:30:36 -04:00
2015-01-08 13:47:15 -05:00
if (results.targetUid) {
set = 'cid:' + data.cid + ':uid:' + results.targetUid + ':tids';
}
2016-09-19 13:08:31 +03:00
if (data.tag) {
set = [set, 'tag:' + data.tag + ':topics'];
}
categories.getCategoryTopics({
cid: data.cid,
2015-01-08 13:47:15 -05:00
set: set,
reverse: reverse,
start: start,
stop: stop,
uid: socket.uid,
2016-01-27 00:05:19 +02:00
targetUid: results.targetUid,
2017-02-17 19:31:21 -07:00
settings: results.settings,
}, function (err, data) {
2014-04-16 14:30:36 -04:00
if (err) {
return callback(err);
}
2014-01-26 17:23:28 -05:00
2016-01-27 20:36:40 +02:00
categories.modifyTopicsByPrivilege(data.topics, results.privileges);
2014-04-16 14:30:36 -04:00
data.privileges = results.privileges;
data.template = {
category: true,
2017-02-17 19:31:21 -07:00
name: 'category',
};
2014-04-16 14:30:36 -04:00
callback(null, data);
});
2014-02-10 14:15:54 -05:00
});
};
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) {
2015-12-28 10:42:15 +02:00
async.parallel({
isAdmin: async.apply(user.isAdministrator, socket.uid),
categories: function (next) {
2015-12-28 10:42:15 +02:00
async.waterfall([
function (next) {
db.getSortedSetRange('cid:0:children', 0, -1, next);
},
2016-11-21 13:47:34 +03:00
function (cids, next) {
privileges.categories.filterCids('read', cids, socket.uid, next);
},
2015-12-28 10:42:15 +02:00
function (cids, next) {
categories.getCategories(cids, socket.uid, next);
2017-02-17 19:31:21 -07:00
},
2015-12-28 10:42:15 +02:00
], next);
2017-02-17 19:31:21 -07:00
},
}, function (err, results) {
2015-12-28 10:42:15 +02:00
if (err) {
return callback(err);
}
results.categories = results.categories.filter(function (category) {
2015-12-28 10:42:15 +02:00
return category && (!category.disabled || results.isAdmin) && !category.link;
});
callback(null, results.categories);
});
};
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) {
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);
});
var cids = [parseInt(cid, 10)];
// filter to subcategories of cid
var any = true;
while (any) {
any = false;
categoryData.forEach(function (c) {
2016-08-22 17:16:52 -05:00
if (cids.indexOf(c.cid) === -1 && cids.indexOf(c.parentCid) !== -1) {
cids.push(c.cid);
any = true;
}
});
}
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
},
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
};
2014-04-10 20:31:57 +01:00
module.exports = SocketCategories;