Show a message if user types in an invalid public key in search.

pull/26/head
Mikunj 7 years ago
parent 1ff1f50e41
commit 9d342e8951

@ -672,14 +672,19 @@
}, },
validateNumber() { validateNumber() {
if (!this.id) return null;
if (this.isPrivate()) { 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 // 33 bytes in hex
this.set({ id: this.id }); this.set({ id: this.id });
return null; return 'Invalid ID Length';
} }
return 'Invalid ID';
} }
return null; return null;
@ -1477,9 +1482,10 @@
if (avatar && avatar.path) { if (avatar && avatar.path) {
return { url: getAbsoluteAttachmentPath(avatar.path), color }; return { url: getAbsoluteAttachmentPath(avatar.path), color };
} else if (this.isPrivate()) { } else if (this.isPrivate()) {
const symbol = this.isValid() ? '#' : '!';
return { return {
color, color,
content: title ? title.trim()[0] : '#', content: title ? title.trim()[0] : symbol,
}; };
} }
return { url: 'images/group_default.png', color }; return { url: 'images/group_default.png', color };

@ -20,8 +20,10 @@
this.listenTo(this.model, 'change', this.render); // auto update this.listenTo(this.model, 'change', this.render); // auto update
}, },
render_attributes() { render_attributes() {
// Show the appropriate message based on model validity
const message = this.model && this.model.isValid() ? i18n('startConversation') : i18n('invalidNumberError');
return { return {
number: i18n('startConversation'), number: message,
title: this.model.getNumber(), title: this.model.getNumber(),
avatar: this.model.getAvatar(), avatar: this.model.getAvatar(),
}; };
@ -68,14 +70,13 @@
filterContacts() { filterContacts() {
const query = this.$input.val().trim(); const query = this.$input.val().trim();
if (query.length) { if (query.length) {
if (this.maybeNumber(query)) {
this.new_contact_view.model.set('id', query); // Update the contact model
this.new_contact_view.render().$el.show(); this.new_contact_view.model.set('id', query);
this.new_contact_view.validate(); this.new_contact_view.render().$el.show();
this.hideHints(); this.new_contact_view.validate();
} else { this.hideHints();
this.new_contact_view.$el.hide();
}
// NOTE: Temporarily allow `then` until we convert the entire file // NOTE: Temporarily allow `then` until we convert the entire file
// to `async` / `await`: // to `async` / `await`:
/* eslint-disable more/no-then */ /* eslint-disable more/no-then */
@ -108,7 +109,6 @@
async createConversation() { async createConversation() {
const isValidNumber = this.new_contact_view.model.isValid(); const isValidNumber = this.new_contact_view.model.isValid();
if (!isValidNumber) { if (!isValidNumber) {
this.new_contact_view.$('.number').text(i18n('invalidNumberError'));
this.$input.focus(); this.$input.focus();
return; return;
} }
@ -155,9 +155,5 @@
this.hintView = null; this.hintView = null;
} }
}, },
maybeNumber(number) {
return number.replace(/[\s]*/g, '').match(/^[0-9a-fA-F]+$/); // hex representation
},
}); });
})(); })();

Loading…
Cancel
Save