Merge branch 'development' into merge/signal

pull/27/head
Mikunj 7 years ago
commit 1a4adf2281

@ -697,14 +697,19 @@
}, },
validateNumber() { validateNumber() {
if (!this.id) return 'Invalid ID';
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;
@ -1720,23 +1725,7 @@
if (!this.isPrivate()) { if (!this.isPrivate()) {
return ''; return '';
} }
const number = this.id; return this.id;
try {
const parsedNumber = libphonenumber.parse(number);
const regionCode = libphonenumber.getRegionCodeForNumber(parsedNumber);
if (regionCode === storage.get('regionCode')) {
return libphonenumber.format(
parsedNumber,
libphonenumber.PhoneNumberFormat.NATIONAL
);
}
return libphonenumber.format(
parsedNumber,
libphonenumber.PhoneNumberFormat.INTERNATIONAL
);
} catch (e) {
return number;
}
}, },
isPrivate() { isPrivate() {
@ -1768,9 +1757,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 */
@ -113,7 +114,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;
} }
@ -160,9 +160,5 @@
this.hintView = null; this.hintView = null;
} }
}, },
maybeNumber(number) {
return number.replace(/[\s]*/g, '').match(/^[0-9a-fA-F]+$/); // hex representation
},
}); });
})(); })();

