Add search field to inbox

Using the search field produces a filtered view of all contacts and
groups containing the input. To make this fast and scalable, add an
index on a 'tokens' array containing words from the conversation name
and different forms of phone number.

Closes #365

// FREEBIE
This commit is contained in:
lilia 2015-10-15 12:10:03 -07:00
parent 7414828bb3
commit f70c22f898
8 changed files with 221 additions and 9 deletions

View file

@ -42,6 +42,7 @@
conversation: this
});
this.on('change:id change:name', this.updateTokens);
this.on('change:avatar', this.updateAvatarUrl);
this.on('destroy', this.revokeAvatarUrl);
},
@ -54,22 +55,33 @@
if (attributes.type !== 'private' && attributes.type !== 'group') {
return "Invalid conversation type: " + attributes.type;
}
},
updateTokens: function() {
var tokens = [];
var name = this.get('name');
if (typeof name === 'string') {
tokens = name.trim().toLowerCase().split(/[\s\-_\(\)\+]+/);
}
// hack
if (this.isPrivate()) {
try {
this.id = libphonenumber.util.verifyNumber(this.id);
var number = libphonenumber.util.splitCountryCode(this.id);
var international_number = '' + number.country_code + number.national_number;
var national_number = '' + number.national_number;
this.set({
e164_number: this.id,
national_number: '' + number.national_number,
international_number: '' + number.country_code + number.national_number
national_number: national_number,
international_number: international_number
});
tokens = tokens.concat(national_number, international_number);
} catch(ex) {
return ex;
}
}
this.set({tokens: tokens});
},
sendMessage: function(body, attachments) {
@ -332,6 +344,25 @@
}));
},
search: function(query) {
query = query.trim().toLowerCase();
if (query.length > 0) {
var lastCharCode = query.charCodeAt(query.length - 1);
var nextChar = String.fromCharCode(lastCharCode + 1);
var upper = query.slice(0, -1) + nextChar;
console.log('searching', query, ' -> ', upper);
return new Promise(function(resolve) {
this.fetch({
index: {
name: 'search', // 'search' index on tokens array
lower: query,
upper: upper
}
}).always(resolve);
}.bind(this));
}
},
fetchGroups: function(number) {
return this.fetch({
index: {