signal-desktop/js/models/conversations.js

243 lines
7.4 KiB
JavaScript
Raw Normal View History

/* 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/>.
*/
(function () {
'use strict';
2014-11-13 05:46:57 +00:00
window.Whisper = window.Whisper || {};
2015-02-08 02:18:53 +00:00
// TODO: Factor out private and group subclasses of Conversation
Whisper.Conversation = Backbone.Model.extend({
database: Whisper.Database,
storeName: 'conversations',
defaults: function() {
var timestamp = new Date().getTime();
return {
unreadCount : 0,
timestamp : timestamp,
active_at : timestamp
};
},
initialize: function() {
this.messageCollection = new Whisper.MessageCollection();
},
validate: function(attributes, options) {
var required = ['type', 'timestamp'];
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
if (missing.length) { return "Conversation must have " + missing; }
2015-02-08 00:24:56 +00:00
if (attributes.type !== 'private' && attributes.type !== 'group') {
return "Invalid conversation type: " + attributes.type;
}
// hack
if (this.get('type') === 'private') {
this.id = libphonenumber.util.verifyNumber(this.id);
var number = libphonenumber.util.splitCountryCode(this.id);
this.set({
e164_number: this.id,
national_number: '' + number.national_number,
international_number: '' + number.country_code + number.national_number
});
}
},
sendMessage: function(body, attachments) {
var now = Date.now();
var message = this.messageCollection.add({
body : body,
conversationId : this.id,
type : 'outgoing',
attachments : attachments,
sent_at : now,
received_at : now
});
message.save();
2014-10-26 07:29:01 +00:00
this.save({
unreadCount : 0,
active_at : now,
timestamp : now,
lastMessage : body
});
var sendFunc;
if (this.get('type') == 'private') {
sendFunc = textsecure.messaging.sendMessageToNumber;
}
else {
sendFunc = textsecure.messaging.sendMessageToGroup;
}
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() {
extension.trigger('message', message); // notify frontend listeners
});
} else {
throw errors;
}
});
},
receiveMessage: function(decrypted) {
var conversation = this;
var timestamp = decrypted.pushMessage.timestamp.toNumber();
var m = this.messageCollection.add({
body : decrypted.message.body,
timestamp : timestamp,
conversationId : this.id,
attachments : decrypted.message.attachments,
type : 'incoming',
sender : decrypted.pushMessage.source
});
if (timestamp > this.get('timestamp')) {
this.set('timestamp', timestamp);
}
this.save({unreadCount: this.get('unreadCount') + 1, active: true});
return new Promise(function (resolve) { m.save().then(resolve(m)) });
},
fetchMessages: function(options) {
2014-12-19 20:55:29 +00:00
return this.messageCollection.fetchConversation(this.id, options);
},
archive: function() {
this.unset('active_at');
},
destroyMessages: function() {
var models = this.messageCollection.models;
this.messageCollection.reset([]);
_.each(models, function(message) { message.destroy(); });
this.archive();
return this.save();
},
getTitle: function() {
return this.get('name') || this.get('members') || this.id;
2015-02-04 19:23:00 +00:00
},
getNumber: function() {
if (this.get('type') === 'private') {
return this.id;
} else {
return '';
}
}
});
Whisper.ConversationCollection = Backbone.Collection.extend({
database: Whisper.Database,
storeName: 'conversations',
2015-02-08 02:18:53 +00:00
model: Whisper.Conversation,
comparator: function(m) {
return -m.get('timestamp');
},
2015-01-11 11:27:22 +00:00
createGroup: function(recipients, name, avatar) {
var attributes = {};
attributes = {
name : name,
members : recipients,
type : 'group',
2015-01-11 11:27:22 +00:00
avatar : avatar
};
var conversation = this.add(attributes, {merge: true});
2015-01-11 11:27:22 +00:00
return textsecure.messaging.createGroup(recipients, name, avatar).then(function(groupId) {
conversation.save({
2014-11-13 05:46:57 +00:00
id : getString(groupId),
groupId : getString(groupId)
});
return conversation;
});
},
findOrCreateForRecipient: function(recipient) {
var attributes = {};
2014-06-08 01:05:11 +00:00
attributes = {
id : recipient,
name : recipient,
type : 'private',
};
var conversation = this.add(attributes, {merge: true});
conversation.save();
return conversation;
},
addIncomingMessage: function(decrypted) {
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,
name : decrypted.message.group.name || 'New group',
type : 'group',
};
} else {
attributes = {
id : decrypted.pushMessage.source,
name : decrypted.pushMessage.source,
type : 'private'
};
}
var conversation = this.add(attributes, {merge: true});
return conversation.receiveMessage(decrypted);
},
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 01:15:57 +00:00
},
fetchGroups: function(number) {
return this.fetch({
index: {
name: 'group',
only: number
}
});
2014-12-19 20:55:29 +00:00
},
fetchActive: function(options) {
return this.fetch(_.extend(options, {
index: {
name: 'inbox', // 'inbox' index on active_at
order: 'desc' // ORDER timestamp DESC
}
// TODO pagination/infinite scroll
// limit: 10, offset: page*10,
}));
}
});
})();