mirror of
https://github.com/wassname/talk.git
synced 2026-07-02 11:04:05 +08:00
Implement cannot ignore staff in users service
`UsersService.ignoreUsers` now checks if any user provided is a staff member. In case it is it throws a ErrCannotIgnoreStaff error.
This commit is contained in:
+7
-1
@@ -2,6 +2,7 @@ const assert = require('assert');
|
||||
const uuid = require('uuid');
|
||||
const bcrypt = require('bcryptjs');
|
||||
const errors = require('../errors');
|
||||
const some = require('lodash/some');
|
||||
|
||||
const {
|
||||
ROOT_URL
|
||||
@@ -879,13 +880,18 @@ module.exports = class UsersService {
|
||||
* @param {String} id the id of the user that is ignoring another users
|
||||
* @param {Array<String>} usersToIgnore Array of user IDs to ignore
|
||||
*/
|
||||
static ignoreUsers(id, usersToIgnore) {
|
||||
static async ignoreUsers(id, usersToIgnore) {
|
||||
assert(Array.isArray(usersToIgnore), 'usersToIgnore is an array');
|
||||
assert(usersToIgnore.every((u) => typeof u === 'string'), 'usersToIgnore is an array of string user IDs');
|
||||
if (usersToIgnore.includes(id)) {
|
||||
throw new Error('Users cannot ignore themselves');
|
||||
}
|
||||
|
||||
const users = await UsersService.findByIdArray(usersToIgnore);
|
||||
if (some(users, (user) => user.isStaff())) {
|
||||
throw errors.ErrCannotIgnoreStaff;
|
||||
}
|
||||
|
||||
// TODO: For each usersToIgnore, make sure they exist?
|
||||
return UserModel.update({id}, {
|
||||
$addToSet: {
|
||||
|
||||
@@ -164,6 +164,19 @@ describe('services.UsersService', () => {
|
||||
const userAfterIgnoring2 = await UsersService.findById(user.id);
|
||||
expect(userAfterIgnoring2.ignoresUsers.length).to.equal(2);
|
||||
});
|
||||
|
||||
it('should not ignore a staff member', async () => {
|
||||
const user = mockUsers[0];
|
||||
const usersToIgnore = [mockUsers[1]];
|
||||
await UsersService.addRoleToUser(usersToIgnore[0].id, 'STAFF');
|
||||
|
||||
try {
|
||||
await UsersService.ignoreUsers(user.id, usersToIgnore.map((u) => u.id));
|
||||
} catch (err) {
|
||||
expect(err.status).to.equal(400);
|
||||
expect(err.translation_key).to.equal('CANNOT_IGNORE_STAFF');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('#ban', () => {
|
||||
|
||||
Reference in New Issue
Block a user