Send contact sync message when becoming friends with contact and auto accept/send friend requests when receiving contact sync

pull/606/head
Beaudan Brown 6 years ago
parent 5529deb019
commit 332cd30052

@ -1239,6 +1239,14 @@
await conversation.setSecondaryStatus(true); await conversation.setSecondaryStatus(true);
} }
if (conversation.isFriendRequestStatusNone()) {
// Will be replaced with automatic friend request
libloki.api.sendBackgroundMessage(conversation.id);
} else {
// Accept any pending friend requests if there are any
conversation.onAcceptFriendRequest({ fromContactSync: true });
}
if (details.profileKey) { if (details.profileKey) {
const profileKey = window.Signal.Crypto.arrayBufferToBase64( const profileKey = window.Signal.Crypto.arrayBufferToBase64(
details.profileKey details.profileKey

@ -774,7 +774,8 @@
}); });
} }
}, },
async setFriendRequestStatus(newStatus) { async setFriendRequestStatus(newStatus, options = {}) {
const { fromContactSync } = options;
// Ensure that the new status is a valid FriendStatusEnum value // Ensure that the new status is a valid FriendStatusEnum value
if (!(newStatus in Object.values(FriendRequestStatusEnum))) { if (!(newStatus in Object.values(FriendRequestStatusEnum))) {
return; return;
@ -791,6 +792,10 @@
Conversation: Whisper.Conversation, Conversation: Whisper.Conversation,
}); });
await this.updateTextInputState(); await this.updateTextInputState();
if (!fromContactSync && newStatus === FriendRequestStatusEnum.friends) {
// Sync contact
this.wrapSend(textsecure.messaging.sendContactSyncMessage(this));
}
} }
}, },
async respondToAllFriendRequests(options) { async respondToAllFriendRequests(options) {
@ -837,12 +842,12 @@
await window.libloki.storage.removeContactPreKeyBundle(this.id); await window.libloki.storage.removeContactPreKeyBundle(this.id);
}, },
// We have accepted an incoming friend request // We have accepted an incoming friend request
async onAcceptFriendRequest() { async onAcceptFriendRequest(options = {}) {
if (this.unlockTimer) { if (this.unlockTimer) {
clearTimeout(this.unlockTimer); clearTimeout(this.unlockTimer);
} }
if (this.hasReceivedFriendRequest()) { if (this.hasReceivedFriendRequest()) {
this.setFriendRequestStatus(FriendRequestStatusEnum.friends); this.setFriendRequestStatus(FriendRequestStatusEnum.friends, options);
await this.respondToAllFriendRequests({ await this.respondToAllFriendRequests({
response: 'accepted', response: 'accepted',
direction: 'incoming', direction: 'incoming',

@ -114,11 +114,7 @@
result.reset(); result.reset();
return result; return result;
} }
async function createContactSyncProtoMessage() { async function createContactSyncProtoMessage(conversations) {
const conversations = await window.Signal.Data.getConversationsWithFriendStatus(
window.friends.friendRequestStatusEnum.friends,
{ ConversationCollection: Whisper.ConversationCollection }
);
// Extract required contacts information out of conversations // Extract required contacts information out of conversations
const rawContacts = conversations.map(conversation => { const rawContacts = conversations.map(conversation => {
const profile = conversation.getLokiProfile(); const profile = conversation.getLokiProfile();
@ -186,7 +182,11 @@
profile, profile,
}); });
// Attach contact list // Attach contact list
const syncMessage = await createContactSyncProtoMessage(); const conversations = await window.Signal.Data.getConversationsWithFriendStatus(
window.friends.friendRequestStatusEnum.friends,
{ ConversationCollection: Whisper.ConversationCollection }
);
const syncMessage = await createContactSyncProtoMessage(conversations);
const content = new textsecure.protobuf.Content({ const content = new textsecure.protobuf.Content({
pairingAuthorisation, pairingAuthorisation,
dataMessage, dataMessage,

@ -563,6 +563,33 @@ MessageSender.prototype = {
return Promise.resolve(); return Promise.resolve();
}, },
async sendContactSyncMessage(contactConversation) {
const primaryDeviceKey = window.storage.get('primaryDevicePubKey');
const allOurDevices = (await libloki.storage.getAllDevicePubKeysForPrimaryPubKey(
primaryDeviceKey
))
// Don't send to ourselves
.filter(pubKey => pubKey !== textsecure.storage.user.getNumber());
if (allOurDevices.length === 0) {
return Promise.resolve();
}
const syncMessage = await libloki.api.createContactSyncProtoMessage([
contactConversation,
]);
const contentMessage = new textsecure.protobuf.Content();
contentMessage.syncMessage = syncMessage;
const silent = true;
return this.sendIndividualProto(
primaryDeviceKey,
contentMessage,
Date.now(),
silent,
{} // options
);
},
sendRequestContactSyncMessage(options) { sendRequestContactSyncMessage(options) {
const myNumber = textsecure.storage.user.getNumber(); const myNumber = textsecure.storage.user.getNumber();
const myDevice = textsecure.storage.user.getDeviceId(); const myDevice = textsecure.storage.user.getDeviceId();
@ -1160,6 +1187,7 @@ textsecure.MessageSender = function MessageSenderWrapper(username, password) {
this.sendRequestContactSyncMessage = sender.sendRequestContactSyncMessage.bind( this.sendRequestContactSyncMessage = sender.sendRequestContactSyncMessage.bind(
sender sender
); );
this.sendContactSyncMessage = sender.sendContactSyncMessage.bind(sender);
this.sendRequestConfigurationSyncMessage = sender.sendRequestConfigurationSyncMessage.bind( this.sendRequestConfigurationSyncMessage = sender.sendRequestConfigurationSyncMessage.bind(
sender sender
); );

Loading…
Cancel
Save