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

126 lines
3.1 KiB
JavaScript

'use strict';
var async = require('async'),
11 years ago
db = require('../database'),
categories = require('../categories'),
11 years ago
privileges = require('../privileges'),
11 years ago
user = require('../user'),
11 years ago
topics = require('../topics'),
websockets = require('./index'),
SocketCategories = {};
SocketCategories.getRecentReplies = function(socket, cid, callback) {
categories.getRecentReplies(cid, socket.uid, 4, callback);
};
SocketCategories.get = function(socket, data, callback) {
11 years ago
categories.getCategoriesByPrivilege(socket.uid, 'find', callback);
};
SocketCategories.loadMore = function(socket, data, callback) {
10 years ago
if (!data) {
11 years ago
return callback(new Error('[[error:invalid-data]]'));
}
async.parallel({
privileges: function(next) {
11 years ago
privileges.categories.get(data.cid, socket.uid, next);
},
settings: function(next) {
user.getSettings(socket.uid, next);
},
targetUid: function(next) {
if (data.author) {
user.getUidByUserslug(data.author, next);
} else {
next();
}
}
}, function(err, results) {
if (err) {
return callback(err);
}
if (!results.privileges.read) {
return callback(new Error('[[error:no-privileges]]'));
}
10 years ago
var set = 'cid:' + data.cid + ':tids',
reverse = false;
if (results.settings.categoryTopicSort === 'newest_to_oldest') {
reverse = true;
} else if (results.settings.categoryTopicSort === 'most_posts') {
reverse = true;
set = 'cid:' + data.cid + ':tids:posts';
}
var start = parseInt(data.after, 10),
stop = start + results.settings.topicsPerPage - 1;
10 years ago
if (results.targetUid) {
set = 'cid:' + data.cid + ':uid:' + results.targetUid + ':tids';
}
categories.getCategoryTopics({
cid: data.cid,
10 years ago
set: set,
reverse: reverse,
start: start,
stop: stop,
uid: socket.uid,
targetUid: results.targetUid
}, function(err, data) {
if (err) {
return callback(err);
}
data.privileges = results.privileges;
callback(null, data);
});
});
};
SocketCategories.getPageCount = function(socket, cid, callback) {
categories.getPageCount(cid, socket.uid, callback);
};
11 years ago
SocketCategories.getTopicCount = function(socket, cid, callback) {
categories.getCategoryField(cid, 'topic_count', callback);
};
SocketCategories.getUsersInCategory = function(socket, cid, callback) {
var uids = websockets.getUidsInRoom('category_' + cid);
user.getMultipleUserFields(uids, ['uid', 'userslug', 'username', 'picture'], callback);
};
11 years ago
SocketCategories.getCategoriesByPrivilege = function(socket, privilege, callback) {
categories.getCategoriesByPrivilege(socket.uid, privilege, callback);
};
11 years ago
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);
});
};
10 years ago
SocketCategories.isModerator = function(socket, cid, callback) {
user.isModerator(socket.uid, cid, callback);
};
module.exports = SocketCategories;