More PR suggestions, now recover from early exit before friend request is sent

pull/63/head
Beaudan 6 years ago
parent 2de01d0343
commit 91a8a82e39

@ -257,6 +257,8 @@
conversation.updateLastMessage(), conversation.updateLastMessage(),
conversation.updateProfile(), conversation.updateProfile(),
conversation.updateProfileAvatar(), conversation.updateProfileAvatar(),
conversation.resetPendingSend(),
conversation.updateProfile(),
]); ]);
}); });
await Promise.all(promises); await Promise.all(promises);

@ -43,12 +43,14 @@
const FriendRequestStatusEnum = Object.freeze({ const FriendRequestStatusEnum = Object.freeze({
// New conversation, no messages sent or received // New conversation, no messages sent or received
none: 0, none: 0,
// This state is used to lock the input early while sending
pendingSend: 1,
// Friend request sent, awaiting response // Friend request sent, awaiting response
requestSent: 1, requestSent: 2,
// Friend request received, awaiting user input // Friend request received, awaiting user input
requestReceived: 2, requestReceived: 3,
// We did it! // We did it!
friends: 3, friends: 4,
}); });
const COLORS = [ const COLORS = [
@ -435,10 +437,16 @@
isNone() { isNone() {
return this.get('friendRequestStatus') === FriendRequestStatusEnum.none; return this.get('friendRequestStatus') === FriendRequestStatusEnum.none;
}, },
hasInputBlocked() {
const status = this.get('friendRequestStatus');
return status === FriendRequestStatusEnum.requestSent ||
status === FriendRequestStatusEnum.requestReceived ||
status === FriendRequestStatusEnum.pendingSend;
},
isPending() { isPending() {
const status = this.get('friendRequestStatus'); const status = this.get('friendRequestStatus');
return status === FriendRequestStatusEnum.requestSent || return status === FriendRequestStatusEnum.requestSent ||
status === FriendRequestStatusEnum.requestSent; status === FriendRequestStatusEnum.requestReceived;
}, },
hasSentFriendRequest() { hasSentFriendRequest() {
return this.get('friendRequestStatus') === FriendRequestStatusEnum.requestSent; return this.get('friendRequestStatus') === FriendRequestStatusEnum.requestSent;
@ -455,6 +463,7 @@
this.trigger('disable:input', false); this.trigger('disable:input', false);
this.trigger('change:placeholder', 'friend-request'); this.trigger('change:placeholder', 'friend-request');
return; return;
case FriendRequestStatusEnum.pendingSend:
case FriendRequestStatusEnum.requestReceived: case FriendRequestStatusEnum.requestReceived:
case FriendRequestStatusEnum.requestSent: case FriendRequestStatusEnum.requestSent:
this.trigger('disable:input', true); this.trigger('disable:input', true);
@ -469,6 +478,9 @@
} }
}, },
async setFriendRequestStatus(newStatus) { async setFriendRequestStatus(newStatus) {
// Ensure that the new status is a valid FriendStatusEnum value
if (!Object.values(FriendRequestStatusEnum).some(v => v === newStatus))
return;
if (this.get('friendRequestStatus') !== newStatus) { if (this.get('friendRequestStatus') !== newStatus) {
this.set({ friendRequestStatus: newStatus }); this.set({ friendRequestStatus: newStatus });
await window.Signal.Data.updateConversation(this.id, this.attributes, { await window.Signal.Data.updateConversation(this.id, this.attributes, {
@ -494,6 +506,11 @@
}) })
); );
}, },
async resetPendingSend() {
if (this.get('friendRequestStatus') === FriendRequestStatusEnum.pendingSend) {
await this.setFriendRequestStatus(FriendRequestStatusEnum.none);
}
},
// We have declined an incoming friend request // We have declined an incoming friend request
async onDeclineFriendRequest() { async onDeclineFriendRequest() {
this.setFriendRequestStatus(FriendRequestStatusEnum.none); this.setFriendRequestStatus(FriendRequestStatusEnum.none);
@ -949,7 +966,7 @@
async sendMessage(body, attachments, quote) { async sendMessage(body, attachments, quote) {
// Input should be blocked if there is a pending friend request // Input should be blocked if there is a pending friend request
if (this.isPending()) if (this.hasInputBlocked())
return; return;
const destination = this.id; const destination = this.id;
const expireTimer = this.get('expireTimer'); const expireTimer = this.get('expireTimer');
@ -1007,6 +1024,7 @@
// because one of them was sent successfully // because one of them was sent successfully
if (friendRequestSent) return null; if (friendRequestSent) return null;
} }
await this.setFriendRequestStatus(FriendRequestStatusEnum.pendingSend);
// Send the friend request! // Send the friend request!
messageWithSchema = await upgradeMessageSchema({ messageWithSchema = await upgradeMessageSchema({
@ -1089,7 +1107,8 @@
expireTimer, expireTimer,
profileKey, profileKey,
options options
) ),
message.isFriendRequest()
) )
) )
); );
@ -1097,11 +1116,13 @@
return true; return true;
}); });
}, },
wrapSend(promise) { wrapSend(promise, isFriendRequest = false) {
return promise.then( return promise.then(
async result => { async result => {
// success // success
if (result) { if (result) {
if (isFriendRequest)
this.onFriendRequestSent();
await this.handleMessageSendResult( await this.handleMessageSendResult(
result.failoverNumbers, result.failoverNumbers,
result.unidentifiedDeliveries result.unidentifiedDeliveries
@ -1903,12 +1924,12 @@
}, },
notify(message) { notify(message) {
if (message.isOutgoing()) return Promise.resolve();
if (message.isFriendRequest()) { if (message.isFriendRequest()) {
if (this.hasSentFriendRequest()) if (this.hasSentFriendRequest())
return this.notifyFriendRequest(message.get('source'), 'accepted') return this.notifyFriendRequest(message.get('source'), 'accepted')
return this.notifyFriendRequest(message.get('source'), 'requested'); return this.notifyFriendRequest(message.get('source'), 'requested');
} }
if (!message.isIncoming()) return Promise.resolve();
const conversationId = this.id; const conversationId = this.id;
return ConversationController.getOrCreateAndWait( return ConversationController.getOrCreateAndWait(

@ -280,7 +280,7 @@ OutgoingMessage.prototype = {
const address = new libsignal.SignalProtocolAddress(number, deviceId); const address = new libsignal.SignalProtocolAddress(number, deviceId);
const ourKey = textsecure.storage.user.getNumber(); const ourKey = textsecure.storage.user.getNumber();
const options = {}; const options = {};
const fallBackEncryption = new libloki.FallBackSessionCipher(address); const fallBackCipher = new libloki.FallBackSessionCipher(address);
// Check if we need to attach the preKeys // Check if we need to attach the preKeys
let sessionCipher; let sessionCipher;
@ -288,7 +288,7 @@ OutgoingMessage.prototype = {
// Encrypt them with the fallback // Encrypt them with the fallback
this.message.preKeyBundleMessage = await libloki.getPreKeyBundleForNumber(number); this.message.preKeyBundleMessage = await libloki.getPreKeyBundleForNumber(number);
window.log.info('attaching prekeys to outgoing message'); window.log.info('attaching prekeys to outgoing message');
sessionCipher = fallBackEncryption; sessionCipher = fallBackCipher;
} else { } else {
sessionCipher = new libsignal.SessionCipher( sessionCipher = new libsignal.SessionCipher(
textsecure.storage.protocol, textsecure.storage.protocol,
@ -453,13 +453,10 @@ OutgoingMessage.prototype = {
log.info('Fallback encryption enabled'); log.info('Fallback encryption enabled');
this.fallBackEncryption = true; this.fallBackEncryption = true;
} }
if (this.fallBackEncryption && conversation) {
await conversation.onFriendRequestSent();
}
}) })
.then(this.reloadDevicesAndSend(number, true)) .then(this.reloadDevicesAndSend(number, true))
.catch(error => { .catch(error => {
conversation.resetPendingSend();
if (error.message === 'Identity key changed') { if (error.message === 'Identity key changed') {
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
error = new textsecure.OutgoingIdentityKeyError( error = new textsecure.OutgoingIdentityKeyError(

Loading…
Cancel
Save