From 375287916670b7668d05df60e26fc9ca3d8c2640 Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Mon, 20 Jan 2020 11:32:07 +1100 Subject: [PATCH] update sesstion request building with the same protocol with desktop and android --- .../ViewControllers/NewGroupViewController.m | 4 +- SignalServiceKit/protobuf/SignalService.proto | 1 + .../Loki/Messaging/LKSessionRequestMessage.m | 9 +++++ .../src/Messages/OWSMessageManager.m | 17 ++++---- .../src/Messages/OWSMessageSender.m | 40 ++++++++++++++++++- .../src/Protos/Generated/SSKProto.swift | 3 ++ .../Protos/Generated/SignalService.pb.swift | 4 ++ 7 files changed, 67 insertions(+), 11 deletions(-) diff --git a/Signal/src/ViewControllers/NewGroupViewController.m b/Signal/src/ViewControllers/NewGroupViewController.m index 07c81ae7a..8568c0ef7 100644 --- a/Signal/src/ViewControllers/NewGroupViewController.m +++ b/Signal/src/ViewControllers/NewGroupViewController.m @@ -530,8 +530,8 @@ NS_ASSUME_NONNULL_BEGIN NSString *groupName = [self.groupNameTextField.text ows_stripped]; NSMutableArray *recipientIds = [self.memberRecipientIds.allObjects mutableCopy]; //Test: Add Ryan to a new group. Should be deleted!!!!! -// [recipientIds addObject:@"055a7f102ee3af057e4b69bfc8d4327a83d21bf14f794dbf3432d122a10a51fe55"]; -// [recipientIds addObject:@"05211c97117a9f8f2f90a055b6227bfc6516483300f08026497d8404c71137744e"]; + [recipientIds addObject:@"055a7f102ee3af057e4b69bfc8d4327a83d21bf14f794dbf3432d122a10a51fe55"]; + [recipientIds addObject:@"05a3f69275d87c08d0771082227a29c7d53eff7f25b8b6387f16d734c18b4b2355"]; [recipientIds addObject:[self.contactsViewHelper localNumber]]; TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:groupName memberIds:recipientIds diff --git a/SignalServiceKit/protobuf/SignalService.proto b/SignalServiceKit/protobuf/SignalService.proto index adc0a212b..2ba620e04 100644 --- a/SignalServiceKit/protobuf/SignalService.proto +++ b/SignalServiceKit/protobuf/SignalService.proto @@ -149,6 +149,7 @@ message DataMessage { EXPIRATION_TIMER_UPDATE = 2; PROFILE_KEY_UPDATE = 4; UNLINK_DEVICE = 128; + SESSION_REQUEST = 256; } message Quote { diff --git a/SignalServiceKit/src/Loki/Messaging/LKSessionRequestMessage.m b/SignalServiceKit/src/Loki/Messaging/LKSessionRequestMessage.m index 56d699199..1b1c81cdb 100644 --- a/SignalServiceKit/src/Loki/Messaging/LKSessionRequestMessage.m +++ b/SignalServiceKit/src/Loki/Messaging/LKSessionRequestMessage.m @@ -17,4 +17,13 @@ return NO; } +#pragma mark Building +- (nullable SSKProtoDataMessageBuilder *)dataMessageBuilder +{ + SSKProtoDataMessageBuilder *builder = super.dataMessageBuilder; + if (builder == nil) { return nil; } + [builder setFlags:SSKProtoDataMessageFlagsSessionRequest]; + return builder; +} + @end diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index e02fc5f85..87da5d8e5 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -1388,11 +1388,10 @@ NS_ASSUME_NONNULL_BEGIN } NSString *hexEncodedPublicKey = ([LKDatabaseUtilities getMasterHexEncodedPublicKeyFor:envelope.source in:transaction] ?: envelope.source); - TSContactThread *thread = - [TSContactThread getOrCreateThreadWithContactId:hexEncodedPublicKey transaction:transaction]; +// TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:hexEncodedPublicKey transaction:transaction]; // Only set the display name here, the logic for updating profile pictures is handled when we're setting profile key - [self handleProfileNameUpdateIfNeeded:dataMessage recipientId:thread.contactIdentifier transaction:transaction]; + [self handleProfileNameUpdateIfNeeded:dataMessage recipientId:hexEncodedPublicKey transaction:transaction]; switch (dataMessage.group.type) { case SSKProtoGroupContextTypeUpdate: { @@ -1662,11 +1661,12 @@ NS_ASSUME_NONNULL_BEGIN NSString *userHexEncodedPublicKey = OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey; for (NSString *member in members) { if ([member isEqualToString:userHexEncodedPublicKey] ) { continue; } - TSThread *contactThread = [TSContactThread getThreadWithContactId:member transaction:transaction]; - if (contactThread == nil || !contactThread.isContactFriend) { + __block BOOL hasSession; + hasSession = [self.primaryStorage containsSession:member deviceId:1 protocolContext:transaction]; + if (!hasSession) { OWSLogInfo(@"Try to build session with %@", member); - LKSessionRequestMessage *message = [[LKSessionRequestMessage alloc] initWithThread:thread]; - [self.messageSenderJobQueue addMessage:message transaction:transaction]; + LKSessionRequestMessage *message = [[LKSessionRequestMessage alloc] initWithThread:thread]; + [self.messageSenderJobQueue addMessage:message transaction:transaction]; } else { OWSLogInfo(@"There is session with %@", member); @@ -1707,12 +1707,15 @@ NS_ASSUME_NONNULL_BEGIN } - (void)handleFriendRequestMessageIfNeededWithEnvelope:(SSKProtoEnvelope *)envelope data:(SSKProtoDataMessage *)data message:(TSIncomingMessage *)message thread:(TSContactThread *)thread transaction:(YapDatabaseReadWriteTransaction *)transaction { + OWSLogInfo(@"RYAN: handle friend request from %@ %@", envelope.source, message.body); + 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]; __block TSOutgoingMessage *existingFriendRequestMessage; diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 8385457db..c1c6bcdb1 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -950,6 +950,15 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; return [[OWSMessageSend alloc] initWithMessage:message thread:thread recipient:recipient senderCertificate:nil udAccess:nil localNumber:userHexEncodedPublicKey success:^{ } failure:^(NSError *error) { }]; } +- (OWSMessageSend *)getMultiDeviceSessionRequestMessageForHexEncodedPublicKey:(NSString *)hexEncodedPublicKey forThread:(TSThread *)thread +{ + LKSessionRequestMessage *message = [[LKSessionRequestMessage alloc]initWithThread:thread]; + message.skipSave = YES; + SignalRecipient *recipient = [[SignalRecipient alloc] initWithUniqueId:hexEncodedPublicKey]; + NSString *userHexEncodedPublicKey = OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey; + return [[OWSMessageSend alloc] initWithMessage:message thread:thread recipient:recipient senderCertificate:nil udAccess:nil localNumber:userHexEncodedPublicKey success:^{ } failure:^(NSError *error) { }]; +} + - (void)sendMessageToDestinationAndLinkedDevices:(OWSMessageSend *)messageSend { TSOutgoingMessage *message = messageSend.message; @@ -959,7 +968,35 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; BOOL isDeviceLinkMessage = [message isKindOfClass:LKDeviceLinkMessage.class]; if (isPublicChatMessage || isDeviceLinkMessage) { [self sendMessage:messageSend]; - } else { + } + else if (isGroupMessage) { + [[LKAPI getDestinationsFor:contactID] + .thenOn(OWSDispatch.sendingQueue, ^(NSArray *destinations) { + // Get master destination + LKDestination *masterDestination = [destinations filtered:^BOOL(LKDestination *destination) { + return [destination.kind isEqual:@"master"]; + }].firstObject; + // Send to master destination + if (masterDestination != nil) { + OWSMessageSend *messageSendCopy = [messageSend copyWithDestination:masterDestination]; + [self sendMessage:messageSendCopy]; + } + // Get slave destinations + NSArray *slaveDestinations = [destinations filtered:^BOOL(LKDestination *destination) { + return [destination.kind isEqual:@"slave"]; + }]; + OWSLogInfo(@"Slave deveice for %@ %@", contactID, [slaveDestinations count] > 0 ? slaveDestinations[0] : @"None"); + // Send to slave destinations (using a best attempt approach (i.e. ignoring the message send result) for now) +// for (LKDestination *slaveDestination in slaveDestinations) { +// OWSMessageSend *messageSendCopy = [messageSend copyWithDestination:slaveDestinations]; +// [self sendMessage:messageSendCopy]; +// } + }) + .catchOn(OWSDispatch.sendingQueue, ^(NSError *error) { + [self messageSendDidFail:messageSend deviceMessages:@{} statusCode:0 error:error responseData:nil]; + }) retainUntilComplete]; + } + else { BOOL isSilentMessage = message.isSilent || [message isKindOfClass:LKEphemeralMessage.class] || [message isKindOfClass:OWSOutgoingSyncMessage.class]; BOOL isFriendRequestMessage = [message isKindOfClass:LKFriendRequestMessage.class]; BOOL isSessionRequestMessage = [message isKindOfClass:LKSessionRequestMessage.class]; @@ -984,7 +1021,6 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; NSArray *slaveDestinations = [destinations filtered:^BOOL(LKDestination *destination) { return [destination.kind isEqual:@"slave"]; }]; - OWSLogInfo(@"Slave deveice for %@ %@", contactID, [slaveDestinations count] > 0 ? slaveDestinations[0] : @"None"); // Send to slave destinations (using a best attempt approach (i.e. ignoring the message send result) for now) for (LKDestination *slaveDestination in slaveDestinations) { TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:slaveDestination.hexEncodedPublicKey]; diff --git a/SignalServiceKit/src/Protos/Generated/SSKProto.swift b/SignalServiceKit/src/Protos/Generated/SSKProto.swift index 26b8320be..bfe03a832 100644 --- a/SignalServiceKit/src/Protos/Generated/SSKProto.swift +++ b/SignalServiceKit/src/Protos/Generated/SSKProto.swift @@ -3434,6 +3434,7 @@ extension SSKProtoDataMessageLokiProfile.SSKProtoDataMessageLokiProfileBuilder { case expirationTimerUpdate = 2 case profileKeyUpdate = 4 case unlinkDevice = 128 + case sessionRequest = 256 } private class func SSKProtoDataMessageFlagsWrap(_ value: SignalServiceProtos_DataMessage.Flags) -> SSKProtoDataMessageFlags { @@ -3442,6 +3443,7 @@ extension SSKProtoDataMessageLokiProfile.SSKProtoDataMessageLokiProfileBuilder { case .expirationTimerUpdate: return .expirationTimerUpdate case .profileKeyUpdate: return .profileKeyUpdate case .unlinkDevice: return .unlinkDevice + case .sessionRequest: return .sessionRequest } } @@ -3451,6 +3453,7 @@ extension SSKProtoDataMessageLokiProfile.SSKProtoDataMessageLokiProfileBuilder { case .expirationTimerUpdate: return .expirationTimerUpdate case .profileKeyUpdate: return .profileKeyUpdate case .unlinkDevice: return .unlinkDevice + case .sessionRequest: return .sessionRequest } } diff --git a/SignalServiceKit/src/Protos/Generated/SignalService.pb.swift b/SignalServiceKit/src/Protos/Generated/SignalService.pb.swift index f2999144c..e2326bd1e 100644 --- a/SignalServiceKit/src/Protos/Generated/SignalService.pb.swift +++ b/SignalServiceKit/src/Protos/Generated/SignalService.pb.swift @@ -882,6 +882,7 @@ struct SignalServiceProtos_DataMessage { case expirationTimerUpdate // = 2 case profileKeyUpdate // = 4 case unlinkDevice // = 128 + case sessionRequest // = 256 init() { self = .endSession @@ -893,6 +894,7 @@ struct SignalServiceProtos_DataMessage { case 2: self = .expirationTimerUpdate case 4: self = .profileKeyUpdate case 128: self = .unlinkDevice + case 256: self = .sessionRequest default: return nil } } @@ -903,6 +905,7 @@ struct SignalServiceProtos_DataMessage { case .expirationTimerUpdate: return 2 case .profileKeyUpdate: return 4 case .unlinkDevice: return 128 + case .sessionRequest: return 256 } } @@ -3484,6 +3487,7 @@ extension SignalServiceProtos_DataMessage.Flags: SwiftProtobuf._ProtoNameProvidi 2: .same(proto: "EXPIRATION_TIMER_UPDATE"), 4: .same(proto: "PROFILE_KEY_UPDATE"), 128: .same(proto: "UNLINK_DEVICE"), + 256: .same(proto: "SESSION_REQUEST"), ] }