fix attachment download

pull/118/head
Ryan ZHAO 5 years ago
parent 5c5cafc427
commit d6f2f6fee2

@ -7,7 +7,7 @@
<key>CarthageVersion</key> <key>CarthageVersion</key>
<string>0.34.0</string> <string>0.34.0</string>
<key>OSXVersion</key> <key>OSXVersion</key>
<string>10.15.3</string> <string>10.15.2</string>
<key>WebRTCCommit</key> <key>WebRTCCommit</key>
<string>1445d719bf05280270e9f77576f80f973fd847f8 M73</string> <string>1445d719bf05280270e9f77576f80f973fd847f8 M73</string>
</dict> </dict>

@ -1118,6 +1118,8 @@ static NSTimeInterval launchStartedAt;
OWSLogInfo(@"Ignoring remote notification; app not ready."); OWSLogInfo(@"Ignoring remote notification; app not ready.");
return; return;
} }
CurrentAppContext().isWakenByRemoteNotification = true;
[LKLogger print:@"[Loki] Silent push notification received; fetching messages."]; [LKLogger print:@"[Loki] Silent push notification received; fetching messages."];
@ -1135,7 +1137,7 @@ static NSTimeInterval launchStartedAt;
[OWSPrimaryStorage.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [OWSPrimaryStorage.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
publicChats = [LKDatabaseUtilities getAllPublicChats: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... if (![publicChat isKindOfClass:LKPublicChat.class]) { continue; } // For some reason publicChat is sometimes a base 64 encoded string...
LKPublicChatPoller *poller = [[LKPublicChatPoller alloc] initForPublicChat:publicChat]; LKPublicChatPoller *poller = [[LKPublicChatPoller alloc] initForPublicChat:publicChat];
[poller stop]; [poller stop];
@ -1146,8 +1148,12 @@ static NSTimeInterval launchStartedAt;
PMKJoin(promises).then(^(id results) { PMKJoin(promises).then(^(id results) {
completionHandler(UIBackgroundFetchResultNewData); completionHandler(UIBackgroundFetchResultNewData);
CurrentAppContext().isWakenByRemoteNotification = false;
[LKLogger print:@"[Loki] UIBackgroundFetchResultNewData"];
}).catch(^(id error) { }).catch(^(id error) {
completionHandler(UIBackgroundFetchResultFailed); completionHandler(UIBackgroundFetchResultFailed);
CurrentAppContext().isWakenByRemoteNotification = false;
[LKLogger print:@"[Loki] UIBackgroundFetchResultFailed"];
}); });
} }
@ -1579,11 +1585,14 @@ static NSTimeInterval launchStartedAt;
{ {
[self setUpLongPollerIfNeeded]; [self setUpLongPollerIfNeeded];
[self.lokiLongPoller startIfNeeded]; [self.lokiLongPoller startIfNeeded];
[LKPublicChatManager.shared startPollersIfNeeded];
[SSKEnvironment.shared.attachmentDownloads startDownloadIfNeeded];
} }
- (void)stopLongPollerIfNeeded - (void)stopLongPollerIfNeeded
{ {
[self.lokiLongPoller stopIfNeeded]; [self.lokiLongPoller stopIfNeeded];
[LKPublicChatManager.shared stopPollers];
} }
- (LKRSSFeed *)lokiNewsFeed - (LKRSSFeed *)lokiNewsFeed

@ -21,7 +21,8 @@ final class LokiPushNotificationManager : NSObject {
} }
// Send token to Loki server // Send token to Loki server
let parameters = [ "token" : hexEncodedToken ] 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) let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
TSNetworkManager.shared().makeRequest(request, success: { _, response in TSNetworkManager.shared().makeRequest(request, success: { _, response in

@ -28,6 +28,7 @@ NSString *const ReportedApplicationStateDidChangeNotification = @"ReportedApplic
@synthesize mainWindow = _mainWindow; @synthesize mainWindow = _mainWindow;
@synthesize appLaunchTime = _appLaunchTime; @synthesize appLaunchTime = _appLaunchTime;
@synthesize isWakenByRemoteNotification = _isWakenByRemoteNotification;
- (instancetype)init - (instancetype)init
{ {
@ -40,6 +41,7 @@ NSString *const ReportedApplicationStateDidChangeNotification = @"ReportedApplic
self.reportedApplicationState = UIApplicationStateInactive; self.reportedApplicationState = UIApplicationStateInactive;
_appLaunchTime = [NSDate new]; _appLaunchTime = [NSDate new];
_isWakenByRemoteNotification = false;
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForeground:) selector:@selector(applicationWillEnterForeground:)

@ -55,6 +55,7 @@ public final class LokiPublicChatPoller : NSObject {
let publicChat = self.publicChat let publicChat = self.publicChat
let userHexEncodedPublicKey = self.userHexEncodedPublicKey let userHexEncodedPublicKey = self.userHexEncodedPublicKey
return LokiPublicChatAPI.getMessages(for: publicChat.channel, on: publicChat.server).done(on: DispatchQueue.global()) { messages in 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 }) let uniqueHexEncodedPublicKeys = Set(messages.map { $0.hexEncodedPublicKey })
func proceed() { func proceed() {
let storage = OWSPrimaryStorage.shared() let storage = OWSPrimaryStorage.shared()

@ -43,6 +43,8 @@ extern NSString *const kAttachmentDownloadAttachmentIDKey;
success:(void (^)(NSArray<TSAttachmentStream *> *attachmentStreams))success success:(void (^)(NSArray<TSAttachmentStream *> *attachmentStreams))success
failure:(void (^)(NSError *error))failure; failure:(void (^)(NSError *error))failure;
- (void)startDownloadIfNeeded;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -264,6 +264,8 @@ typedef void (^AttachmentDownloadFailure)(NSError *error);
- (void)startDownloadIfPossible - (void)startDownloadIfPossible
{ {
if (CurrentAppContext().isWakenByRemoteNotification) { return; }
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
OWSAttachmentDownloadJob *_Nullable job; OWSAttachmentDownloadJob *_Nullable job;
@ -342,6 +344,16 @@ typedef void (^AttachmentDownloadFailure)(NSError *error);
#pragma mark - #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 - (void)retrieveAttachmentForJob:(OWSAttachmentDownloadJob *)job
success:(void (^)(TSAttachmentStream *attachmentStream))successHandler success:(void (^)(TSAttachmentStream *attachmentStream))successHandler
failure:(void (^)(NSError *error))failureHandler failure:(void (^)(NSError *error))failureHandler

@ -37,6 +37,8 @@ NSString *NSStringForUIApplicationState(UIApplicationState value);
@property (nonatomic, readonly) BOOL isMainApp; @property (nonatomic, readonly) BOOL isMainApp;
@property (nonatomic, readonly) BOOL isMainAppAndActive; @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. // Whether the user is using a right-to-left language like Arabic.
@property (nonatomic, readonly) BOOL isRTL; @property (nonatomic, readonly) BOOL isRTL;

Loading…
Cancel
Save