Let message models manage blob urls for attachments

// FREEBIE
This commit is contained in:
lilia 2015-09-13 19:42:49 -07:00
parent 04c8796bd3
commit 59313b5177

View file

@ -8,6 +8,10 @@
var Message = window.Whisper.Message = Backbone.Model.extend({
database : Whisper.Database,
storeName : 'messages',
initialize: function() {
this.on('change:attachments', this.updateImageUrl);
this.on('destroy', this.revokeImageUrl);
},
defaults : function() {
return {
timestamp: new Date().getTime(),
@ -57,6 +61,30 @@
return this.get('body');
},
updateImageUrl: function() {
this.revokeImageUrl();
var attachment = message.get('attachments')[0];
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;
},
getContact: function() {
if (this.collection) {
return this.collection.conversation.contactCollection.get(this.get('source'));