Add replayable network errors

Support for manual message retry.

// FREEBIE
This commit is contained in:
lilia 2015-09-22 15:52:42 -07:00
parent bc03bdbfc4
commit fbb65d1988
4 changed files with 60 additions and 2 deletions

View file

@ -9,6 +9,7 @@
var Type = {
SEND_MESSAGE: 1,
INIT_SESSION: 2,
NETWORK_REQUEST: 3,
};
window.textsecure = window.textsecure || {};
window.textsecure.replay = {
@ -58,6 +59,20 @@
OutgoingIdentityKeyError.prototype = new ReplayableError();
OutgoingIdentityKeyError.prototype.constructor = OutgoingIdentityKeyError;
function NetworkError(number, jsonData, legacy, code) {
ReplayableError.call(this, {
functionCode : Type.NETWORK_REQUEST,
args : [number, jsonData, legacy]
});
this.name = 'NetworkError';
this.message = 'Network request failed'
this.code = code;
this.number = number;
}
NetworkError.prototype = new ReplayableError();
NetworkError.prototype.constructor = NetworkError;
window.textsecure.NetworkError = NetworkError;
window.textsecure.IncomingIdentityKeyError = IncomingIdentityKeyError;
window.textsecure.OutgoingIdentityKeyError = OutgoingIdentityKeyError;
window.textsecure.ReplayableError = ReplayableError;
@ -39618,10 +39633,19 @@ MessageSender.prototype = {
});
})).then(function(jsonData) {
var legacy = (message instanceof textsecure.protobuf.DataMessage);
return this.server.sendMessages(number, jsonData, legacy);
return this.sendRequest(number, jsonData, legacy);
}.bind(this));
},
sendRequest: function(number, jsonData, legacy) {
return this.server.sendMessages(number, jsonData, legacy).catch(function(e) {
if (e.name === 'HTTPError' && e.code === -1) {
throw new NetworkError(number, jsonData, legacy);
}
throw e;
});
},
makeAttachmentPointer: function(attachment) {
if (typeof attachment !== 'object' || attachment == null) {
return Promise.resolve(undefined);
@ -40005,6 +40029,7 @@ window.textsecure = window.textsecure || {};
textsecure.MessageSender = function(url, username, password) {
var sender = new MessageSender(url, username, password);
textsecure.replay.registerFunction(sender.tryMessageAgain.bind(sender), textsecure.replay.Type.SEND_MESSAGE);
textsecure.replay.registerFunction(sender.sendRequest.bind(sender), textsecure.replay.Type.NETWORK_REQUEST);
this.sendRequestGroupSyncMessage = sender.sendRequestGroupSyncMessage .bind(sender);
this.sendRequestContactSyncMessage = sender.sendRequestContactSyncMessage.bind(sender);

View file

@ -165,6 +165,14 @@
});
},
resend: function(number) {
var error = this.getSendError(number);
if (error) {
var promise = new textsecure.ReplayableError(error).replay();
this.send(promise);
}
},
resolveConflict: function(number) {
var error = this.getKeyConflict(number);
if (error) {

View file

@ -8,6 +8,7 @@
var Type = {
SEND_MESSAGE: 1,
INIT_SESSION: 2,
NETWORK_REQUEST: 3,
};
window.textsecure = window.textsecure || {};
window.textsecure.replay = {
@ -57,6 +58,20 @@
OutgoingIdentityKeyError.prototype = new ReplayableError();
OutgoingIdentityKeyError.prototype.constructor = OutgoingIdentityKeyError;
function NetworkError(number, jsonData, legacy, code) {
ReplayableError.call(this, {
functionCode : Type.NETWORK_REQUEST,
args : [number, jsonData, legacy]
});
this.name = 'NetworkError';
this.message = 'Network request failed'
this.code = code;
this.number = number;
}
NetworkError.prototype = new ReplayableError();
NetworkError.prototype.constructor = NetworkError;
window.textsecure.NetworkError = NetworkError;
window.textsecure.IncomingIdentityKeyError = IncomingIdentityKeyError;
window.textsecure.OutgoingIdentityKeyError = OutgoingIdentityKeyError;
window.textsecure.ReplayableError = ReplayableError;

View file

@ -37,10 +37,19 @@ MessageSender.prototype = {
});
})).then(function(jsonData) {
var legacy = (message instanceof textsecure.protobuf.DataMessage);
return this.server.sendMessages(number, jsonData, legacy);
return this.sendRequest(number, jsonData, legacy);
}.bind(this));
},
sendRequest: function(number, jsonData, legacy) {
return this.server.sendMessages(number, jsonData, legacy).catch(function(e) {
if (e.name === 'HTTPError' && e.code === -1) {
throw new NetworkError(number, jsonData, legacy);
}
throw e;
});
},
makeAttachmentPointer: function(attachment) {
if (typeof attachment !== 'object' || attachment == null) {
return Promise.resolve(undefined);
@ -424,6 +433,7 @@ window.textsecure = window.textsecure || {};
textsecure.MessageSender = function(url, username, password) {
var sender = new MessageSender(url, username, password);
textsecure.replay.registerFunction(sender.tryMessageAgain.bind(sender), textsecure.replay.Type.SEND_MESSAGE);
textsecure.replay.registerFunction(sender.sendRequest.bind(sender), textsecure.replay.Type.NETWORK_REQUEST);
this.sendRequestGroupSyncMessage = sender.sendRequestGroupSyncMessage .bind(sender);
this.sendRequestContactSyncMessage = sender.sendRequestContactSyncMessage.bind(sender);