2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-05-04 06:34:13 +00:00
|
|
|
*/
|
|
|
|
|
Finish abstracting native client
Firstly, don't initialize textsecure.nativclient unless the browser
supports it. The mimetype-check trick is hewn from nacl-common.js.
Secondly, nativeclient crypto functions will all automatically wait for
the module to load before sending messages, so we needn't register any
onload callbacks outside nativeclient.js. (Previously, if you wanted to
do crypto with native client, you would have to register a call back and
wait for the module to load.) Now that the native client crypto is
encapsulated behind a nice interface, it can handle all that
onload-callback jazz internally: if the module isn't loaded when you
call a nativeclient function, return a promise that waits for the load
callback, and eventually resolves with the result of the requested
command. This removes the need for textsecure.registerOnLoadCallback.
Finally, although native client has its quirks, it's significantly
faster than the alternative (emscripten compiled js), so this commit
also lets the crypto backend use native client opportunistically, if
it's available, falling back to js if not, which should make us
compatible with older versions of chrome and chromium.
2014-11-09 01:26:20 +00:00
|
|
|
;(function() {
|
|
|
|
'use strict';
|
2016-08-09 23:11:46 +00:00
|
|
|
window.onInvalidStateError = function(e) {
|
|
|
|
console.log(e);
|
|
|
|
};
|
|
|
|
|
2016-02-03 01:55:27 +00:00
|
|
|
console.log('background page reloaded');
|
2017-04-13 19:10:42 +00:00
|
|
|
console.log('environment:', window.config.environment);
|
2015-09-25 18:10:25 +00:00
|
|
|
|
2017-07-25 01:43:35 +00:00
|
|
|
var initialLoadComplete = false;
|
2017-07-28 01:38:41 +00:00
|
|
|
window.owsDesktopApp = {};
|
2015-05-23 00:06:49 +00:00
|
|
|
|
2015-09-25 18:10:25 +00:00
|
|
|
// start a background worker for ecc
|
2017-03-08 00:54:46 +00:00
|
|
|
textsecure.startWorker('js/libsignal-protocol-worker.js');
|
2017-05-07 21:18:22 +00:00
|
|
|
Whisper.KeyChangeListener.init(textsecure.storage.protocol);
|
2017-05-22 22:24:24 +00:00
|
|
|
textsecure.storage.protocol.on('removePreKey', function() {
|
|
|
|
getAccountManager().refreshPreKeys();
|
|
|
|
});
|
2015-09-01 23:56:17 +00:00
|
|
|
|
2017-04-13 17:47:30 +00:00
|
|
|
var SERVER_URL = window.config.serverUrl;
|
2016-09-08 21:59:37 +00:00
|
|
|
var SERVER_PORTS = [80, 4433, 8443];
|
2015-09-22 00:05:19 +00:00
|
|
|
var messageReceiver;
|
|
|
|
window.getSocketStatus = function() {
|
|
|
|
if (messageReceiver) {
|
|
|
|
return messageReceiver.getStatus();
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
};
|
2017-04-12 00:50:13 +00:00
|
|
|
Whisper.events = _.clone(Backbone.Events);
|
2017-02-14 19:27:59 +00:00
|
|
|
var accountManager;
|
2015-09-01 19:57:02 +00:00
|
|
|
window.getAccountManager = function() {
|
2017-02-14 19:27:59 +00:00
|
|
|
if (!accountManager) {
|
|
|
|
var USERNAME = storage.get('number_id');
|
|
|
|
var PASSWORD = storage.get('password');
|
|
|
|
accountManager = new textsecure.AccountManager(
|
|
|
|
SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD
|
|
|
|
);
|
|
|
|
accountManager.addEventListener('registration', function() {
|
|
|
|
if (!Whisper.Registration.everDone()) {
|
|
|
|
storage.put('safety-numbers-approval', false);
|
|
|
|
}
|
|
|
|
Whisper.Registration.markDone();
|
|
|
|
console.log("dispatching registration event");
|
2017-04-12 00:50:13 +00:00
|
|
|
Whisper.events.trigger('registration_done');
|
2017-02-14 19:27:59 +00:00
|
|
|
});
|
|
|
|
}
|
2016-09-20 20:42:33 +00:00
|
|
|
return accountManager;
|
2015-09-01 19:57:02 +00:00
|
|
|
};
|
|
|
|
|
2015-05-14 22:44:43 +00:00
|
|
|
storage.fetch();
|
2017-08-08 00:24:59 +00:00
|
|
|
|
|
|
|
// We need this 'first' check because we don't want to start the app up any other time
|
|
|
|
// than the first time. And storage.fetch() will cause onready() to fire.
|
|
|
|
var first = true;
|
2015-05-12 22:14:20 +00:00
|
|
|
storage.onready(function() {
|
2017-08-08 00:24:59 +00:00
|
|
|
if (!first) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
start();
|
|
|
|
});
|
|
|
|
|
|
|
|
window.getSyncRequest = function() {
|
|
|
|
return new textsecure.SyncRequest(textsecure.messaging, messageReceiver);
|
|
|
|
};
|
|
|
|
|
|
|
|
Whisper.events.on('shutdown', function() {
|
|
|
|
if (messageReceiver) {
|
|
|
|
messageReceiver.close().then(function() {
|
|
|
|
messageReceiver = null;
|
|
|
|
Whisper.events.trigger('shutdown-complete');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Whisper.events.trigger('shutdown-complete');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function start() {
|
2017-09-07 01:18:46 +00:00
|
|
|
ConversationController.load();
|
|
|
|
|
2016-02-13 01:18:36 +00:00
|
|
|
window.dispatchEvent(new Event('storage_ready'));
|
2015-09-22 00:05:19 +00:00
|
|
|
|
2017-02-04 05:21:18 +00:00
|
|
|
console.log("listening for registration events");
|
2017-04-12 00:50:13 +00:00
|
|
|
Whisper.events.on('registration_done', function() {
|
2017-02-04 05:21:18 +00:00
|
|
|
console.log("handling registration event");
|
2017-08-08 00:24:59 +00:00
|
|
|
connect(true);
|
2015-08-31 17:40:25 +00:00
|
|
|
});
|
2014-12-20 00:49:18 +00:00
|
|
|
|
2017-04-25 00:59:11 +00:00
|
|
|
var appView = window.owsDesktopApp.appView = new Whisper.AppView({el: $('body')});
|
2017-04-12 21:20:56 +00:00
|
|
|
|
2017-04-12 00:50:13 +00:00
|
|
|
Whisper.WallClockListener.init(Whisper.events);
|
|
|
|
Whisper.RotateSignedPreKeyListener.init(Whisper.events);
|
|
|
|
Whisper.ExpiringMessagesListener.init(Whisper.events);
|
2017-03-31 01:11:13 +00:00
|
|
|
|
2017-08-08 00:24:59 +00:00
|
|
|
if (Whisper.Import.isIncomplete()) {
|
|
|
|
console.log('Import was interrupted, showing import error screen');
|
|
|
|
appView.openImporter();
|
|
|
|
} else if (Whisper.Registration.everDone()) {
|
|
|
|
connect();
|
2017-04-12 21:20:56 +00:00
|
|
|
appView.openInbox({
|
|
|
|
initialLoadComplete: initialLoadComplete
|
|
|
|
});
|
|
|
|
} else {
|
2017-08-08 00:24:59 +00:00
|
|
|
appView.openInstallChoice();
|
2017-03-31 01:11:13 +00:00
|
|
|
}
|
2017-04-12 21:20:56 +00:00
|
|
|
|
2017-07-24 14:58:20 +00:00
|
|
|
Whisper.events.on('showDebugLog', function() {
|
2017-07-24 15:09:26 +00:00
|
|
|
appView.openDebugLog();
|
2017-07-24 14:58:20 +00:00
|
|
|
});
|
2017-04-12 21:20:56 +00:00
|
|
|
Whisper.events.on('unauthorized', function() {
|
|
|
|
appView.inboxView.networkStatusView.update();
|
|
|
|
});
|
|
|
|
Whisper.events.on('reconnectTimer', function() {
|
|
|
|
appView.inboxView.networkStatusView.setSocketReconnectInterval(60000);
|
|
|
|
});
|
2017-04-26 19:12:08 +00:00
|
|
|
Whisper.events.on('contactsync', function() {
|
|
|
|
if (appView.installView) {
|
|
|
|
appView.openInbox();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Whisper.events.on('contactsync:begin', function() {
|
|
|
|
if (appView.installView && appView.installView.showSync) {
|
|
|
|
appView.installView.showSync();
|
|
|
|
}
|
|
|
|
});
|
2017-04-12 21:20:56 +00:00
|
|
|
|
2017-09-01 18:30:41 +00:00
|
|
|
Whisper.Notifications.on('click', function(conversation) {
|
2017-04-28 01:21:52 +00:00
|
|
|
showWindow();
|
2017-09-01 18:30:41 +00:00
|
|
|
if (conversation) {
|
|
|
|
appView.openConversation(conversation);
|
|
|
|
} else {
|
|
|
|
appView.openInbox({
|
|
|
|
initialLoadComplete: initialLoadComplete
|
|
|
|
});
|
|
|
|
}
|
2017-04-25 00:59:11 +00:00
|
|
|
});
|
2017-08-08 00:24:59 +00:00
|
|
|
}
|
2015-08-31 17:40:25 +00:00
|
|
|
|
2016-06-16 22:33:18 +00:00
|
|
|
window.getSyncRequest = function() {
|
|
|
|
return new textsecure.SyncRequest(textsecure.messaging, messageReceiver);
|
|
|
|
};
|
|
|
|
|
Full export, migration banner, and full migration workflow - behind flag (#1342)
* Add support for backup and restore
This first pass works for all stores except messages, pending some scaling
improvements.
// FREEBIE
* Import of messages and attachments
Properly sanitize filenames. Logging information that will help with
debugging but won't threaten privacy (no contact or group names),
where the on-disk directories have this information to make things
human-readable
FREEBIE
* First fully operational single-action export and import!
FREEBIE
* Add migration export flow
A banner alert leads to a blocking ui for the migration. We close the socket and
wait for incoming messages to drain before starting the export.
FREEBIE
* A number of updates for the export flow
1. We don't immediately pop the directory selection dialog box, instead
showing an explicit 'choose directory' button after explaining what is
about to happen
2. We show a 'submit debug log' button on most steps of the process
3. We handle export errors and encourage the user to double-check their
filesystem then submit their log
4. We are resilient to restarts during the process
5. We handle the user cancelling out of the directory selection dialog
differently from other errors.
6. The export process is now serialized: non-messages, then messages.
7. After successful export, show where the data is on disk
FREEBUE
* Put migration behind a flag
FREEBIE
* Shut down websocket before proceeding with export
FREEBIE
* Add MigrationView to test/index.html to fix test
FREEBIE
* Remove 'Submit Debug Log' button when the export process is complete
FREEBIE
* Create a 'Signal Export' directory below user-chosen dir
This cleans things up a bit so we don't litter the user's target
directory with lots of stuff.
FREEBIE
* Clarify MessageReceiver.drain() method comments
FREEBIE
* A couple updates for clarity - event names, else handling
Also the removal of wait(), which wasn't used anywhere.
FREEBIE
* A number of wording updates for the export flow
FREEBIE
* Export complete: put dir on its own line, make text selectable
FREEBIE
2017-08-28 20:06:10 +00:00
|
|
|
Whisper.events.on('start-shutdown', function() {
|
|
|
|
if (messageReceiver) {
|
|
|
|
messageReceiver.close().then(function() {
|
|
|
|
messageReceiver = null;
|
|
|
|
Whisper.events.trigger('shutdown-complete');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Whisper.events.trigger('shutdown-complete');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-08 00:24:59 +00:00
|
|
|
function connect(firstRun) {
|
|
|
|
window.removeEventListener('online', connect);
|
2017-04-12 21:20:56 +00:00
|
|
|
|
2016-09-20 20:42:33 +00:00
|
|
|
if (!Whisper.Registration.isDone()) { return; }
|
2017-08-08 00:24:59 +00:00
|
|
|
if (Whisper.Import.isIncomplete()) { return; }
|
2014-12-20 00:49:18 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
if (messageReceiver) { messageReceiver.close(); }
|
2015-09-01 19:13:38 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
var USERNAME = storage.get('number_id');
|
|
|
|
var PASSWORD = storage.get('password');
|
|
|
|
var mySignalingKey = storage.get('signaling_key');
|
2015-09-01 19:13:38 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
// initialize the socket and start listening for messages
|
2016-04-04 03:06:27 +00:00
|
|
|
messageReceiver = new textsecure.MessageReceiver(
|
2017-03-10 23:19:40 +00:00
|
|
|
SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD, mySignalingKey
|
2016-04-04 03:06:27 +00:00
|
|
|
);
|
2015-09-22 00:05:19 +00:00
|
|
|
messageReceiver.addEventListener('message', onMessageReceived);
|
|
|
|
messageReceiver.addEventListener('receipt', onDeliveryReceipt);
|
|
|
|
messageReceiver.addEventListener('contact', onContactReceived);
|
|
|
|
messageReceiver.addEventListener('group', onGroupReceived);
|
|
|
|
messageReceiver.addEventListener('sent', onSentMessage);
|
2016-02-20 00:28:08 +00:00
|
|
|
messageReceiver.addEventListener('read', onReadReceipt);
|
2017-06-19 16:08:16 +00:00
|
|
|
messageReceiver.addEventListener('verified', onVerified);
|
2015-09-22 00:05:19 +00:00
|
|
|
messageReceiver.addEventListener('error', onError);
|
2017-07-25 01:43:35 +00:00
|
|
|
messageReceiver.addEventListener('empty', onEmpty);
|
2017-07-25 23:00:06 +00:00
|
|
|
messageReceiver.addEventListener('progress', onProgress);
|
2017-06-15 23:12:58 +00:00
|
|
|
|
2016-04-04 03:06:27 +00:00
|
|
|
window.textsecure.messaging = new textsecure.MessageSender(
|
2017-03-10 23:19:40 +00:00
|
|
|
SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD
|
2016-04-04 03:06:27 +00:00
|
|
|
);
|
2017-01-09 14:45:47 +00:00
|
|
|
|
2017-09-14 19:08:10 +00:00
|
|
|
// Because v0.43.2 introduced a bug that lost contact details, v0.43.4 introduces
|
|
|
|
// a one-time contact sync to restore all lost contact/group information. We
|
|
|
|
// disable this checking if a user is first registering.
|
|
|
|
var key = 'chrome-contact-sync-v0.43.4';
|
|
|
|
if (!storage.get(key)) {
|
|
|
|
storage.put(key, true);
|
|
|
|
|
|
|
|
if (!firstRun && textsecure.storage.user.getDeviceId() != '1') {
|
|
|
|
window.getSyncRequest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
if (firstRun === true && textsecure.storage.user.getDeviceId() != '1') {
|
2016-09-12 19:09:56 +00:00
|
|
|
if (!storage.get('theme-setting') && textsecure.storage.get('userAgent') === 'OWI') {
|
|
|
|
storage.put('theme-setting', 'ios');
|
|
|
|
}
|
2016-01-14 21:40:26 +00:00
|
|
|
var syncRequest = new textsecure.SyncRequest(textsecure.messaging, messageReceiver);
|
2017-04-13 01:43:00 +00:00
|
|
|
Whisper.events.trigger('contactsync:begin');
|
2016-01-14 21:40:26 +00:00
|
|
|
syncRequest.addEventListener('success', function() {
|
|
|
|
console.log('sync successful');
|
2016-06-16 22:33:18 +00:00
|
|
|
storage.put('synced_at', Date.now());
|
2017-04-13 01:43:00 +00:00
|
|
|
Whisper.events.trigger('contactsync');
|
2016-01-14 21:40:26 +00:00
|
|
|
});
|
|
|
|
syncRequest.addEventListener('timeout', function() {
|
|
|
|
console.log('sync timed out');
|
2017-04-13 01:43:00 +00:00
|
|
|
Whisper.events.trigger('contactsync');
|
2015-10-01 01:47:41 +00:00
|
|
|
});
|
2015-09-01 19:13:38 +00:00
|
|
|
}
|
2015-09-22 00:05:19 +00:00
|
|
|
}
|
2015-09-01 19:13:38 +00:00
|
|
|
|
2017-07-25 01:43:35 +00:00
|
|
|
function onEmpty() {
|
|
|
|
initialLoadComplete = true;
|
|
|
|
|
|
|
|
var interval = setInterval(function() {
|
2017-04-12 21:20:56 +00:00
|
|
|
var view = window.owsDesktopApp.appView;
|
2017-07-25 01:43:35 +00:00
|
|
|
if (view) {
|
|
|
|
clearInterval(interval);
|
|
|
|
interval = null;
|
|
|
|
view.onEmpty();
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
}
|
2017-07-25 23:00:06 +00:00
|
|
|
function onProgress(ev) {
|
|
|
|
var count = ev.count;
|
|
|
|
|
2017-04-12 21:20:56 +00:00
|
|
|
var view = window.owsDesktopApp.appView;
|
2017-07-25 23:00:06 +00:00
|
|
|
if (view) {
|
|
|
|
view.onProgress(count);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 01:43:35 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function onContactReceived(ev) {
|
2017-07-21 17:59:41 +00:00
|
|
|
var details = ev.contactDetails;
|
2016-12-17 12:59:07 +00:00
|
|
|
|
2017-07-21 17:59:41 +00:00
|
|
|
var id = details.number;
|
2016-12-17 12:59:07 +00:00
|
|
|
var c = new Whisper.Conversation({
|
2017-07-21 17:59:41 +00:00
|
|
|
id: id
|
2016-12-17 12:59:07 +00:00
|
|
|
});
|
2017-07-21 17:59:41 +00:00
|
|
|
var error = c.validateNumber();
|
|
|
|
if (error) {
|
|
|
|
console.log('Invalid contact received', error && error.stack ? error.stack : error);
|
|
|
|
return;
|
2016-12-17 12:59:07 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 16:10:41 +00:00
|
|
|
return ConversationController.getOrCreateAndWait(id, 'private')
|
2017-08-30 16:35:04 +00:00
|
|
|
.then(function(conversation) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
conversation.save({
|
|
|
|
name: details.name,
|
|
|
|
avatar: details.avatar,
|
|
|
|
color: details.color,
|
|
|
|
active_at: conversation.get('active_at') || Date.now(),
|
|
|
|
}).then(resolve, reject);
|
2017-09-01 14:42:41 +00:00
|
|
|
}).then(function() {
|
|
|
|
if (details.verified) {
|
|
|
|
var verified = details.verified;
|
|
|
|
var ev = new Event('verified');
|
|
|
|
ev.verified = {
|
|
|
|
state: verified.state,
|
|
|
|
destination: verified.destination,
|
|
|
|
identityKey: verified.identityKey.toArrayBuffer(),
|
|
|
|
};
|
|
|
|
ev.viaContactSync = true;
|
|
|
|
return onVerified(ev);
|
|
|
|
}
|
2017-08-30 16:35:04 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(ev.confirm)
|
|
|
|
.catch(function(error) {
|
|
|
|
console.log(
|
|
|
|
'onContactReceived error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
2017-07-21 17:59:41 +00:00
|
|
|
});
|
2015-09-22 00:05:19 +00:00
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function onGroupReceived(ev) {
|
2017-07-21 17:59:41 +00:00
|
|
|
var details = ev.groupDetails;
|
|
|
|
var id = details.id;
|
|
|
|
|
2017-09-01 16:10:41 +00:00
|
|
|
return ConversationController.getOrCreateAndWait(id, 'group').then(function(conversation) {
|
2017-07-21 17:59:41 +00:00
|
|
|
var updates = {
|
|
|
|
name: details.name,
|
|
|
|
members: details.members,
|
|
|
|
avatar: details.avatar,
|
|
|
|
type: 'group',
|
|
|
|
};
|
|
|
|
if (details.active) {
|
|
|
|
updates.active_at = Date.now();
|
|
|
|
} else {
|
|
|
|
updates.left = true;
|
|
|
|
}
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
conversation.save(updates).then(resolve, reject);
|
|
|
|
}).then(ev.confirm);
|
|
|
|
});
|
2015-09-22 00:05:19 +00:00
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function onMessageReceived(ev) {
|
|
|
|
var data = ev.data;
|
2017-07-17 22:46:00 +00:00
|
|
|
var message = initIncomingMessage(data);
|
|
|
|
|
2017-07-25 01:43:35 +00:00
|
|
|
return isMessageDuplicate(message).then(function(isDuplicate) {
|
2017-07-17 22:46:00 +00:00
|
|
|
if (isDuplicate) {
|
|
|
|
console.log('Received duplicate message', message.idForLogging());
|
|
|
|
ev.confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-01 16:10:41 +00:00
|
|
|
var type, id;
|
|
|
|
if (data.message.group) {
|
|
|
|
type = 'group';
|
|
|
|
id = data.message.group.id;
|
|
|
|
} else {
|
|
|
|
type = 'private';
|
|
|
|
id = data.source;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ConversationController.getOrCreateAndWait(id, type).then(function() {
|
|
|
|
return message.handleDataMessage(data.message, ev.confirm, {
|
|
|
|
initialLoadComplete: initialLoadComplete
|
|
|
|
});
|
2017-07-25 01:43:35 +00:00
|
|
|
});
|
2017-07-17 22:46:00 +00:00
|
|
|
});
|
2015-09-22 00:05:19 +00:00
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function onSentMessage(ev) {
|
|
|
|
var now = new Date().getTime();
|
|
|
|
var data = ev.data;
|
|
|
|
|
|
|
|
var message = new Whisper.Message({
|
|
|
|
source : textsecure.storage.user.getNumber(),
|
2017-07-17 22:46:00 +00:00
|
|
|
sourceDevice : data.device,
|
2015-09-22 00:05:19 +00:00
|
|
|
sent_at : data.timestamp,
|
|
|
|
received_at : now,
|
|
|
|
conversationId : data.destination,
|
|
|
|
type : 'outgoing',
|
2016-09-21 00:19:51 +00:00
|
|
|
sent : true,
|
|
|
|
expirationStartTimestamp: data.expirationStartTimestamp,
|
2015-09-22 00:05:19 +00:00
|
|
|
});
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2017-07-25 01:43:35 +00:00
|
|
|
return isMessageDuplicate(message).then(function(isDuplicate) {
|
2017-07-17 22:46:00 +00:00
|
|
|
if (isDuplicate) {
|
|
|
|
console.log('Received duplicate message', message.idForLogging());
|
|
|
|
ev.confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-01 16:10:41 +00:00
|
|
|
var type, id;
|
|
|
|
if (data.message.group) {
|
|
|
|
type = 'group';
|
|
|
|
id = data.message.group.id;
|
|
|
|
} else {
|
|
|
|
type = 'private';
|
|
|
|
id = data.destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ConversationController.getOrCreateAndWait(id, type).then(function() {
|
|
|
|
return message.handleDataMessage(data.message, ev.confirm, {
|
|
|
|
initialLoadComplete: initialLoadComplete
|
|
|
|
});
|
2017-07-25 01:43:35 +00:00
|
|
|
});
|
2017-07-17 22:46:00 +00:00
|
|
|
});
|
2015-09-22 00:05:19 +00:00
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2017-07-17 22:46:00 +00:00
|
|
|
function isMessageDuplicate(message) {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
var fetcher = new Whisper.Message();
|
|
|
|
var options = {
|
|
|
|
index: {
|
|
|
|
name: 'unique',
|
|
|
|
value: [
|
|
|
|
message.get('source'),
|
|
|
|
message.get('sourceDevice'),
|
|
|
|
message.get('sent_at')
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fetcher.fetch(options).always(function() {
|
|
|
|
if (fetcher.get('id')) {
|
|
|
|
return resolve(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(false);
|
|
|
|
});
|
|
|
|
}).catch(function(error) {
|
|
|
|
console.log('isMessageDuplicate error:', error && error.stack ? error.stack : error);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initIncomingMessage(data) {
|
2015-09-22 00:05:19 +00:00
|
|
|
var message = new Whisper.Message({
|
2017-07-17 22:46:00 +00:00
|
|
|
source : data.source,
|
|
|
|
sourceDevice : data.sourceDevice,
|
|
|
|
sent_at : data.timestamp,
|
2017-08-12 00:06:48 +00:00
|
|
|
received_at : data.receivedAt || Date.now(),
|
2017-07-17 22:46:00 +00:00
|
|
|
conversationId : data.source,
|
2016-02-22 05:37:47 +00:00
|
|
|
type : 'incoming',
|
2016-02-26 20:25:32 +00:00
|
|
|
unread : 1
|
2015-09-22 00:05:19 +00:00
|
|
|
});
|
2014-12-20 00:49:18 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
return message;
|
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function onError(ev) {
|
2017-08-25 21:24:16 +00:00
|
|
|
var error = ev.error;
|
|
|
|
console.log(error);
|
|
|
|
console.log(error.stack);
|
2015-06-19 22:32:25 +00:00
|
|
|
|
2017-08-25 21:24:16 +00:00
|
|
|
if (error.name === 'HTTPError' && (error.code == 401 || error.code == 403)) {
|
2016-09-20 20:42:33 +00:00
|
|
|
Whisper.Registration.remove();
|
2017-04-12 21:11:51 +00:00
|
|
|
Whisper.events.trigger('unauthorized');
|
2015-09-22 00:05:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-06-19 22:32:25 +00:00
|
|
|
|
2017-08-25 21:24:16 +00:00
|
|
|
if (error.name === 'HTTPError' && error.code == -1) {
|
2015-09-22 00:05:19 +00:00
|
|
|
// Failed to connect to server
|
|
|
|
if (navigator.onLine) {
|
|
|
|
console.log('retrying in 1 minute');
|
2017-08-08 00:24:59 +00:00
|
|
|
setTimeout(connect, 60000);
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2017-04-12 21:11:51 +00:00
|
|
|
Whisper.events.trigger('reconnectTimer');
|
2015-09-22 00:05:19 +00:00
|
|
|
} else {
|
|
|
|
console.log('offline');
|
2017-08-28 20:20:53 +00:00
|
|
|
if (messageReceiver) { messageReceiver.close(); }
|
2017-08-08 00:24:59 +00:00
|
|
|
window.addEventListener('online', connect);
|
2015-07-16 20:16:25 +00:00
|
|
|
}
|
2015-09-22 00:05:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-16 20:16:25 +00:00
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
if (ev.proto) {
|
2017-08-25 21:24:16 +00:00
|
|
|
if (error.name === 'MessageCounterError') {
|
|
|
|
if (ev.confirm) {
|
|
|
|
ev.confirm();
|
|
|
|
}
|
2015-12-04 02:37:45 +00:00
|
|
|
// Ignore this message. It is likely a duplicate delivery
|
|
|
|
// because the server lost our ack the first time.
|
|
|
|
return;
|
|
|
|
}
|
2015-09-22 00:05:19 +00:00
|
|
|
var envelope = ev.proto;
|
2017-08-08 21:03:57 +00:00
|
|
|
var message = initIncomingMessage(envelope);
|
2017-07-25 17:33:28 +00:00
|
|
|
|
2017-08-25 21:24:16 +00:00
|
|
|
return message.saveErrors(error).then(function() {
|
2017-07-25 17:33:28 +00:00
|
|
|
var id = message.get('conversationId');
|
2017-09-01 16:10:41 +00:00
|
|
|
return ConversationController.getOrCreateAndWait(id, 'private').then(function(conversation) {
|
2016-03-25 21:28:09 +00:00
|
|
|
conversation.set({
|
2015-09-22 00:05:19 +00:00
|
|
|
active_at: Date.now(),
|
|
|
|
unreadCount: conversation.get('unreadCount') + 1
|
2015-07-10 19:24:12 +00:00
|
|
|
});
|
2016-03-25 21:28:09 +00:00
|
|
|
|
|
|
|
var conversation_timestamp = conversation.get('timestamp');
|
|
|
|
var message_timestamp = message.get('timestamp');
|
|
|
|
if (!conversation_timestamp || message_timestamp > conversation_timestamp) {
|
|
|
|
conversation.set({ timestamp: message.get('sent_at') });
|
|
|
|
}
|
2017-07-25 17:41:51 +00:00
|
|
|
|
2015-11-07 22:11:13 +00:00
|
|
|
conversation.trigger('newmessage', message);
|
2017-07-25 01:43:35 +00:00
|
|
|
if (initialLoadComplete) {
|
|
|
|
conversation.notify(message);
|
|
|
|
}
|
2017-07-25 17:41:51 +00:00
|
|
|
|
2017-08-25 21:24:16 +00:00
|
|
|
if (ev.confirm) {
|
|
|
|
ev.confirm();
|
|
|
|
}
|
|
|
|
|
2017-07-25 17:41:51 +00:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
conversation.save().then(resolve, reject);
|
|
|
|
});
|
2015-09-15 21:40:37 +00:00
|
|
|
});
|
2015-09-22 00:05:19 +00:00
|
|
|
});
|
2015-05-12 22:14:20 +00:00
|
|
|
}
|
2014-12-20 00:49:18 +00:00
|
|
|
|
2017-08-25 21:24:16 +00:00
|
|
|
throw error;
|
2015-09-22 00:05:19 +00:00
|
|
|
}
|
2015-05-18 20:48:48 +00:00
|
|
|
|
2016-02-20 00:28:08 +00:00
|
|
|
function onReadReceipt(ev) {
|
2016-09-21 22:06:31 +00:00
|
|
|
var read_at = ev.timestamp;
|
|
|
|
var timestamp = ev.read.timestamp;
|
|
|
|
var sender = ev.read.sender;
|
2017-07-24 19:27:48 +00:00
|
|
|
console.log('read receipt', sender, timestamp);
|
2017-07-25 01:43:35 +00:00
|
|
|
|
2017-07-17 22:46:00 +00:00
|
|
|
var receipt = Whisper.ReadReceipts.add({
|
2016-09-21 22:06:31 +00:00
|
|
|
sender : sender,
|
|
|
|
timestamp : timestamp,
|
|
|
|
read_at : read_at
|
|
|
|
});
|
2017-07-25 01:43:35 +00:00
|
|
|
|
2017-07-17 22:46:00 +00:00
|
|
|
receipt.on('remove', ev.confirm);
|
2017-08-04 01:10:45 +00:00
|
|
|
|
|
|
|
// Calling this directly so we can wait for completion
|
|
|
|
return Whisper.ReadReceipts.onReceipt(receipt);
|
2016-02-20 00:28:08 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 16:08:16 +00:00
|
|
|
function onVerified(ev) {
|
|
|
|
var number = ev.verified.destination;
|
|
|
|
var key = ev.verified.identityKey;
|
2017-06-15 23:37:20 +00:00
|
|
|
var state;
|
|
|
|
|
2017-07-21 17:59:41 +00:00
|
|
|
var c = new Whisper.Conversation({
|
|
|
|
id: number
|
|
|
|
});
|
|
|
|
var error = c.validateNumber();
|
|
|
|
if (error) {
|
|
|
|
console.log(
|
|
|
|
'Invalid verified sync received',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-19 16:08:16 +00:00
|
|
|
switch(ev.verified.state) {
|
2017-07-21 17:59:41 +00:00
|
|
|
case textsecure.protobuf.Verified.State.DEFAULT:
|
|
|
|
state = 'DEFAULT';
|
|
|
|
break;
|
|
|
|
case textsecure.protobuf.Verified.State.VERIFIED:
|
|
|
|
state = 'VERIFIED';
|
|
|
|
break;
|
|
|
|
case textsecure.protobuf.Verified.State.UNVERIFIED:
|
|
|
|
state = 'UNVERIFIED';
|
|
|
|
break;
|
2017-06-15 23:37:20 +00:00
|
|
|
}
|
2017-06-15 23:12:58 +00:00
|
|
|
|
2017-07-11 01:25:21 +00:00
|
|
|
console.log('got verified sync for', number, state,
|
|
|
|
ev.viaContactSync ? 'via contact sync' : '');
|
2017-06-20 17:52:06 +00:00
|
|
|
|
2017-09-01 16:10:41 +00:00
|
|
|
return ConversationController.getOrCreateAndWait(number, 'private').then(function(contact) {
|
2017-07-21 17:59:41 +00:00
|
|
|
var options = {
|
|
|
|
viaSyncMessage: true,
|
|
|
|
viaContactSync: ev.viaContactSync,
|
|
|
|
key: key
|
|
|
|
};
|
2017-07-04 00:31:57 +00:00
|
|
|
|
2017-07-21 17:59:41 +00:00
|
|
|
if (state === 'VERIFIED') {
|
|
|
|
return contact.setVerified(options).then(ev.confirm);
|
|
|
|
} else if (state === 'DEFAULT') {
|
|
|
|
return contact.setVerifiedDefault(options).then(ev.confirm);
|
|
|
|
} else {
|
|
|
|
return contact.setUnverified(options).then(ev.confirm);
|
|
|
|
}
|
|
|
|
});
|
2017-06-15 23:12:58 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 00:05:19 +00:00
|
|
|
function onDeliveryReceipt(ev) {
|
|
|
|
var pushMessage = ev.proto;
|
|
|
|
var timestamp = pushMessage.timestamp.toNumber();
|
2016-04-09 07:16:21 +00:00
|
|
|
console.log(
|
|
|
|
'delivery receipt from',
|
|
|
|
pushMessage.source + '.' + pushMessage.sourceDevice,
|
|
|
|
timestamp
|
|
|
|
);
|
2015-12-09 22:43:53 +00:00
|
|
|
|
2017-07-17 22:46:00 +00:00
|
|
|
var receipt = Whisper.DeliveryReceipts.add({
|
|
|
|
timestamp: timestamp,
|
|
|
|
source: pushMessage.source
|
2015-09-22 00:05:19 +00:00
|
|
|
});
|
2017-07-25 01:43:35 +00:00
|
|
|
|
2017-07-17 22:46:00 +00:00
|
|
|
receipt.on('remove', ev.confirm);
|
2017-08-04 01:10:45 +00:00
|
|
|
|
|
|
|
// Calling this directly so we can wait for completion
|
|
|
|
return Whisper.DeliveryReceipts.onReceipt(receipt);
|
2015-09-22 00:05:19 +00:00
|
|
|
}
|
Finish abstracting native client
Firstly, don't initialize textsecure.nativclient unless the browser
supports it. The mimetype-check trick is hewn from nacl-common.js.
Secondly, nativeclient crypto functions will all automatically wait for
the module to load before sending messages, so we needn't register any
onload callbacks outside nativeclient.js. (Previously, if you wanted to
do crypto with native client, you would have to register a call back and
wait for the module to load.) Now that the native client crypto is
encapsulated behind a nice interface, it can handle all that
onload-callback jazz internally: if the module isn't loaded when you
call a nativeclient function, return a promise that waits for the load
callback, and eventually resolves with the result of the requested
command. This removes the need for textsecure.registerOnLoadCallback.
Finally, although native client has its quirks, it's significantly
faster than the alternative (emscripten compiled js), so this commit
also lets the crypto backend use native client opportunistically, if
it's available, falling back to js if not, which should make us
compatible with older versions of chrome and chromium.
2014-11-09 01:26:20 +00:00
|
|
|
})();
|