Cable-Desktop/js/models/conversations.js

341 lines
10 KiB
JavaScript
Raw Normal View History

/*
* vim: ts=4:sw=4:expandtab
*/
(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({
database: Whisper.Database,
storeName: 'conversations',
defaults: function() {
var timestamp = new Date().getTime();
return {
unreadCount : 0,
timestamp : timestamp,
};
},
initialize: function() {
this.contactCollection = new Whisper.ConversationCollection();
this.messageCollection = new Whisper.MessageCollection([], {
conversation: this
});
this.on('change:avatar', this.updateAvatarUrl);
2015-03-18 01:10:18 +01:00
this.on('destroy', this.revokeAvatarUrl);
},
validate: function(attributes, options) {
var required = ['id', 'type'];
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;
}
// hack
if (this.get('type') === 'private') {
try {
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
});
} catch(ex) {
return ex;
}
}
},
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,
2015-07-08 04:21:10 +02:00
received_at : now
});
message.save();
2014-10-26 08:29:01 +01:00
this.save({
unreadCount : 0,
active_at : now,
timestamp : now,
lastMessage : body
}).then(function() {
updateInbox();
});
var sendFunc;
if (this.get('type') == 'private') {
sendFunc = textsecure.messaging.sendMessageToNumber;
}
else {
sendFunc = textsecure.messaging.sendMessageToGroup;
}
sendFunc(this.get('id'), body, attachments, now).then(function() {
2015-07-08 04:21:10 +02:00
message.save({'sent': true});
}.bind(this)).catch(function(errors) {
if (errors instanceof Error) {
errors = [errors];
}
var keyErrors = [];
_.each(errors, function(e) {
if (e.error.name === 'OutgoingIdentityKeyError') {
keyErrors.push(e.error);
}
});
if (keyErrors.length) {
message.save({ errors : keyErrors }).then(function() {
updateInbox();
});
} else {
2015-04-09 21:50:11 +02:00
if (!(errors instanceof Array)) {
errors = [errors];
}
2015-04-01 21:53:29 +02:00
errors.map(function(e) {
if (e.error && e.error.stack) {
2015-04-09 21:50:11 +02:00
console.error(e.error.stack);
2015-04-01 21:53:29 +02:00
}
});
throw errors;
}
});
},
endSession: function() {
if (this.get('type') === 'private') {
var now = Date.now();
2015-07-16 20:05:47 +02:00
var message = this.messageCollection.add({
conversationId : this.id,
type : 'outgoing',
sent_at : now,
received_at : now,
flags : textsecure.protobuf.DataMessage.Flags.END_SESSION
2015-07-16 20:05:47 +02:00
});
message.save();
textsecure.messaging.closeSession(this.id).then(function() {
message.save({sent: true});
});
}
},
leaveGroup: function() {
var now = Date.now();
if (this.get('type') === 'group') {
textsecure.messaging.leaveGroup(this.id);
this.messageCollection.add({
group_update: { left: 'You' },
conversationId : this.id,
type : 'outgoing',
sent_at : now,
received_at : now
}).save();
}
},
markRead: function() {
if (this.get('unreadCount') > 0) {
this.save({unreadCount: 0});
}
},
2015-07-08 01:03:12 +02:00
fetchMessages: function() {
2015-09-08 02:22:20 +02:00
if (!this.id) { return false; }
2015-07-08 01:03:12 +02:00
return this.messageCollection.fetchConversation(this.id);
},
fetchContacts: function(options) {
if (this.isPrivate()) {
this.contactCollection.reset([this]);
} else {
2015-03-23 20:01:58 +01:00
var members = this.get('members') || [];
this.contactCollection.reset(
2015-03-23 20:01:58 +01:00
members.map(function(number) {
var c = this.collection.add({id: number, type: 'private'});
c.fetch();
return c;
}.bind(this))
);
}
},
2015-08-27 21:38:51 +02:00
reload: function() {
this.fetch().then(this.fetchContacts.bind(this));
this.fetchMessages();
},
archive: function() {
this.set({active_at: null});
},
destroyMessages: function() {
var models = this.messageCollection.models;
this.messageCollection.reset([]);
_.each(models, function(message) { message.destroy(); });
this.archive();
return this.save().then(function() {
updateInbox();
});
},
getTitle: function() {
if (this.isPrivate()) {
return this.get('name') || this.id;
} else {
return this.get('name') || 'Unknown group';
}
2015-02-04 20:23:00 +01:00
},
getNumber: function() {
if (this.get('type') === 'private') {
return this.id;
} else {
return '';
}
},
isPrivate: function() {
return this.get('type') === 'private';
},
2015-03-18 01:10:18 +01:00
revokeAvatarUrl: function() {
if (this.avatarUrl) {
URL.revokeObjectURL(this.avatarUrl);
this.avatarUrl = null;
}
2015-03-18 01:10:18 +01:00
},
updateAvatarUrl: function(silent) {
2015-03-18 01:10:18 +01:00
this.revokeAvatarUrl();
var avatar = this.get('avatar');
if (avatar) {
this.avatarUrl = URL.createObjectURL(
new Blob([avatar.data], {type: avatar.contentType})
);
} else {
this.avatarUrl = null;
}
if (!silent) {
this.trigger('change');
}
},
getAvatar: function() {
if (this.avatarUrl === undefined) {
this.updateAvatarUrl(true);
}
if (this.avatarUrl) {
return { url: this.avatarUrl };
} else if (this.isPrivate()) {
var title = this.get('name');
if (!title) {
return { content: '#', color: 'gray' };
}
var initials = title.trim()[0];
return {
color: 1 + (Math.abs(this.hashCode()) % 15),
content: initials
};
} else {
2015-06-26 20:23:37 +02:00
return { url: '/images/group_default.png', color: 'gray' };
}
},
resolveConflicts: function(conflict) {
var number = conflict.number;
var identityKey = conflict.identityKey;
if (this.isPrivate()) {
number = this.id;
2015-03-24 02:36:38 +01:00
} else if (!_.include(this.get('members'), number)) {
throw 'Tried to resolve conflicts for a unknown group member';
}
if (!this.messageCollection.hasKeyConflicts()) {
throw 'No conflicts to resolve';
}
return textsecure.storage.axolotl.removeIdentityKey(number).then(function() {
return textsecure.storage.axolotl.putIdentityKey(number, identityKey).then(function() {
var promise = Promise.resolve();
this.messageCollection.each(function(message) {
if (message.hasKeyConflict(number)) {
promise = promise.then(function() {
return message.resolveConflict(number);
});
}
});
return promise;
}.bind(this));
2015-04-09 23:58:26 +02:00
}.bind(this));
},
hashCode: function() {
if (this.hash === undefined) {
var string = this.getTitle() || '';
if (string.length === 0) {
return 0;
}
var hash = 0;
for (var i = 0; i < string.length; i++) {
hash = ((hash<<5)-hash) + string.charCodeAt(i);
hash = hash & hash; // Convert to 32bit integer
}
this.hash = hash;
}
return this.hash;
}
});
Whisper.ConversationCollection = Backbone.Collection.extend({
database: Whisper.Database,
storeName: 'conversations',
2015-02-08 03:18:53 +01:00
model: Whisper.Conversation,
comparator: function(m) {
return -m.get('timestamp');
},
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
}
});
},
fetchActive: function() {
// Ensures all active conversations are included in this collection,
// and updates their attributes, but removes nothing.
return this.fetch({
index: {
name: 'inbox', // 'inbox' index on active_at
order: 'desc' // ORDER timestamp DESC
// TODO pagination/infinite scroll
// limit: 10, offset: page*10,
},
remove: false
});
}
});
})();