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-12 02:13:09 +02:00
|
|
|
(function () {
|
2014-11-13 23:35:37 +01:00
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
2014-05-12 02:13:09 +02:00
|
|
|
|
2015-05-21 00:52:25 +02:00
|
|
|
var Message = window.Whisper.Message = Backbone.Model.extend({
|
2014-12-12 04:41:40 +01:00
|
|
|
database : Whisper.Database,
|
|
|
|
storeName : 'messages',
|
|
|
|
defaults : function() {
|
2014-11-21 00:43:51 +01:00
|
|
|
return {
|
|
|
|
timestamp: new Date().getTime(),
|
|
|
|
attachments: []
|
|
|
|
};
|
|
|
|
},
|
2014-11-13 23:35:37 +01:00
|
|
|
validate: function(attributes, options) {
|
2014-12-12 04:41:40 +01:00
|
|
|
var required = ['conversationId', 'received_at', 'sent_at'];
|
2014-11-13 23:35:37 +01:00
|
|
|
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
|
|
|
|
if (missing.length) {
|
|
|
|
console.log("Message missing attributes: " + missing);
|
|
|
|
}
|
2015-02-17 20:54:15 +01:00
|
|
|
},
|
|
|
|
isEndSession: function() {
|
2015-06-01 23:08:21 +02:00
|
|
|
var flag = textsecure.protobuf.DataMessage.Flags.END_SESSION;
|
2015-02-17 20:54:15 +01:00
|
|
|
return !!(this.get('flags') & flag);
|
|
|
|
},
|
|
|
|
isGroupUpdate: function() {
|
|
|
|
return !!(this.get('group_update'));
|
2015-02-25 01:02:33 +01:00
|
|
|
},
|
|
|
|
isIncoming: function() {
|
|
|
|
return this.get('type') === 'incoming';
|
2015-03-12 01:49:01 +01:00
|
|
|
},
|
2015-03-20 00:17:26 +01:00
|
|
|
getDescription: function() {
|
|
|
|
if (this.isGroupUpdate()) {
|
2015-03-23 23:44:47 +01: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-20 00:17:26 +01:00
|
|
|
}
|
|
|
|
if (this.isEndSession()) {
|
2015-03-23 23:44:47 +01:00
|
|
|
return 'Secure session ended.';
|
2015-03-20 00:17:26 +01:00
|
|
|
}
|
2015-03-20 18:47:58 +01:00
|
|
|
if (this.isIncoming() && this.hasKeyConflicts()) {
|
|
|
|
return 'Received message with unknown identity key.';
|
|
|
|
}
|
2015-03-20 00:17:26 +01:00
|
|
|
|
|
|
|
return this.get('body');
|
|
|
|
},
|
2015-03-12 01:49:01 +01:00
|
|
|
getContact: function() {
|
2015-03-12 04:41:15 +01:00
|
|
|
if (this.collection) {
|
|
|
|
return this.collection.conversation.contactCollection.get(this.get('source'));
|
|
|
|
}
|
2015-02-24 01:23:22 +01:00
|
|
|
},
|
|
|
|
isOutgoing: function() {
|
|
|
|
return this.get('type') === 'outgoing';
|
|
|
|
},
|
2015-02-18 03:03:05 +01: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 01:23:22 +01:00
|
|
|
return _.find(this.get('errors'), function(e) {
|
2015-02-18 03:03:05 +01:00
|
|
|
return (e.name === 'IncomingIdentityKeyError' ||
|
|
|
|
e.name === 'OutgoingIdentityKeyError') &&
|
|
|
|
e.number === number;
|
2015-02-24 01:23:22 +01:00
|
|
|
});
|
2015-02-18 03:03:05 +01:00
|
|
|
},
|
|
|
|
resolveConflict: function(number) {
|
|
|
|
var error = this.getKeyConflict(number);
|
|
|
|
if (error) {
|
|
|
|
var promise = new textsecure.ReplayableError(error).replay();
|
|
|
|
if (this.isIncoming()) {
|
2015-06-17 02:46:53 +02:00
|
|
|
promise.then(function(dataMessage) {
|
|
|
|
this.handleDataMessage(dataMessage);
|
2015-02-18 03:03:05 +01:00
|
|
|
this.save('errors', []);
|
|
|
|
}.bind(this)).catch(function(e) {
|
|
|
|
//this.save('errors', [_.pick(e, ['name', 'message'])]);
|
|
|
|
var errors = this.get('errors').concat(
|
|
|
|
_.pick(e, ['name', 'message'])
|
|
|
|
);
|
|
|
|
this.save('errors', errors);
|
|
|
|
}.bind(this));
|
|
|
|
} else {
|
|
|
|
promise.then(function() {
|
|
|
|
this.save('errors', _.reject(this.get('errors'), function(e) {
|
|
|
|
return e.name === 'OutgoingIdentityKeyError' &&
|
|
|
|
e.number === number;
|
|
|
|
}));
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
}
|
2015-03-19 00:26:55 +01:00
|
|
|
},
|
2015-06-17 02:46:53 +02:00
|
|
|
handleDataMessage: function(dataMessage) {
|
2015-03-19 00:26:55 +01: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 20:33:01 +02:00
|
|
|
var type = message.get('type');
|
2015-03-19 00:26:55 +01:00
|
|
|
var timestamp = message.get('sent_at');
|
2015-06-01 23:08:21 +02:00
|
|
|
var conversationId = message.get('conversationId');
|
2015-06-17 02:46:53 +02:00
|
|
|
if (dataMessage.group) {
|
|
|
|
conversationId = dataMessage.group.id;
|
2015-06-01 23:08:21 +02:00
|
|
|
}
|
|
|
|
var conversation = new Whisper.Conversation({id: conversationId});
|
|
|
|
conversation.fetch().always(function() {
|
|
|
|
var now = new Date().getTime();
|
|
|
|
var attributes = { type: 'private' };
|
2015-06-17 02:46:53 +02:00
|
|
|
if (dataMessage.group) {
|
2015-06-01 23:08:21 +02:00
|
|
|
var group_update = {};
|
|
|
|
attributes = {
|
|
|
|
type: 'group',
|
2015-06-17 02:46:53 +02:00
|
|
|
groupId: dataMessage.group.id,
|
2015-06-01 23:08:21 +02:00
|
|
|
};
|
2015-06-17 02:46:53 +02:00
|
|
|
if (dataMessage.group.type === textsecure.protobuf.GroupContext.Type.UPDATE) {
|
2015-03-19 21:49:09 +01:00
|
|
|
attributes = {
|
2015-06-01 23:08:21 +02:00
|
|
|
type : 'group',
|
2015-06-17 02:46:53 +02:00
|
|
|
groupId : dataMessage.group.id,
|
|
|
|
name : dataMessage.group.name,
|
|
|
|
avatar : dataMessage.group.avatar,
|
|
|
|
members : dataMessage.group.members,
|
2015-03-19 21:49:09 +01:00
|
|
|
};
|
2015-06-17 02:46:53 +02:00
|
|
|
group_update = conversation.changedAttributes(_.pick(dataMessage.group, 'name', 'avatar'));
|
|
|
|
var difference = _.difference(dataMessage.group.members, conversation.get('members'));
|
2015-06-01 23:08:21 +02:00
|
|
|
if (difference.length > 0) {
|
|
|
|
group_update.joined = difference;
|
2015-03-19 00:26:55 +01:00
|
|
|
}
|
|
|
|
}
|
2015-06-17 02:46:53 +02:00
|
|
|
else if (dataMessage.group.type === textsecure.protobuf.GroupContext.Type.QUIT) {
|
2015-06-01 23:08:21 +02:00
|
|
|
group_update = { left: source };
|
|
|
|
attributes.members = _.without(conversation.get('members'), source);
|
|
|
|
}
|
2015-05-18 23:23:09 +02:00
|
|
|
|
2015-06-01 23:08:21 +02: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 02:46:53 +02:00
|
|
|
if (dataMessage.group && dataMessage.group.id) { // group sync
|
2015-06-01 23:08:21 +02: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 23:23:09 +02:00
|
|
|
message.set({
|
|
|
|
delivered: (message.get('delivered') || 0) + 1
|
|
|
|
});
|
|
|
|
}
|
2015-06-01 23:08:21 +02: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 23:23:09 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-01 23:08:21 +02:00
|
|
|
}
|
|
|
|
attributes.active_at = now;
|
|
|
|
if (type === 'incoming') {
|
|
|
|
attributes.unreadCount = conversation.get('unreadCount') + 1;
|
|
|
|
}
|
|
|
|
conversation.set(attributes);
|
2015-03-19 00:26:55 +01:00
|
|
|
|
2015-06-01 23:08:21 +02:00
|
|
|
message.set({
|
2015-06-17 02:46:53 +02:00
|
|
|
body : dataMessage.body,
|
2015-06-01 23:08:21 +02:00
|
|
|
conversationId : conversation.id,
|
2015-06-17 02:46:53 +02:00
|
|
|
attachments : dataMessage.attachments,
|
2015-06-01 23:08:21 +02:00
|
|
|
decrypted_at : now,
|
2015-06-17 02:46:53 +02:00
|
|
|
flags : dataMessage.flags,
|
2015-06-01 23:08:21 +02:00
|
|
|
errors : []
|
|
|
|
});
|
2015-03-19 00:26:55 +01:00
|
|
|
|
2015-06-01 23:08:21 +02:00
|
|
|
if (message.get('sent_at') > conversation.get('timestamp')) {
|
|
|
|
conversation.set({
|
|
|
|
timestamp: message.get('sent_at'),
|
|
|
|
lastMessage: message.get('body')
|
|
|
|
});
|
|
|
|
}
|
2015-03-19 00:26:55 +01:00
|
|
|
|
2015-06-01 23:08:21 +02:00
|
|
|
conversation.save().then(function() {
|
|
|
|
message.save().then(function() {
|
|
|
|
extension.trigger('updateInbox'); // inbox fetch
|
|
|
|
if (message.isIncoming()) {
|
|
|
|
notifyConversation(message);
|
|
|
|
} else {
|
|
|
|
updateConversation(conversation.id);
|
|
|
|
}
|
2015-03-19 00:26:55 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-13 23:35:37 +01:00
|
|
|
}
|
2015-03-19 00:26:55 +01:00
|
|
|
|
2014-11-13 23:35:37 +01:00
|
|
|
});
|
2014-05-18 23:26:55 +02:00
|
|
|
|
2014-11-13 23:35:37 +01:00
|
|
|
Whisper.MessageCollection = Backbone.Collection.extend({
|
2014-12-12 04:41:40 +01:00
|
|
|
model : Message,
|
|
|
|
database : Whisper.Database,
|
|
|
|
storeName : 'messages',
|
|
|
|
comparator : 'received_at',
|
2015-03-12 01:49:01 +01:00
|
|
|
initialize : function(models, options) {
|
|
|
|
if (options) {
|
|
|
|
this.conversation = options.conversation;
|
|
|
|
}
|
|
|
|
},
|
2014-12-12 04:41:40 +01:00
|
|
|
destroyAll : function () {
|
2014-11-13 23:35:37 +01:00
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
fetchSentAt: function(timestamp) {
|
|
|
|
return this.fetch({
|
|
|
|
index: {
|
|
|
|
// 'receipt' index on sent_at
|
|
|
|
name: 'receipt',
|
|
|
|
only: timestamp
|
|
|
|
}
|
|
|
|
});
|
2014-12-19 21:55:29 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
fetchConversation: function(conversationId, options) {
|
|
|
|
options = options || {};
|
|
|
|
options.index = {
|
|
|
|
// 'conversation' index on [conversationId, received_at]
|
|
|
|
name : 'conversation',
|
|
|
|
lower : [conversationId],
|
2015-02-19 00:07:07 +01:00
|
|
|
upper : [conversationId, Number.MAX_VALUE]
|
2014-12-19 21:55:29 +01: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 03:03:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
hasKeyConflicts: function() {
|
|
|
|
return this.any(function(m) { return m.hasKeyConflicts(); });
|
2014-11-13 23:35:37 +01:00
|
|
|
}
|
|
|
|
});
|
2015-02-19 09:20:22 +01:00
|
|
|
})();
|