Move ConversationController to its own file
Encapsulate the global conversation cache collection against accidental access, avoiding the data-clobbering bug fixed in previous commit. Also move some one-off program initialization code from panel controller to background.js // FREEBIE
This commit is contained in:
parent
8f28c3af68
commit
0017f196ef
4 changed files with 90 additions and 77 deletions
|
@ -281,6 +281,7 @@
|
||||||
|
|
||||||
<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/bimap.js"></script>
|
||||||
|
<script type="text/javascript" src="js/conversation_controller.js"></script>
|
||||||
<script type="text/javascript" src="js/panel_controller.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>
|
||||||
|
|
|
@ -18,6 +18,18 @@
|
||||||
|
|
||||||
textsecure.protocol_wrapper.startWorker();
|
textsecure.protocol_wrapper.startWorker();
|
||||||
|
|
||||||
|
ConversationController.updateInbox();
|
||||||
|
|
||||||
|
extension.onLaunched(function() {
|
||||||
|
storage.onready(function() {
|
||||||
|
if (textsecure.registration.isDone()) {
|
||||||
|
openInbox();
|
||||||
|
} else {
|
||||||
|
extension.install();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
storage.fetch();
|
storage.fetch();
|
||||||
storage.onready(function() {
|
storage.onready(function() {
|
||||||
var messageReceiver;
|
var messageReceiver;
|
||||||
|
@ -27,6 +39,8 @@
|
||||||
}
|
}
|
||||||
extension.on('registration_done', init);
|
extension.on('registration_done', init);
|
||||||
|
|
||||||
|
setUnreadCount(storage.get("unreadCount", 0));
|
||||||
|
|
||||||
window.getSocketStatus = function() {
|
window.getSocketStatus = function() {
|
||||||
if (messageReceiver) {
|
if (messageReceiver) {
|
||||||
return messageReceiver.getStatus();
|
return messageReceiver.getStatus();
|
||||||
|
|
73
js/conversation_controller.js
Normal file
73
js/conversation_controller.js
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
/*global $, Whisper, Backbone, textsecure, extension*/
|
||||||
|
/*
|
||||||
|
* vim: ts=4:sw=4:expandtab
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This script should only be included in background.html
|
||||||
|
(function () {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
window.Whisper = window.Whisper || {};
|
||||||
|
|
||||||
|
var conversations = new Whisper.ConversationCollection();
|
||||||
|
var inboxCollection = new (Backbone.Collection.extend({
|
||||||
|
initialize: function() {
|
||||||
|
this.on('change:active_at', this.sort);
|
||||||
|
this.on('change:unreadCount', this.updateUnreadCount);
|
||||||
|
|
||||||
|
this.listenTo(conversations, 'add change:active_at', this.addActive);
|
||||||
|
},
|
||||||
|
comparator: function(model) {
|
||||||
|
return -model.get('active_at');
|
||||||
|
},
|
||||||
|
addActive: function(model) {
|
||||||
|
if (model.get('active_at')) {
|
||||||
|
this.add(model);
|
||||||
|
} else {
|
||||||
|
this.remove(model);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
updateUnreadCount: function(model, count) {
|
||||||
|
var prev = model.previous('unreadCount') || 0;
|
||||||
|
if (count < prev) { // decreased
|
||||||
|
var newUnreadCount = storage.get("unreadCount", 0) - (prev - count);
|
||||||
|
setUnreadCount(newUnreadCount);
|
||||||
|
storage.put("unreadCount", newUnreadCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))();
|
||||||
|
|
||||||
|
window.getInboxCollection = function() {
|
||||||
|
return inboxCollection;
|
||||||
|
};
|
||||||
|
|
||||||
|
window.ConversationController = {
|
||||||
|
get: function(id) {
|
||||||
|
return conversations.get(id);
|
||||||
|
},
|
||||||
|
create: function(attrs) {
|
||||||
|
var conversation = conversations.add(attrs);
|
||||||
|
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);
|
||||||
|
} else {
|
||||||
|
reject();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
updateInbox: function() {
|
||||||
|
conversations.fetchActive();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})();
|
|
@ -9,78 +9,13 @@
|
||||||
|
|
||||||
window.Whisper = window.Whisper || {};
|
window.Whisper = window.Whisper || {};
|
||||||
|
|
||||||
var conversations = new Whisper.ConversationCollection();
|
window.setUnreadCount = function(count) {
|
||||||
var inboxCollection = new (Backbone.Collection.extend({
|
|
||||||
initialize: function() {
|
|
||||||
this.on('change:active_at', this.sort);
|
|
||||||
this.on('change:unreadCount', this.updateUnreadCount);
|
|
||||||
|
|
||||||
this.listenTo(conversations, 'add change:active_at', this.addActive);
|
|
||||||
},
|
|
||||||
comparator: function(model) {
|
|
||||||
return -model.get('active_at');
|
|
||||||
},
|
|
||||||
addActive: function(model) {
|
|
||||||
if (model.get('active_at')) {
|
|
||||||
this.add(model);
|
|
||||||
} else {
|
|
||||||
this.remove(model);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
updateUnreadCount: function(model, count) {
|
|
||||||
var prev = model.previous('unreadCount') || 0;
|
|
||||||
if (count < prev) { // decreased
|
|
||||||
var newUnreadCount = storage.get("unreadCount", 0) - (prev - count);
|
|
||||||
setUnreadCount(newUnreadCount);
|
|
||||||
storage.put("unreadCount", newUnreadCount);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}))();
|
|
||||||
|
|
||||||
window.getInboxCollection = function() {
|
|
||||||
return inboxCollection;
|
|
||||||
};
|
|
||||||
|
|
||||||
window.ConversationController = {
|
|
||||||
get: function(id) {
|
|
||||||
return conversations.get(id);
|
|
||||||
},
|
|
||||||
create: function(attrs) {
|
|
||||||
var conversation = conversations.add(attrs);
|
|
||||||
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);
|
|
||||||
} else {
|
|
||||||
reject();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
updateInbox: function() {
|
|
||||||
conversations.fetchActive();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
ConversationController.updateInbox();
|
|
||||||
setUnreadCount(storage.get("unreadCount", 0));
|
|
||||||
|
|
||||||
function setUnreadCount(count) {
|
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
extension.navigator.setBadgeText(count);
|
extension.navigator.setBadgeText(count);
|
||||||
} else {
|
} else {
|
||||||
extension.navigator.setBadgeText("");
|
extension.navigator.setBadgeText("");
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
window.notifyConversation = function(message) {
|
window.notifyConversation = function(message) {
|
||||||
var conversationId = message.get('conversationId');
|
var conversationId = message.get('conversationId');
|
||||||
|
@ -176,14 +111,4 @@
|
||||||
open = null;
|
open = null;
|
||||||
return o;
|
return o;
|
||||||
};
|
};
|
||||||
|
|
||||||
extension.onLaunched(function() {
|
|
||||||
storage.onready(function() {
|
|
||||||
if (textsecure.registration.isDone()) {
|
|
||||||
openInbox();
|
|
||||||
} else {
|
|
||||||
extension.install();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Reference in a new issue