Skip to content
Snippets Groups Projects
Commit a199b120 authored by Gabriel Engel's avatar Gabriel Engel
Browse files

Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into develop

parents 56879311 f5ff04cb
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,9 @@ Meteor.methods
check message, Object
if not Meteor.userId()
throw new Meteor.Error('error-invalid-user', "Invalid user", { method: 'sendMessage' })
if message.ts
tsDiff = Math.abs(moment(message.ts).diff())
if tsDiff > 60000
......@@ -17,9 +20,6 @@ Meteor.methods
if message.msg?.length > RocketChat.settings.get('Message_MaxAllowedSize')
throw new Meteor.Error('error-message-size-exceeded', 'Message size exceeds Message_MaxAllowedSize', { method: 'sendMessage' })
if not Meteor.userId()
throw new Meteor.Error('error-invalid-user', "Invalid user", { method: 'sendMessage' })
user = RocketChat.models.Users.findOneById Meteor.userId(), fields: username: 1, name: 1
room = Meteor.call 'canAccessRoom', message.rid, user._id
......@@ -42,10 +42,12 @@ Meteor.methods
RocketChat.sendMessage user, message, room
# Limit a user to sending 5 msgs/second
# Limit a user, who does not have the "bot" role, to sending 5 msgs/second
DDPRateLimiter.addRule
type: 'method'
name: 'sendMessage'
userId: (userId) ->
return RocketChat.models.Users.findOneById(userId)?.username isnt RocketChat.settings.get('InternalHubot_Username')
user = RocketChat.models.Users.findOneById(userId)
return true if not user?.roles
return 'bot' not in user.roles
, 5, 1000
......@@ -353,7 +353,7 @@ class ModelUsers extends RocketChat.models._Base
address: s.trim(data.email)
]
else
unsetData.name = 1
unsetData.emails = 1
if data.phone?
if not _.isEmpty(s.trim(data.phone))
......
......@@ -14,7 +14,7 @@ this.Commands = {
promptTranscript: function() {
if (Livechat.transcript) {
const user = Meteor.user();
let email = user.emails && user.emails.length > 0 ? user.emails[0].address : '';
let email = user.visitorEmails && user.visitorEmails.length > 0 ? user.visitorEmails[0].address : '';
swal({
title: t('Chat_ended'),
......
......@@ -10,8 +10,8 @@ Template.visitorEdit.helpers({
email() {
const visitor = Template.instance().visitor.get();
if (visitor.emails && visitor.emails.length > 0) {
return visitor.emails[0].address;
if (visitor.visitorEmails && visitor.visitorEmails.length > 0) {
return visitor.visitorEmails[0].address;
}
},
......
......@@ -15,7 +15,7 @@
<ul>
{{#if utc}}<li><i class="icon-clock"></i>{{userTime}} (UTC {{utc}})</li>{{/if}}
{{#each emails}} <li><i class="icon-mail"></i> {{address}}{{#if verified}}&nbsp;<i class="icon-ok"></i>{{/if}}</li> {{/each}}
{{#each visitorEmails}} <li><i class="icon-mail"></i> {{address}}{{#if verified}}&nbsp;<i class="icon-ok"></i>{{/if}}</li> {{/each}}
{{#each phone}} <li><i class="icon-phone"></i> {{phoneNumber}}</li> {{/each}}
{{#if lastLogin}} <li><i class="icon-calendar"></i> {{_ "Created_at"}}: {{createdAt}}</li> {{/if}}
{{#if lastLogin}} <li><i class="icon-calendar"></i> {{_ "Last_login"}}: {{lastLogin}}</li> {{/if}}
......
......@@ -106,15 +106,7 @@ RocketChat.Livechat = {
var existingUser = null;
if (s.trim(email) !== '' && (existingUser = RocketChat.models.Users.findOneByEmailAddress(email))) {
if (existingUser.type !== 'visitor') {
throw new Meteor.Error('error-invalid-user', 'This email belongs to a registered user.');
}
updateUser.$addToSet = {
globalRoles: 'livechat-guest'
};
if (s.trim(email) !== '' && (existingUser = RocketChat.models.Users.findOneGuestByEmailAddress(email))) {
if (loginToken) {
updateUser.$addToSet['services.resume.loginTokens'] = loginToken;
}
......@@ -156,7 +148,7 @@ RocketChat.Livechat = {
}
if (email && email.trim() !== '') {
updateUser.$set.emails = [
updateUser.$set.visitorEmails = [
{ address: email }
];
}
......@@ -178,7 +170,7 @@ RocketChat.Livechat = {
if (phone) {
updateData.phone = phone;
}
const ret = RocketChat.models.Users.saveUserById(_id, updateData);
const ret = RocketChat.models.Users.saveGuestById(_id, updateData);
Meteor.defer(() => {
RocketChat.callbacks.run('livechat.saveGuest', updateData);
......@@ -387,8 +379,8 @@ RocketChat.Livechat = {
postData.crmData = room.crmData;
}
if (visitor.emails && visitor.emails.length > 0) {
postData.visitor.email = visitor.emails[0].address;
if (visitor.visitorEmails && visitor.visitorEmails.length > 0) {
postData.visitor.email = visitor.visitorEmails[0].address;
}
if (visitor.phone && visitor.phone.length > 0) {
postData.visitor.phone = visitor.phone[0].phoneNumber;
......
......@@ -214,3 +214,60 @@ RocketChat.models.Users.getNextVisitorUsername = function() {
return 'guest-' + (livechatCount.value.value + 1);
};
RocketChat.models.Users.saveGuestById = function(_id, data) {
const setData = {};
const unsetData = {};
if (data.name) {
if (!_.isEmpty(s.trim(data.name))) {
setData.name = s.trim(data.name);
} else {
unsetData.name = 1;
}
}
if (data.email) {
if (!_.isEmpty(s.trim(data.email))) {
setData.visitorEmails = [
{ address: s.trim(data.email) }
];
} else {
unsetData.visitorEmails = 1;
}
}
if (data.phone) {
if (!_.isEmpty(s.trim(data.phone))) {
setData.phone = [
{ phoneNumber: s.trim(data.phone) }
];
} else {
unsetData.phone = 1;
}
}
const update = {};
if (!_.isEmpty(setData)) {
update.$set = setData;
}
if (!_.isEmpty(unsetData)) {
update.$unset = unsetData;
}
if (_.isEmpty(update)) {
return true;
}
return this.update({ _id }, update);
};
RocketChat.models.Users.findOneGuestByEmailAddress = function(emailAddress) {
const query = {
'visitorEmails.address': new RegExp('^' + s.escapeRegExp(emailAddress) + '$', 'i')
};
return this.findOne(query);
};
Meteor.startup(function() {
RocketChat.models.Rooms.tryEnsureIndex({ code: 1 });
RocketChat.models.Rooms.tryEnsureIndex({ open: 1 }, { sparse: 1 });
RocketChat.models.Users.tryEnsureIndex({ 'visitorEmails.address': 1 });
});
RocketChat.Migrations.add({
version: 72,
up: function() {
RocketChat.models.Users.find({ type: 'visitor', 'emails.address': { $exists: true } }, { emails: 1 }).forEach(function(visitor) {
RocketChat.models.Users.update({ _id: visitor._id }, {
$set: {
visitorEmails: visitor.emails
},
$unset: {
emails: 1
}
});
});
}
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment