mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-05 15:20:39 +01:00
@@ -30,7 +30,7 @@ module.exports = function (User) {
|
||||
}
|
||||
|
||||
// for debugging
|
||||
blocked = [2];
|
||||
// blocked = [2];
|
||||
|
||||
blocked = blocked.map(uid => parseInt(uid, 10)).filter(Boolean);
|
||||
User.blocks._cache.set(uid, blocked);
|
||||
@@ -41,6 +41,10 @@ module.exports = function (User) {
|
||||
User.blocks.add = function (targetUid, uid, callback) {
|
||||
async.waterfall([
|
||||
async.apply(db.sortedSetAdd.bind(db), 'uid:' + uid + ':blocked_uids', Date.now(), targetUid),
|
||||
function (next) {
|
||||
User.blocks._cache.del(uid);
|
||||
setImmediate(next);
|
||||
},
|
||||
async.apply(User.blocks.list, uid),
|
||||
], callback);
|
||||
};
|
||||
@@ -48,6 +52,10 @@ module.exports = function (User) {
|
||||
User.blocks.remove = function (targetUid, uid, callback) {
|
||||
async.waterfall([
|
||||
async.apply(db.sortedSetRemove.bind(db), 'uid:' + uid + ':blocked_uids', targetUid),
|
||||
function (next) {
|
||||
User.blocks._cache.del(uid);
|
||||
setImmediate(next);
|
||||
},
|
||||
async.apply(User.blocks.list, uid),
|
||||
], callback);
|
||||
};
|
||||
|
||||
110
test/user.js
110
test/user.js
@@ -1753,4 +1753,114 @@ describe('User', function () {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('user blocking methods', function (done) {
|
||||
let blockeeUid;
|
||||
before(function (done) {
|
||||
User.create({
|
||||
username: 'blockee',
|
||||
email: 'blockee@example.org',
|
||||
fullname: 'Block me',
|
||||
}, function (err, uid) {
|
||||
blockeeUid = uid;
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.remove()', function () {
|
||||
it('should unblock a uid', function (done) {
|
||||
User.blocks.remove(blockeeUid, 1, function (err, blocked_uids) {
|
||||
assert.ifError(err);
|
||||
assert.strictEqual(Array.isArray(blocked_uids), true);
|
||||
assert.strictEqual(blocked_uids.length, 0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('.add()', function () {
|
||||
it('should block a uid', function (done) {
|
||||
User.blocks.add(blockeeUid, 1, function (err, blocked_uids) {
|
||||
assert.ifError(err);
|
||||
assert.strictEqual(Array.isArray(blocked_uids), true);
|
||||
assert.strictEqual(blocked_uids.length, 1);
|
||||
assert.strictEqual(blocked_uids.includes(blockeeUid), true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('.list()', function () {
|
||||
it('should return a list of blocked uids', function (done) {
|
||||
User.blocks.list(1, function (err, blocked_uids) {
|
||||
assert.ifError(err);
|
||||
assert.strictEqual(Array.isArray(blocked_uids), true);
|
||||
assert.strictEqual(blocked_uids.length, 1);
|
||||
assert.strictEqual(blocked_uids.includes(blockeeUid), true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('.filter()', function () {
|
||||
it('should remove entries by blocked uids and return filtered set', function (done) {
|
||||
User.blocks.filter(1, [{
|
||||
foo: 'foo',
|
||||
uid: blockeeUid,
|
||||
}, {
|
||||
foo: 'bar',
|
||||
uid: 1,
|
||||
}, {
|
||||
foo: 'baz',
|
||||
uid: blockeeUid,
|
||||
}], function (err, filtered) {
|
||||
assert.ifError(err);
|
||||
assert.strictEqual(Array.isArray(filtered), true);
|
||||
assert.strictEqual(filtered.length, 1);
|
||||
assert.equal(filtered[0].uid, 1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should allow property argument to be passed in to customise checked property', function (done) {
|
||||
User.blocks.filter(1, 'fromuid', [{
|
||||
foo: 'foo',
|
||||
fromuid: blockeeUid,
|
||||
}, {
|
||||
foo: 'bar',
|
||||
fromuid: 1,
|
||||
}, {
|
||||
foo: 'baz',
|
||||
fromuid: blockeeUid,
|
||||
}], function (err, filtered) {
|
||||
assert.ifError(err);
|
||||
assert.strictEqual(Array.isArray(filtered), true);
|
||||
assert.strictEqual(filtered.length, 1);
|
||||
assert.equal(filtered[0].fromuid, 1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not process invalid sets', function (done) {
|
||||
User.blocks.filter(1, [{ foo: 'foo' }, { foo: 'bar' }, { foo: 'baz' }], function (err, filtered) {
|
||||
assert.ifError(err);
|
||||
assert.strictEqual(Array.isArray(filtered), true);
|
||||
assert.strictEqual(filtered.length, 3);
|
||||
filtered.forEach(function (obj) {
|
||||
assert.strictEqual(obj.hasOwnProperty('foo'), true);
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should process plain sets that just contain uids', function (done) {
|
||||
User.blocks.filter(1, [1, blockeeUid], function (err, filtered) {
|
||||
assert.ifError(err);
|
||||
assert.strictEqual(filtered.length, 1);
|
||||
assert.strictEqual(filtered[0], 1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user