Accepting FRs to all devices

pull/1138/head
Vincent 5 years ago
parent 60ddaa14be
commit 5db9795076

@ -4,6 +4,7 @@
log, log,
i18n, i18n,
Backbone, Backbone,
libloki,
ConversationController, ConversationController,
MessageController, MessageController,
storage, storage,
@ -245,6 +246,12 @@
this.messageCollection.forEach(m => m.trigger('change')); this.messageCollection.forEach(m => m.trigger('change'));
}, },
async acceptFriendRequest() { async acceptFriendRequest() {
console.log(`[vince][fr] accepting friend request from conversations.js`);
console.log('[vince][fr] this.id:', this.id);
console.log(`[vince][fr] primary pubkey`, this.getPrimaryDevicePubKey());
// Friend request messages are always send to primary device conversation
const messages = await window.Signal.Data.getMessagesByConversation( const messages = await window.Signal.Data.getMessagesByConversation(
this.id, this.id,
{ {
@ -253,6 +260,24 @@
type: 'friend-request', type: 'friend-request',
} }
); );
const priamryMessages = await window.Signal.Data.getMessagesByConversation(
this.getPrimaryDevicePubKey(),
{
limit: 1,
MessageCollection: Whisper.MessageCollection,
type: 'friend-request',
}
);
console.log(`[vince][fr] messages: `, messages);
console.log(`[vince][fr] Primary messages: `, priamryMessages);
// LAST MODEL IS POINTING TO THE PRIMARY DEVICE'S LASTMODEL. NOT SECONDARY FOR A2 --> B
const lastMessageModel = messages.at(0); const lastMessageModel = messages.at(0);
if (lastMessageModel) { if (lastMessageModel) {
lastMessageModel.acceptFriendRequest(); lastMessageModel.acceptFriendRequest();
@ -970,35 +995,75 @@
}); });
}, },
async respondToAllFriendRequests(options) { async respondToAllFriendRequests(options) {
console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`);
console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`);
console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`);
console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`);
console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`);
console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`);
console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`);
console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`);
const { response, status, direction = null } = options; const { response, status, direction = null } = options;
// Ignore if no response supplied // Ignore if no response supplied
if (!response) { if (!response) {
return; return;
} }
const primaryConversation = ConversationController.get(
// const primaryConversation = ConversationController.get(
// this.getPrimaryDevicePubKey()
// );
// // Should never happen
// if (!primaryConversation) {
// return;
// }
// const pending = await primaryConversation.getFriendRequests(
// direction,
// status
// );
// await Promise.all(
// pending.map(async request => {
// if (request.hasErrors()) {
// return;
// }
// request.set({ friendStatus: response });
// await window.Signal.Data.saveMessage(request.attributes, {
// Message: Whisper.Message,
// });
// primaryConversation.trigger('updateMessage', request);
// })
// );
// Accept FRs from all the user's devices
const allDevices = await libloki.storage.getAllDevicePubKeysForPrimaryPubKey(
this.getPrimaryDevicePubKey() this.getPrimaryDevicePubKey()
); );
// Should never happen
if (!primaryConversation) { const allConversationsWithUser = allDevices.map(d => ConversationController.get(d));
return;
}
const pending = await primaryConversation.getFriendRequests( console.log(`[vince][fr] allDevices: `, allDevices);
direction, console.log('[vince][fr] allConversationsWithUser:', allConversationsWithUser);
status
);
await Promise.all(
pending.map(async request => {
if (request.hasErrors()) { if (!allDevices.length) {
return; return;
} }
request.set({ friendStatus: response }); const pendingRequests = allConversationsWithUser.map(async c => {
await window.Signal.Data.saveMessage(request.attributes, { await c.getFriendRequests(direction, status);
Message: Whisper.Message,
}); });
primaryConversation.trigger('updateMessage', request);
}) console.log(`[vince][fr] Pending requests: `, pendingRequests);
);
}, },
async respondToAllPendingFriendRequests(options) { async respondToAllPendingFriendRequests(options) {
return this.respondToAllFriendRequests({ return this.respondToAllFriendRequests({

@ -415,10 +415,18 @@
}, },
async acceptFriendRequest() { async acceptFriendRequest() {
// THIS FUNCTION RUNS WHEN "ACCEPT" BUTTON IN CHAT IS CLICKED
if (this.get('friendStatus') !== 'pending') { if (this.get('friendStatus') !== 'pending') {
return; return;
} }
const conversation = await this.getSourceDeviceConversation(); const conversation = await this.getSourceDeviceConversation();
console.log('[vince][fr] conversation:', conversation);
console.log('[vince][fr] sourceDeviceConversation():', await this.getSourceDeviceConversation());
// If we somehow received an old friend request (e.g. after having restored // If we somehow received an old friend request (e.g. after having restored
// from seed, we won't be able to accept it, we should initiate our own // from seed, we won't be able to accept it, we should initiate our own
// friend request to reset the session: // friend request to reset the session:
@ -2548,11 +2556,16 @@
// We need to map the original message source to the primary device // We need to map the original message source to the primary device
// only map to primary device if this is NOT a friend request. // only map to primary device if this is NOT a friend request.
// Otherwise you can enter a stalemate. // Otherwise you can enter a stalemate.
const conditionalSource = message.get('type') === 'friend-request'
? source // const conditionalSource = message.get('type') === 'friend-request'
: primarySource; // ? source
// : primarySource;
// if (source !== ourNumber) {
// message.set({ source: conditionalSource });
// }
if (source !== ourNumber) { if (source !== ourNumber) {
message.set({ source: conditionalSource }); message.set({ primarySource });
} }
const id = await window.Signal.Data.saveMessage(message.attributes, { const id = await window.Signal.Data.saveMessage(message.attributes, {

Loading…
Cancel
Save