From d7ada7566037b2695da06f8fcfc99b00955c81e2 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Fri, 23 Aug 2019 10:27:22 +1000 Subject: [PATCH] Fix date not being parsed properly. Fix message indicators on groups. Fix receipts being sent in groups. --- Signal/src/AppDelegate.m | 2 +- .../src/Loki/API/LokiGroupChatAPI.swift | 16 +++++++++++++--- .../src/Messages/OWSReadReceiptManager.m | 5 ++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index f29d76ebb..29ba4f2d5 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -1493,7 +1493,7 @@ static NSTimeInterval launchStartedAt; if (isPublicChatSetUp) { return; } NSString *title = NSLocalizedString(@"Loki Public Chat", @""); NSData *groupID = [[[LKGroupChatAPI.serverURL stringByAppendingString:@"."] stringByAppendingString:@(LKGroupChatAPI.publicChatID).stringValue] dataUsingEncoding:NSUTF8StringEncoding]; - TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:title memberIds:@[ OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey ] image:nil groupId:groupID]; + TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:title memberIds:@[ OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey, LKGroupChatAPI.serverURL ] image:nil groupId:groupID]; __block TSGroupThread *thread; [OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { thread = [TSGroupThread getOrCreateThreadWithGroupModel:group transaction:transaction]; diff --git a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift index 5de157785..e99e69eae 100644 --- a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift @@ -33,12 +33,18 @@ public final class LokiGroupChatAPI : NSObject { let url = URL(string: "\(serverURL)/loki/v1/get_challenge?pubKey=\(userHexEncodedPublicKey)")! let request = TSRequest(url: url) return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }.map { rawResponse in - guard let json = rawResponse as? JSON, let cipherText64 = json["cipherText64"] as? String, let serverPubKey64 = json["serverPubKey64"] as? String, let cipherText = Data(base64Encoded: cipherText64), let serverPubKey = Data(base64Encoded: serverPubKey64) else { + guard let json = rawResponse as? JSON, let cipherText64 = json["cipherText64"] as? String, let serverPubKey64 = json["serverPubKey64"] as? String, let cipherText = Data(base64Encoded: cipherText64), var serverPubKey = Data(base64Encoded: serverPubKey64) else { throw Error.tokenParsingFailed } + // If we have length 33 then the pubkey is prefixed with 05, so we need to remove a byte + if (serverPubKey.count == 33) { + let hex = serverPubKey.hexadecimalString + serverPubKey = Data.data(fromHex: hex.substring(from: 2))! + } + // Cipher text has the 16 bit iv prepended to it - guard let tokenData = try? DiffieHellman.decrypt(cipherText: cipherText, publicKey: serverPubKey, privateKey: identityKeyPair.privateKey), let token = String(bytes: tokenData, encoding: .utf8) else { + guard let tokenData = try? DiffieHellman.decrypt(cipherText: cipherText, publicKey: serverPubKey, privateKey: identityKeyPair.privateKey), let token = String(bytes: tokenData, encoding: .utf8), token.count > 0 else { throw Error.tokenDecryptionFailed } @@ -95,7 +101,11 @@ public final class LokiGroupChatAPI : NSObject { request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ] let displayName = userDisplayName return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }.map { rawResponse in - guard let json = rawResponse as? JSON, let message = json["data"] as? JSON, let serverID = message["id"] as? UInt, let body = message["text"] as? String, let dateAsString = message["created_at"] as? String, let date = ISO8601DateFormatter().date(from: dateAsString) else { + // ISO8601DateFormatter doesn't support milliseconds for iOS 10 and below + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" + + guard let json = rawResponse as? JSON, let message = json["data"] as? JSON, let serverID = message["id"] as? UInt, let body = message["text"] as? String, let dateAsString = message["created_at"] as? String, let date = dateFormatter.date(from: dateAsString) else { print("[Loki] Couldn't parse messages for group chat with ID: \(group) from: \(rawResponse).") throw Error.messageParsingFailed } diff --git a/SignalServiceKit/src/Messages/OWSReadReceiptManager.m b/SignalServiceKit/src/Messages/OWSReadReceiptManager.m index e08fe371f..779d319fe 100644 --- a/SignalServiceKit/src/Messages/OWSReadReceiptManager.m +++ b/SignalServiceKit/src/Messages/OWSReadReceiptManager.m @@ -290,7 +290,10 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE OWSLogVerbose(@"Ignoring read receipt for self-sender."); return; } - + + // Don't enqueue message for group threads + if (message.thread.isGroupThread) { return; } + if ([self areReadReceiptsEnabled]) { OWSLogVerbose(@"Enqueuing read receipt for sender."); [self.outgoingReceiptManager enqueueReadReceiptForEnvelope:messageAuthorId timestamp:message.timestamp];