2014-11-13 22:35:37 +00: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 04:48:46 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
2014-11-13 05:46:57 +00:00
|
|
|
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
2014-11-13 00:48:28 +00:00
|
|
|
function encodeAttachments (attachments) {
|
|
|
|
return Promise.all(attachments.map(function(a) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var dataView = new DataView(a.data);
|
|
|
|
var blob = new Blob([dataView], { type: a.contentType });
|
|
|
|
var FR = new FileReader();
|
|
|
|
FR.onload = function(e) {
|
|
|
|
resolve(e.target.result);
|
|
|
|
};
|
|
|
|
FR.onerror = reject;
|
|
|
|
FR.readAsDataURL(blob);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
};
|
2014-05-17 04:48:46 +00:00
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
var Conversation = Whisper.Conversation = Backbone.Model.extend({
|
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'conversations',
|
2014-05-17 04:48:46 +00:00
|
|
|
defaults: function() {
|
|
|
|
return {
|
2014-11-13 05:46:57 +00:00
|
|
|
name: 'New Conversation',
|
2014-05-17 04:48:46 +00:00
|
|
|
image: '/images/default.png',
|
|
|
|
unreadCount: 0,
|
2014-06-08 01:00:51 +00:00
|
|
|
timestamp: new Date().getTime(),
|
|
|
|
active: true
|
2014-05-17 04:48:46 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
initialize: function() {
|
|
|
|
this.messageCollection = new Whisper.MessageCollection();
|
|
|
|
},
|
|
|
|
|
2014-05-17 04:48:46 +00:00
|
|
|
validate: function(attributes, options) {
|
2014-10-16 21:11:50 +00:00
|
|
|
var required = ['type', 'timestamp', 'image', 'name'];
|
2014-05-17 04:48:46 +00:00
|
|
|
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
|
2014-11-13 22:35:37 +00:00
|
|
|
if (missing.length) { return "Conversation must have " + missing; }
|
2014-05-17 04:48:46 +00:00
|
|
|
},
|
|
|
|
|
2014-10-25 01:44:30 +00:00
|
|
|
sendMessage: function(message, attachments) {
|
2014-11-15 01:48:57 +00:00
|
|
|
return encodeAttachments(attachments).then(function(base64_attachments) {
|
2014-11-02 21:48:35 +00:00
|
|
|
var timestamp = Date.now();
|
2014-11-16 23:30:40 +00:00
|
|
|
this.messageCollection.add({
|
|
|
|
body : message,
|
|
|
|
timestamp : timestamp,
|
|
|
|
conversationId : this.id,
|
|
|
|
conversationType : this.get('type'),
|
|
|
|
type : 'outgoing',
|
|
|
|
attachments : base64_attachments
|
2014-11-13 22:35:37 +00:00
|
|
|
}).save();
|
2014-10-26 07:29:01 +00:00
|
|
|
|
2014-11-02 21:48:35 +00:00
|
|
|
this.save({ timestamp: timestamp,
|
|
|
|
unreadCount: 0,
|
|
|
|
active: true});
|
2014-08-11 06:34:29 +00:00
|
|
|
|
2014-11-02 21:48:35 +00:00
|
|
|
if (this.get('type') == 'private') {
|
|
|
|
return textsecure.messaging.sendMessageToNumber(this.get('id'), message, attachments);
|
2014-06-03 16:39:29 +00:00
|
|
|
}
|
2014-11-02 21:48:35 +00:00
|
|
|
else {
|
|
|
|
return textsecure.messaging.sendMessageToGroup(this.get('groupId'), message, attachments);
|
2014-06-03 16:39:29 +00:00
|
|
|
}
|
2014-11-02 21:48:35 +00:00
|
|
|
}.bind(this)).then(function(result) {
|
|
|
|
console.log(result);
|
|
|
|
}).catch(function(error) {
|
|
|
|
console.log(error);
|
|
|
|
});
|
2014-05-17 04:48:46 +00:00
|
|
|
},
|
|
|
|
|
2014-11-13 00:48:28 +00:00
|
|
|
receiveMessage: function(decrypted) {
|
2014-11-13 22:35:37 +00:00
|
|
|
var conversation = this;
|
2014-11-16 21:19:51 +00:00
|
|
|
return encodeAttachments(decrypted.message.attachments).then(function(base64_attachments) {
|
2014-11-13 00:48:28 +00:00
|
|
|
var timestamp = decrypted.pushMessage.timestamp.toNumber();
|
2014-11-16 23:30:40 +00:00
|
|
|
var m = this.messageCollection.add({
|
2014-11-13 00:48:28 +00:00
|
|
|
body: decrypted.message.body,
|
2014-11-13 22:35:37 +00:00
|
|
|
timestamp: timestamp,
|
|
|
|
conversationId: this.id,
|
|
|
|
conversationType: this.get('type'),
|
2014-11-13 00:48:28 +00:00
|
|
|
attachments: base64_attachments,
|
|
|
|
type: 'incoming',
|
2014-11-13 22:35:37 +00:00
|
|
|
sender: decrypted.pushMessage.source
|
2014-11-13 00:48:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (timestamp > this.get('timestamp')) {
|
|
|
|
this.set('timestamp', timestamp);
|
|
|
|
}
|
|
|
|
this.save({unreadCount: this.get('unreadCount') + 1, active: true});
|
2014-11-16 21:19:51 +00:00
|
|
|
|
2014-11-16 23:30:40 +00:00
|
|
|
return new Promise(function (resolve) { m.save().then(resolve(m)) });
|
2014-11-13 00:48:28 +00:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2014-11-16 23:30:40 +00:00
|
|
|
fetchMessages: function(options) {
|
2014-11-16 21:19:51 +00:00
|
|
|
options = options || {};
|
|
|
|
options.conditions = {conversationId: this.id };
|
|
|
|
return this.messageCollection.fetch(options);
|
2014-11-16 23:30:40 +00:00
|
|
|
}
|
2014-05-17 04:48:46 +00:00
|
|
|
});
|
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
Whisper.ConversationCollection = Backbone.Collection.extend({
|
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'conversations',
|
|
|
|
model: Conversation,
|
2014-10-18 14:08:57 +00:00
|
|
|
|
|
|
|
comparator: function(m) {
|
|
|
|
return -m.get('timestamp');
|
|
|
|
},
|
|
|
|
|
2014-08-11 06:34:29 +00:00
|
|
|
createGroup: function(recipients, name) {
|
|
|
|
var attributes = {};
|
|
|
|
attributes = {
|
|
|
|
name : name,
|
2014-10-16 21:14:00 +00:00
|
|
|
numbers : recipients,
|
2014-08-11 06:34:29 +00:00
|
|
|
type : 'group',
|
|
|
|
};
|
2014-11-13 22:35:37 +00:00
|
|
|
var conversation = this.add(attributes, {merge: true});
|
2014-10-16 21:14:00 +00:00
|
|
|
return textsecure.messaging.createGroup(recipients, name).then(function(groupId) {
|
2014-11-13 22:35:37 +00:00
|
|
|
conversation.save({
|
2014-11-13 05:46:57 +00:00
|
|
|
id : getString(groupId),
|
|
|
|
groupId : getString(groupId)
|
|
|
|
});
|
2014-11-13 22:35:37 +00:00
|
|
|
return conversation;
|
2014-10-16 21:14:00 +00:00
|
|
|
});
|
2014-08-11 06:34:29 +00:00
|
|
|
},
|
|
|
|
|
2014-06-03 16:39:29 +00:00
|
|
|
findOrCreateForRecipient: function(recipient) {
|
2014-05-17 04:48:46 +00:00
|
|
|
var attributes = {};
|
2014-06-08 01:05:11 +00:00
|
|
|
attributes = {
|
|
|
|
id : recipient,
|
|
|
|
name : recipient,
|
|
|
|
type : 'private',
|
|
|
|
};
|
2014-11-13 22:35:37 +00:00
|
|
|
var conversation = this.add(attributes, {merge: true});
|
|
|
|
conversation.save();
|
|
|
|
return conversation;
|
2014-05-17 04:48:46 +00:00
|
|
|
},
|
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
addIncomingMessage: function(decrypted) {
|
2014-05-17 04:48:46 +00:00
|
|
|
var attributes = {};
|
|
|
|
if (decrypted.message.group) {
|
|
|
|
attributes = {
|
2014-10-17 21:32:37 +00:00
|
|
|
id : decrypted.message.group.id,
|
2014-10-17 00:50:36 +00:00
|
|
|
groupId : decrypted.message.group.id,
|
2014-10-17 20:14:09 +00:00
|
|
|
name : decrypted.message.group.name || 'New group',
|
2014-05-17 04:48:46 +00:00
|
|
|
type : 'group',
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
attributes = {
|
|
|
|
id : decrypted.pushMessage.source,
|
|
|
|
name : decrypted.pushMessage.source,
|
|
|
|
type : 'private'
|
|
|
|
};
|
|
|
|
}
|
2014-11-13 22:35:37 +00:00
|
|
|
var conversation = this.add(attributes, {merge: true});
|
2014-11-16 21:19:51 +00:00
|
|
|
return conversation.receiveMessage(decrypted);
|
2014-11-13 00:48:28 +00:00
|
|
|
},
|
2014-10-18 14:08:57 +00:00
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
destroyAll: function () {
|
|
|
|
return Promise.all(this.models.map(function(m) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
m.destroy().then(resolve).fail(reject);
|
|
|
|
});
|
|
|
|
}));
|
2014-11-13 00:48:28 +00:00
|
|
|
}
|
2014-11-13 22:35:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.Conversations = new Whisper.ConversationCollection();
|
2014-05-17 04:48:46 +00:00
|
|
|
})();
|