Store attachments as binary blobs

Move base64 encoding of attachments to an AttachmentView. This makes
image rendering an asynchronous task so we fire an update event to
indicate to the parent MessageListView that its content has changed
height and it is time to scroll down.
pull/749/head
lilia 11 years ago
parent bf22da209f
commit 99a2685f93

@ -60,12 +60,10 @@
<ul class='volley'> <ul class='volley'>
<li class='message'> <li class='message'>
{{ message }} {{ message }}
<div class='attachments'></div>
<span class='timestamp'>{{ timestamp }}</span> <span class='timestamp'>{{ timestamp }}</span>
</li> </li>
</ul> </ul>
{{#attachments}}
<img src={{.}}>
{{/attachments}}
</div> </div>
</script> </script>
<script type='text/x-tmpl-mustache' id='contact'> <script type='text/x-tmpl-mustache' id='contact'>

@ -18,21 +18,6 @@
window.Whisper = window.Whisper || {}; window.Whisper = window.Whisper || {};
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);
});
}));
};
var Conversation = Whisper.Conversation = Backbone.Model.extend({ var Conversation = Whisper.Conversation = Backbone.Model.extend({
database: Whisper.Database, database: Whisper.Database,
storeName: 'conversations', storeName: 'conversations',
@ -57,7 +42,6 @@
}, },
sendMessage: function(message, attachments) { sendMessage: function(message, attachments) {
return encodeAttachments(attachments).then(function(base64_attachments) {
var timestamp = Date.now(); var timestamp = Date.now();
this.messageCollection.add({ this.messageCollection.add({
body : message, body : message,
@ -65,7 +49,7 @@
conversationId : this.id, conversationId : this.id,
conversationType : this.get('type'), conversationType : this.get('type'),
type : 'outgoing', type : 'outgoing',
attachments : base64_attachments attachments : attachments,
}).save(); }).save();
this.save({ timestamp: timestamp, this.save({ timestamp: timestamp,
@ -78,23 +62,17 @@
else { else {
return textsecure.messaging.sendMessageToGroup(this.get('groupId'), message, attachments); return textsecure.messaging.sendMessageToGroup(this.get('groupId'), message, attachments);
} }
}.bind(this)).then(function(result) {
console.log(result);
}).catch(function(error) {
console.log(error);
});
}, },
receiveMessage: function(decrypted) { receiveMessage: function(decrypted) {
var conversation = this; var conversation = this;
return encodeAttachments(decrypted.message.attachments).then(function(base64_attachments) {
var timestamp = decrypted.pushMessage.timestamp.toNumber(); var timestamp = decrypted.pushMessage.timestamp.toNumber();
var m = this.messageCollection.add({ var m = this.messageCollection.add({
body: decrypted.message.body, body: decrypted.message.body,
timestamp: timestamp, timestamp: timestamp,
conversationId: this.id, conversationId: this.id,
conversationType: this.get('type'), conversationType: this.get('type'),
attachments: base64_attachments, attachments: decrypted.message.attachments,
type: 'incoming', type: 'incoming',
sender: decrypted.pushMessage.source sender: decrypted.pushMessage.source
}); });
@ -105,7 +83,6 @@
this.save({unreadCount: this.get('unreadCount') + 1, active: true}); this.save({unreadCount: this.get('unreadCount') + 1, active: true});
return new Promise(function (resolve) { m.save().then(resolve(m)) }); return new Promise(function (resolve) { m.save().then(resolve(m)) });
}.bind(this));
}, },
fetchMessages: function(options) { fetchMessages: function(options) {

@ -21,7 +21,12 @@
var Message = Backbone.Model.extend({ var Message = Backbone.Model.extend({
database: Whisper.Database, database: Whisper.Database,
storeName: 'messages', storeName: 'messages',
defaults: function() { return { timestamp: new Date().getTime() }; }, defaults: function() {
return {
timestamp: new Date().getTime(),
attachments: []
};
},
validate: function(attributes, options) { validate: function(attributes, options) {
var required = ['timestamp', 'conversationId']; var required = ['timestamp', 'conversationId'];
var missing = _.filter(required, function(attr) { return !attributes[attr]; }); var missing = _.filter(required, function(attr) { return !attributes[attr]; });

@ -20,10 +20,9 @@ var Whisper = Whisper || {};
previewImages: function() { previewImages: function() {
this.$el.find('img').remove(); this.$el.find('img').remove();
var files = this.$input.prop('files'); var files = this.$input.prop('files');
var onload = this.addThumb.bind(this);
for (var i = 0; i < files.length; i++) { for (var i = 0; i < files.length; i++) {
var FR = new FileReader(); var FR = new FileReader();
FR.onload = onload; FR.onload = this.addThumb.bind(this);
FR.readAsDataURL(files[i]); FR.readAsDataURL(files[i]);
} }
}, },

@ -8,9 +8,11 @@ var Whisper = Whisper || {};
className: 'discussion', className: 'discussion',
itemView: Whisper.MessageView, itemView: Whisper.MessageView,
events: { events: {
'add': 'scrollToBottom' 'add': 'scrollToBottom',
'update *': 'scrollToBottom'
}, },
scrollToBottom: function() { scrollToBottom: function() {
// TODO: Avoid scrolling if user has manually scrolled up?
this.$el.scrollTop(this.el.scrollHeight); this.$el.scrollTop(this.el.scrollHeight);
}, },
addAll: function() { addAll: function() {

@ -1,8 +1,45 @@
var Whisper = Whisper || {}; /* 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 () { (function () {
'use strict'; 'use strict';
var AttachmentView = Backbone.View.extend({
tagName: "img",
encode: function () {
return new Promise(function(resolve, reject) {
var blob = new Blob([this.model.data], { type: this.model.contentType });
var FR = new FileReader();
FR.onload = function(e) {
resolve(e.target.result);
};
FR.onerror = reject;
FR.readAsDataURL(blob);
}.bind(this));
},
render: function() {
this.encode().then(function(base64) {
this.$el.attr('src', base64);
this.$el.trigger('update');
}.bind(this));
return this;
}
});
window.Whisper = window.Whisper || {};
Whisper.MessageView = Backbone.View.extend({ Whisper.MessageView = Backbone.View.extend({
tagName: "li", tagName: "li",
className: "entry", className: "entry",
@ -19,17 +56,22 @@ var Whisper = Whisper || {};
}, },
render: function() { render: function() {
this.$el.html( this.$el.html(
Mustache.render(this.template, { Mustache.render(this.template, {
message: this.model.get('body'), message: this.model.get('body'),
timestamp: moment(this.model.get('timestamp')).fromNow(), timestamp: moment(this.model.get('timestamp')).fromNow(),
attachments: this.model.get('attachments'), bubble_class: this.model.get('type') === 'outgoing' ? 'sent' : 'incoming',
bubble_class: this.model.get('type') === 'outgoing' ? 'sent' : 'incoming', sender: this.model.get('conversationType') === 'group' ? this.model.get('sender') : ''
sender: this.model.get('conversationType') === 'group' ? this.model.get('sender') : '' })
}) );
);
this.$el.find('.attachments').append(
return this; this.model.get('attachments').map(function(attachment) {
return new AttachmentView({model: attachment}).render().el;
})
);
return this;
} }
}); });

@ -64,9 +64,10 @@ li.entry.outgoing .avatar {
li.entry img { li.entry img {
max-width: 100%; max-width: 100%;
margin-top: 5px;
} }
.attachments img { .send .attachments img {
width: 30px; width: 30px;
height: 30px; height: 30px;
} }

Loading…
Cancel
Save