From 0b7742ecd75868a94f3a169dfed0bf669697d94e Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 1 Dec 2015 15:29:39 -0800 Subject: [PATCH] Create contact by number with no country code or + Search box finds or creates a conversation given a phone number in local (to the user's region) or international format. Previously you had to enter e164 format to set up the conversation correctly. If the number is not valid, do not open the conversation. TODO: user feedback on invalid numbers. // FREEBIE --- js/models/conversations.js | 18 ++++++++++++++++-- js/views/conversation_search_view.js | 14 +++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 093d65b12..cdfeb8529 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 8031733fd..3db9e8094 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) {