From 9d342e8951ea7848063e9f89d7816fa33ec5f9cc Mon Sep 17 00:00:00 2001 From: Mikunj Date: Wed, 7 Nov 2018 11:41:46 +1100 Subject: [PATCH] Show a message if user types in an invalid public key in search. --- js/models/conversations.js | 16 +++++++++++----- js/views/conversation_search_view.js | 24 ++++++++++-------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index c618baa5e..4135aa2b3 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -672,14 +672,19 @@ }, validateNumber() { + if (!this.id) return null; + if (this.isPrivate()) { - if (this.id.length === 33 * 2) { + // Check if it's hex + const isHex = this.id.replace(/[\s]*/g, '').match(/^[0-9a-fA-F]+$/); + if (!isHex) return 'Invalid Hex ID'; + + // Check if it has a valid length + if (this.id.length !== 33 * 2) { // 33 bytes in hex this.set({ id: this.id }); - return null; + return 'Invalid ID Length'; } - - return 'Invalid ID'; } return null; @@ -1477,9 +1482,10 @@ if (avatar && avatar.path) { return { url: getAbsoluteAttachmentPath(avatar.path), color }; } else if (this.isPrivate()) { + const symbol = this.isValid() ? '#' : '!'; return { color, - content: title ? title.trim()[0] : '#', + content: title ? title.trim()[0] : symbol, }; } return { url: 'images/group_default.png', color }; diff --git a/js/views/conversation_search_view.js b/js/views/conversation_search_view.js index 774500752..f1cdc981a 100644 --- a/js/views/conversation_search_view.js +++ b/js/views/conversation_search_view.js @@ -20,8 +20,10 @@ this.listenTo(this.model, 'change', this.render); // auto update }, render_attributes() { + // Show the appropriate message based on model validity + const message = this.model && this.model.isValid() ? i18n('startConversation') : i18n('invalidNumberError'); return { - number: i18n('startConversation'), + number: message, title: this.model.getNumber(), avatar: this.model.getAvatar(), }; @@ -68,14 +70,13 @@ filterContacts() { const query = this.$input.val().trim(); if (query.length) { - if (this.maybeNumber(query)) { - this.new_contact_view.model.set('id', query); - this.new_contact_view.render().$el.show(); - this.new_contact_view.validate(); - this.hideHints(); - } else { - this.new_contact_view.$el.hide(); - } + + // Update the contact model + this.new_contact_view.model.set('id', query); + this.new_contact_view.render().$el.show(); + this.new_contact_view.validate(); + this.hideHints(); + // NOTE: Temporarily allow `then` until we convert the entire file // to `async` / `await`: /* eslint-disable more/no-then */ @@ -108,7 +109,6 @@ async createConversation() { const isValidNumber = this.new_contact_view.model.isValid(); if (!isValidNumber) { - this.new_contact_view.$('.number').text(i18n('invalidNumberError')); this.$input.focus(); return; } @@ -155,9 +155,5 @@ this.hintView = null; } }, - - maybeNumber(number) { - return number.replace(/[\s]*/g, '').match(/^[0-9a-fA-F]+$/); // hex representation - }, }); })();