diff options
Diffstat (limited to 'models/users.js')
-rw-r--r-- | models/users.js | 176 |
1 files changed, 141 insertions, 35 deletions
diff --git a/models/users.js b/models/users.js index 790ee0a1..edf1a203 100644 --- a/models/users.js +++ b/models/users.js @@ -1,3 +1,7 @@ +// Sandstorm context is detected using the METEOR_SETTINGS environment variable +// in the package definition. +const isSandstorm = Meteor.settings && Meteor.settings.public && + Meteor.settings.public.sandstorm; Users = Meteor.users; Users.attachSchema(new SimpleSchema({ @@ -55,6 +59,10 @@ Users.attachSchema(new SimpleSchema({ type: String, optional: true, }, + 'profile.hiddenSystemMessages': { + type: Boolean, + optional: true, + }, 'profile.initials': { type: String, optional: true, @@ -71,6 +79,10 @@ Users.attachSchema(new SimpleSchema({ type: [String], optional: true, }, + 'profile.showCardsCountAt': { + type: Number, + optional: true, + }, 'profile.starredBoards': { type: [String], optional: true, @@ -79,6 +91,10 @@ Users.attachSchema(new SimpleSchema({ type: [String], optional: true, }, + 'profile.icode': { + type: String, + optional: true, + }, services: { type: Object, optional: true, @@ -105,6 +121,16 @@ if (Meteor.isClient) { return board && board.hasMember(this._id); }, + isNotCommentOnly() { + const board = Boards.findOne(Session.get('currentBoard')); + return board && board.hasMember(this._id) && !board.hasCommentOnly(this._id); + }, + + isCommentOnly() { + const board = Boards.findOne(Session.get('currentBoard')); + return board && board.hasCommentOnly(this._id); + }, + isBoardAdmin() { const board = Boards.findOne(Session.get('currentBoard')); return board && board.hasAdmin(this._id); @@ -147,6 +173,11 @@ Users.helpers({ return _.contains(notifications, activityId); }, + hasHiddenSystemMessages() { + const profile = this.profile || {}; + return profile.hiddenSystemMessages || false; + }, + getEmailBuffer() { const {emailBuffer = []} = this.profile; return emailBuffer; @@ -167,6 +198,11 @@ Users.helpers({ } }, + getLimitToShowCardsCount() { + const profile = this.profile || {}; + return profile.showCardsCountAt; + }, + getName() { const profile = this.profile || {}; return profile.fullname || this.username; @@ -227,6 +263,14 @@ Users.mutations({ this.addTag(tag); }, + toggleSystem(value = false) { + return { + $set: { + 'profile.hiddenSystemMessages': !value, + }, + }; + }, + addNotification(activityId) { return { $addToSet: { @@ -262,6 +306,10 @@ Users.mutations({ setAvatarUrl(avatarUrl) { return { $set: { 'profile.avatarUrl': avatarUrl }}; }, + + setShowCardsCountAt(limit) { + return { $set: { 'profile.showCardsCountAt': limit } }; + }, }); Meteor.methods({ @@ -274,6 +322,14 @@ Meteor.methods({ Users.update(this.userId, {$set: { username }}); } }, + toggleSystemMessages() { + const user = Meteor.user(); + user.toggleSystem(user.hasHiddenSystemMessages()); + }, + changeLimitToShowCardsCount(limit) { + check(limit, Number); + Meteor.user().setShowCardsCountAt(limit); + }, }); if (Meteor.isServer) { @@ -306,8 +362,9 @@ if (Meteor.isServer) { if (user._id === inviter._id) throw new Meteor.Error('error-user-notAllowSelf'); } else { if (posAt <= 0) throw new Meteor.Error('error-user-doesNotExist'); - - const email = username; + if (Settings.findOne().disableRegistration) throw new Meteor.Error('error-user-notCreated'); + // Set in lowercase email before creating account + const email = username.toLowerCase(); username = email.substring(0, posAt); const newUserId = Accounts.createUser({ username, email }); if (!newUserId) throw new Meteor.Error('error-user-notCreated'); @@ -326,27 +383,51 @@ if (Meteor.isServer) { board.addMember(user._id); user.addInvite(boardId); - try { - const params = { - user: user.username, - inviter: inviter.username, - board: board.title, - url: board.absoluteUrl(), - }; - const lang = user.getLanguage(); - Email.send({ - to: user.emails[0].address, - from: Accounts.emailTemplates.from, - subject: TAPi18n.__('email-invite-subject', params, lang), - text: TAPi18n.__('email-invite-text', params, lang), - }); - } catch (e) { - throw new Meteor.Error('email-fail', e.message); + if (Settings.findOne().mailUrl()) { + try { + const params = { + user: user.username, + inviter: inviter.username, + board: board.title, + url: board.absoluteUrl(), + }; + const lang = user.getLanguage(); + Email.send({ + to: user.emails[0].address.toLowerCase(), + from: Accounts.emailTemplates.from, + subject: TAPi18n.__('email-invite-subject', params, lang), + text: TAPi18n.__('email-invite-text', params, lang), + }); + } catch (e) { + throw new Meteor.Error('email-fail', e.message); + } } - return { username: user.username, email: user.emails[0].address }; }, }); + Accounts.onCreateUser((options, user) => { + const userCount = Users.find().count(); + if (userCount === 0){ + user.isAdmin = true; + return user; + } + const disableRegistration = Settings.findOne().disableRegistration; + if (!disableRegistration) { + return user; + } + + if (!options || !options.profile) { + throw new Meteor.Error('error-invitation-code-blank', 'The invitation code is required'); + } + const invitationCode = InvitationCodes.findOne({code: options.profile.invitationcode, email: options.email, valid: true}); + if (!invitationCode) { + throw new Meteor.Error('error-invitation-code-not-exist', 'The invitation code doesn\'t exist'); + }else{ + user.profile = {icode: options.profile.invitationcode}; + } + + return user; + }); } if (Meteor.isServer) { @@ -394,24 +475,49 @@ if (Meteor.isServer) { return fakeUserId.get() || getUserId(); }; - Users.after.insert((userId, doc) => { - const fakeUser = { - extendAutoValueContext: { - userId: doc._id, - }, - }; - - fakeUserId.withValue(doc._id, () => { - // Insert the Welcome Board - Boards.insert({ - title: TAPi18n.__('welcome-board'), - permission: 'private', - }, fakeUser, (err, boardId) => { - - ['welcome-list1', 'welcome-list2'].forEach((title) => { - Lists.insert({ title: TAPi18n.__(title), boardId }, fakeUser); + if (!isSandstorm) { + Users.after.insert((userId, doc) => { + const fakeUser = { + extendAutoValueContext: { + userId: doc._id, + }, + }; + + fakeUserId.withValue(doc._id, () => { + // Insert the Welcome Board + Boards.insert({ + title: TAPi18n.__('welcome-board'), + permission: 'private', + }, fakeUser, (err, boardId) => { + + ['welcome-list1', 'welcome-list2'].forEach((title) => { + Lists.insert({ title: TAPi18n.__(title), boardId }, fakeUser); + }); }); }); }); + } + + Users.after.insert((userId, doc) => { + + //invite user to corresponding boards + const disableRegistration = Settings.findOne().disableRegistration; + if (disableRegistration) { + const invitationCode = InvitationCodes.findOne({code: doc.profile.icode, valid:true}); + if (!invitationCode) { + throw new Meteor.Error('error-invitation-code-not-exist'); + }else{ + invitationCode.boardsToBeInvited.forEach((boardId) => { + const board = Boards.findOne(boardId); + board.addMember(doc._id); + }); + if (!doc.profile) { + doc.profile = {}; + } + doc.profile.invitedBoards = invitationCode.boardsToBeInvited; + Users.update(doc._id, {$set:{profile: doc.profile}}); + InvitationCodes.update(invitationCode._id, {$set: {valid:false}}); + } + } }); } |