Added handling of accept and decline events.

This commit is contained in:
Mikunj 2018-11-09 15:28:33 +11:00
parent 9dc19044b9
commit b9e85bb141
4 changed files with 54 additions and 33 deletions

View file

@ -560,7 +560,7 @@
} else {
appView.openStandalone();
}
Whisper.events.on('showDebugLog', () => {
appView.openDebugLog();
});
@ -596,6 +596,14 @@
}
});
// Gets called when a user accepts or declines a friend request
Whisper.events.on('friendRequestUpdated', friendRequest => {
const { pubKey, ...message } = friendRequest;
if (messageReceiver) {
messageReceiver.onFriendRequestUpdate(pubKey, message);
}
})
Whisper.events.on('showFriendRequest', friendRequest => {
if (appView) {
appView.showFriendRequest(friendRequest);

View file

@ -300,6 +300,7 @@
const target = this.get('to');
const status = this.get('status') || 'pending';
const type = this.get('requestType') || 'incoming';
const conversation = this.getConversation();
//TODO: Not sure how we go about confirming and deleting message on server side
// I.e do we send a network request from the model? or call a function in the conversation to send the new status
@ -308,6 +309,11 @@
await window.Signal.Data.saveMessage(this.attributes, {
Message: Whisper.Message,
});
window.Whisper.events.trigger('friendRequestUpdated', {
pubKey: conversation.id,
...this.attributes,
});
};
const onDecline = async () => {
@ -315,6 +321,11 @@
await window.Signal.Data.saveMessage(this.attributes, {
Message: Whisper.Message,
});
window.Whisper.events.trigger('friendRequestUpdated', {
pubKey: conversation.id,
...this.attributes,
});
};
return {

View file

@ -178,16 +178,14 @@
});
}
},
showFriendRequest({ pubKey, message, accept, decline }) {
const dialog = new Whisper.ConfirmationDialogView({
title: `${pubKey} sent you a friend request:`,
message,
okText: 'Accept',
cancelText: 'Decline',
resolve: accept,
reject: decline,
});
this.el.append(dialog.el);
async showFriendRequest({ pubKey, message }) {
const controller = window.ConversationController;
const conversation = await controller.getOrCreateAndWait(pubKey, 'private');
if (conversation) {
conversation.addFriendRequest(message, 'incoming');
}
this.openConversation(conversation);
},
});
})();

View file

@ -836,21 +836,20 @@ MessageReceiver.prototype.extend({
}
});
},
async promptUserToAcceptFriendRequest(pubKey, message) {
pubKey = pubKey.slice(0, 30) + '...';
let p = new Promise(resolve => {
window.Whisper.events.trigger('showFriendRequest', {
pubKey,
message,
accept: () => {
resolve(true);
},
decline: () => {
resolve(false);
},
});
promptUserToAcceptFriendRequest(pubKey, message) {
// pubKey = pubKey.slice(0, 30) + '...';
window.Whisper.events.trigger('showFriendRequest', {
pubKey,
message,
});
return await p;
},
// A handler function for when a friend request is accepted or declined
onFriendRequestUpdate(pubKey, message) {
if (!message || !message.requestType || !message.status) return;
if (message.requestType === 'incoming' && message.status === 'accepted') {
libloki.sendEmptyMessageWithPreKeys(pubKey);
}
console.log(`Friend request for ${pubKey} was ${message.status}`, message);
},
async innerHandleContentMessage(envelope, plaintext) {
const content = textsecure.protobuf.Content.decode(plaintext);
@ -862,20 +861,23 @@ MessageReceiver.prototype.extend({
conversation = ConversationController.get(envelope.source);
} catch (e) {}
if (!conversation) {
const accepted = await this.promptUserToAcceptFriendRequest(
this.promptUserToAcceptFriendRequest(
envelope.source,
content.dataMessage.body
);
if (accepted) {
// send our own prekeys as a response - no need to wait
libloki.sendEmptyMessageWithPreKeys(envelope.source);
} else {
console.log('friend request declined!');
return;
}
return;
// if (accepted) {
// // send our own prekeys as a response - no need to wait
// libloki.sendEmptyMessageWithPreKeys(envelope.source);
// } else {
// console.log('friend request declined!');
// return;
// }
}
}
//TODO: Check with sacha if this code needs to be called after friend request is accepted
if (content.preKeyBundleMessage) {
await this.handlePreKeyBundleMessage(
envelope,
@ -1458,6 +1460,8 @@ textsecure.MessageReceiver = function MessageReceiverWrapper(
);
this.getStatus = messageReceiver.getStatus.bind(messageReceiver);
this.close = messageReceiver.close.bind(messageReceiver);
this.onFriendRequestUpdate = messageReceiver.onFriendRequestUpdate.bind(messageReceiver);
messageReceiver.connect();
};