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
This commit is contained in:
lilia 2015-09-28 13:33:26 -07:00
parent f9ce27f2c8
commit 8453424ebd
2 changed files with 41 additions and 46 deletions

View file

@ -98,50 +98,20 @@
else {
sendFunc = textsecure.messaging.sendMessageToGroup;
}
sendFunc(this.get('id'), body, attachments, now).then(function() {
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;
}
});
message.send(sendFunc(this.get('id'), body, attachments, now));
},
endSession: function() {
if (this.isPrivate()) {
var now = Date.now();
var message = this.messageCollection.add({
var message = this.messageCollection.create({
conversationId : this.id,
type : 'outgoing',
sent_at : now,
received_at : now,
flags : textsecure.protobuf.DataMessage.Flags.END_SESSION
});
message.save();
textsecure.messaging.closeSession(this.id).then(function() {
message.save({sent: true});
});
message.send(textsecure.messaging.closeSession(this.id));
}
},
@ -154,40 +124,32 @@
group_update = this.pick(['name', 'avatar', 'members']);
}
var now = Date.now();
var message = this.messageCollection.add({
var message = this.messageCollection.create({
conversationId : this.id,
type : 'outgoing',
sent_at : now,
received_at : now,
group_update : group_update
});
message.save();
textsecure.messaging.updateGroup(
message.send(textsecure.messaging.updateGroup(
this.id,
this.get('name'),
this.get('avatar'),
this.get('members')
).catch(function(errors) {
message.save({errors: errors.map(function(e){return e.error;})});
}).then(function() {
message.save({sent: true});
});
));
},
leaveGroup: function() {
var now = Date.now();
if (this.get('type') === 'group') {
var message = this.messageCollection.add({
var message = this.messageCollection.create({
group_update: { left: 'You' },
conversationId : this.id,
type : 'outgoing',
sent_at : now,
received_at : now
});
message.save();
textsecure.messaging.leaveGroup(this.id).then(function() {
message.save({sent: true});
});
message.send(textsecure.messaging.leaveGroup(this.id));
}
},

View file

@ -131,6 +131,39 @@
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) {
var error = this.getKeyConflict(number);
if (error) {