From e023bb50c22e9fc83ca224ae55e30eaa5598fc41 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Tue, 16 Jun 2020 16:33:14 +1000 Subject: [PATCH] Implement remaining protocol changes --- SignalMessaging/contacts/OWSSyncManager.m | 2 +- .../FriendRequestProtocol.swift | 2 +- .../SessionManagementProtocol.swift | 12 +++- .../Sync Messages/SyncMessagesProtocol.swift | 6 +- .../src/Messages/OWSMessageManager.m | 56 ++++++++++--------- .../src/Messages/OWSMessageSender.m | 4 ++ 6 files changed, 50 insertions(+), 32 deletions(-) diff --git a/SignalMessaging/contacts/OWSSyncManager.m b/SignalMessaging/contacts/OWSSyncManager.m index 2eeb3c827..bbba314c7 100644 --- a/SignalMessaging/contacts/OWSSyncManager.m +++ b/SignalMessaging/contacts/OWSSyncManager.m @@ -270,7 +270,7 @@ NSString *const kSyncManagerLastContactSyncKey = @"kTSStorageManagerOWSSyncManag - (AnyPromise *)syncContact:(NSString *)hexEncodedPubKey transaction:(YapDatabaseReadTransaction *)transaction { - return [LKSyncMessagesProtocol syncContactWithHexEncodedPublicKey:hexEncodedPubKey in:transaction]; + return [LKSyncMessagesProtocol syncContactWithPublicKey:hexEncodedPubKey in:transaction]; } - (AnyPromise *)syncAllContacts diff --git a/SignalServiceKit/src/Loki/Protocol/Friend Requests/FriendRequestProtocol.swift b/SignalServiceKit/src/Loki/Protocol/Friend Requests/FriendRequestProtocol.swift index ac5bdf5f7..c0069a9e3 100644 --- a/SignalServiceKit/src/Loki/Protocol/Friend Requests/FriendRequestProtocol.swift +++ b/SignalServiceKit/src/Loki/Protocol/Friend Requests/FriendRequestProtocol.swift @@ -135,7 +135,7 @@ public final class FriendRequestProtocol : NSObject { } } - @objc(sendFriendRequestAcceptedMessageToHexEncodedPublicKey:using:) + @objc(sendFriendRequestAcceptedMessageToPublicKey:using:) public static func sendFriendRequestAcceptedMessage(to publicKey: String, using transaction: YapDatabaseReadWriteTransaction) { guard ECKeyPair.isValidHexEncodedPublicKey(candidate: publicKey) else { print("[Loki] Invalid Session ID: \(publicKey).") diff --git a/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift b/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift index 2ce1285d5..2f3979c85 100644 --- a/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift +++ b/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift @@ -178,8 +178,8 @@ public final class SessionManagementProtocol : NSObject { } } - @objc(isSessionRestoreMessage:) - public static func isSessionRestoreMessage(_ dataMessage: SSKProtoDataMessage) -> Bool { + @objc(isSessionRestorationRequest:) + public static func isSessionRestorationRequest(_ dataMessage: SSKProtoDataMessage) -> Bool { let sessionRestoreFlag = SSKProtoDataMessage.SSKProtoDataMessageFlags.sessionRestore return dataMessage.flags & UInt32(sessionRestoreFlag.rawValue) != 0 } @@ -217,6 +217,14 @@ public final class SessionManagementProtocol : NSObject { storage.setPreKeyBundle(preKeyBundle, forContact: sender, transaction: transaction) } + @objc(sendSessionEstablishedMessageToPublicKey:in:) + public static func sendSessionEstablishedMessage(to publicKey: String, in transaction: YapDatabaseReadWriteTransaction) { + let thread = TSContactThread.getOrCreateThread(withContactId: publicKey, transaction: transaction) + let ephemeralMessage = EphemeralMessage(thread: thread) + let messageSenderJobQueue = SSKEnvironment.shared.messageSenderJobQueue + messageSenderJobQueue.add(message: ephemeralMessage, transaction: transaction) + } + @objc(handleEndSessionMessageReceivedInThread:using:) public static func handleEndSessionMessageReceived(in thread: TSContactThread, using transaction: YapDatabaseReadWriteTransaction) { let publicKey = thread.contactIdentifier() diff --git a/SignalServiceKit/src/Loki/Protocol/Sync Messages/SyncMessagesProtocol.swift b/SignalServiceKit/src/Loki/Protocol/Sync Messages/SyncMessagesProtocol.swift index 3cdb56421..71ce766e5 100644 --- a/SignalServiceKit/src/Loki/Protocol/Sync Messages/SyncMessagesProtocol.swift +++ b/SignalServiceKit/src/Loki/Protocol/Sync Messages/SyncMessagesProtocol.swift @@ -40,10 +40,10 @@ public final class SyncMessagesProtocol : NSObject { } } - @objc(syncContactWithHexEncodedPublicKey:in:) - public static func syncContact(_ hexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> AnyPromise { + @objc(syncContactWithPublicKey:in:) + public static func syncContact(_ publicKey: String, in transaction: YapDatabaseReadTransaction) -> AnyPromise { let syncManager = SSKEnvironment.shared.syncManager - return syncManager.syncContacts(for: [ SignalAccount(recipientId: hexEncodedPublicKey) ]) + return syncManager.syncContacts(for: [ SignalAccount(recipientId: publicKey) ]) } @objc(syncAllContacts) diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index 853ae7d39..69d698fd9 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -423,12 +423,6 @@ NS_ASSUME_NONNULL_BEGIN envelope.timestamp); return; } - - // Loki: Ignore any friend requests from before restoration - if ([LKFriendRequestProtocol isFriendRequestFromBeforeRestoration:envelope]) { - [LKLogger print:@"[Loki] Ignoring friend request from before restoration."]; - return; - } if (envelope.content != nil) { NSError *error; @@ -438,7 +432,13 @@ NS_ASSUME_NONNULL_BEGIN return; } OWSLogInfo(@"Handling content: .", [self descriptionForContent:contentProto]); - + + // Loki: Ignore any friend requests from before restoration + if ([LKFriendRequestProtocol isFriendRequestFromBeforeRestoration:envelope]) { + [LKLogger print:@"[Loki] Ignoring friend request from before restoration."]; + return; + } + // Loki: Ignore any duplicate sync transcripts if ([LKSyncMessagesProtocol isDuplicateSyncMessage:contentProto fromHexEncodedPublicKey:envelope.source]) { return; } @@ -452,7 +452,7 @@ NS_ASSUME_NONNULL_BEGIN } // Loki: Handle session restoration request if needed - if ([LKSessionManagementProtocol isSessionRestoreMessage:contentProto.dataMessage]) { return; } // Don't process the message any further + if ([LKSessionManagementProtocol isSessionRestorationRequest:contentProto.dataMessage]) { return; } // Don't process the message any further // Loki: Handle friend request acceptance if needed [LKFriendRequestProtocol handleFriendRequestAcceptanceIfNeeded:envelope in:transaction]; @@ -1033,8 +1033,8 @@ NS_ASSUME_NONNULL_BEGIN } // Loki: Handle session reset - NSString *hexEncodedPublicKey = envelope.source; - TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:hexEncodedPublicKey transaction:transaction]; + NSString *sender = envelope.source; + TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:sender transaction:transaction]; [LKSessionManagementProtocol handleEndSessionMessageReceivedInThread:thread using:transaction]; } @@ -1434,11 +1434,10 @@ NS_ASSUME_NONNULL_BEGIN serverTimestamp:serverTimestamp wasReceivedByUD:wasReceivedByUD]; - // Loki: Parse Loki specific properties if needed - /* - if (envelope.isPtpMessage) { incomingMessage.isP2P = YES; } - */ - if (dataMessage.publicChatInfo != nil && dataMessage.publicChatInfo.hasServerID) { incomingMessage.openGroupServerMessageID = dataMessage.publicChatInfo.serverID; } + // Loki: Set open group server ID if needed + if (dataMessage.publicChatInfo != nil && dataMessage.publicChatInfo.hasServerID) { + incomingMessage.openGroupServerMessageID = dataMessage.publicChatInfo.serverID; + } NSArray *attachmentPointers = [TSAttachmentPointer attachmentPointersFromProtos:dataMessage.attachments @@ -1529,15 +1528,9 @@ NS_ASSUME_NONNULL_BEGIN serverTimestamp:serverTimestamp wasReceivedByUD:wasReceivedByUD]; - // TODO: Are we sure this works correctly with multi device? [LKSessionMetaProtocol updateDisplayNameIfNeededForHexEncodedPublicKey:incomingMessage.authorId using:dataMessage appendingShortID:YES in:transaction]; [LKSessionMetaProtocol updateProfileKeyIfNeededForHexEncodedPublicKey:thread.contactIdentifier using:dataMessage]; - // Loki: Parse Loki specific properties if needed - /* - if (envelope.isPtpMessage) { incomingMessage.isP2P = YES; } - */ - NSArray *attachmentPointers = [TSAttachmentPointer attachmentPointersFromProtos:dataMessage.attachments albumMessage:incomingMessage]; for (TSAttachmentPointer *pointer in attachmentPointers) { @@ -1545,15 +1538,28 @@ NS_ASSUME_NONNULL_BEGIN [incomingMessage.attachmentIds addObject:pointer.uniqueId]; } - // Loki: Do this before the check below + // Loki: Handle friend request if needed [LKFriendRequestProtocol handleFriendRequestMessageIfNeededFromEnvelope:envelope using:transaction]; if (body.length == 0 && attachmentPointers.count < 1 && !contact) { - OWSLogWarn(@"Ignoring empty incoming message from: %@ with timestamp: %lu.", - hexEncodedPublicKey, - (unsigned long)timestamp); + if (envelope.type == SSKProtoEnvelopeTypeFriendRequest) { + // Loki: This is needed for compatibility with refactored desktop clients + [LKSessionManagementProtocol sendSessionEstablishedMessageToPublicKey:hexEncodedPublicKey in:transaction]; + } else { + OWSLogWarn(@"Ignoring empty incoming message from: %@ with timestamp: %lu.", hexEncodedPublicKey, (unsigned long)timestamp); + } return nil; } + + // Loki: This is needed for compatibility with refactored desktop clients + LKFriendRequestStatus friendRequestStatus = [self.primaryStorage getFriendRequestStatusForContact:hexEncodedPublicKey transaction:transaction]; + if (friendRequestStatus == LKFriendRequestStatusNone || friendRequestStatus == LKFriendRequestStatusRequestExpired) { + [self.primaryStorage setFriendRequestStatus:LKFriendRequestStatusRequestReceived forContact:hexEncodedPublicKey transaction:transaction]; + } else if (friendRequestStatus == LKFriendRequestStatusRequestSent) { + [self.primaryStorage setFriendRequestStatus:LKFriendRequestStatusFriends forContact:hexEncodedPublicKey transaction:transaction]; + [LKFriendRequestProtocol sendFriendRequestAcceptedMessageToPublicKey:hexEncodedPublicKey using:transaction]; + [LKSyncMessagesProtocol syncContactWithPublicKey:masterHexEncodedPublicKey in:transaction]; + } // Loki: If we received a message from a contact in the last 2 minutes that wasn't P2P, then we need to ping them. // We assume this occurred because they don't have our P2P details. diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index e1966f677..281160c74 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -13,6 +13,7 @@ #import "OWSDevice.h" #import "OWSDisappearingMessagesJob.h" #import "OWSDispatch.h" +#import "OWSEndSessionMessage.h" #import "OWSError.h" #import "OWSIdentityManager.h" #import "OWSMessageServiceParams.h" @@ -1191,6 +1192,9 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; } NSDictionary *signalMessageInfo = deviceMessages.firstObject; SSKProtoEnvelopeType type = ((NSNumber *)signalMessageInfo[@"type"]).integerValue; + if ([message isKindOfClass:OWSEndSessionMessage.class]) { + type = SSKProtoEnvelopeTypeFriendRequest; + } uint64_t timestamp = message.timestamp; NSString *senderID = type == SSKProtoEnvelopeTypeUnidentifiedSender ? @"" : userHexEncodedPublicKey; uint32_t senderDeviceID = type == SSKProtoEnvelopeTypeUnidentifiedSender ? 0 : OWSDevicePrimaryDeviceId;