From f4755cb4b1f341bf54e64b1deb6f31499154462a Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 12 Jan 2014 04:07:13 -1000 Subject: [PATCH] Updates --- background.js | 13 ++++++++----- helpers.js | 38 +++++++++++++++++++++++++++++++++++++- manifest.json | 2 +- options.js | 7 +++++-- popup.html | 4 ++-- popup.js | 43 +++++++++++++++++++++++++++++++++++++------ 6 files changed, 90 insertions(+), 17 deletions(-) diff --git a/background.js b/background.js index f886ad45..0ab7c473 100644 --- a/background.js +++ b/background.js @@ -1,8 +1,11 @@ -function check_first_install() { - if (localStorage.getItem('first_install_ran')) - return; - +if (localStorage.getItem('first_install_ran')) { localStorage.setItem('first_install_ran', 1); chrome.tabs.create({url: "options.html"}); +} else { + if (isRegistrationDone()) { + subscribeToPush(function(message) { + console.log("Got message from " + message.source + ": \"" + getString(message.message)); + storeMessage(message); + }); + } } -check_first_install(); diff --git a/helpers.js b/helpers.js index 19f05044..49a22400 100644 --- a/helpers.js +++ b/helpers.js @@ -37,9 +37,13 @@ function base64DecToArr (sBase64, nBlocksSize) { /********************************* *** Type conversion utilities *** *********************************/ +var StaticByteBufferProto = new dcodeIO.ByteBuffer().__proto__; +var StaticUint8ArrayProto = new Uint8Array().__proto__; function getString(thing) { - if (thing == "[object Uint8Array]") + if (thing.__proto__ == StaticUint8ArrayProto) return String.fromCharCode.apply(null, thing); + if (thing != undefined && thing.__proto__ == StaticByteBufferProto) + return thing.toString("utf8"); return thing; } @@ -85,6 +89,8 @@ var storage = {}; storage.putEncrypted = function(key, value) { //TODO + if (value === undefined) + throw "Tried to store undefined"; localStorage.setItem("e" + key, JSON.stringify(getString(value))); } @@ -97,6 +103,8 @@ storage.getEncrypted = function(key, defaultValue) { } storage.putUnencrypted = function(key, value) { + if (value === undefined) + throw "Tried to store undefined"; localStorage.setItem("u" + key, JSON.stringify(getString(value))); } @@ -107,6 +115,34 @@ storage.getUnencrypted = function(key, defaultValue) { return JSON.parse(value); } +function registrationDone() { + storage.putUnencrypted("registration_done", ""); +} + +function isRegistrationDone() { + return storage.getUnencrypted("registration_done") !== undefined; +} + +function getMessageMap() { + return storage.getEncrypted("messageMap", {}); +} + +function storeMessage(outgoingMessageSignal) { + var messageMap = getMessageMap(); + var conversation = messageMap[outgoingMessageSignal.source]; //TODO: Also support Group message IDs here + if (conversation === undefined) { + conversation = [] + messageMap[outgoingMessageSignal.source] = conversation; + } + + conversation[conversation.length] = { message: getString(outgoingMessageSignal.message), + destinations: outgoingMessageSignal.destinations, + sender: outgoingMessageSignal.source, + timestamp: outgoingMessageSignal.timestamp.div(dcodeIO.Long.fromNumber(1000)).toNumber() }; + storage.putEncrypted("messageMap", messageMap); + chrome.runtime.sendMessage(conversation[conversation.length - 1]); +} + /******************************************* *** Utilities to manage keys/randomness *** *******************************************/ diff --git a/manifest.json b/manifest.json index 3345a1db..5f494c52 100644 --- a/manifest.json +++ b/manifest.json @@ -16,7 +16,7 @@ }, "background": { - "scripts": ["background.js"] + "pages": ["background.html"] }, "options_page": "options.html" diff --git a/options.js b/options.js index 98ffba3d..58265d2b 100644 --- a/options.js +++ b/options.js @@ -52,7 +52,10 @@ $('#init-go').click(function() { $('#verify3done').html('done'); doAjax({call: 'keys', httpType: 'PUT', do_auth: true, jsonData: keys, success_callback: function(response) { - $('#verify4done').html('done'); + $('#complete-number').html(number); + $('#verify').hide(); + $('#setup-complete').show(); + registrationDone(); }, error_callback: function(code) { alert(code); //TODO } @@ -81,7 +84,7 @@ $('#init-go').click(function() { } }); -if (storage.getUnencrypted("number_id") === undefined) { +if (!isRegistrationDone()) { $('#init-setup').show(); } else { $('#complete-number').html(storage.getUnencrypted("number_id").split(".")[0]); diff --git a/popup.html b/popup.html index 52cb756d..5acbefbd 100644 --- a/popup.html +++ b/popup.html @@ -1,12 +1,12 @@ - Inbox Send
- +
diff --git a/popup.js b/popup.js index ab5eb4a1..5f504642 100644 --- a/popup.js +++ b/popup.js @@ -1,14 +1,45 @@ $('#inbox_link').onclick = function() { - $('#inbox').style('display: none;'); $('#send').style('display: block;'); + $('#inbox').show(); + $('#send').hide(); } $('#send_link').onclick = function() { - $('#send').style('display: block;'); $('#send').style('display: none;'); + $('#inbox').hide(); + $('#send').show(); } -if (storage.getUnencrypted("number_id") === undefined) +if (storage.getUnencrypted("number_id") === undefined) { chrome.tabs.create({url: "options.html"}); -else { - subscribeToPush(function(message) { - console.log("GOT MESSAGE IN POPUP! " + message); +} else { + function fillMessages() { + var MAX_MESSAGES_PER_CONVERSATION = 4; + var MAX_CONVERSATIONS = 5; + + var conversations = []; + + var messageMap = getMessageMap(); + for (conversation in messageMap) { + var messages = messageMap[conversation]; + messages.sort(function(a, b) { return b.timestamp - a.timestamp; }); + conversations[conversations.length] = messages; + } + + conversations.sort(function(a, b) { return b[0].timestamp - a[0].timestamp }); + + var ul = $('#messages'); + ul.html(''); + for (var i = 0; i < MAX_CONVERSATIONS && i < conversations.length; i++) { + var conversation = conversations[i]; + ul.append('
  • '); + for (var j = 0; j < MAX_MESSAGES_PER_CONVERSATION && conversation.length; j++) { + ul.append(JSON.stringify(conversation[j])); + } + ul.append('
  • '); + } + } + + chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { + console.log("Got push message from background.js " + JSON.stringify(request)); + fillMessages(); }); + fillMessages(); }