2015-09-07 23:53:43 +02:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-11-13 23:35:37 +01:00
|
|
|
*/
|
2014-05-17 06:48:46 +02:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
2014-11-13 06:46:57 +01:00
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
2015-02-08 03:18:53 +01:00
|
|
|
// TODO: Factor out private and group subclasses of Conversation
|
|
|
|
|
2015-09-10 09:46:50 +02:00
|
|
|
var COLORS = [
|
2015-12-26 07:17:30 +01:00
|
|
|
'#EF5350', // red
|
|
|
|
'#EC407A', // pink
|
|
|
|
'#AB47BC', // purple
|
|
|
|
'#7E57C2', // deep purple
|
|
|
|
'#5C6BC0', // indigo
|
|
|
|
'#2196F3', // blue
|
|
|
|
'#03A9F4', // light blue
|
|
|
|
'#00BCD4', // cyan
|
|
|
|
'#009688', // teal
|
|
|
|
'#4CAF50', // green
|
|
|
|
'#7CB342', // light green
|
|
|
|
'#FF9800', // orange
|
|
|
|
'#FF5722', // deep orange
|
|
|
|
'#FFB300', // amber
|
|
|
|
'#607D8B', // blue grey
|
2015-09-10 09:46:50 +02:00
|
|
|
];
|
|
|
|
|
2015-02-08 03:18:53 +01:00
|
|
|
Whisper.Conversation = Backbone.Model.extend({
|
2014-11-13 23:35:37 +01:00
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'conversations',
|
2014-05-17 06:48:46 +02:00
|
|
|
defaults: function() {
|
2016-02-24 21:03:25 +01:00
|
|
|
return { unreadCount : 0 };
|
2014-05-17 06:48:46 +02:00
|
|
|
},
|
|
|
|
|
2014-11-13 23:35:37 +01:00
|
|
|
initialize: function() {
|
2015-09-08 02:22:59 +02:00
|
|
|
this.contactCollection = new Backbone.Collection();
|
2015-03-12 01:49:01 +01:00
|
|
|
this.messageCollection = new Whisper.MessageCollection([], {
|
|
|
|
conversation: this
|
|
|
|
});
|
2015-03-17 23:06:21 +01:00
|
|
|
|
|
|
|
this.on('change:avatar', this.updateAvatarUrl);
|
2015-03-18 01:10:18 +01:00
|
|
|
this.on('destroy', this.revokeAvatarUrl);
|
2016-02-22 06:37:47 +01:00
|
|
|
this.on('read', this.countUnread);
|
|
|
|
},
|
|
|
|
|
|
|
|
countUnread: function() {
|
|
|
|
return this.getUnread().then(function(unreadMessages) {
|
|
|
|
this.save({unreadCount: unreadMessages.length});
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
getUnread: function() {
|
|
|
|
var conversationId = this.id;
|
|
|
|
var unreadMessages = new Whisper.MessageCollection();
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
return unreadMessages.fetch({
|
|
|
|
index: {
|
|
|
|
// 'unread' index
|
2016-02-26 21:25:32 +01:00
|
|
|
name : 'unread',
|
|
|
|
lower : [conversationId],
|
|
|
|
upper : [conversationId, Number.MAX_VALUE],
|
2016-02-22 06:37:47 +01:00
|
|
|
}
|
|
|
|
}).always(function() {
|
|
|
|
resolve(unreadMessages);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-13 23:35:37 +01:00
|
|
|
},
|
|
|
|
|
2014-05-17 06:48:46 +02:00
|
|
|
validate: function(attributes, options) {
|
2015-06-03 19:29:20 +02:00
|
|
|
var required = ['id', 'type'];
|
2015-01-28 13:19:58 +01:00
|
|
|
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
|
|
|
|
if (missing.length) { return "Conversation must have " + missing; }
|
|
|
|
|
2015-02-08 01:24:56 +01:00
|
|
|
if (attributes.type !== 'private' && attributes.type !== 'group') {
|
|
|
|
return "Invalid conversation type: " + attributes.type;
|
|
|
|
}
|
2015-10-16 22:00:38 +02:00
|
|
|
|
2015-12-05 02:38:41 +01:00
|
|
|
var error = this.validateNumber();
|
|
|
|
if (error) { return error; }
|
|
|
|
|
|
|
|
this.updateTokens();
|
2015-12-02 00:29:39 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
validateNumber: function() {
|
2015-12-05 02:38:41 +01:00
|
|
|
if (this.isPrivate()) {
|
|
|
|
var regionCode = storage.get('regionCode');
|
|
|
|
var number = libphonenumber.util.parseNumber(this.id, regionCode);
|
|
|
|
if (number.isValidNumber) {
|
|
|
|
this.set({ id: number.e164 });
|
2015-12-02 00:29:39 +01:00
|
|
|
} else {
|
2015-12-05 02:38:41 +01:00
|
|
|
return number.error || "Invalid phone number";
|
2015-12-02 00:29:39 +01:00
|
|
|
}
|
2015-10-16 22:00:38 +02:00
|
|
|
}
|
2015-10-15 21:10:03 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
updateTokens: function() {
|
|
|
|
var tokens = [];
|
|
|
|
var name = this.get('name');
|
|
|
|
if (typeof name === 'string') {
|
2015-12-05 02:38:41 +01:00
|
|
|
tokens.push(name.toLowerCase());
|
|
|
|
tokens = tokens.concat(name.trim().toLowerCase().split(/[\s\-_\(\)\+]+/));
|
2015-10-15 21:10:03 +02:00
|
|
|
}
|
2015-09-10 02:47:45 +02:00
|
|
|
if (this.isPrivate()) {
|
2015-12-05 02:38:41 +01:00
|
|
|
var regionCode = storage.get('regionCode');
|
|
|
|
var number = libphonenumber.util.parseNumber(this.id, regionCode);
|
|
|
|
tokens.push(
|
|
|
|
number.nationalNumber,
|
|
|
|
number.countryCode + number.nationalNumber
|
|
|
|
);
|
2015-01-28 13:19:58 +01:00
|
|
|
}
|
2015-10-15 21:10:03 +02:00
|
|
|
this.set({tokens: tokens});
|
2014-05-17 06:48:46 +02:00
|
|
|
},
|
|
|
|
|
2014-12-20 09:36:44 +01:00
|
|
|
sendMessage: function(body, attachments) {
|
2014-12-12 04:41:40 +01:00
|
|
|
var now = Date.now();
|
2014-12-20 09:36:44 +01:00
|
|
|
var message = this.messageCollection.add({
|
|
|
|
body : body,
|
2014-12-12 04:41:40 +01:00
|
|
|
conversationId : this.id,
|
|
|
|
type : 'outgoing',
|
|
|
|
attachments : attachments,
|
|
|
|
sent_at : now,
|
2015-07-08 04:21:10 +02:00
|
|
|
received_at : now
|
2014-12-20 09:36:44 +01:00
|
|
|
});
|
2015-11-20 01:57:48 +01:00
|
|
|
if (this.isPrivate()) {
|
|
|
|
message.set({destination: this.id});
|
|
|
|
}
|
2014-12-20 09:36:44 +01:00
|
|
|
message.save();
|
2014-10-26 08:29:01 +01:00
|
|
|
|
2014-12-12 04:41:40 +01:00
|
|
|
this.save({
|
|
|
|
unreadCount : 0,
|
2014-12-23 08:05:51 +01:00
|
|
|
active_at : now,
|
|
|
|
timestamp : now,
|
2015-11-03 01:46:52 +01:00
|
|
|
lastMessage : message.getNotificationText()
|
2014-12-12 04:41:40 +01:00
|
|
|
});
|
2014-08-11 08:34:29 +02:00
|
|
|
|
2014-12-20 09:36:44 +01:00
|
|
|
var sendFunc;
|
2014-11-02 22:48:35 +01:00
|
|
|
if (this.get('type') == 'private') {
|
2014-12-20 09:36:44 +01:00
|
|
|
sendFunc = textsecure.messaging.sendMessageToNumber;
|
2014-06-03 18:39:29 +02:00
|
|
|
}
|
2014-11-02 22:48:35 +01:00
|
|
|
else {
|
2014-12-20 09:36:44 +01:00
|
|
|
sendFunc = textsecure.messaging.sendMessageToGroup;
|
2014-06-03 18:39:29 +02:00
|
|
|
}
|
2015-09-28 22:33:26 +02:00
|
|
|
message.send(sendFunc(this.get('id'), body, attachments, now));
|
2014-05-17 06:48:46 +02:00
|
|
|
},
|
|
|
|
|
2016-02-19 02:27:57 +01:00
|
|
|
isSearchable: function() {
|
|
|
|
return !this.get('left') || !!this.get('lastMessage');
|
|
|
|
},
|
|
|
|
|
2015-02-13 05:36:44 +01:00
|
|
|
endSession: function() {
|
2015-09-10 02:47:45 +02:00
|
|
|
if (this.isPrivate()) {
|
2015-03-24 03:08:05 +01:00
|
|
|
var now = Date.now();
|
2015-09-28 22:33:26 +02:00
|
|
|
var message = this.messageCollection.create({
|
2015-03-24 03:08:05 +01:00
|
|
|
conversationId : this.id,
|
|
|
|
type : 'outgoing',
|
|
|
|
sent_at : now,
|
|
|
|
received_at : now,
|
2016-02-23 21:27:49 +01:00
|
|
|
destination : this.id,
|
2015-06-01 23:08:21 +02:00
|
|
|
flags : textsecure.protobuf.DataMessage.Flags.END_SESSION
|
2015-07-16 20:05:47 +02:00
|
|
|
});
|
2015-12-11 21:05:50 +01:00
|
|
|
message.send(textsecure.messaging.closeSession(this.id, now));
|
2015-02-13 05:36:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2015-09-22 04:12:06 +02:00
|
|
|
updateGroup: function(group_update) {
|
|
|
|
if (this.isPrivate()) {
|
|
|
|
throw new Error("Called update group on private conversation");
|
|
|
|
}
|
|
|
|
if (group_update === undefined) {
|
|
|
|
group_update = this.pick(['name', 'avatar', 'members']);
|
|
|
|
}
|
|
|
|
var now = Date.now();
|
2015-09-28 22:33:26 +02:00
|
|
|
var message = this.messageCollection.create({
|
2015-09-22 04:12:06 +02:00
|
|
|
conversationId : this.id,
|
|
|
|
type : 'outgoing',
|
|
|
|
sent_at : now,
|
|
|
|
received_at : now,
|
|
|
|
group_update : group_update
|
|
|
|
});
|
2015-09-28 22:33:26 +02:00
|
|
|
message.send(textsecure.messaging.updateGroup(
|
2015-09-22 04:12:06 +02:00
|
|
|
this.id,
|
|
|
|
this.get('name'),
|
|
|
|
this.get('avatar'),
|
|
|
|
this.get('members')
|
2015-09-28 22:33:26 +02:00
|
|
|
));
|
2015-09-22 04:12:06 +02:00
|
|
|
},
|
|
|
|
|
2015-02-13 05:36:44 +01:00
|
|
|
leaveGroup: function() {
|
2015-03-24 03:08:05 +01:00
|
|
|
var now = Date.now();
|
2015-02-13 05:36:44 +01:00
|
|
|
if (this.get('type') === 'group') {
|
2016-02-11 23:12:09 +01:00
|
|
|
this.save({left: true});
|
2015-09-28 22:33:26 +02:00
|
|
|
var message = this.messageCollection.create({
|
2015-03-24 03:08:05 +01:00
|
|
|
group_update: { left: 'You' },
|
|
|
|
conversationId : this.id,
|
|
|
|
type : 'outgoing',
|
|
|
|
sent_at : now,
|
|
|
|
received_at : now
|
2015-09-18 22:39:22 +02:00
|
|
|
});
|
2015-09-28 22:33:26 +02:00
|
|
|
message.send(textsecure.messaging.leaveGroup(this.id));
|
2015-02-13 05:36:44 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-11 20:06:19 +01:00
|
|
|
markRead: function() {
|
|
|
|
if (this.get('unreadCount') > 0) {
|
2016-02-22 06:37:47 +01:00
|
|
|
this.save({ unreadCount: 0 });
|
2015-11-25 23:03:53 +01:00
|
|
|
var conversationId = this.id;
|
2016-02-22 06:37:47 +01:00
|
|
|
Whisper.Notifications.remove(Whisper.Notifications.where({
|
|
|
|
conversationId: conversationId
|
|
|
|
}));
|
|
|
|
|
2016-02-26 21:35:02 +01:00
|
|
|
this.getUnread().then(function(unreadMessages) {
|
|
|
|
var read = unreadMessages.map(function(m) {
|
|
|
|
m.markRead();
|
|
|
|
return {
|
|
|
|
sender : m.get('source'),
|
|
|
|
timestamp : m.get('sent_at')
|
|
|
|
};
|
|
|
|
});
|
|
|
|
if (read.length > 0) {
|
|
|
|
textsecure.messaging.sendReadReceipts(read);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 20:06:19 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-08 01:03:12 +02:00
|
|
|
fetchMessages: function() {
|
2015-09-08 02:22:20 +02:00
|
|
|
if (!this.id) { return false; }
|
2015-07-08 01:03:12 +02:00
|
|
|
return this.messageCollection.fetchConversation(this.id);
|
2014-12-12 04:41:40 +01:00
|
|
|
},
|
|
|
|
|
2015-02-25 01:02:33 +01:00
|
|
|
fetchContacts: function(options) {
|
2015-09-14 05:59:51 +02:00
|
|
|
return new Promise(function(resolve) {
|
|
|
|
if (this.isPrivate()) {
|
|
|
|
this.contactCollection.reset([this]);
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
var promises = [];
|
|
|
|
var members = this.get('members') || [];
|
|
|
|
this.contactCollection.reset(
|
|
|
|
members.map(function(number) {
|
|
|
|
var c = ConversationController.create({
|
|
|
|
id : number,
|
|
|
|
type : 'private'
|
|
|
|
});
|
|
|
|
promises.push(new Promise(function(resolve) {
|
|
|
|
c.fetch().always(resolve);
|
|
|
|
}));
|
|
|
|
return c;
|
|
|
|
}.bind(this))
|
|
|
|
);
|
|
|
|
resolve(Promise.all(promises));
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2015-02-25 01:02:33 +01:00
|
|
|
},
|
|
|
|
|
2014-12-03 00:47:28 +01:00
|
|
|
destroyMessages: function() {
|
2016-01-27 21:42:27 +01:00
|
|
|
this.messageCollection.fetch({
|
|
|
|
index: {
|
|
|
|
// 'conversation' index on [conversationId, received_at]
|
|
|
|
name : 'conversation',
|
|
|
|
lower : [this.id],
|
|
|
|
upper : [this.id, Number.MAX_VALUE],
|
|
|
|
}
|
|
|
|
}).then(function() {
|
|
|
|
var models = this.messageCollection.models;
|
|
|
|
this.messageCollection.reset([]);
|
|
|
|
_.each(models, function(message) { message.destroy(); });
|
|
|
|
this.save({active_at: null, lastMessage: ''}); // archive
|
|
|
|
}.bind(this));
|
2015-01-24 21:36:04 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getTitle: function() {
|
2015-03-19 21:49:09 +01:00
|
|
|
if (this.isPrivate()) {
|
2015-12-05 00:09:53 +01:00
|
|
|
return this.get('name') || this.getNumber();
|
2015-03-19 21:49:09 +01:00
|
|
|
} else {
|
|
|
|
return this.get('name') || 'Unknown group';
|
|
|
|
}
|
2015-02-04 20:23:00 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getNumber: function() {
|
2015-12-05 00:09:53 +01:00
|
|
|
if (!this.isPrivate()) {
|
2015-02-04 20:23:00 +01:00
|
|
|
return '';
|
|
|
|
}
|
2015-12-05 00:09:53 +01:00
|
|
|
var number = this.id;
|
|
|
|
try {
|
|
|
|
var parsedNumber = libphonenumber.parse(number);
|
|
|
|
var regionCode = libphonenumber.getRegionCodeForNumber(parsedNumber);
|
|
|
|
if (regionCode === storage.get('regionCode')) {
|
|
|
|
return libphonenumber.format(parsedNumber, libphonenumber.PhoneNumberFormat.NATIONAL);
|
|
|
|
} else {
|
|
|
|
return libphonenumber.format(parsedNumber, libphonenumber.PhoneNumberFormat.INTERNATIONAL);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return number;
|
|
|
|
}
|
2015-02-25 01:02:33 +01:00
|
|
|
},
|
2015-02-24 01:23:22 +01:00
|
|
|
|
2015-02-25 01:02:33 +01:00
|
|
|
isPrivate: function() {
|
|
|
|
return this.get('type') === 'private';
|
2015-03-17 23:06:21 +01:00
|
|
|
},
|
|
|
|
|
2015-03-18 01:10:18 +01:00
|
|
|
revokeAvatarUrl: function() {
|
2015-03-17 23:06:21 +01:00
|
|
|
if (this.avatarUrl) {
|
|
|
|
URL.revokeObjectURL(this.avatarUrl);
|
|
|
|
this.avatarUrl = null;
|
|
|
|
}
|
2015-03-18 01:10:18 +01:00
|
|
|
},
|
|
|
|
|
2015-06-09 21:03:28 +02:00
|
|
|
updateAvatarUrl: function(silent) {
|
2015-03-18 01:10:18 +01:00
|
|
|
this.revokeAvatarUrl();
|
2015-03-17 23:06:21 +01:00
|
|
|
var avatar = this.get('avatar');
|
|
|
|
if (avatar) {
|
|
|
|
this.avatarUrl = URL.createObjectURL(
|
|
|
|
new Blob([avatar.data], {type: avatar.contentType})
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.avatarUrl = null;
|
|
|
|
}
|
2015-06-09 21:03:28 +02:00
|
|
|
if (!silent) {
|
|
|
|
this.trigger('change');
|
|
|
|
}
|
2015-03-17 23:06:21 +01:00
|
|
|
},
|
|
|
|
|
2015-06-19 02:05:00 +02:00
|
|
|
getAvatar: function() {
|
2015-03-17 23:06:21 +01:00
|
|
|
if (this.avatarUrl === undefined) {
|
2015-06-09 21:03:28 +02:00
|
|
|
this.updateAvatarUrl(true);
|
2015-03-17 23:06:21 +01:00
|
|
|
}
|
2015-06-19 02:05:00 +02:00
|
|
|
if (this.avatarUrl) {
|
|
|
|
return { url: this.avatarUrl };
|
|
|
|
} else if (this.isPrivate()) {
|
2015-06-25 22:32:05 +02:00
|
|
|
var title = this.get('name');
|
|
|
|
if (!title) {
|
2015-09-10 09:46:50 +02:00
|
|
|
return { content: '#', color: '#999999' };
|
2015-06-25 22:32:05 +02:00
|
|
|
}
|
2015-06-19 02:05:00 +02:00
|
|
|
var initials = title.trim()[0];
|
|
|
|
return {
|
2015-09-10 09:46:50 +02:00
|
|
|
color: COLORS[Math.abs(this.hashCode()) % 15],
|
2015-06-19 02:05:00 +02:00
|
|
|
content: initials
|
|
|
|
};
|
|
|
|
} else {
|
2015-06-26 20:23:37 +02:00
|
|
|
return { url: '/images/group_default.png', color: 'gray' };
|
2015-06-19 02:05:00 +02:00
|
|
|
}
|
2015-02-24 01:23:22 +01:00
|
|
|
},
|
|
|
|
|
2015-09-23 00:52:33 +02:00
|
|
|
getNotificationIcon: function() {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
var avatar = this.getAvatar();
|
|
|
|
if (avatar.url) {
|
|
|
|
resolve(avatar.url);
|
|
|
|
} else {
|
|
|
|
resolve(new Whisper.IdenticonSVGView(avatar).getDataUrl());
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2015-07-31 20:14:43 +02:00
|
|
|
resolveConflicts: function(conflict) {
|
|
|
|
var number = conflict.number;
|
|
|
|
var identityKey = conflict.identityKey;
|
2015-02-18 03:03:05 +01:00
|
|
|
if (this.isPrivate()) {
|
|
|
|
number = this.id;
|
2015-03-24 02:36:38 +01:00
|
|
|
} else if (!_.include(this.get('members'), number)) {
|
2015-02-18 03:03:05 +01:00
|
|
|
throw 'Tried to resolve conflicts for a unknown group member';
|
2015-02-24 01:23:22 +01:00
|
|
|
}
|
|
|
|
|
2015-02-18 03:03:05 +01:00
|
|
|
if (!this.messageCollection.hasKeyConflicts()) {
|
|
|
|
throw 'No conflicts to resolve';
|
2015-02-24 01:23:22 +01:00
|
|
|
}
|
|
|
|
|
2015-04-21 22:33:29 +02:00
|
|
|
return textsecure.storage.axolotl.removeIdentityKey(number).then(function() {
|
2015-07-31 20:14:43 +02:00
|
|
|
return textsecure.storage.axolotl.putIdentityKey(number, identityKey).then(function() {
|
2015-11-06 03:40:10 +01:00
|
|
|
var promise = Promise.resolve();
|
2015-07-31 20:14:43 +02:00
|
|
|
this.messageCollection.each(function(message) {
|
|
|
|
if (message.hasKeyConflict(number)) {
|
2015-11-06 03:40:10 +01:00
|
|
|
var resolveConflict = function() {
|
|
|
|
return message.resolveConflict(number);
|
|
|
|
};
|
|
|
|
promise = promise.then(resolveConflict, resolveConflict);
|
2015-07-31 20:14:43 +02:00
|
|
|
}
|
|
|
|
});
|
2015-11-06 03:40:10 +01:00
|
|
|
return promise;
|
2015-07-31 20:14:43 +02:00
|
|
|
}.bind(this));
|
2015-04-09 23:58:26 +02:00
|
|
|
}.bind(this));
|
2015-06-19 02:05:00 +02:00
|
|
|
},
|
2015-11-07 23:11:13 +01:00
|
|
|
notify: function(message) {
|
|
|
|
if (!message.isIncoming()) {
|
|
|
|
this.markRead();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (window.isOpen() && window.isFocused()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
window.drawAttention();
|
|
|
|
var sender = ConversationController.create({
|
|
|
|
id: message.get('source'), type: 'private'
|
|
|
|
});
|
|
|
|
var conversationId = this.id;
|
|
|
|
sender.fetch().then(function() {
|
|
|
|
sender.getNotificationIcon().then(function(iconUrl) {
|
|
|
|
Whisper.Notifications.add({
|
|
|
|
title : sender.getTitle(),
|
|
|
|
message : message.getNotificationText(),
|
|
|
|
iconUrl : iconUrl,
|
|
|
|
imageUrl : message.getImageUrl(),
|
|
|
|
conversationId : conversationId
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2015-06-19 02:05:00 +02:00
|
|
|
hashCode: function() {
|
|
|
|
if (this.hash === undefined) {
|
|
|
|
var string = this.getTitle() || '';
|
|
|
|
if (string.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
var hash = 0;
|
|
|
|
for (var i = 0; i < string.length; i++) {
|
|
|
|
hash = ((hash<<5)-hash) + string.charCodeAt(i);
|
|
|
|
hash = hash & hash; // Convert to 32bit integer
|
|
|
|
}
|
|
|
|
|
|
|
|
this.hash = hash;
|
|
|
|
}
|
|
|
|
return this.hash;
|
2014-11-17 00:30:40 +01:00
|
|
|
}
|
2014-05-17 06:48:46 +02:00
|
|
|
});
|
|
|
|
|
2014-11-13 23:35:37 +01:00
|
|
|
Whisper.ConversationCollection = Backbone.Collection.extend({
|
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'conversations',
|
2015-02-08 03:18:53 +01:00
|
|
|
model: Whisper.Conversation,
|
2014-10-18 16:08:57 +02:00
|
|
|
|
|
|
|
comparator: function(m) {
|
|
|
|
return -m.get('timestamp');
|
|
|
|
},
|
|
|
|
|
2014-11-13 23:35:37 +01:00
|
|
|
destroyAll: function () {
|
|
|
|
return Promise.all(this.models.map(function(m) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
m.destroy().then(resolve).fail(reject);
|
|
|
|
});
|
|
|
|
}));
|
2014-12-20 02:15:57 +01:00
|
|
|
},
|
|
|
|
|
2015-10-15 21:10:03 +02:00
|
|
|
search: function(query) {
|
|
|
|
query = query.trim().toLowerCase();
|
|
|
|
if (query.length > 0) {
|
2015-12-09 20:43:26 +01:00
|
|
|
query = query.replace(/[-.\(\)]*/g,'').replace(/^\+(\d*)$/, '$1');
|
2015-10-15 21:10:03 +02:00
|
|
|
var lastCharCode = query.charCodeAt(query.length - 1);
|
|
|
|
var nextChar = String.fromCharCode(lastCharCode + 1);
|
|
|
|
var upper = query.slice(0, -1) + nextChar;
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
this.fetch({
|
|
|
|
index: {
|
|
|
|
name: 'search', // 'search' index on tokens array
|
|
|
|
lower: query,
|
2015-12-31 23:05:41 +01:00
|
|
|
upper: upper,
|
|
|
|
excludeUpper: true
|
2015-10-15 21:10:03 +02:00
|
|
|
}
|
|
|
|
}).always(resolve);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-28 01:11:42 +01:00
|
|
|
fetchAlphabetical: function() {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
this.fetch({
|
|
|
|
index: {
|
|
|
|
name: 'search', // 'search' index on tokens array
|
|
|
|
},
|
|
|
|
limit: 100
|
|
|
|
}).always(resolve);
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2014-12-20 02:15:57 +01:00
|
|
|
fetchGroups: function(number) {
|
|
|
|
return this.fetch({
|
|
|
|
index: {
|
|
|
|
name: 'group',
|
|
|
|
only: number
|
|
|
|
}
|
|
|
|
});
|
2015-05-26 22:28:43 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
fetchActive: function() {
|
|
|
|
// Ensures all active conversations are included in this collection,
|
|
|
|
// and updates their attributes, but removes nothing.
|
|
|
|
return this.fetch({
|
|
|
|
index: {
|
|
|
|
name: 'inbox', // 'inbox' index on active_at
|
|
|
|
order: 'desc' // ORDER timestamp DESC
|
|
|
|
// TODO pagination/infinite scroll
|
|
|
|
// limit: 10, offset: page*10,
|
|
|
|
},
|
|
|
|
remove: false
|
|
|
|
});
|
2014-11-13 01:48:28 +01:00
|
|
|
}
|
2014-11-13 23:35:37 +01:00
|
|
|
});
|
2014-05-17 06:48:46 +02:00
|
|
|
})();
|