Added handling of accept and decline events.

pull/28/head
Mikunj 7 years ago
parent 9dc19044b9
commit b9e85bb141

@ -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 => { Whisper.events.on('showFriendRequest', friendRequest => {
if (appView) { if (appView) {
appView.showFriendRequest(friendRequest); appView.showFriendRequest(friendRequest);

@ -300,6 +300,7 @@
const target = this.get('to'); const target = this.get('to');
const status = this.get('status') || 'pending'; const status = this.get('status') || 'pending';
const type = this.get('requestType') || 'incoming'; const type = this.get('requestType') || 'incoming';
const conversation = this.getConversation();
//TODO: Not sure how we go about confirming and deleting message on server side //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 // 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, { await window.Signal.Data.saveMessage(this.attributes, {
Message: Whisper.Message, Message: Whisper.Message,
}); });
window.Whisper.events.trigger('friendRequestUpdated', {
pubKey: conversation.id,
...this.attributes,
});
}; };
const onDecline = async () => { const onDecline = async () => {
@ -315,6 +321,11 @@
await window.Signal.Data.saveMessage(this.attributes, { await window.Signal.Data.saveMessage(this.attributes, {
Message: Whisper.Message, Message: Whisper.Message,
}); });
window.Whisper.events.trigger('friendRequestUpdated', {
pubKey: conversation.id,
...this.attributes,
});
}; };
return { return {

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

@ -836,21 +836,20 @@ MessageReceiver.prototype.extend({
} }
}); });
}, },
async promptUserToAcceptFriendRequest(pubKey, message) { promptUserToAcceptFriendRequest(pubKey, message) {
pubKey = pubKey.slice(0, 30) + '...'; // pubKey = pubKey.slice(0, 30) + '...';
let p = new Promise(resolve => {
window.Whisper.events.trigger('showFriendRequest', { window.Whisper.events.trigger('showFriendRequest', {
pubKey, pubKey,
message, message,
accept: () => {
resolve(true);
},
decline: () => {
resolve(false);
},
});
}); });
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) { async innerHandleContentMessage(envelope, plaintext) {
const content = textsecure.protobuf.Content.decode(plaintext); const content = textsecure.protobuf.Content.decode(plaintext);
@ -862,20 +861,23 @@ MessageReceiver.prototype.extend({
conversation = ConversationController.get(envelope.source); conversation = ConversationController.get(envelope.source);
} catch (e) {} } catch (e) {}
if (!conversation) { if (!conversation) {
const accepted = await this.promptUserToAcceptFriendRequest( this.promptUserToAcceptFriendRequest(
envelope.source, envelope.source,
content.dataMessage.body 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) { if (content.preKeyBundleMessage) {
await this.handlePreKeyBundleMessage( await this.handlePreKeyBundleMessage(
envelope, envelope,
@ -1458,6 +1460,8 @@ textsecure.MessageReceiver = function MessageReceiverWrapper(
); );
this.getStatus = messageReceiver.getStatus.bind(messageReceiver); this.getStatus = messageReceiver.getStatus.bind(messageReceiver);
this.close = messageReceiver.close.bind(messageReceiver); this.close = messageReceiver.close.bind(messageReceiver);
this.onFriendRequestUpdate = messageReceiver.onFriendRequestUpdate.bind(messageReceiver);
messageReceiver.connect(); messageReceiver.connect();
}; };

Loading…
Cancel
Save