From d6f2f6fee2f76617f671483a720b2bea4f193daa Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Thu, 20 Feb 2020 14:41:16 +1100 Subject: [PATCH 1/2] fix attachment download --- Signal/Signal-Info.plist | 2 +- Signal/src/AppDelegate.m | 11 ++++++++++- .../Loki/Utilities/LokiPushNotificationManager.swift | 3 ++- Signal/src/util/MainAppContext.m | 2 ++ .../Loki/API/Public Chat/LokiPublicChatPoller.swift | 1 + .../Messages/Attachments/OWSAttachmentDownloads.h | 2 ++ .../Messages/Attachments/OWSAttachmentDownloads.m | 12 ++++++++++++ SignalServiceKit/src/Util/AppContext.h | 2 ++ 8 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Signal/Signal-Info.plist b/Signal/Signal-Info.plist index 342d37adb..9d7c02e10 100644 --- a/Signal/Signal-Info.plist +++ b/Signal/Signal-Info.plist @@ -7,7 +7,7 @@ CarthageVersion 0.34.0 OSXVersion - 10.15.3 + 10.15.2 WebRTCCommit 1445d719bf05280270e9f77576f80f973fd847f8 M73 diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 3039232bf..23a834ade 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -1118,6 +1118,8 @@ static NSTimeInterval launchStartedAt; OWSLogInfo(@"Ignoring remote notification; app not ready."); return; } + + CurrentAppContext().isWakenByRemoteNotification = true; [LKLogger print:@"[Loki] Silent push notification received; fetching messages."]; @@ -1135,7 +1137,7 @@ static NSTimeInterval launchStartedAt; [OWSPrimaryStorage.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { publicChats = [LKDatabaseUtilities getAllPublicChats:transaction]; }]; - for (LKPublicChat *publicChat in publicChats) { + for (LKPublicChat *publicChat in publicChats.allValues) { if (![publicChat isKindOfClass:LKPublicChat.class]) { continue; } // For some reason publicChat is sometimes a base 64 encoded string... LKPublicChatPoller *poller = [[LKPublicChatPoller alloc] initForPublicChat:publicChat]; [poller stop]; @@ -1146,8 +1148,12 @@ static NSTimeInterval launchStartedAt; PMKJoin(promises).then(^(id results) { completionHandler(UIBackgroundFetchResultNewData); + CurrentAppContext().isWakenByRemoteNotification = false; + [LKLogger print:@"[Loki] UIBackgroundFetchResultNewData"]; }).catch(^(id error) { completionHandler(UIBackgroundFetchResultFailed); + CurrentAppContext().isWakenByRemoteNotification = false; + [LKLogger print:@"[Loki] UIBackgroundFetchResultFailed"]; }); } @@ -1579,11 +1585,14 @@ static NSTimeInterval launchStartedAt; { [self setUpLongPollerIfNeeded]; [self.lokiLongPoller startIfNeeded]; + [LKPublicChatManager.shared startPollersIfNeeded]; + [SSKEnvironment.shared.attachmentDownloads startDownloadIfNeeded]; } - (void)stopLongPollerIfNeeded { [self.lokiLongPoller stopIfNeeded]; + [LKPublicChatManager.shared stopPollers]; } - (LKRSSFeed *)lokiNewsFeed diff --git a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift b/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift index 19eb5977c..4be3f8c79 100644 --- a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift +++ b/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift @@ -21,7 +21,8 @@ final class LokiPushNotificationManager : NSObject { } // Send token to Loki server let parameters = [ "token" : hexEncodedToken ] - let url = URL(string: "https://live.apns.getsession.org/register")! +// let url = URL(string: "https://live.apns.getsession.org/register")! + let url = URL(string: "https://dev.apns.getsession.org/register")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] TSNetworkManager.shared().makeRequest(request, success: { _, response in diff --git a/Signal/src/util/MainAppContext.m b/Signal/src/util/MainAppContext.m index bfb746148..30bf7524e 100644 --- a/Signal/src/util/MainAppContext.m +++ b/Signal/src/util/MainAppContext.m @@ -28,6 +28,7 @@ NSString *const ReportedApplicationStateDidChangeNotification = @"ReportedApplic @synthesize mainWindow = _mainWindow; @synthesize appLaunchTime = _appLaunchTime; +@synthesize isWakenByRemoteNotification = _isWakenByRemoteNotification; - (instancetype)init { @@ -40,6 +41,7 @@ NSString *const ReportedApplicationStateDidChangeNotification = @"ReportedApplic self.reportedApplicationState = UIApplicationStateInactive; _appLaunchTime = [NSDate new]; + _isWakenByRemoteNotification = false; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) diff --git a/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatPoller.swift b/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatPoller.swift index dee48d5a7..3e52d6a10 100644 --- a/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatPoller.swift +++ b/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatPoller.swift @@ -55,6 +55,7 @@ public final class LokiPublicChatPoller : NSObject { let publicChat = self.publicChat let userHexEncodedPublicKey = self.userHexEncodedPublicKey return LokiPublicChatAPI.getMessages(for: publicChat.channel, on: publicChat.server).done(on: DispatchQueue.global()) { messages in + print("[Loki] Poll for new open group messages, message count \(messages.count)") let uniqueHexEncodedPublicKeys = Set(messages.map { $0.hexEncodedPublicKey }) func proceed() { let storage = OWSPrimaryStorage.shared() diff --git a/SignalServiceKit/src/Messages/Attachments/OWSAttachmentDownloads.h b/SignalServiceKit/src/Messages/Attachments/OWSAttachmentDownloads.h index 1701e9296..c36382ca6 100644 --- a/SignalServiceKit/src/Messages/Attachments/OWSAttachmentDownloads.h +++ b/SignalServiceKit/src/Messages/Attachments/OWSAttachmentDownloads.h @@ -43,6 +43,8 @@ extern NSString *const kAttachmentDownloadAttachmentIDKey; success:(void (^)(NSArray *attachmentStreams))success failure:(void (^)(NSError *error))failure; +- (void)startDownloadIfNeeded; + @end NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Messages/Attachments/OWSAttachmentDownloads.m b/SignalServiceKit/src/Messages/Attachments/OWSAttachmentDownloads.m index 4db343ed9..f853fd484 100644 --- a/SignalServiceKit/src/Messages/Attachments/OWSAttachmentDownloads.m +++ b/SignalServiceKit/src/Messages/Attachments/OWSAttachmentDownloads.m @@ -264,6 +264,8 @@ typedef void (^AttachmentDownloadFailure)(NSError *error); - (void)startDownloadIfPossible { + if (CurrentAppContext().isWakenByRemoteNotification) { return; } + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ OWSAttachmentDownloadJob *_Nullable job; @@ -342,6 +344,16 @@ typedef void (^AttachmentDownloadFailure)(NSError *error); #pragma mark - +- (void)startDownloadIfNeeded +{ + if (self.attachmentDownloadJobQueue.count > 0) { + [LKLogger print:@"[Loki] start uncompleted attachment download tasks"]; + [self startDownloadIfPossible]; + } +} + +#pragma mark - + - (void)retrieveAttachmentForJob:(OWSAttachmentDownloadJob *)job success:(void (^)(TSAttachmentStream *attachmentStream))successHandler failure:(void (^)(NSError *error))failureHandler diff --git a/SignalServiceKit/src/Util/AppContext.h b/SignalServiceKit/src/Util/AppContext.h index bcb8cde8d..5ca48b4d3 100755 --- a/SignalServiceKit/src/Util/AppContext.h +++ b/SignalServiceKit/src/Util/AppContext.h @@ -37,6 +37,8 @@ NSString *NSStringForUIApplicationState(UIApplicationState value); @property (nonatomic, readonly) BOOL isMainApp; @property (nonatomic, readonly) BOOL isMainAppAndActive; +//A flag to determine if the attatchment downloading tasks should run +@property (nonatomic) BOOL isWakenByRemoteNotification; // Whether the user is using a right-to-left language like Arabic. @property (nonatomic, readonly) BOOL isRTL; From db2e6f8eec2e61165c55aa7a194b4604a28056b7 Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Fri, 21 Feb 2020 11:08:16 +1100 Subject: [PATCH 2/2] clean --- Signal/src/Loki/Utilities/LokiPushNotificationManager.swift | 4 ++-- .../src/Loki/API/Public Chats/LokiPublicChatPoller.swift | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift b/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift index f021c0064..411f9a229 100644 --- a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift +++ b/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift @@ -22,8 +22,8 @@ final class LokiPushNotificationManager : NSObject { } // Send token to Loki server let parameters = [ "token" : hexEncodedToken ] -// let url = URL(string: "https://live.apns.getsession.org/register")! - let url = URL(string: "https://dev.apns.getsession.org/register")! + let url = URL(string: "https://live.apns.getsession.org/register")! +// let url = URL(string: "https://dev.apns.getsession.org/register")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] TSNetworkManager.shared().makeRequest(request, success: { _, response in diff --git a/SignalServiceKit/src/Loki/API/Public Chats/LokiPublicChatPoller.swift b/SignalServiceKit/src/Loki/API/Public Chats/LokiPublicChatPoller.swift index 3ed9583d6..fbf50c42d 100644 --- a/SignalServiceKit/src/Loki/API/Public Chats/LokiPublicChatPoller.swift +++ b/SignalServiceKit/src/Loki/API/Public Chats/LokiPublicChatPoller.swift @@ -55,7 +55,6 @@ public final class LokiPublicChatPoller : NSObject { let publicChat = self.publicChat let userHexEncodedPublicKey = self.userHexEncodedPublicKey return LokiPublicChatAPI.getMessages(for: publicChat.channel, on: publicChat.server).done(on: DispatchQueue.global()) { messages in - print("[Loki] Poll for new open group messages, message count \(messages.count)") let uniqueHexEncodedPublicKeys = Set(messages.map { $0.hexEncodedPublicKey }) func proceed() { let storage = OWSPrimaryStorage.shared()