diff --git a/js/models/messages.js b/js/models/messages.js index a971757fe260..dead3309767b 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -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'));