diff --git a/services/actions.js b/services/actions.js index adffdd134..d6f709632 100644 --- a/services/actions.js +++ b/services/actions.js @@ -78,7 +78,7 @@ module.exports = class ActionsService { }); // Emit that there was a new action created. - await events.emitAsync(ACTIONS_NEW, foundAction); + events.emit(ACTIONS_NEW, foundAction); return foundAction; } @@ -233,7 +233,7 @@ module.exports = class ActionsService { } // Emit that the action was deleted. - await events.emitAsync(ACTIONS_DELETE, action); + events.emit(ACTIONS_DELETE, action); return action; } diff --git a/services/comments.js b/services/comments.js index 086c228aa..495472a81 100644 --- a/services/comments.js +++ b/services/comments.js @@ -44,7 +44,7 @@ module.exports = class CommentsService { const savedCommentModel = await commentModel.save(); // Emit that the comment was created! - await events.emitAsync(COMMENTS_NEW, savedCommentModel); + events.emit(COMMENTS_NEW, savedCommentModel); return savedCommentModel; } @@ -193,7 +193,7 @@ module.exports = class CommentsService { } } - await events.emitAsync(COMMENTS_EDIT, originalComment, editedComment); + events.emit(COMMENTS_EDIT, originalComment, editedComment); return editedComment; } @@ -305,7 +305,7 @@ module.exports = class CommentsService { // Emit that the comment was edited, and pass the original comment and the // edited comment. - await events.emitAsync(COMMENTS_EDIT, originalComment, editedComment); + events.emit(COMMENTS_EDIT, originalComment, editedComment); return editedComment; } diff --git a/services/events/constants.js b/services/events/constants.js index 78729c73a..c66e13faf 100644 --- a/services/events/constants.js +++ b/services/events/constants.js @@ -1,4 +1,5 @@ module.exports = { + USERS_NEW: 'USERS_NEW', ACTIONS_DELETE: 'ACTIONS_DELETE', ACTIONS_NEW: 'ACTIONS_NEW', COMMENTS_NEW: 'COMMENTS_NEW', diff --git a/services/events/index.js b/services/events/index.js index 214e23d65..ff5e93cf0 100644 --- a/services/events/index.js +++ b/services/events/index.js @@ -1,22 +1,32 @@ const {EventEmitter2} = require('eventemitter2'); +const constants = require('./constants'); const debug = require('debug')('talk:services:events'); const enabled = require('debug').enabled('talk:services:events'); -const events = new EventEmitter2({ +const emitter = new EventEmitter2({ wildcard: true, }); // If event debugging is enabled, bind the debugger to all events being emitted // and log a debug message. if (enabled) { - events.onAny(function(event) { + emitter.onAny(function(event) { debug(`[${event}] ${arguments.length - 1} argument${arguments.length - 1 === 1 ? '' : 's'}`); }); } +// Allow any number of listeners to attach to this. +emitter.setMaxListeners(0); + // The default error handler. -events.on('error', (err) => { +emitter.on('error', (err) => { console.error('events error:', err); }); -module.exports = events; +emitter.on('newListener', (event) => { + if (!(event in constants)) { + throw new Error(`Event[${event}] not a valid event name`); + } +}); + +module.exports = emitter; diff --git a/services/users.js b/services/users.js index 719666788..6080a0369 100644 --- a/services/users.js +++ b/services/users.js @@ -3,14 +3,15 @@ const bcrypt = require('bcryptjs'); const errors = require('../errors'); const some = require('lodash/some'); const merge = require('lodash/merge'); -const events = require('./events'); const timeago = require('./timeago'); const { + USERS_NEW, USERS_SUSPENSION_CHANGE, USERS_BAN_CHANGE, USERS_USERNAME_STATUS_CHANGE, } = require('./events/constants'); +const events = require('./events'); const { ROOT_URL @@ -23,7 +24,6 @@ const { const debug = require('debug')('talk:services:users'); const UserModel = require('../models/user'); -const USER_ROLES = require('../models/enum/user_roles'); const RECAPTCHA_WINDOW = '10m'; // 10 minutes. const RECAPTCHA_INCORRECT_TRIGGER = 5; // after 3 incorrect attempts, recaptcha will be required. @@ -347,43 +347,47 @@ class UsersService { * @param {Object} profile - User social/external profile * @param {Function} done [description] */ - static findOrCreateExternalUser({id, provider, displayName}) { - return UserModel - .findOne({ - profiles: { - $elemMatch: { - id, - provider + static async findOrCreateExternalUser({id, provider, displayName}) { + let user = await UserModel.findOne({ + profiles: { + $elemMatch: { + id, + provider + } + } + }); + if (user) { + return user; + } + + // User does not exist and need to be created. + + // Create an initial username for the user. + let username = UsersService.castUsername(displayName); + + // The user was not found, lets create them! + user = new UserModel({ + username, + lowercaseUsername: username.toLowerCase(), + roles: [], + profiles: [{id, provider}], + status: { + username: { + status: 'UNSET', + history: { + status: 'UNSET' } } - }) - .then((user) => { - if (user) { - return user; - } + } + }); - // User does not exist and need to be created. + // Save the user in the database. + await user.save(); - let username = UsersService.castUsername(displayName); + // Emit that the user was created. + events.emit(USERS_NEW, user); - // The user was not found, lets create them! - user = new UserModel({ - username, - lowercaseUsername: username.toLowerCase(), - roles: [], - profiles: [{id, provider}], - status: { - username: { - status: 'UNSET', - history: { - status: 'UNSET' - } - } - } - }); - - return user.save(); - }); + return user; } /** @@ -545,6 +549,9 @@ class UsersService { throw err; } + // Emit that the user was created. + events.emit(USERS_NEW, user); + return user; }