diff --git a/background.html b/background.html index 769cff1f..b473c5dc 100644 --- a/background.html +++ b/background.html @@ -282,6 +282,8 @@ + + @@ -306,8 +308,6 @@ - -
diff --git a/js/background.js b/js/background.js index 1c9f6088..df74a182 100644 --- a/js/background.js +++ b/js/background.js @@ -65,7 +65,7 @@ function onContactReceived(ev) { var contactDetails = ev.contactDetails; - new Whisper.Conversation({ + ConversationController.create({ name: contactDetails.name, id: contactDetails.number, avatar: contactDetails.avatar, @@ -75,7 +75,7 @@ function onGroupReceived(ev) { var groupDetails = ev.groupDetails; - new Whisper.Conversation({ + ConversationController.create({ id: groupDetails.id, name: groupDetails.name, members: groupDetails.members, diff --git a/js/models/conversations.js b/js/models/conversations.js index 7a78986f..7a521610 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -163,6 +163,7 @@ }, fetchMessages: function() { + if(!this.id) { return false; } return this.messageCollection.fetchConversation(this.id); }, @@ -181,6 +182,11 @@ } }, + reload: function() { + this.fetch().then(this.fetchContacts.bind(this)); + this.fetchMessages(); + }, + archive: function() { this.set({active_at: null}); }, diff --git a/js/models/messages.js b/js/models/messages.js index 6cd1fb29..f67e2ff0 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -136,7 +136,7 @@ if (dataMessage.group) { conversationId = dataMessage.group.id; } - var conversation = new Whisper.Conversation({id: conversationId}); + var conversation = ConversationController.create({id: conversationId}); conversation.fetch().always(function() { var now = new Date().getTime(); var attributes = { type: 'private' }; @@ -218,6 +218,7 @@ }); } + conversation.messageCollection.add(message); conversation.save().then(function() { message.save().then(function() { updateInbox(); diff --git a/js/panel_controller.js b/js/panel_controller.js index 4e75d05c..3a454ce4 100644 --- a/js/panel_controller.js +++ b/js/panel_controller.js @@ -43,15 +43,50 @@ } }); - window.updateInbox = function() { - conversations.fetchActive().then(function() { - inbox.reset(conversations.filter(function(model) { - return model.get('active_at'); - })); - }); + window.ConversationController = { + create: function(attrs) { + var conversation = conversations.add(attrs); + if (conversation.get('active_at')) { + inbox.add(conversation); + } + return conversation; + }, + findOrCreatePrivateById: function(id) { + var conversation = conversations.add({ id: id, type: 'private' }); + return new Promise(function(resolve, reject) { + conversation.fetch().then(function() { + resolve(conversation); + }).fail(function() { + var saved = conversation.save(); // false or indexedDBRequest + if (saved) { + saved.then(function() { + resolve(conversation); + }).fail(reject); + } + reject(); + }); + }); + }, + findById: function(id) { + var conversation = conversations.add({id: id}); + conversation.fetch(); + return conversation; + }, + updateInbox: function() { + conversations.fetchActive().then(function() { + inbox.reset(conversations.filter(function(model) { + return model.get('active_at'); + })); + }); + } }; - updateInbox(); + window.updateInbox = function() { // TODO: remove + ConversationController.updateInbox(); + }; + conversations.on('change:active_at', ConversationController.updateInbox); + + ConversationController.updateInbox(); setUnreadCount(storage.get("unreadCount", 0)); function setUnreadCount(count) { @@ -78,12 +113,6 @@ windowMap.remove('windowId', windowId); } - window.getConversation = function(attrs) { - var conversation = window.inbox.get(attrs.id) || attrs; - conversation = conversations.add(conversation); - return conversation; - }; - window.notifyConversation = function(message) { var conversationId = message.get('conversationId'); if (inboxOpened) { @@ -91,8 +120,8 @@ updateConversation(conversationId); extension.windows.drawAttention(inboxWindowId); } else if (Whisper.Notifications.isEnabled()) { - var conversation = getConversation({id: message.get('conversationId')}); - var sender = getConversation({id: message.get('source')}); + var conversation = conversations.add({id: conversationId}); + var sender = conversations.add({id: message.get('source')}); conversation.fetch().then(function() { sender.fetch().then(function() { var notification = new Notification(sender.getTitle(), { @@ -114,11 +143,8 @@ }; window.openConversation = function openConversation (modelId) { - var conversation = getConversation({id: modelId}); - conversation.fetch().then(function() { - conversation.fetchContacts(); - }); - conversation.fetchMessages(); + var conversation = conversations.add({id: modelId}); + conversation.reload(); return conversation; }; diff --git a/js/views/conversation_list_item_view.js b/js/views/conversation_list_item_view.js index 4877ccdb..acb7153b 100644 --- a/js/views/conversation_list_item_view.js +++ b/js/views/conversation_list_item_view.js @@ -35,7 +35,7 @@ select: function(e) { this.$el.addClass('selected'); - this.$el.trigger('select', {modelId: this.model.id}); + this.$el.trigger('select', {conversation: this.model}); }, render: function() { diff --git a/js/views/inbox_view.js b/js/views/inbox_view.js index 7e7aa85e..99ed0440 100644 --- a/js/views/inbox_view.js +++ b/js/views/inbox_view.js @@ -113,7 +113,8 @@ 'select .gutter .contact': 'openConversation' }, openConversation: function(e, data) { - var conversation = bg.openConversation(data.modelId); + var conversation = data.conversation; + conversation.reload(); this.conversation_stack.open(conversation); this.hideCompose(); }, diff --git a/js/views/new_conversation_view.js b/js/views/new_conversation_view.js index 0b9e201e..e2057000 100644 --- a/js/views/new_conversation_view.js +++ b/js/views/new_conversation_view.js @@ -87,27 +87,14 @@ if (this.getRecipients().length > 1) { this.createGroup(); } else { - this.createConversation(); + var id = this.getRecipients().at(0).id; + ConversationController.findOrCreatePrivateById(id).then(function(conversation) { + conversation.save('active_at', Date.now()); + this.trigger('open', { conversation: conversation }); + }.bind(this)); } }, - createConversation: function() { - var conversation = new Whisper.Conversation({ - id: this.getRecipients().at(0).id, - type: 'private' - }); - conversation.fetch().then(function() { - this.trigger('open', { modelId: conversation.id }); - }.bind(this)).fail(function() { - var saved = conversation.save(); // false or indexedDBRequest - if (saved) { - saved.then(function() { - this.trigger('open', { modelId: conversation.id }); - }.bind(this)); - } - }.bind(this)); - }, - createGroup: function() { var name = this.$('.new-group-update-form .name').val(); if (!name.trim().length) { @@ -128,9 +115,9 @@ members: members, active_at: Date.now() }; - var group = new Whisper.Conversation(attributes); + var group = ConversationController.create(attributes); group.save().then(function() { - this.trigger('open', {modelId: groupId}); + this.trigger('open', { conversation: group }); }.bind(this)); var now = Date.now(); var message = group.messageCollection.add({ diff --git a/js/views/recipients_input_view.js b/js/views/recipients_input_view.js index 3b85a55c..9e6da619 100644 --- a/js/views/recipients_input_view.js +++ b/js/views/recipients_input_view.js @@ -91,9 +91,9 @@ }) }); this.$('.contacts').append(this.typeahead_view.el); + this.initNewContact(); this.listenTo(this.typeahead, 'reset', this.filterContacts); - this.initNewContact(); }, render_attributes: function() { @@ -132,7 +132,7 @@ // Creates a view to display a new contact this.new_contact_view = new Whisper.ConversationListItemView({ el: this.$new_contact, - model: new Whisper.Conversation({ + model: ConversationController.create({ type: 'private', newContact: true }) @@ -146,7 +146,7 @@ }, addRecipient: function(e, data) { - this.recipients.add(this.typeahead.remove(data.modelId)); + this.recipients.add(this.typeahead.remove(data.conversation.id)); this.resetTypeahead(); },