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

115 lines
2.7 KiB
JavaScript
Raw Normal View History

'use strict';
2014-04-16 14:30:36 -04:00
var async = require('async'),
2014-06-27 15:35:53 -04:00
db = require('../database'),
2014-04-16 14:30:36 -04:00
categories = require('../categories'),
2014-05-15 20:49:47 -04:00
privileges = require('../privileges'),
2014-06-27 15:35:53 -04:00
user = require('../user'),
2014-08-29 15:57:20 -04:00
topics = require('../topics'),
2014-08-13 18:44:40 -04:00
websockets = require('./index'),
SocketCategories = {};
SocketCategories.getRecentReplies = function(socket, cid, callback) {
2014-05-17 18:59:34 -04:00
privileges.categories.can('read', cid, socket.uid, function(err, canRead) {
if (err) {
return callback(err);
}
2014-05-15 20:49:47 -04:00
if (!canRead) {
return callback(null, []);
}
categories.getRecentReplies(cid, socket.uid, 4, callback);
});
};
SocketCategories.get = function(socket, data, callback) {
2014-08-22 18:05:50 -04:00
categories.getCategoriesByPrivilege(socket.uid, 'find', callback);
};
SocketCategories.loadMore = function(socket, data, callback) {
2014-01-17 12:49:21 -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) {
user.getSettings(socket.uid, next);
},
targetUid: function(next) {
if (data.author) {
user.getUidByUserslug(data.author, next);
} else {
next();
}
2014-04-16 14:30:36 -04:00
}
}, function(err, results) {
if (err) {
return callback(err);
}
if (!results.privileges.read) {
return callback(new Error('[[error:no-privileges]]'));
}
2014-02-10 14:15:54 -05:00
var start = parseInt(data.after, 10),
2014-04-16 14:30:36 -04:00
end = start + results.settings.topicsPerPage - 1;
categories.getCategoryTopics({
cid: data.cid,
start: start,
stop: end,
uid: socket.uid,
targetUid: results.targetUid
}, function(err, data) {
2014-04-16 14:30:36 -04:00
if (err) {
return callback(err);
}
2014-01-26 17:23:28 -05:00
2014-04-16 14:30:36 -04:00
data.privileges = results.privileges;
callback(null, data);
});
2014-02-10 14:15:54 -05:00
});
};
2014-01-24 23:05:59 -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
2014-04-04 12:42:41 -04:00
SocketCategories.getTopicCount = function(socket, cid, callback) {
categories.getCategoryField(cid, 'topic_count', callback);
};
2014-08-13 18:44:40 -04:00
SocketCategories.getUsersInCategory = function(socket, cid, callback) {
var uids = websockets.getUidsInRoom('category_' + cid);
user.getMultipleUserFields(uids, ['uid', 'userslug', 'username', 'picture'], callback);
};
2014-08-15 15:45:01 -04:00
SocketCategories.getCategoriesByPrivilege = function(socket, privilege, callback) {
categories.getCategoriesByPrivilege(socket.uid, privilege, callback);
};
2014-08-29 15:57:20 -04:00
SocketCategories.watch = function(socket, cid, callback) {
user.watchCategory(socket.uid, cid, function(err) {
if (err) {
return callback(err);
}
topics.pushUnreadCount(socket.uid, callback);
});
};
SocketCategories.ignore = function(socket, cid, callback) {
user.ignoreCategory(socket.uid, cid, function(err) {
if (err) {
return callback(err);
}
topics.pushUnreadCount(socket.uid, callback);
});
};
2014-04-10 20:31:57 +01:00
module.exports = SocketCategories;