Refactor for less model duplication

This commit is contained in:
lilia 2015-08-27 12:38:51 -07:00
parent c4eac76032
commit baa55c9018
9 changed files with 71 additions and 50 deletions

View file

@ -282,6 +282,8 @@
<script type="text/javascript" src="js/models/conversations.js"></script> <script type="text/javascript" src="js/models/conversations.js"></script>
<script type="text/javascript" src="js/chromium.js"></script> <script type="text/javascript" src="js/chromium.js"></script>
<script type="text/javascript" src="js/bimap.js"></script>
<script type="text/javascript" src="js/panel_controller.js"></script>
<script type="text/javascript" src="js/views/whisper_view.js"></script> <script type="text/javascript" src="js/views/whisper_view.js"></script>
<script type="text/javascript" src="js/views/toast_view.js"></script> <script type="text/javascript" src="js/views/toast_view.js"></script>
@ -306,8 +308,6 @@
<script type="text/javascript" src="js/views/inbox_view.js"></script> <script type="text/javascript" src="js/views/inbox_view.js"></script>
<script type="text/javascript" src="js/views/confirmation_dialog_view.js"></script> <script type="text/javascript" src="js/views/confirmation_dialog_view.js"></script>
<script type="text/javascript" src="js/bimap.js"></script>
<script type="text/javascript" src="js/panel_controller.js"></script>
<script type="text/javascript" src="js/background.js"></script> <script type="text/javascript" src="js/background.js"></script>
</head> </head>
<body> <body>

View file

