Fix stuck pending messages state

Refactor outgoing message error handling to use the same success and
error handlers. This creates a somewhat strange pattern, where we call
send and pass in the promise that resolves when sending is complete, but
there's enough variety in the libtextsecure syntax for different message
sending routines that it belongs at the conversation level and only the
post-processing stuff is really shared by all messages.

// FREEBIE
pull/749/head
lilia 10 years ago
parent f9ce27f2c8
commit 8453424ebd

@ -98,50 +98,20 @@
else { else {
sendFunc = textsecure.messaging.sendMessageToGroup; sendFunc = textsecure.messaging.sendMessageToGroup;
} }
sendFunc(this.get('id'), body, attachments, now).then(function() { message.send(sendFunc(this.get('id'), body, attachments, now));
message.save({'sent': true});
}.bind(this)).catch(function(errors) {
if (errors instanceof Error) {
errors = [errors];
}
var keyErrors = [];
_.each(errors, function(e) {
console.log(e);
console.log(e.stack);
if (e.error.name === 'OutgoingIdentityKeyError') {
keyErrors.push(e.error);
}
});
if (keyErrors.length) {
message.save({ errors : keyErrors });
} else {
if (!(errors instanceof Array)) {
errors = [errors];
}
errors.map(function(e) {
if (e.error && e.error.stack) {
console.error(e.error.stack);
}
});
throw errors;
}
});
}, },
endSession: function() { endSession: function() {
if (this.isPrivate()) { if (this.isPrivate()) {
var now = Date.now(); var now = Date.now();
var message = this.messageCollection.add({ var message = this.messageCollection.create({
conversationId : this.id, conversationId : this.id,
type : 'outgoing', type : 'outgoing',
sent_at : now, sent_at : now,
received_at : now, received_at : now,
flags : textsecure.protobuf.DataMessage.Flags.END_SESSION flags : textsecure.protobuf.DataMessage.Flags.END_SESSION
}); });
message.save(); message.send(textsecure.messaging.closeSession(this.id));
textsecure.messaging.closeSession(this.id).then(function() {
message.save({sent: true});
});
} }
}, },
@ -154,40 +124,32 @@
group_update = this.pick(['name', 'avatar', 'members']); group_update = this.pick(['name', 'avatar', 'members']);
} }
var now = Date.now(); var now = Date.now();
var message = this.messageCollection.add({ var message = this.messageCollection.create({
conversationId : this.id, conversationId : this.id,
type : 'outgoing', type : 'outgoing',
sent_at : now, sent_at : now,
received_at : now, received_at : now,
group_update : group_update group_update : group_update
}); });
message.save(); message.send(textsecure.messaging.updateGroup(
textsecure.messaging.updateGroup(
this.id, this.id,
this.get('name'), this.get('name'),
this.get('avatar'), this.get('avatar'),
this.get('members') this.get('members')
).catch(function(errors) { ));
message.save({errors: errors.map(function(e){return e.error;})});
}).then(function() {
message.save({sent: true});
});
}, },
leaveGroup: function() { leaveGroup: function() {
var now = Date.now(); var now = Date.now();
if (this.get('type') === 'group') { if (this.get('type') === 'group') {
var message = this.messageCollection.add({ var message = this.messageCollection.create({
group_update: { left: 'You' }, group_update: { left: 'You' },
conversationId : this.id, conversationId : this.id,
type : 'outgoing', type : 'outgoing',
sent_at : now, sent_at : now,
received_at : now received_at : now
}); });
message.save(); message.send(textsecure.messaging.leaveGroup(this.id));
textsecure.messaging.leaveGroup(this.id).then(function() {
message.save({sent: true});
});
} }
}, },

@ -131,6 +131,39 @@
e.number === number; e.number === number;
}); });
}, },
send: function(promise) {
return promise.then(function() {
this.save({sent: true});
}.bind(this)).catch(function(errors) {
this.save({sent: true});
if (errors instanceof Error) {
errors = [errors];
}
var keyErrors = [];
_.each(errors, function(e) {
console.log(e);
console.log(e.stack);
if (e.error.name === 'OutgoingIdentityKeyError') {
keyErrors.push(e.error);
}
});
if (keyErrors.length) {
message.save({ errors : keyErrors });
} else {
if (!(errors instanceof Array)) {
errors = [errors];
}
errors.map(function(e) {
if (e.error && e.error.stack) {
console.error(e.error.stack);
}
});
throw errors;
}
}.bind(this));
},
resolveConflict: function(number) { resolveConflict: function(number) {
var error = this.getKeyConflict(number); var error = this.getKeyConflict(number);
if (error) { if (error) {

Loading…
Cancel
Save