From 4a31a84daa783a864225aecf4ec1339b2b5724c9 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 19 Nov 2020 16:51:08 +1100 Subject: [PATCH] Fix messages showing up as failed --- Session/Configuration.swift | 1 + SessionMessagingKit/Configuration.swift | 3 +++ ...ndler.swift => MessageReceiverDelegate.swift} | 0 .../Sending & Receiving/MessageSender.swift | 6 +++++- .../MessageSenderDelegate.swift | 5 +++++ Signal.xcodeproj/project.pbxproj | 12 ++++++++---- .../Messaging/MessageSenderDelegate.swift | 7 ++++++- SignalUtilitiesKit/Messaging/TSIncomingMessage.m | 1 - SignalUtilitiesKit/Messaging/TSOutgoingMessage.h | 3 ++- SignalUtilitiesKit/Messaging/TSOutgoingMessage.m | 16 +++++++++++++++- 10 files changed, 45 insertions(+), 9 deletions(-) rename SessionMessagingKit/Sending & Receiving/{MessageHandler.swift => MessageReceiverDelegate.swift} (100%) create mode 100644 SessionMessagingKit/Sending & Receiving/MessageSenderDelegate.swift diff --git a/Session/Configuration.swift b/Session/Configuration.swift index 020a53c49..0a03a6e0b 100644 --- a/Session/Configuration.swift +++ b/Session/Configuration.swift @@ -8,6 +8,7 @@ final class Configuration : NSObject { @objc static func performMainSetup() { SNMessagingKit.configure( storage: Storage.shared, + messageSenderDelegate: MessageSenderDelegate.shared, messageReceiverDelegate: MessageReceiverDelegate.shared, signalStorage: OWSPrimaryStorage.shared(), identityKeyStore: OWSIdentityManager.shared(), diff --git a/SessionMessagingKit/Configuration.swift b/SessionMessagingKit/Configuration.swift index 0994ad56c..f514bc6aa 100644 --- a/SessionMessagingKit/Configuration.swift +++ b/SessionMessagingKit/Configuration.swift @@ -2,6 +2,7 @@ import SessionProtocolKit public struct Configuration { public let storage: SessionMessagingKitStorageProtocol + public let messageSenderDelegate: MessageSenderDelegate public let messageReceiverDelegate: MessageReceiverDelegate public let signalStorage: SessionStore & PreKeyStore & SignedPreKeyStore public let identityKeyStore: IdentityKeyStore @@ -18,6 +19,7 @@ public enum SNMessagingKit { // Just to make the external API nice public static func configure( storage: SessionMessagingKitStorageProtocol, + messageSenderDelegate: MessageSenderDelegate, messageReceiverDelegate: MessageReceiverDelegate, signalStorage: SessionStore & PreKeyStore & SignedPreKeyStore, identityKeyStore: IdentityKeyStore, @@ -29,6 +31,7 @@ public enum SNMessagingKit { // Just to make the external API nice ) { Configuration.shared = Configuration( storage: storage, + messageSenderDelegate: messageSenderDelegate, messageReceiverDelegate: messageReceiverDelegate, signalStorage: signalStorage, identityKeyStore: identityKeyStore, diff --git a/SessionMessagingKit/Sending & Receiving/MessageHandler.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiverDelegate.swift similarity index 100% rename from SessionMessagingKit/Sending & Receiving/MessageHandler.swift rename to SessionMessagingKit/Sending & Receiving/MessageReceiverDelegate.swift diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender.swift b/SessionMessagingKit/Sending & Receiving/MessageSender.swift index dcb9858dc..870193b0e 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender.swift @@ -130,11 +130,15 @@ public final class MessageSender : NSObject { seal.reject(error) } let _ = promise.done(on: DispatchQueue.main) { + let storage = Configuration.shared.storage + storage.withAsync({ transaction in + Configuration.shared.messageSenderDelegate.handleSuccessfulMessageSend(message, using: transaction) + }, completion: { }) if case .contact(_) = destination { NotificationCenter.default.post(name: .messageSent, object: NSNumber(value: message.sentTimestamp!)) } let notifyPNServerJob = NotifyPNServerJob(message: snodeMessage) - Configuration.shared.storage.with { transaction in + storage.with { transaction in JobQueue.shared.add(notifyPNServerJob, using: transaction) } } diff --git a/SessionMessagingKit/Sending & Receiving/MessageSenderDelegate.swift b/SessionMessagingKit/Sending & Receiving/MessageSenderDelegate.swift new file mode 100644 index 000000000..0b6b91ea0 --- /dev/null +++ b/SessionMessagingKit/Sending & Receiving/MessageSenderDelegate.swift @@ -0,0 +1,5 @@ + +public protocol MessageSenderDelegate { + + func handleSuccessfulMessageSend(_ message: Message, using transaction: Any) +} diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index e653c010b..620b29787 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -244,6 +244,7 @@ B85357BF23A1AE0800AAF6CD /* SeedReminderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357BE23A1AE0800AAF6CD /* SeedReminderView.swift */; }; B85357C323A1BD1200AAF6CD /* SeedVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357C223A1BD1200AAF6CD /* SeedVC.swift */; }; B8544E3323D50E4900299F14 /* AppearanceUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8544E3223D50E4900299F14 /* AppearanceUtilities.swift */; }; + B867665825663BBA00B197C5 /* MessageSenderDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B867665725663BBA00B197C5 /* MessageSenderDelegate.swift */; }; B86BD08423399ACF000F5AE3 /* Modal.swift in Sources */ = {isa = PBXBuildFile; fileRef = B86BD08323399ACF000F5AE3 /* Modal.swift */; }; B86BD08623399CEF000F5AE3 /* SeedModal.swift in Sources */ = {isa = PBXBuildFile; fileRef = B86BD08523399CEF000F5AE3 /* SeedModal.swift */; }; B8783E9E23EB948D00404FB8 /* UILabel+Interaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8783E9D23EB948D00404FB8 /* UILabel+Interaction.swift */; }; @@ -907,7 +908,7 @@ C3CA3B1D255CF3C800F4C6D4 /* MessageSender+Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3CA3B1C255CF3C800F4C6D4 /* MessageSender+Utilities.swift */; }; C3CA3B2F255CF84E00F4C6D4 /* NullMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3CA3B2E255CF84E00F4C6D4 /* NullMessage.swift */; }; C3D0972B2510499C00F6E3E4 /* BackgroundPoller.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3D0972A2510499C00F6E3E4 /* BackgroundPoller.swift */; }; - C3D697382564DCE6004AF766 /* MessageHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3D697372564DCE6004AF766 /* MessageHandler.swift */; }; + C3D697382564DCE6004AF766 /* MessageReceiverDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3D697372564DCE6004AF766 /* MessageReceiverDelegate.swift */; }; C3DAB3242480CB2B00725F25 /* SRCopyableLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3DAB3232480CB2A00725F25 /* SRCopyableLabel.swift */; }; C3DFFAC623E96F0D0058DAF8 /* Sheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3DFFAC523E96F0D0058DAF8 /* Sheet.swift */; }; C3E5C2FA251DBABB0040DFFC /* EditClosedGroupVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3E5C2F9251DBABB0040DFFC /* EditClosedGroupVC.swift */; }; @@ -1348,6 +1349,7 @@ B85357C223A1BD1200AAF6CD /* SeedVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedVC.swift; sourceTree = ""; }; B8544E3023D16CA500299F14 /* DeviceUtilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceUtilities.swift; sourceTree = ""; }; B8544E3223D50E4900299F14 /* AppearanceUtilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppearanceUtilities.swift; sourceTree = ""; }; + B867665725663BBA00B197C5 /* MessageSenderDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageSenderDelegate.swift; sourceTree = ""; }; B86BD08323399ACF000F5AE3 /* Modal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Modal.swift; sourceTree = ""; }; B86BD08523399CEF000F5AE3 /* SeedModal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedModal.swift; sourceTree = ""; }; B8783E9D23EB948D00404FB8 /* UILabel+Interaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UILabel+Interaction.swift"; sourceTree = ""; }; @@ -2004,7 +2006,7 @@ C3CA3B1C255CF3C800F4C6D4 /* MessageSender+Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MessageSender+Utilities.swift"; sourceTree = ""; }; C3CA3B2E255CF84E00F4C6D4 /* NullMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NullMessage.swift; sourceTree = ""; }; C3D0972A2510499C00F6E3E4 /* BackgroundPoller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundPoller.swift; sourceTree = ""; }; - C3D697372564DCE6004AF766 /* MessageHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageHandler.swift; sourceTree = ""; }; + C3D697372564DCE6004AF766 /* MessageReceiverDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageReceiverDelegate.swift; sourceTree = ""; }; C3D697492564DEDC004AF766 /* MessageReceiverDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageReceiverDelegate.swift; sourceTree = ""; }; C3DAB3232480CB2A00725F25 /* SRCopyableLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SRCopyableLabel.swift; sourceTree = ""; }; C3DFFAC523E96F0D0058DAF8 /* Sheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sheet.swift; sourceTree = ""; }; @@ -2657,9 +2659,10 @@ C3471FA32555439E00297E91 /* Notification+MessageSender.swift */, C300A5F12554B09800555489 /* MessageSender.swift */, C3471ECA2555356A00297E91 /* MessageSender+Encryption.swift */, + B867665725663BBA00B197C5 /* MessageSenderDelegate.swift */, C300A5FB2554B0A000555489 /* MessageReceiver.swift */, C3471F4B25553AB000297E91 /* MessageReceiver+Decryption.swift */, - C3D697372564DCE6004AF766 /* MessageHandler.swift */, + C3D697372564DCE6004AF766 /* MessageReceiverDelegate.swift */, ); path = "Sending & Receiving"; sourceTree = ""; @@ -5112,6 +5115,7 @@ C3C2A7682553A3D900C340D1 /* VisibleMessage+Contact.swift in Sources */, C3A721392558BDFA0043A11F /* OpenGroupAPI.swift in Sources */, C3A71D0B2558989C0043A11F /* MessageWrapper.swift in Sources */, + B867665825663BBA00B197C5 /* MessageSenderDelegate.swift in Sources */, C3A71D1E25589AC30043A11F /* WebSocketProto.swift in Sources */, C3A721382558BDFA0043A11F /* OpenGroupMessage.swift in Sources */, C3C2A7852553AAF300C340D1 /* SessionProtos.pb.swift in Sources */, @@ -5126,7 +5130,7 @@ C352A349255781F400338F3E /* AttachmentDownloadJob.swift in Sources */, C352A30925574D8500338F3E /* Message+Destination.swift in Sources */, C300A5E72554B07300555489 /* ExpirationTimerUpdate.swift in Sources */, - C3D697382564DCE6004AF766 /* MessageHandler.swift in Sources */, + C3D697382564DCE6004AF766 /* MessageReceiverDelegate.swift in Sources */, C352A2FF25574B6300338F3E /* MessageSendJob.swift in Sources */, C3C2A75F2553A3C500C340D1 /* VisibleMessage+LinkPreview.swift in Sources */, C3C2A74425539EB700C340D1 /* Message.swift in Sources */, diff --git a/SignalUtilitiesKit/Messaging/MessageSenderDelegate.swift b/SignalUtilitiesKit/Messaging/MessageSenderDelegate.swift index 4a7706de3..4ddc84b2d 100644 --- a/SignalUtilitiesKit/Messaging/MessageSenderDelegate.swift +++ b/SignalUtilitiesKit/Messaging/MessageSenderDelegate.swift @@ -1,8 +1,13 @@ -public final class MessageSenderDelegate : SharedSenderKeysDelegate { +public final class MessageSenderDelegate : SessionMessagingKit.MessageSenderDelegate, SharedSenderKeysDelegate { public static let shared = MessageSenderDelegate() + public func handleSuccessfulMessageSend(_ message: Message, using transaction: Any) { + guard let tsMessage = TSOutgoingMessage.find(withTimestamp: message.sentTimestamp!) else { return } + tsMessage.update(withSentRecipient: message.recipient!, wasSentByUD: true, transaction: transaction as! YapDatabaseReadWriteTransaction) + } + public func requestSenderKey(for groupPublicKey: String, senderPublicKey: String, using transaction: Any) { print("[Loki] Requesting sender key for group public key: \(groupPublicKey), sender public key: \(senderPublicKey).") let transaction = transaction as! YapDatabaseReadWriteTransaction diff --git a/SignalUtilitiesKit/Messaging/TSIncomingMessage.m b/SignalUtilitiesKit/Messaging/TSIncomingMessage.m index a2d30d92a..40092305c 100644 --- a/SignalUtilitiesKit/Messaging/TSIncomingMessage.m +++ b/SignalUtilitiesKit/Messaging/TSIncomingMessage.m @@ -106,7 +106,6 @@ NS_ASSUME_NONNULL_BEGIN return foundMessage; } - - (OWSInteractionType)interactionType { return OWSInteractionType_IncomingMessage; diff --git a/SignalUtilitiesKit/Messaging/TSOutgoingMessage.h b/SignalUtilitiesKit/Messaging/TSOutgoingMessage.h index f5d9e340b..dfd9d73e2 100644 --- a/SignalUtilitiesKit/Messaging/TSOutgoingMessage.h +++ b/SignalUtilitiesKit/Messaging/TSOutgoingMessage.h @@ -140,9 +140,10 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) { @property (nonatomic, readonly) BOOL isOnline; -/// Loki: Time to live for the message in milliseconds. @property (nonatomic, readonly) uint ttl; ++ (nullable instancetype)findMessageWithTimestamp:(uint64_t)timestamp; + /** * The data representation of this message, to be encrypted, before being sent. */ diff --git a/SignalUtilitiesKit/Messaging/TSOutgoingMessage.m b/SignalUtilitiesKit/Messaging/TSOutgoingMessage.m index 58a0bdb64..5b2449b23 100644 --- a/SignalUtilitiesKit/Messaging/TSOutgoingMessage.m +++ b/SignalUtilitiesKit/Messaging/TSOutgoingMessage.m @@ -6,7 +6,7 @@ #import "TSOutgoingMessage.h" #import "NSString+SSK.h" - +#import "TSDatabaseSecondaryIndexes.h" #import "OWSPrimaryStorage.h" #import "ProfileManagerProtocol.h" #import "ProtoUtils.h" @@ -502,6 +502,20 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt return NO; } ++ (nullable instancetype)findMessageWithTimestamp:(uint64_t)timestamp +{ + __block TSOutgoingMessage *result; + [LKStorage readWithBlock:^(YapDatabaseReadTransaction *transaction) { + [TSDatabaseSecondaryIndexes enumerateMessagesWithTimestamp:timestamp withBlock:^(NSString *collection, NSString *key, BOOL *stop) { + TSInteraction *interaction = [TSInteraction fetchObjectWithUniqueID:key transaction:transaction]; + if ([interaction isKindOfClass:[TSOutgoingMessage class]]) { + result = (TSOutgoingMessage *)interaction; + } + } usingTransaction:transaction]; + }]; + return result; +} + - (OWSInteractionType)interactionType { return OWSInteractionType_OutgoingMessage;