Update friend request auto-acceptance for multi device

pull/63/head
Niels Andriesse 6 years ago
parent 954b4567fb
commit a1977442f0

@ -1534,13 +1534,8 @@ NS_ASSUME_NONNULL_BEGIN
} }
} }
- (void)handleFriendRequestMessageIfNeededWithEnvelope:(SSKProtoEnvelope *)envelope data:(SSKProtoDataMessage *)data message:(TSIncomingMessage *)message thread:(TSThread *)thread transaction:(YapDatabaseReadWriteTransaction *)transaction { - (BOOL)canFriendRequestBeAutoAcceptedForThread:(TSContactThread *)thread transaction:(YapDatabaseReadWriteTransaction *)transaction
if (envelope.isGroupChatMessage) { {
return NSLog(@"[Loki] Ignoring friend request in group chat.", @"");
}
if (envelope.type != SSKProtoEnvelopeTypeFriendRequest) {
return NSLog(@"[Loki] handleFriendRequestMessageIfNeededWithEnvelope:data:message:thread:transaction was called with an envelope that isn't of type SSKProtoEnvelopeTypeFriendRequest.");
}
if (thread.hasCurrentUserSentFriendRequest) { if (thread.hasCurrentUserSentFriendRequest) {
// This can happen if Alice sent Bob a friend request, Bob declined, but then Bob changed his // This can happen if Alice sent Bob a friend request, Bob declined, but then Bob changed his
// mind and sent a friend request to Alice. In this case we want Alice to auto-accept the request // mind and sent a friend request to Alice. In this case we want Alice to auto-accept the request
@ -1552,16 +1547,60 @@ NS_ASSUME_NONNULL_BEGIN
// before updating Alice's thread's friend request status to LKThreadFriendRequestStatusFriends, // before updating Alice's thread's friend request status to LKThreadFriendRequestStatusFriends,
// we can end up in a deadlock where both users' threads' friend request statuses are // we can end up in a deadlock where both users' threads' friend request statuses are
// LKThreadFriendRequestStatusRequestSent. // LKThreadFriendRequestStatusRequestSent.
return YES;
}
NSString *senderHexEncodedPublicKey = thread.contactIdentifier;
NSString *userHexEncodedPublicKey = OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey;
NSString *userMasterHexEncodedPublicKey = [LKDatabaseUtilities getMasterHexEncodedPublicKeyFor:userHexEncodedPublicKey in:transaction];
NSMutableSet<NSString *> *userLinkedDeviceHexEncodedPublicKeys = [NSMutableSet new];
NSSet<LKDeviceLink *> *userDeviceLinks = [LKDatabaseUtilities getDeviceLinksFor:userMasterHexEncodedPublicKey in:transaction];
for (LKDeviceLink *deviceLink in userDeviceLinks) {
[userLinkedDeviceHexEncodedPublicKeys addObject:deviceLink.master.hexEncodedPublicKey];
[userLinkedDeviceHexEncodedPublicKeys addObject:deviceLink.slave.hexEncodedPublicKey];
}
if ([userLinkedDeviceHexEncodedPublicKeys containsObject:senderHexEncodedPublicKey]) {
// Auto-accept any friend requests from the user's own linked devices
return YES;
}
NSString *senderMasterHexEncodedPublicKey = [LKDatabaseUtilities getMasterHexEncodedPublicKeyFor:senderHexEncodedPublicKey in:transaction];
NSMutableSet<TSContactThread *> *senderLinkedDeviceThreads = [NSMutableSet new];
NSSet<LKDeviceLink *> *senderDeviceLinks = [LKDatabaseUtilities getDeviceLinksFor:senderMasterHexEncodedPublicKey in:transaction];
for (LKDeviceLink *deviceLink in senderDeviceLinks) {
[senderLinkedDeviceThreads addObject:[TSContactThread getThreadWithContactId:deviceLink.master.hexEncodedPublicKey transaction:transaction]];
[senderLinkedDeviceThreads addObject:[TSContactThread getThreadWithContactId:deviceLink.slave.hexEncodedPublicKey transaction:transaction]];
}
if ([senderLinkedDeviceThreads contains:^BOOL(NSObject *object) {
TSContactThread *thread = (TSContactThread *)object;
return thread.isContactFriend;
}]) {
// Auto-accept if the user is friends with any of the sender's linked devices.
return YES;
}
return NO;
}
- (void)handleFriendRequestMessageIfNeededWithEnvelope:(SSKProtoEnvelope *)envelope data:(SSKProtoDataMessage *)data message:(TSIncomingMessage *)message thread:(TSContactThread *)thread transaction:(YapDatabaseReadWriteTransaction *)transaction {
if (envelope.isGroupChatMessage) {
return NSLog(@"[Loki] Ignoring friend request in group chat.", @"");
}
if (envelope.type != SSKProtoEnvelopeTypeFriendRequest) {
return NSLog(@"[Loki] handleFriendRequestMessageIfNeededWithEnvelope:data:message:thread:transaction was called with an envelope that isn't of type SSKProtoEnvelopeTypeFriendRequest.");
}
if ([self canFriendRequestBeAutoAcceptedForThread:thread transaction:transaction]) {
[thread saveFriendRequestStatus:LKThreadFriendRequestStatusFriends withTransaction:transaction]; [thread saveFriendRequestStatus:LKThreadFriendRequestStatusFriends withTransaction:transaction];
TSOutgoingMessage *existingFriendRequestMessage = (TSOutgoingMessage *)[thread.lastInteraction as:TSOutgoingMessage.class]; __block TSOutgoingMessage *existingFriendRequestMessage;
if (existingFriendRequestMessage != nil && existingFriendRequestMessage.isFriendRequest) { [thread enumerateInteractionsWithTransaction:transaction usingBlock:^(TSInteraction *interaction, YapDatabaseReadTransaction *transaction) {
if ([interaction isKindOfClass:TSOutgoingMessage.class] && ((TSOutgoingMessage *)interaction).isFriendRequest) {
existingFriendRequestMessage = (TSOutgoingMessage *)interaction;
}
}];
if (existingFriendRequestMessage != nil) {
[existingFriendRequestMessage saveFriendRequestStatus:LKMessageFriendRequestStatusAccepted withTransaction:transaction]; [existingFriendRequestMessage saveFriendRequestStatus:LKMessageFriendRequestStatusAccepted withTransaction:transaction];
} }
// The two lines below are equivalent to calling [ThreadUtil enqueueFriendRequestAcceptanceMessageInThread:thread] // The two lines below are equivalent to calling [ThreadUtil enqueueFriendRequestAcceptanceMessageInThread:thread]
LKEphemeralMessage *backgroundMessage = [[LKEphemeralMessage alloc] initInThread:thread]; LKEphemeralMessage *backgroundMessage = [[LKEphemeralMessage alloc] initInThread:thread];
[self.messageSenderJobQueue addMessage:backgroundMessage transaction:transaction]; [self.messageSenderJobQueue addMessage:backgroundMessage transaction:transaction];
} else { } else if (!thread.isContactFriend) {
if (!thread.isContactFriend) {
// Checking that the sender of the message isn't already a friend is necessary because otherwise // Checking that the sender of the message isn't already a friend is necessary because otherwise
// the following situation can occur: Alice and Bob are friends. Bob loses his database and his // the following situation can occur: Alice and Bob are friends. Bob loses his database and his
// friend request status is reset to LKThreadFriendRequestStatusNone. Bob now sends Alice a friend // friend request status is reset to LKThreadFriendRequestStatusNone. Bob now sends Alice a friend
@ -1597,7 +1636,6 @@ NS_ASSUME_NONNULL_BEGIN
// [self handleEndSessionMessageWithEnvelope:envelope dataMessage:data transaction:transaction]; // [self handleEndSessionMessageWithEnvelope:envelope dataMessage:data transaction:transaction];
} }
} }
}
- (void)handleFriendRequestAcceptanceIfNeededWithEnvelope:(SSKProtoEnvelope *)envelope transaction:(YapDatabaseReadWriteTransaction *)transaction { - (void)handleFriendRequestAcceptanceIfNeededWithEnvelope:(SSKProtoEnvelope *)envelope transaction:(YapDatabaseReadWriteTransaction *)transaction {
// If we get an envelope that isn't a friend request, then we can infer that we had to use // If we get an envelope that isn't a friend request, then we can infer that we had to use

Loading…
Cancel
Save