2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-11-13 22:35:37 +00:00
|
|
|
*/
|
2014-05-12 00:13:09 +00:00
|
|
|
(function () {
|
2014-11-13 22:35:37 +00:00
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
2014-05-12 00:13:09 +00:00
|
|
|
|
2015-05-20 22:52:25 +00:00
|
|
|
var Message = window.Whisper.Message = Backbone.Model.extend({
|
2014-12-12 03:41:40 +00:00
|
|
|
database : Whisper.Database,
|
|
|
|
storeName : 'messages',
|
2015-09-14 02:42:49 +00:00
|
|
|
initialize: function() {
|
|
|
|
this.on('change:attachments', this.updateImageUrl);
|
|
|
|
this.on('destroy', this.revokeImageUrl);
|
|
|
|
},
|
2014-12-12 03:41:40 +00:00
|
|
|
defaults : function() {
|
2014-11-20 23:43:51 +00:00
|
|
|
return {
|
|
|
|
timestamp: new Date().getTime(),
|
|
|
|
attachments: []
|
|
|
|
};
|
|
|
|
},
|
2014-11-13 22:35:37 +00:00
|
|
|
validate: function(attributes, options) {
|
2014-12-12 03:41:40 +00:00
|
|
|
var required = ['conversationId', 'received_at', 'sent_at'];
|
2014-11-13 22:35:37 +00:00
|
|
|
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
|
|
|
|
if (missing.length) {
|
|
|
|
console.log("Message missing attributes: " + missing);
|
|
|
|
}
|
2015-02-17 19:54:15 +00:00
|
|
|
},
|
|
|
|
isEndSession: function() {
|
2015-06-01 21:08:21 +00:00
|
|
|
var flag = textsecure.protobuf.DataMessage.Flags.END_SESSION;
|
2015-02-17 19:54:15 +00:00
|
|
|
return !!(this.get('flags') & flag);
|
|
|
|
},
|
|
|
|
isGroupUpdate: function() {
|
|
|
|
return !!(this.get('group_update'));
|
2015-02-25 00:02:33 +00:00
|
|
|
},
|
|
|
|
isIncoming: function() {
|
|
|
|
return this.get('type') === 'incoming';
|
2015-03-12 00:49:01 +00:00
|
|
|
},
|
2015-03-19 23:17:26 +00:00
|
|
|
getDescription: function() {
|
|
|
|
if (this.isGroupUpdate()) {
|
2015-03-23 22:44:47 +00:00
|
|
|
var group_update = this.get('group_update');
|
|
|
|
if (group_update.left) {
|
|
|
|
return group_update.left + ' left the group.';
|
|
|
|
}
|
|
|
|
|
|
|
|
var messages = ['Updated the group.'];
|
|
|
|
if (group_update.name) {
|
|
|
|
messages.push("Title is now '" + group_update.name + "'.");
|
|
|
|
}
|
|
|
|
if (group_update.joined) {
|
|
|
|
messages.push(group_update.joined.join(', ') + ' joined the group.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages.join(' ');
|
2015-03-19 23:17:26 +00:00
|
|
|
}
|
|
|
|
if (this.isEndSession()) {
|
2015-03-23 22:44:47 +00:00
|
|
|
return 'Secure session ended.';
|
2015-03-19 23:17:26 +00:00
|
|
|
}
|
2015-03-20 17:47:58 +00:00
|
|
|
if (this.isIncoming() && this.hasKeyConflicts()) {
|
|
|
|
return 'Received message with unknown identity key.';
|
|
|
|
}
|
2015-09-30 21:27:18 +00:00
|
|
|
if (this.isIncoming() && this.hasErrors()) {
|
2015-09-30 22:22:59 +00:00
|
|
|
return 'Error handling incoming message.';
|
2015-09-30 21:27:18 +00:00
|
|
|
}
|
2015-03-19 23:17:26 +00:00
|
|
|
|
|
|
|
return this.get('body');
|
|
|
|
},
|
2015-09-14 03:25:04 +00:00
|
|
|
getNotificationText: function() {
|
|
|
|
var description = this.getDescription();
|
|
|
|
if (description) {
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
if (this.get('attachments').length > 0) {
|
|
|
|
return 'Media message';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
},
|
2015-09-14 02:42:49 +00:00
|
|
|
updateImageUrl: function() {
|
|
|
|
this.revokeImageUrl();
|
2015-09-14 03:25:04 +00:00
|
|
|
var attachment = this.get('attachments')[0];
|
2015-09-14 02:42:49 +00:00
|
|
|
if (attachment) {
|
|
|
|
var blob = new Blob([attachment.data], {
|
|
|
|
type: attachment.contentType
|
|
|
|
});
|
|
|
|
this.imageUrl = URL.createObjectURL(blob);
|
|
|
|
} else {
|
|
|
|
this.imageUrl = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
revokeImageUrl: function() {
|
|
|
|
if (this.imageUrl) {
|
|
|
|
URL.revokeObjectURL(this.imageUrl);
|
|
|
|
this.imageUrl = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getImageUrl: function() {
|
|
|
|
if (this.imageUrl === undefined) {
|
|
|
|
this.updateImageUrl();
|
|
|
|
}
|
|
|
|
return this.imageUrl;
|
|
|
|
},
|
2015-03-12 00:49:01 +00:00
|
|
|
getContact: function() {
|
2015-09-17 05:06:20 +00:00
|
|
|
var conversationId = this.get('source');
|
|
|
|
if (!this.isIncoming()) {
|
|
|
|
conversationId = textsecure.storage.user.getNumber();
|
2015-03-12 03:41:15 +00:00
|
|
|
}
|
2015-09-17 05:06:20 +00:00
|
|
|
var c = ConversationController.get(conversationId);
|
|
|
|
if (!c) {
|
2015-09-17 18:41:49 +00:00
|
|
|
c = ConversationController.create({id: conversationId});
|
2015-09-17 05:06:20 +00:00
|
|
|
c.fetch();
|
|
|
|
}
|
|
|
|
return c;
|
2015-02-24 00:23:22 +00:00
|
|
|
},
|
|
|
|
isOutgoing: function() {
|
|
|
|
return this.get('type') === 'outgoing';
|
|
|
|
},
|
2015-09-30 21:27:18 +00:00
|
|
|
hasErrors: function() {
|
|
|
|
return _.size(this.get('errors')) > 0;
|
|
|
|
},
|
2015-02-18 02:03:05 +00:00
|
|
|
hasKeyConflicts: function() {
|
|
|
|
return _.any(this.get('errors'), function(e) {
|
|
|
|
return (e.name === 'IncomingIdentityKeyError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
hasKeyConflict: function(number) {
|
|
|
|
return _.any(this.get('errors'), function(e) {
|
|
|
|
return (e.name === 'IncomingIdentityKeyError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError') &&
|
|
|
|
e.number === number;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getKeyConflict: function(number) {
|
2015-02-24 00:23:22 +00:00
|
|
|
return _.find(this.get('errors'), function(e) {
|
2015-02-18 02:03:05 +00:00
|
|
|
return (e.name === 'IncomingIdentityKeyError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError') &&
|
|
|
|
e.number === number;
|
2015-02-24 00:23:22 +00:00
|
|
|
});
|
2015-02-18 02:03:05 +00:00
|
|
|
},
|
2015-09-28 20:33:26 +00:00
|
|
|
|
|
|
|
send: function(promise) {
|
|
|
|
return promise.then(function() {
|
|
|
|
this.save({sent: true});
|
|
|
|
}.bind(this)).catch(function(errors) {
|
2015-10-02 01:21:20 +00:00
|
|
|
this.set({sent: true});
|
|
|
|
this.saveErrors(errors);
|
2015-09-28 20:33:26 +00:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2015-10-02 01:21:20 +00:00
|
|
|
saveErrors: function(errors) {
|
|
|
|
if (!(errors instanceof Array)) {
|
|
|
|
errors = [errors];
|
|
|
|
}
|
|
|
|
errors.forEach(function(e) {
|
|
|
|
console.log(e);
|
|
|
|
console.log(e.reason, e.stack);
|
|
|
|
});
|
2015-10-02 19:14:34 +00:00
|
|
|
errors = errors.map(function(e) {
|
2015-10-21 20:33:26 +00:00
|
|
|
if (e.constructor === Error || e.constructor === TypeError) {
|
2015-10-02 19:14:34 +00:00
|
|
|
return _.pick(e, 'name', 'message', 'code', 'number', 'reason');
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
});
|
2015-10-02 19:28:29 +00:00
|
|
|
errors = errors.concat(this.get('errors') || []);
|
|
|
|
|
|
|
|
return this.save({errors : errors});
|
2015-10-02 19:14:34 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
removeConflictFor: function(number) {
|
|
|
|
var errors = _.reject(this.get('errors'), function(e) {
|
|
|
|
return e.number === number &&
|
|
|
|
(e.name === 'IncomingIdentityKeyError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError');
|
2015-10-02 01:21:20 +00:00
|
|
|
});
|
2015-10-02 19:14:34 +00:00
|
|
|
this.set({errors: errors});
|
2015-10-02 01:21:20 +00:00
|
|
|
},
|
|
|
|
|
2015-10-03 01:31:07 +00:00
|
|
|
removeOutgoingErrors: function(number) {
|
|
|
|
var errors = _.partition(this.get('errors'), function(e) {
|
|
|
|
return e.number === number &&
|
|
|
|
(e.name === 'OutgoingMessageError' ||
|
|
|
|
e.name === 'SendMessageNetworkError');
|
|
|
|
});
|
|
|
|
this.set({errors: errors[1]});
|
|
|
|
return errors[0][0];
|
|
|
|
},
|
|
|
|
|
2015-09-22 22:52:42 +00:00
|
|
|
resend: function(number) {
|
2015-10-03 01:31:07 +00:00
|
|
|
var error = this.removeOutgoingErrors(number);
|
2015-09-22 22:52:42 +00:00
|
|
|
if (error) {
|
|
|
|
var promise = new textsecure.ReplayableError(error).replay();
|
|
|
|
this.send(promise);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-18 02:03:05 +00:00
|
|
|
resolveConflict: function(number) {
|
|
|
|
var error = this.getKeyConflict(number);
|
|
|
|
if (error) {
|
2015-10-02 19:14:34 +00:00
|
|
|
this.removeConflictFor(number);
|
2015-02-18 02:03:05 +00:00
|
|
|
var promise = new textsecure.ReplayableError(error).replay();
|
|
|
|
if (this.isIncoming()) {
|
2015-10-02 01:21:20 +00:00
|
|
|
promise = promise.then(function(dataMessage) {
|
2015-06-17 00:46:53 +00:00
|
|
|
this.handleDataMessage(dataMessage);
|
2015-02-18 02:03:05 +00:00
|
|
|
}.bind(this));
|
2015-10-02 19:14:34 +00:00
|
|
|
} else {
|
|
|
|
promise = promise.then(function() {
|
|
|
|
this.save();
|
|
|
|
}.bind(this));
|
2015-02-18 02:03:05 +00:00
|
|
|
}
|
2015-10-02 19:14:34 +00:00
|
|
|
promise.catch(function(e) {
|
2015-10-02 01:21:20 +00:00
|
|
|
this.saveErrors(e);
|
|
|
|
}.bind(this));
|
2015-10-02 19:14:34 +00:00
|
|
|
|
2015-08-01 00:20:33 +00:00
|
|
|
return promise;
|
2015-02-18 02:03:05 +00:00
|
|
|
}
|
2015-03-18 23:26:55 +00:00
|
|
|
},
|
2015-06-17 00:46:53 +00:00
|
|
|
handleDataMessage: function(dataMessage) {
|
2015-03-18 23:26:55 +00:00
|
|
|
// This function can be called from the background script on an
|
|
|
|
// incoming message or from the frontend after the user accepts an
|
|
|
|
// identity key change.
|
|
|
|
var message = this;
|
|
|
|
var source = message.get('source');
|
2015-06-17 18:33:01 +00:00
|
|
|
var type = message.get('type');
|
2015-03-18 23:26:55 +00:00
|
|
|
var timestamp = message.get('sent_at');
|
2015-06-01 21:08:21 +00:00
|
|
|
var conversationId = message.get('conversationId');
|
2015-06-17 00:46:53 +00:00
|
|
|
if (dataMessage.group) {
|
|
|
|
conversationId = dataMessage.group.id;
|
2015-06-01 21:08:21 +00:00
|
|
|
}
|
2015-08-27 19:38:51 +00:00
|
|
|
var conversation = ConversationController.create({id: conversationId});
|
2015-06-01 21:08:21 +00:00
|
|
|
conversation.fetch().always(function() {
|
|
|
|
var now = new Date().getTime();
|
|
|
|
var attributes = { type: 'private' };
|
2015-06-17 00:46:53 +00:00
|
|
|
if (dataMessage.group) {
|
2015-06-01 21:08:21 +00:00
|
|
|
var group_update = {};
|
|
|
|
attributes = {
|
|
|
|
type: 'group',
|
2015-06-17 00:46:53 +00:00
|
|
|
groupId: dataMessage.group.id,
|
2015-06-01 21:08:21 +00:00
|
|
|
};
|
2015-06-17 00:46:53 +00:00
|
|
|
if (dataMessage.group.type === textsecure.protobuf.GroupContext.Type.UPDATE) {
|
2015-03-19 20:49:09 +00:00
|
|
|
attributes = {
|
2015-06-01 21:08:21 +00:00
|
|
|
type : 'group',
|
2015-06-17 00:46:53 +00:00
|
|
|
groupId : dataMessage.group.id,
|
|
|
|
name : dataMessage.group.name,
|
|
|
|
avatar : dataMessage.group.avatar,
|
|
|
|
members : dataMessage.group.members,
|
2015-03-19 20:49:09 +00:00
|
|
|
};
|
2015-08-02 17:10:43 +00:00
|
|
|
group_update = conversation.changedAttributes(_.pick(dataMessage.group, 'name', 'avatar')) || {};
|
2015-06-17 00:46:53 +00:00
|
|
|
var difference = _.difference(dataMessage.group.members, conversation.get('members'));
|
2015-06-01 21:08:21 +00:00
|
|
|
if (difference.length > 0) {
|
|
|
|
group_update.joined = difference;
|
2015-03-18 23:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-17 00:46:53 +00:00
|
|
|
else if (dataMessage.group.type === textsecure.protobuf.GroupContext.Type.QUIT) {
|
2015-06-01 21:08:21 +00:00
|
|
|
group_update = { left: source };
|
|
|
|
attributes.members = _.without(conversation.get('members'), source);
|
|
|
|
}
|
2015-05-18 21:23:09 +00:00
|
|
|
|
2015-06-01 21:08:21 +00:00
|
|
|
if (_.keys(group_update).length > 0) {
|
|
|
|
message.set({group_update: group_update});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type === 'outgoing') {
|
|
|
|
// lazy hack - check for receipts that arrived early.
|
2015-06-17 00:46:53 +00:00
|
|
|
if (dataMessage.group && dataMessage.group.id) { // group sync
|
2015-06-01 21:08:21 +00:00
|
|
|
var members = conversation.get('members') || [];
|
|
|
|
var receipts = window.receipts.where({ timestamp: timestamp });
|
|
|
|
for (var i in receipts) {
|
|
|
|
if (members.indexOf(receipts[i].get('source')) > -1) {
|
|
|
|
window.receipts.remove(receipts[i]);
|
2015-05-18 21:23:09 +00:00
|
|
|
message.set({
|
|
|
|
delivered: (message.get('delivered') || 0) + 1
|
|
|
|
});
|
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var receipt = window.receipts.findWhere({
|
|
|
|
timestamp: timestamp,
|
|
|
|
source: conversationId
|
|
|
|
});
|
|
|
|
if (receipt) {
|
|
|
|
window.receipts.remove(receipt);
|
|
|
|
message.set({
|
|
|
|
delivered: (message.get('delivered') || 0) + 1
|
|
|
|
});
|
2015-05-18 21:23:09 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
}
|
|
|
|
attributes.active_at = now;
|
|
|
|
if (type === 'incoming') {
|
|
|
|
attributes.unreadCount = conversation.get('unreadCount') + 1;
|
|
|
|
}
|
|
|
|
conversation.set(attributes);
|
2015-03-18 23:26:55 +00:00
|
|
|
|
2015-06-01 21:08:21 +00:00
|
|
|
message.set({
|
2015-06-17 00:46:53 +00:00
|
|
|
body : dataMessage.body,
|
2015-06-01 21:08:21 +00:00
|
|
|
conversationId : conversation.id,
|
2015-06-17 00:46:53 +00:00
|
|
|
attachments : dataMessage.attachments,
|
2015-06-01 21:08:21 +00:00
|
|
|
decrypted_at : now,
|
2015-06-17 00:46:53 +00:00
|
|
|
flags : dataMessage.flags,
|
2015-06-01 21:08:21 +00:00
|
|
|
errors : []
|
|
|
|
});
|
2015-03-18 23:26:55 +00:00
|
|
|
|
2015-07-22 22:24:03 +00:00
|
|
|
var conversation_timestamp = conversation.get('timestamp');
|
|
|
|
if (!conversation_timestamp || message.get('sent_at') > conversation_timestamp) {
|
2015-06-01 21:08:21 +00:00
|
|
|
conversation.set({
|
|
|
|
timestamp: message.get('sent_at'),
|
|
|
|
lastMessage: message.get('body')
|
|
|
|
});
|
|
|
|
}
|
2015-08-28 01:02:39 +00:00
|
|
|
else if (!conversation.get('lastMessage')) {
|
|
|
|
conversation.set({
|
|
|
|
lastMessage: message.get('body')
|
|
|
|
});
|
|
|
|
}
|
2015-03-18 23:26:55 +00:00
|
|
|
|
2015-06-01 21:08:21 +00:00
|
|
|
conversation.save().then(function() {
|
|
|
|
message.save().then(function() {
|
2015-09-09 23:53:34 +00:00
|
|
|
if (message.isIncoming()) {
|
|
|
|
notifyConversation(message);
|
2015-10-01 00:52:13 +00:00
|
|
|
} else {
|
|
|
|
conversation.trigger('newmessages');
|
2015-09-09 23:53:34 +00:00
|
|
|
}
|
2015-03-18 23:26:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-13 22:35:37 +00:00
|
|
|
}
|
2015-03-18 23:26:55 +00:00
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
});
|
2014-05-18 21:26:55 +00:00
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
Whisper.MessageCollection = Backbone.Collection.extend({
|
2014-12-12 03:41:40 +00:00
|
|
|
model : Message,
|
|
|
|
database : Whisper.Database,
|
|
|
|
storeName : 'messages',
|
|
|
|
comparator : 'received_at',
|
2015-03-12 00:49:01 +00:00
|
|
|
initialize : function(models, options) {
|
|
|
|
if (options) {
|
|
|
|
this.conversation = options.conversation;
|
|
|
|
}
|
|
|
|
},
|
2014-12-12 03:41:40 +00:00
|
|
|
destroyAll : function () {
|
2014-11-13 22:35:37 +00:00
|
|
|
return Promise.all(this.models.map(function(m) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
m.destroy().then(resolve).fail(reject);
|
|
|
|
});
|
|
|
|
}));
|
2014-12-20 01:15:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fetchSentAt: function(timestamp) {
|
|
|
|
return this.fetch({
|
|
|
|
index: {
|
|
|
|
// 'receipt' index on sent_at
|
|
|
|
name: 'receipt',
|
|
|
|
only: timestamp
|
|
|
|
}
|
|
|
|
});
|
2014-12-19 20:55:29 +00:00
|
|
|
},
|
|
|
|
|
2015-07-07 23:03:12 +00:00
|
|
|
fetchConversation: function(conversationId) {
|
2015-07-08 18:50:09 +00:00
|
|
|
var options = {remove: false};
|
2014-12-19 20:55:29 +00:00
|
|
|
options.index = {
|
|
|
|
// 'conversation' index on [conversationId, received_at]
|
|
|
|
name : 'conversation',
|
|
|
|
lower : [conversationId],
|
2015-02-18 23:07:07 +00:00
|
|
|
upper : [conversationId, Number.MAX_VALUE]
|
2014-12-19 20:55:29 +00:00
|
|
|
// SELECT messages WHERE conversationId = this.id ORDER
|
|
|
|
// received_at DESC
|
|
|
|
};
|
|
|
|
// TODO pagination/infinite scroll
|
|
|
|
// limit: 10, offset: page*10,
|
|
|
|
return this.fetch(options);
|
2015-02-18 02:03:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
hasKeyConflicts: function() {
|
|
|
|
return this.any(function(m) { return m.hasKeyConflicts(); });
|
2014-11-13 22:35:37 +00:00
|
|
|
}
|
|
|
|
});
|
2015-02-19 08:20:22 +00:00
|
|
|
})();
|