mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 03:55:55 +01:00
@@ -6,7 +6,7 @@ var _ = require('lodash');
|
|||||||
|
|
||||||
var db = require('../database');
|
var db = require('../database');
|
||||||
var user = require('../user');
|
var user = require('../user');
|
||||||
var Groups = require('../groups');
|
var groups = require('../groups');
|
||||||
var plugins = require('../plugins');
|
var plugins = require('../plugins');
|
||||||
var privileges = require('../privileges');
|
var privileges = require('../privileges');
|
||||||
const cache = require('../cache');
|
const cache = require('../cache');
|
||||||
@@ -130,10 +130,45 @@ Categories.getCategoriesByPrivilege = function (set, uid, privilege, callback) {
|
|||||||
Categories.getModerators = function (cid, callback) {
|
Categories.getModerators = function (cid, callback) {
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function (next) {
|
function (next) {
|
||||||
Groups.getMembers('cid:' + cid + ':privileges:moderate', 0, -1, next);
|
Categories.getModeratorUids([cid], next);
|
||||||
},
|
},
|
||||||
function (uids, next) {
|
function (uids, next) {
|
||||||
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
|
user.getUsersFields(uids[0], ['uid', 'username', 'userslug', 'picture'], next);
|
||||||
|
},
|
||||||
|
], callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Categories.getModeratorUids = function (cids, callback) {
|
||||||
|
var sets;
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
var groupNames = cids.reduce(function (memo, cid) {
|
||||||
|
memo.push('cid:' + cid + ':privileges:moderate');
|
||||||
|
memo.push('cid:' + cid + ':privileges:groups:moderate');
|
||||||
|
return memo;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
groups.getMembersOfGroups(groupNames, next);
|
||||||
|
},
|
||||||
|
function (memberSets, next) {
|
||||||
|
// Every other set is actually a list of user groups, not uids, so convert those to members
|
||||||
|
sets = memberSets.reduce(function (memo, set, idx) {
|
||||||
|
if (idx % 2) {
|
||||||
|
memo.groupNames.push(set);
|
||||||
|
} else {
|
||||||
|
memo.uids.push(set);
|
||||||
|
}
|
||||||
|
|
||||||
|
return memo;
|
||||||
|
}, { groupNames: [], uids: [] });
|
||||||
|
|
||||||
|
groups.getMembersOfGroups(sets.groupNames, next);
|
||||||
|
},
|
||||||
|
function (groupUids, next) {
|
||||||
|
const moderatorUids = cids.map(function (cid, index) {
|
||||||
|
return _.union(sets.uids[index].concat(groupUids[index]));
|
||||||
|
});
|
||||||
|
next(null, moderatorUids);
|
||||||
},
|
},
|
||||||
], callback);
|
], callback);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -134,9 +134,7 @@ module.exports = function (Groups) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Groups.getMembersOfGroups = function (groupNames, callback) {
|
Groups.getMembersOfGroups = function (groupNames, callback) {
|
||||||
db.getSortedSetsMembers(groupNames.map(function (name) {
|
db.getSortedSetsMembers(groupNames.map(name => 'group:' + name + ':members'), callback);
|
||||||
return 'group:' + name + ':members';
|
|
||||||
}), callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.isMember = function (uid, groupName, callback) {
|
Groups.isMember = function (uid, groupName, callback) {
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
var db = require('../database');
|
var db = require('../database');
|
||||||
var user = require('../user');
|
var user = require('../user');
|
||||||
var meta = require('../meta');
|
var meta = require('../meta');
|
||||||
|
var groups = require('../groups');
|
||||||
var topics = require('../topics');
|
var topics = require('../topics');
|
||||||
|
var categories = require('../categories');
|
||||||
var notifications = require('../notifications');
|
var notifications = require('../notifications');
|
||||||
var privileges = require('../privileges');
|
var privileges = require('../privileges');
|
||||||
var plugins = require('../plugins');
|
var plugins = require('../plugins');
|
||||||
@@ -18,7 +21,7 @@ module.exports = function (Posts) {
|
|||||||
user.getUserFields(uid, ['uid', 'reputation', 'postcount'], next);
|
user.getUserFields(uid, ['uid', 'reputation', 'postcount'], next);
|
||||||
},
|
},
|
||||||
function (userData, next) {
|
function (userData, next) {
|
||||||
var shouldQueue = meta.config.postQueue && (!userData.uid || userData.reputation < 0 || userData.postcount <= 0);
|
const shouldQueue = meta.config.postQueue && (!userData.uid || userData.reputation < 0 || userData.postcount <= 0);
|
||||||
plugins.fireHook('filter:post.shouldQueue', {
|
plugins.fireHook('filter:post.shouldQueue', {
|
||||||
shouldQueue: shouldQueue,
|
shouldQueue: shouldQueue,
|
||||||
uid: uid,
|
uid: uid,
|
||||||
@@ -31,6 +34,44 @@ module.exports = function (Posts) {
|
|||||||
], callback);
|
], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function removeQueueNotification(id, callback) {
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
notifications.rescind('post-queue-' + id, next);
|
||||||
|
},
|
||||||
|
function (next) {
|
||||||
|
getParsedObject(id, next);
|
||||||
|
},
|
||||||
|
function (data, next) {
|
||||||
|
if (!data) {
|
||||||
|
return callback();
|
||||||
|
}
|
||||||
|
getCid(data.type, data, next);
|
||||||
|
},
|
||||||
|
function (cid, next) {
|
||||||
|
getNotificationUids(cid, next);
|
||||||
|
},
|
||||||
|
function (uids, next) {
|
||||||
|
uids.forEach(uid => user.notifications.pushCount(uid));
|
||||||
|
next();
|
||||||
|
},
|
||||||
|
], callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNotificationUids(cid, callback) {
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
async.parallel([
|
||||||
|
async.apply(groups.getMembersOfGroups, ['administrators', 'Global Moderators']),
|
||||||
|
async.apply(categories.getModeratorUids, [cid]),
|
||||||
|
], next);
|
||||||
|
},
|
||||||
|
function (results, next) {
|
||||||
|
next(null, _.union(results));
|
||||||
|
},
|
||||||
|
], callback);
|
||||||
|
}
|
||||||
|
|
||||||
Posts.addToQueue = function (data, callback) {
|
Posts.addToQueue = function (data, callback) {
|
||||||
var type = data.title ? 'topic' : 'reply';
|
var type = data.title ? 'topic' : 'reply';
|
||||||
var id = type + '-' + Date.now();
|
var id = type + '-' + Date.now();
|
||||||
@@ -64,14 +105,21 @@ module.exports = function (Posts) {
|
|||||||
path: '/post-queue',
|
path: '/post-queue',
|
||||||
}, next);
|
}, next);
|
||||||
},
|
},
|
||||||
cid: function (next) {
|
uids: function (next) {
|
||||||
getCid(type, data, next);
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
getCid(type, data, next);
|
||||||
|
},
|
||||||
|
function (cid, next) {
|
||||||
|
getNotificationUids(cid, next);
|
||||||
|
},
|
||||||
|
], next);
|
||||||
},
|
},
|
||||||
}, next);
|
}, next);
|
||||||
},
|
},
|
||||||
function (results, next) {
|
function (results, next) {
|
||||||
if (results.notification) {
|
if (results.notification) {
|
||||||
notifications.pushGroups(results.notification, ['administrators', 'Global Moderators', 'cid:' + results.cid + ':privileges:moderate'], next);
|
notifications.push(results.uids, next);
|
||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
@@ -127,15 +175,15 @@ module.exports = function (Posts) {
|
|||||||
|
|
||||||
Posts.removeFromQueue = function (id, callback) {
|
Posts.removeFromQueue = function (id, callback) {
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
removeQueueNotification(id, next);
|
||||||
|
},
|
||||||
function (next) {
|
function (next) {
|
||||||
db.sortedSetRemove('post:queue', id, next);
|
db.sortedSetRemove('post:queue', id, next);
|
||||||
},
|
},
|
||||||
function (next) {
|
function (next) {
|
||||||
db.delete('post:queue:' + id, next);
|
db.delete('post:queue:' + id, next);
|
||||||
},
|
},
|
||||||
function (next) {
|
|
||||||
notifications.rescind('post-queue-' + id, next);
|
|
||||||
},
|
|
||||||
], callback);
|
], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -291,32 +291,10 @@ User.getModeratorUids = function (callback) {
|
|||||||
async.waterfall([
|
async.waterfall([
|
||||||
async.apply(categories.getAllCidsFromSet, 'categories:cid'),
|
async.apply(categories.getAllCidsFromSet, 'categories:cid'),
|
||||||
function (cids, next) {
|
function (cids, next) {
|
||||||
var groupNames = cids.reduce(function (memo, cid) {
|
categories.getModeratorUids(cids, next);
|
||||||
memo.push('cid:' + cid + ':privileges:moderate');
|
|
||||||
memo.push('cid:' + cid + ':privileges:groups:moderate');
|
|
||||||
return memo;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
groups.getMembersOfGroups(groupNames, next);
|
|
||||||
},
|
},
|
||||||
function (memberSets, next) {
|
function (uids, next) {
|
||||||
// Every other set is actually a list of user groups, not uids, so convert those to members
|
next(null, _.union(uids));
|
||||||
var sets = memberSets.reduce(function (memo, set, idx) {
|
|
||||||
if (idx % 2) {
|
|
||||||
memo.working.push(set);
|
|
||||||
} else {
|
|
||||||
memo.regular.push(set);
|
|
||||||
}
|
|
||||||
|
|
||||||
return memo;
|
|
||||||
}, { working: [], regular: [] });
|
|
||||||
|
|
||||||
groups.getMembersOfGroups(sets.working, function (err, memberSets) {
|
|
||||||
next(err, sets.regular.concat(memberSets || []));
|
|
||||||
});
|
|
||||||
},
|
|
||||||
function (memberSets, next) {
|
|
||||||
next(null, _.union.apply(_, memberSets));
|
|
||||||
},
|
},
|
||||||
], callback);
|
], callback);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -800,6 +800,33 @@ describe('Categories', function () {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Categories.getModeratorUids', function () {
|
||||||
|
before(function (done) {
|
||||||
|
async.series([
|
||||||
|
async.apply(groups.create, { name: 'testGroup' }),
|
||||||
|
async.apply(groups.join, 'cid:1:privileges:groups:moderate', 'testGroup'),
|
||||||
|
async.apply(groups.join, 'testGroup', 1),
|
||||||
|
], done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should retrieve all users with moderator bit in category privilege', function (done) {
|
||||||
|
Categories.getModeratorUids([1, 2], function (err, uids) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.strictEqual(2, uids.length);
|
||||||
|
assert.strictEqual(1, parseInt(uids[0], 10));
|
||||||
|
assert.strictEqual(0, uids[1].length);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
after(function (done) {
|
||||||
|
async.series([
|
||||||
|
async.apply(groups.leave, 'cid:1:privileges:groups:moderate', 'testGroup'),
|
||||||
|
async.apply(groups.destroy, 'testGroup'),
|
||||||
|
], done);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user