diff --git a/js/models/conversations.js b/js/models/conversations.js index 093d65b12a..cdfeb85294 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -57,7 +57,20 @@ } if (!attributes.tokens) { - this.updateTokens(); + return this.updateTokens(); + } + }, + + validateNumber: function() { + try { + this.id = libphonenumber.util.verifyNumber(this.id); + } catch (ex) { + if (ex === "Invalid country calling code") { + var regionCode = storage.get('regionCode'); + this.id = libphonenumber.util.verifyNumber(this.id, regionCode); + } else { + throw ex; + } } }, @@ -70,12 +83,13 @@ if (this.isPrivate()) { try { - this.id = libphonenumber.util.verifyNumber(this.id); + this.validateNumber(); var number = libphonenumber.util.splitCountryCode(this.id); var international_number = '' + number.country_code + number.national_number; var national_number = '' + number.national_number; this.set({ + id: this.id, e164_number: this.id, national_number: national_number, international_number: international_number diff --git a/js/views/conversation_search_view.js b/js/views/conversation_search_view.js index 8031733fd1..3db9e80944 100644 --- a/js/views/conversation_search_view.js +++ b/js/views/conversation_search_view.js @@ -66,9 +66,17 @@ }, createConversation: function() { - this.$el.trigger('open', this.new_contact_view.model); - this.initNewContact(); - this.resetTypeahead(); + var conversation = this.new_contact_view.model; + var error = conversation.validate(conversation.attributes); + if (!error) { + ConversationController.findOrCreatePrivateById( + this.new_contact_view.model.id + ).then(function(conversation) { + this.$el.trigger('open', conversation); + this.initNewContact(); + this.resetTypeahead(); + }.bind(this)); + } }, open: function(e, conversation) {