@ -38,7 +38,7 @@
}); });
describe('Conversation', function() { describe('Conversation', function() {
var attributes = { type: 'private', id: '+18085555555' }; var attributes = { type: 'private', id: '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' };
before(async () => { before(async () => {
var convo = new Whisper.ConversationCollection().add(attributes); var convo = new Whisper.ConversationCollection().add(attributes);
await window.Signal.Data.saveConversation(convo.attributes, { await window.Signal.Data.saveConversation(convo.attributes, {
@ -59,7 +59,7 @@
after(clearDatabase); after(clearDatabase);
it('sorts its contacts in an intl-friendly way', function() { it('sorts its contacts in an intl-friendly way', function() {
var convo = new Whisper.Conversation({ id: '+18085555555' }); var convo = new Whisper.Conversation({ id: '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' });
convo.contactCollection.add( convo.contactCollection.add(
new Whisper.Conversation({ new Whisper.Conversation({
name: 'C', name: 'C',
@ -83,7 +83,7 @@
it('contains its own messages', async function() { it('contains its own messages', async function() {
var convo = new Whisper.ConversationCollection().add({ var convo = new Whisper.ConversationCollection().add({
id: '+18085555555', id: '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab',
}); });
await convo.fetchMessages(); await convo.fetchMessages();
assert.notEqual(convo.messageCollection.length, 0); assert.notEqual(convo.messageCollection.length, 0);
@ -91,7 +91,7 @@
it('contains only its own messages', async function() { it('contains only its own messages', async function() {
var convo = new Whisper.ConversationCollection().add({ var convo = new Whisper.ConversationCollection().add({
id: '+18085556666', id: '6eb56f06737d0966239e70d431d4dfd9e57c1e7dddacaf61907fcbc14295e424fd',
}); });
await convo.fetchMessages(); await convo.fetchMessages();
assert.strictEqual(convo.messageCollection.length, 0); assert.strictEqual(convo.messageCollection.length, 0);
@ -109,7 +109,7 @@
it('has a title', function() { it('has a title', function() {
var convos = new Whisper.ConversationCollection(); var convos = new Whisper.ConversationCollection();
var convo = convos.add(attributes); var convo = convos.add(attributes);
assert.equal(convo.getTitle(), '+1 808-555-5555'); assert.equal(convo.getTitle(), '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab');
convo = convos.add({ type: '' }); convo = convos.add({ type: '' });
assert.equal(convo.getTitle(), 'Unknown group'); assert.equal(convo.getTitle(), 'Unknown group');
@ -121,7 +121,7 @@
it('returns the number', function() { it('returns the number', function() {
var convos = new Whisper.ConversationCollection(); var convos = new Whisper.ConversationCollection();
var convo = convos.add(attributes); var convo = convos.add(attributes);
assert.equal(convo.getNumber(), '+1 808-555-5555'); assert.equal(convo.getNumber(), '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab');
convo = convos.add({ type: '' }); convo = convos.add({ type: '' });
assert.equal(convo.getNumber(), ''); assert.equal(convo.getNumber(), '');
@ -134,41 +134,21 @@
assert.property(avatar, 'color'); assert.property(avatar, 'color');
}); });
describe('phone number parsing', function() { describe('when set to private', function() {
after(function() { it('correctly validates hex numbers', function() {
storage.remove('regionCode'); const regularId = new Whisper.Conversation({ type: 'private', id: '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' });
const invalidId = new Whisper.Conversation({ type: 'private', id: 'j71d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' });
assert.ok(regularId.isValid());
assert.notOk(invalidId.isValid());
}); });
function checkAttributes(number) {
var convo = new Whisper.ConversationCollection().add({ it('correctly validates length', function() {
type: 'private', const regularId = new Whisper.Conversation({ type: 'private', id: '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' });
}); const shortId = new Whisper.Conversation({ type: 'private', id: '771d11d' });
convo.set('id', number); const longId = new Whisper.Conversation({ type: 'private', id: '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94abaa' });
convo.validate(convo.attributes); assert.ok(regularId.isValid());
assert.strictEqual(convo.get('id'), '+14155555555', number); assert.notOk(shortId.isValid());
} assert.notOk(longId.isValid());
it('processes the phone number when validating', function() {
['+14155555555'].forEach(checkAttributes);
});
it('defaults to the local regionCode', function() {
storage.put('regionCode', 'US');
['14155555555', '4155555555'].forEach(checkAttributes);
});
it('works with common phone number formats', function() {
storage.put('regionCode', 'US');
[
'415 555 5555',
'415-555-5555',
'(415) 555 5555',
'(415) 555-5555',
'1 415 555 5555',
'1 415-555-5555',
'1 (415) 555 5555',
'1 (415) 555-5555',
'+1 415 555 5555',
'+1 415-555-5555',
'+1 (415) 555 5555',
'+1 (415) 555-5555',
].forEach(checkAttributes);
}); });
}); });
}); });
@ -178,7 +158,7 @@
beforeEach(async function() { beforeEach(async function() {
convo = new Whisper.ConversationCollection().add({ convo = new Whisper.ConversationCollection().add({
id: '+14155555555', id: '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab',
type: 'private', type: 'private',
name: 'John Doe', name: 'John Doe',
}); });
@ -202,21 +182,20 @@
}) })
); );
} }
it('matches by partial phone number', function() { it('matches by partial keys', function() {
return testSearch([ return testSearch([
'1', '1',
'4', '771',
'+1', '1e',
'415', '56d9bfc3d74115c3322',
'4155', '6d9bfc3d74115c33225a632321b509ac17a13fdeac71165d',
'4155555555', '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab'
'14155555555',
'+14155555555',
]); ]);
}); });
it('matches by name', function() { // TODO: Re-enable once we have nickanme functionality
return testSearch(['John', 'Doe', 'john', 'doe', 'John Doe', 'john doe']); // it('matches by name', function() {
}); // return testSearch(['John', 'Doe', 'john', 'doe', 'John Doe', 'john doe']);
// });
it('does not match +', async function() { it('does not match +', async function() {
var collection = new Whisper.ConversationCollection(); var collection = new Whisper.ConversationCollection();
await collection.search('+'); await collection.search('+');

@ -1,28 +1,4 @@
describe('ConversationSearchView', function() { describe('ConversationSearchView', function() {
it('should match partial numbers', function() {
var $el = $('<div><div class="new-contact contact hide"></div></div>');
var view = new Whisper.ConversationSearchView({
el: $el,
input: $('<input>'),
}).render();
var maybe_numbers = [
'+1 415',
'+1415',
'+1415',
'415',
'(415)',
' (415',
'(415) 123 4567',
'+1 (415) 123 4567',
' +1 (415) 123 4567',
'1 (415) 123 4567',
'1 415-123-4567',
'415-123-4567',
];
maybe_numbers.forEach(function(n) {
assert.ok(view.maybeNumber(n), n);
});
});
describe('Searching for left groups', function() { describe('Searching for left groups', function() {
let convo; let convo;

Loading…
Cancel
Save