2014-11-13 23:35:37 +01:00
|
|
|
/* vim: ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
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() {
|
2014-12-12 04:41:40 +01:00
|
|
|
var timestamp = new Date().getTime();
|
2014-05-17 06:48:46 +02:00
|
|
|
return {
|
2014-12-12 04:41:40 +01:00
|
|
|
unreadCount : 0,
|
|
|
|
timestamp : timestamp,
|
|
|
|
active_at : timestamp
|
2014-05-17 06:48:46 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2014-11-13 23:35:37 +01:00
|
|
|
initialize: function() {
|
|
|
|
this.messageCollection = new Whisper.MessageCollection();
|
|
|
|
},
|
|
|
|
|
2014-05-17 06:48:46 +02:00
|
|
|
validate: function(attributes, options) {
|
2015-02-08 19:43:27 +01:00
|
|
|
var required = ['id', 'type', 'timestamp'];
|
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-01-29 06:28:49 +01:00
|
|
|
// hack
|
2015-01-28 13:19:58 +01:00
|
|
|
if (this.get('type') === 'private') {
|
2015-02-08 03:19:04 +01:00
|
|
|
try {
|
|
|
|
this.id = libphonenumber.util.verifyNumber(this.id);
|
|
|
|
var number = libphonenumber.util.splitCountryCode(this.id);
|
2015-01-28 13:19:58 +01:00
|
|
|
|
2015-02-08 03:19:04 +01:00
|
|
|
this.set({
|
|
|
|
e164_number: this.id,
|
|
|
|
national_number: '' + number.national_number,
|
|
|
|
international_number: '' + number.country_code + number.national_number
|
|
|
|
});
|
|
|
|
} catch(ex) {
|
|
|
|
return ex;
|
|
|
|
}
|
2015-01-28 13:19:58 +01:00
|
|
|
}
|
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,
|
|
|
|
received_at : now
|
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,
|
|
|
|
lastMessage : body
|
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
|
|
|
}
|
2014-12-23 00:09:49 +01:00
|
|
|
sendFunc(this.get('id'), body, attachments, now).catch(function(errors) {
|
|
|
|
var keyErrors = [];
|
|
|
|
_.each(errors, function(e) {
|
|
|
|
if (e.error.name === 'OutgoingIdentityKeyError') {
|
|
|
|
e.error.args.push(message.id);
|
|
|
|
keyErrors.push(e.error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (keyErrors.length) {
|
|
|
|
message.save({ errors : keyErrors }).then(function() {
|
2014-12-20 09:36:44 +01:00
|
|
|
extension.trigger('message', message); // notify frontend listeners
|
|
|
|
});
|
|
|
|
} else {
|
2014-12-23 00:09:49 +01:00
|
|
|
throw errors;
|
2014-12-20 09:36:44 +01:00
|
|
|
}
|
|
|
|
});
|
2014-05-17 06:48:46 +02:00
|
|
|
},
|
|
|
|
|
2015-02-13 05:36:44 +01:00
|
|
|
endSession: function() {
|
|
|
|
if (this.get('type') === 'private') {
|
|
|
|
textsecure.messaging.closeSession(this.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
leaveGroup: function() {
|
|
|
|
if (this.get('type') === 'group') {
|
|
|
|
textsecure.messaging.leaveGroup(this.id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-11-13 01:48:28 +01:00
|
|
|
receiveMessage: function(decrypted) {
|
2014-11-21 00:43:51 +01:00
|
|
|
var conversation = this;
|
2014-11-13 01:48:28 +01:00
|
|
|
var timestamp = decrypted.pushMessage.timestamp.toNumber();
|
2014-11-17 00:30:40 +01:00
|
|
|
var m = this.messageCollection.add({
|
2014-12-19 04:42:37 +01:00
|
|
|
body : decrypted.message.body,
|
|
|
|
timestamp : timestamp,
|
|
|
|
conversationId : this.id,
|
|
|
|
attachments : decrypted.message.attachments,
|
|
|
|
type : 'incoming',
|
|
|
|
sender : decrypted.pushMessage.source
|
2014-11-13 01:48:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (timestamp > this.get('timestamp')) {
|
|
|
|
this.set('timestamp', timestamp);
|
|
|
|
}
|
|
|
|
this.save({unreadCount: this.get('unreadCount') + 1, active: true});
|
2014-11-16 22:19:51 +01:00
|
|
|
|
2014-11-17 00:30:40 +01:00
|
|
|
return new Promise(function (resolve) { m.save().then(resolve(m)) });
|
2014-11-13 01:48:28 +01:00
|
|
|
},
|
|
|
|
|
2014-11-17 00:30:40 +01:00
|
|
|
fetchMessages: function(options) {
|
2014-12-19 21:55:29 +01:00
|
|
|
return this.messageCollection.fetchConversation(this.id, options);
|
2014-12-12 04:41:40 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
archive: function() {
|
|
|
|
this.unset('active_at');
|
2014-12-03 00:47:28 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
destroyMessages: function() {
|
|
|
|
var models = this.messageCollection.models;
|
|
|
|
this.messageCollection.reset([]);
|
|
|
|
_.each(models, function(message) { message.destroy(); });
|
2014-12-12 04:41:40 +01:00
|
|
|
this.archive();
|
|
|
|
return this.save();
|
2015-01-24 21:36:04 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getTitle: function() {
|
|
|
|
return this.get('name') || this.get('members') || this.id;
|
2015-02-04 20:23:00 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getNumber: function() {
|
|
|
|
if (this.get('type') === 'private') {
|
|
|
|
return this.id;
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
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');
|
|
|
|
},
|
|
|
|
|
2015-01-11 12:27:22 +01:00
|
|
|
createGroup: function(recipients, name, avatar) {
|
2014-08-11 08:34:29 +02:00
|
|
|
var attributes = {};
|
|
|
|
attributes = {
|
|
|
|
name : name,
|
2014-12-12 04:41:40 +01:00
|
|
|
members : recipients,
|
2014-08-11 08:34:29 +02:00
|
|
|
type : 'group',
|
2015-01-11 12:27:22 +01:00
|
|
|
avatar : avatar
|
2014-08-11 08:34:29 +02:00
|
|
|
};
|
2014-11-13 23:35:37 +01:00
|
|
|
var conversation = this.add(attributes, {merge: true});
|
2015-01-11 12:27:22 +01:00
|
|
|
return textsecure.messaging.createGroup(recipients, name, avatar).then(function(groupId) {
|
2014-11-13 23:35:37 +01:00
|
|
|
conversation.save({
|
2014-11-13 06:46:57 +01:00
|
|
|
id : getString(groupId),
|
|
|
|
groupId : getString(groupId)
|
|
|
|
});
|
2014-11-13 23:35:37 +01:00
|
|
|
return conversation;
|
2014-10-16 23:14:00 +02:00
|
|
|
});
|
2014-08-11 08:34:29 +02:00
|
|
|
},
|
|
|
|
|
2014-06-03 18:39:29 +02:00
|
|
|
findOrCreateForRecipient: function(recipient) {
|
2014-05-17 06:48:46 +02:00
|
|
|
var attributes = {};
|
2014-06-08 03:05:11 +02:00
|
|
|
attributes = {
|
|
|
|
id : recipient,
|
|
|
|
name : recipient,
|
|
|
|
type : 'private',
|
|
|
|
};
|
2014-11-13 23:35:37 +01:00
|
|
|
var conversation = this.add(attributes, {merge: true});
|
|
|
|
conversation.save();
|
|
|
|
return conversation;
|
2014-05-17 06:48:46 +02:00
|
|
|
},
|
|
|
|
|
2014-11-13 23:35:37 +01:00
|
|
|
addIncomingMessage: function(decrypted) {
|
2014-05-17 06:48:46 +02:00
|
|
|
var attributes = {};
|
|
|
|
if (decrypted.message.group) {
|
|
|
|
attributes = {
|
2014-10-17 23:32:37 +02:00
|
|
|
id : decrypted.message.group.id,
|
2014-10-17 02:50:36 +02:00
|
|
|
groupId : decrypted.message.group.id,
|
2014-10-17 22:14:09 +02:00
|
|
|
name : decrypted.message.group.name || 'New group',
|
2014-05-17 06:48:46 +02:00
|
|
|
type : 'group',
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
attributes = {
|
|
|
|
id : decrypted.pushMessage.source,
|
|
|
|
name : decrypted.pushMessage.source,
|
|
|
|
type : 'private'
|
|
|
|
};
|
|
|
|
}
|
2014-11-13 23:35:37 +01:00
|
|
|
var conversation = this.add(attributes, {merge: true});
|
2014-11-16 22:19:51 +01:00
|
|
|
return conversation.receiveMessage(decrypted);
|
2014-11-13 01:48:28 +01:00
|
|
|
},
|
2014-10-18 16:08:57 +02:00
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
fetchGroups: function(number) {
|
|
|
|
return this.fetch({
|
|
|
|
index: {
|
|
|
|
name: 'group',
|
|
|
|
only: number
|
|
|
|
}
|
|
|
|
});
|
2014-11-13 01:48:28 +01:00
|
|
|
}
|
2015-02-11 00:27:26 +01:00
|
|
|
|
2014-11-13 23:35:37 +01:00
|
|
|
});
|
2014-05-17 06:48:46 +02:00
|
|
|
})();
|