mirror of
https://github.com/wassname/talk.git
synced 2026-07-31 12:50:48 +08:00
Merge branch 'master' of github.com:coralproject/talk into plugin_examples
This commit is contained in:
+15
-1
@@ -36,7 +36,14 @@ class MetadataService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an object on the metadata field of an object.
|
||||
* Sets an object on the metadata field of an object. An example could be:
|
||||
*
|
||||
* @example
|
||||
* const MetadataService = require('services/metadata');
|
||||
* const CommentModel = require('models/comment');
|
||||
*
|
||||
* // Sets the property `loaded` on the comment with `id=1`.
|
||||
* MetadataService.set(CommentModel, '1', 'loaded', true);
|
||||
*
|
||||
* @static
|
||||
* @param {mongoose.Model} model the mongoose model for the object
|
||||
@@ -60,6 +67,13 @@ class MetadataService {
|
||||
/**
|
||||
* Removes the value for the metadata field as the specific key.
|
||||
*
|
||||
* @example
|
||||
* const MetadataService = require('services/metadata');
|
||||
* const CommentModel = require('models/comment');
|
||||
*
|
||||
* // Removes the property `loaded` on the comment with `id=1`.
|
||||
* MetadataService.unset(CommentModel, '1', 'loaded');
|
||||
*
|
||||
* @static
|
||||
* @param {mongoose.Model} model the mongoose model for the object
|
||||
* @param {String} id the value for the field `id` of the model
|
||||
|
||||
+47
-6
@@ -1,3 +1,4 @@
|
||||
const assert = require('assert');
|
||||
const bcrypt = require('bcrypt');
|
||||
const url = require('url');
|
||||
const jwt = require('jsonwebtoken');
|
||||
@@ -253,26 +254,28 @@ module.exports = class UsersService {
|
||||
* @param {Boolean} checkAgainstWordlist enables cheching against the wordlist
|
||||
* @return {Promise}
|
||||
*/
|
||||
static isValidUsername(username, checkAgainstWordlist = true) {
|
||||
static async isValidUsername(username, checkAgainstWordlist = true) {
|
||||
const onlyLettersNumbersUnderscore = /^[A-Za-z0-9_]+$/;
|
||||
|
||||
if (!username) {
|
||||
return Promise.reject(errors.ErrMissingUsername);
|
||||
throw errors.ErrMissingUsername;
|
||||
}
|
||||
|
||||
if (!onlyLettersNumbersUnderscore.test(username)) {
|
||||
|
||||
return Promise.reject(errors.ErrSpecialChars);
|
||||
throw errors.ErrSpecialChars;
|
||||
}
|
||||
|
||||
if (checkAgainstWordlist) {
|
||||
|
||||
// check for profanity
|
||||
console.log('Username profanity check disabled: ', Wordlist.usernameCheck(username));
|
||||
let err = await Wordlist.usernameCheck(username);
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// No errors found!
|
||||
return Promise.resolve(username);
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -834,4 +837,42 @@ module.exports = class UsersService {
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Ignore another user
|
||||
* @param {String} userId the id of the user that is ignoring another users
|
||||
* @param {String[]} usersToIgnore Array of user IDs to ignore
|
||||
*/
|
||||
static ignoreUsers(userId, 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(userId)) {
|
||||
throw new Error('Users cannot ignore themselves');
|
||||
}
|
||||
|
||||
// TODO: For each usersToIgnore, make sure they exist?
|
||||
return UserModel.update({id: userId}, {
|
||||
$addToSet: {
|
||||
ignoresUsers: {
|
||||
$each: usersToIgnore
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop ignoring other users
|
||||
* @param {String} userId the id of the user that is ignoring another users
|
||||
* @param {String[]} usersToStopIgnoring Array of user IDs to stop ignoring
|
||||
*/
|
||||
static async stopIgnoringUsers(userId, usersToStopIgnoring) {
|
||||
assert(Array.isArray(usersToStopIgnoring), 'usersToStopIgnoring is an array');
|
||||
assert(usersToStopIgnoring.every(u => typeof u === 'string'), 'usersToStopIgnoring is an array of string user IDs');
|
||||
await UserModel.update({id: userId}, {
|
||||
$pullAll: {
|
||||
ignoresUsers: usersToStopIgnoring
|
||||
}
|
||||
});
|
||||
console.log('Mongo wrote stopIgnoringUsers', usersToStopIgnoring);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user