@ -65,7 +65,7 @@
function onContactReceived(ev) { function onContactReceived(ev) {
var contactDetails = ev.contactDetails; var contactDetails = ev.contactDetails;
new Whisper.Conversation({ ConversationController.create({
name: contactDetails.name, name: contactDetails.name,
id: contactDetails.number, id: contactDetails.number,
avatar: contactDetails.avatar, avatar: contactDetails.avatar,
@ -75,7 +75,7 @@
function onGroupReceived(ev) { function onGroupReceived(ev) {
var groupDetails = ev.groupDetails; var groupDetails = ev.groupDetails;
new Whisper.Conversation({ ConversationController.create({
id: groupDetails.id, id: groupDetails.id,
name: groupDetails.name, name: groupDetails.name,
members: groupDetails.members, members: groupDetails.members,

View file

@ -163,6 +163,7 @@
}, },
fetchMessages: function() { fetchMessages: function() {
if(!this.id) { return false; }
return this.messageCollection.fetchConversation(this.id); return this.messageCollection.fetchConversation(this.id);
}, },
@ -181,6 +182,11 @@
} }
}, },
reload: function() {
this.fetch().then(this.fetchContacts.bind(this));
this.fetchMessages();
},
archive: function() { archive: function() {
this.set({active_at: null}); this.set({active_at: null});
}, },

View file

@ -136,7 +136,7 @@
if (dataMessage.group) { if (dataMessage.group) {
conversationId = dataMessage.group.id; conversationId = dataMessage.group.id;
} }
var conversation = new Whisper.Conversation({id: conversationId}); var conversation = ConversationController.create({id: conversationId});
conversation.fetch().always(function() { conversation.fetch().always(function() {
var now = new Date().getTime(); var now = new Date().getTime();
var attributes = { type: 'private' }; var attributes = { type: 'private' };
@ -218,6 +218,7 @@
}); });
} }
conversation.messageCollection.add(message);
conversation.save().then(function() { conversation.save().then(function() {
message.save().then(function() { message.save().then(function() {
updateInbox(); updateInbox();

View file

@ -43,15 +43,50 @@
} }
}); });
window.updateInbox = function() { 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() { conversations.fetchActive().then(function() {
inbox.reset(conversations.filter(function(model) { inbox.reset(conversations.filter(function(model) {
return model.get('active_at'); 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)); setUnreadCount(storage.get("unreadCount", 0));
function setUnreadCount(count) { function setUnreadCount(count) {
@ -78,12 +113,6 @@
windowMap.remove('windowId', windowId); 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) { window.notifyConversation = function(message) {
var conversationId = message.get('conversationId'); var conversationId = message.get('conversationId');
if (inboxOpened) { if (inboxOpened) {
@ -91,8 +120,8 @@
updateConversation(conversationId); updateConversation(conversationId);
extension.windows.drawAttention(inboxWindowId); extension.windows.drawAttention(inboxWindowId);
} else if (Whisper.Notifications.isEnabled()) { } else if (Whisper.Notifications.isEnabled()) {
var conversation = getConversation({id: message.get('conversationId')}); var conversation = conversations.add({id: conversationId});
var sender = getConversation({id: message.get('source')}); var sender = conversations.add({id: message.get('source')});
conversation.fetch().then(function() { conversation.fetch().then(function() {
sender.fetch().then(function() { sender.fetch().then(function() {
var notification = new Notification(sender.getTitle(), { var notification = new Notification(sender.getTitle(), {
@ -114,11 +143,8 @@
}; };
window.openConversation = function openConversation (modelId) { window.openConversation = function openConversation (modelId) {
var conversation = getConversation({id: modelId}); var conversation = conversations.add({id: modelId});
conversation.fetch().then(function() { conversation.reload();
conversation.fetchContacts();
});
conversation.fetchMessages();
return conversation; return conversation;
}; };

View file

@ -35,7 +35,7 @@
select: function(e) { select: function(e) {
this.$el.addClass('selected'); this.$el.addClass('selected');
this.$el.trigger('select', {modelId: this.model.id}); this.$el.trigger('select', {conversation: this.model});
}, },
render: function() { render: function() {

View file

@ -113,7 +113,8 @@
'select .gutter .contact': 'openConversation' 'select .gutter .contact': 'openConversation'
}, },
openConversation: function(e, data) { openConversation: function(e, data) {
var conversation = bg.openConversation(data.modelId); var conversation = data.conversation;
conversation.reload();
this.conversation_stack.open(conversation); this.conversation_stack.open(conversation);
this.hideCompose(); this.hideCompose();
}, },

View file

@ -87,25 +87,12 @@
if (this.getRecipients().length > 1) { if (this.getRecipients().length > 1) {
this.createGroup(); this.createGroup();
} else { } 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 });
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));
} }
}.bind(this));
}, },
createGroup: function() { createGroup: function() {
@ -128,9 +115,9 @@
members: members, members: members,
active_at: Date.now() active_at: Date.now()
}; };
var group = new Whisper.Conversation(attributes); var group = ConversationController.create(attributes);
group.save().then(function() { group.save().then(function() {
this.trigger('open', {modelId: groupId}); this.trigger('open', { conversation: group });
}.bind(this)); }.bind(this));
var now = Date.now(); var now = Date.now();
var message = group.messageCollection.add({ var message = group.messageCollection.add({

View file

@ -91,9 +91,9 @@
}) })
}); });
this.$('.contacts').append(this.typeahead_view.el); this.$('.contacts').append(this.typeahead_view.el);
this.initNewContact();
this.listenTo(this.typeahead, 'reset', this.filterContacts); this.listenTo(this.typeahead, 'reset', this.filterContacts);
this.initNewContact();
}, },
render_attributes: function() { render_attributes: function() {
@ -132,7 +132,7 @@
// Creates a view to display a new contact // Creates a view to display a new contact
this.new_contact_view = new Whisper.ConversationListItemView({ this.new_contact_view = new Whisper.ConversationListItemView({
el: this.$new_contact, el: this.$new_contact,
model: new Whisper.Conversation({ model: ConversationController.create({
type: 'private', type: 'private',
newContact: true newContact: true
}) })
@ -146,7 +146,7 @@
}, },
addRecipient: function(e, data) { addRecipient: function(e, data) {
this.recipients.add(this.typeahead.remove(data.modelId)); this.recipients.add(this.typeahead.remove(data.conversation.id));
this.resetTypeahead(); this.resetTypeahead();
}, },