2015-01-18 15:43:25 -10:00
|
|
|
/*global $, Whisper, Backbone, textsecure, extension*/
|
2015-09-07 14:53:43 -07:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-01-18 15:43:25 -10:00
|
|
|
*/
|
|
|
|
|
2015-01-21 18:27:42 -10:00
|
|
|
// This script should only be included in background.html
|
2015-01-18 15:43:25 -10:00
|
|
|
(function () {
|
2015-02-11 02:47:50 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
|
|
|
var conversations = new Whisper.ConversationCollection();
|
2015-09-08 19:19:30 -07:00
|
|
|
var inboxCollection = new (Backbone.Collection.extend({
|
|
|
|
initialize: function() {
|
|
|
|
this.on('change:active_at', this.sort);
|
|
|
|
this.on('change:unreadCount', this.updateUnreadCount);
|
2015-02-11 02:47:50 -08:00
|
|
|
|
2015-09-08 19:19:30 -07:00
|
|
|
this.listenTo(conversations, 'add change:active_at', this.addActive);
|
|
|
|
},
|
2015-05-26 13:28:43 -07:00
|
|
|
comparator: function(model) {
|
|
|
|
return -model.get('active_at');
|
2015-09-08 19:19:30 -07:00
|
|
|
},
|
|
|
|
addActive: function(model) {
|
|
|
|
if (model.get('active_at')) {
|
|
|
|
this.add(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);
|
|
|
|
}
|
2015-05-26 13:28:43 -07:00
|
|
|
}
|
2015-09-08 19:19:30 -07:00
|
|
|
}))();
|
2015-05-26 13:28:43 -07:00
|
|
|
|
2015-09-08 19:19:30 -07:00
|
|
|
window.getInboxCollection = function() {
|
|
|
|
return inboxCollection;
|
|
|
|
};
|
2015-05-26 13:28:43 -07:00
|
|
|
|
2015-08-27 12:38:51 -07:00
|
|
|
window.ConversationController = {
|
2015-08-27 15:21:25 -07:00
|
|
|
get: function(id) {
|
|
|
|
return conversations.get(id);
|
|
|
|
},
|
2015-08-27 12:38:51 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
reject();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
updateInbox: function() {
|
2015-09-08 19:19:30 -07:00
|
|
|
conversations.fetchActive();
|
2015-08-27 12:38:51 -07:00
|
|
|
}
|
2015-07-15 14:08:58 -07:00
|
|
|
};
|
2015-05-26 13:28:43 -07:00
|
|
|
|
2015-08-27 12:38:51 -07:00
|
|
|
ConversationController.updateInbox();
|
2015-05-26 13:28:43 -07:00
|
|
|
setUnreadCount(storage.get("unreadCount", 0));
|
|
|
|
|
|
|
|
function setUnreadCount(count) {
|
|
|
|
if (count > 0) {
|
|
|
|
extension.navigator.setBadgeText(count);
|
|
|
|
} else {
|
|
|
|
extension.navigator.setBadgeText("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 15:06:21 -07:00
|
|
|
window.notifyConversation = function(message) {
|
2015-05-24 16:19:33 -07:00
|
|
|
var conversationId = message.get('conversationId');
|
2015-09-08 19:19:30 -07:00
|
|
|
var conversation = ConversationController.get(conversationId);
|
|
|
|
if (!conversation) {
|
|
|
|
conversation = conversations.create({id: conversationId});
|
|
|
|
conversation.fetch();
|
|
|
|
}
|
2015-08-26 16:10:44 -07:00
|
|
|
if (inboxOpened) {
|
2015-09-14 13:47:47 -07:00
|
|
|
conversation.trigger('newmessages');
|
2015-08-26 16:10:44 -07:00
|
|
|
extension.windows.drawAttention(inboxWindowId);
|
2015-05-24 16:19:33 -07:00
|
|
|
} else if (Whisper.Notifications.isEnabled()) {
|
2015-09-08 19:19:30 -07:00
|
|
|
var sender = ConversationController.create({id: message.get('source')});
|
2015-03-17 15:06:21 -07:00
|
|
|
conversation.fetch().then(function() {
|
2015-03-19 16:17:26 -07:00
|
|
|
sender.fetch().then(function() {
|
2015-09-13 20:25:04 -07:00
|
|
|
sender.getNotificationIcon().then(function(iconUrl) {
|
|
|
|
Whisper.Notifications.add({
|
|
|
|
title : sender.getTitle(),
|
|
|
|
message : message.getNotificationText(),
|
|
|
|
iconUrl : iconUrl,
|
|
|
|
imageUrl : message.getImageUrl(),
|
|
|
|
conversationId: conversation.id
|
2015-09-10 00:46:50 -07:00
|
|
|
});
|
2015-03-19 16:17:26 -07:00
|
|
|
});
|
2015-03-17 15:06:21 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
2015-08-27 15:21:25 -07:00
|
|
|
openConversation(conversation);
|
2015-08-27 14:59:04 -07:00
|
|
|
ConversationController.updateInbox();
|
2015-03-17 15:06:21 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-11 02:47:50 -08:00
|
|
|
/* Inbox window controller */
|
|
|
|
var inboxOpened = false;
|
2015-05-22 17:10:01 -07:00
|
|
|
var inboxWindowId = 'inbox';
|
2015-05-12 14:01:45 -07:00
|
|
|
window.openInbox = function() {
|
2015-02-11 02:47:50 -08:00
|
|
|
if (inboxOpened === false) {
|
|
|
|
inboxOpened = true;
|
|
|
|
extension.windows.open({
|
2015-05-12 14:01:45 -07:00
|
|
|
id: 'inbox',
|
2015-02-11 02:47:50 -08:00
|
|
|
url: 'index.html',
|
|
|
|
type: 'panel',
|
2015-05-21 13:05:52 -07:00
|
|
|
frame: 'none',
|
2015-02-11 02:47:50 -08:00
|
|
|
focused: true,
|
2015-08-25 16:47:15 -07:00
|
|
|
width: 580,
|
2015-07-29 11:23:56 -07:00
|
|
|
height: 440,
|
|
|
|
minWidth: 230,
|
|
|
|
minHeight: 150
|
2015-02-11 02:47:50 -08:00
|
|
|
}, function (windowInfo) {
|
|
|
|
inboxWindowId = windowInfo.id;
|
2015-03-12 11:23:41 -07:00
|
|
|
|
2015-07-01 22:28:06 +02:00
|
|
|
windowInfo.onClosed.addListener(function () {
|
2015-08-27 15:21:25 -07:00
|
|
|
inboxOpened = false;
|
2015-07-01 22:28:06 +02:00
|
|
|
});
|
|
|
|
|
2015-03-12 11:23:41 -07:00
|
|
|
// close the panel if background.html is refreshed
|
2015-05-13 11:23:59 -07:00
|
|
|
extension.windows.beforeUnload(function() {
|
2015-03-12 11:23:41 -07:00
|
|
|
// TODO: reattach after reload instead of closing.
|
2015-07-01 22:28:06 +02:00
|
|
|
extension.windows.remove(inboxWindowId);
|
2015-03-12 11:23:41 -07:00
|
|
|
});
|
2015-02-11 02:47:50 -08:00
|
|
|
});
|
|
|
|
} else if (inboxOpened === true) {
|
2015-05-12 14:01:45 -07:00
|
|
|
extension.windows.focus(inboxWindowId, function (error) {
|
|
|
|
if (error) {
|
|
|
|
inboxOpened = false;
|
|
|
|
openInbox();
|
|
|
|
}
|
|
|
|
});
|
2015-01-21 18:27:42 -10:00
|
|
|
}
|
2015-05-12 14:01:45 -07:00
|
|
|
};
|
2015-05-15 13:54:29 -07:00
|
|
|
|
2015-09-04 18:33:14 -07:00
|
|
|
var open;
|
2015-09-13 20:25:04 -07:00
|
|
|
window.openConversation = function(conversation) {
|
2015-09-04 18:33:14 -07:00
|
|
|
if (inboxOpened === true) {
|
|
|
|
var appWindow = chrome.app.window.get(inboxWindowId);
|
|
|
|
appWindow.contentWindow.openConversation(conversation);
|
|
|
|
} else {
|
|
|
|
open = conversation;
|
|
|
|
openInbox();
|
|
|
|
}
|
2015-09-13 20:25:04 -07:00
|
|
|
};
|
2015-09-04 18:33:14 -07:00
|
|
|
window.getOpenConversation = function() {
|
|
|
|
var o = open;
|
|
|
|
open = null;
|
|
|
|
return o;
|
|
|
|
};
|
|
|
|
|
2015-05-15 13:54:29 -07:00
|
|
|
extension.onLaunched(function() {
|
2015-05-20 14:24:44 -07:00
|
|
|
storage.onready(function() {
|
|
|
|
if (textsecure.registration.isDone()) {
|
|
|
|
openInbox();
|
|
|
|
} else {
|
|
|
|
extension.install();
|
|
|
|
}
|
|
|
|
});
|
2015-05-15 13:54:29 -07:00
|
|
|
});
|
2015-01-21 18:27:42 -10:00
|
|
|
})();
|