|  |  |  | @ -1097,12 +1097,25 @@ static NSTimeInterval launchStartedAt; | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  |      | 
		
	
		
			
				|  |  |  |  |     [LKLogger print:@"[Loki] Silent push notification received; fetching messages."]; | 
		
	
		
			
				|  |  |  |  |     __block AnyPromise *job = [AppEnvironment.shared.messageFetcherJob run].then(^{ | 
		
	
		
			
				|  |  |  |  |         job = nil; | 
		
	
		
			
				|  |  |  |  |      | 
		
	
		
			
				|  |  |  |  |     __block AnyPromise *fetchMessagesPromise = [AppEnvironment.shared.messageFetcherJob run].then(^{ | 
		
	
		
			
				|  |  |  |  |         fetchMessagesPromise = nil; | 
		
	
		
			
				|  |  |  |  |     }).catch(^{ | 
		
	
		
			
				|  |  |  |  |         job = nil; | 
		
	
		
			
				|  |  |  |  |         fetchMessagesPromise = nil; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     [job retainUntilComplete]; | 
		
	
		
			
				|  |  |  |  |     [fetchMessagesPromise retainUntilComplete]; | 
		
	
		
			
				|  |  |  |  |      | 
		
	
		
			
				|  |  |  |  |     __block NSDictionary<NSString *, LKPublicChat *> *publicChats; | 
		
	
		
			
				|  |  |  |  |     [OWSPrimaryStorage.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { | 
		
	
		
			
				|  |  |  |  |         publicChats = [LKDatabaseUtilities getAllPublicChats:transaction]; | 
		
	
		
			
				|  |  |  |  |     }]; | 
		
	
		
			
				|  |  |  |  |     for (LKPublicChat *publicChat in publicChats) { | 
		
	
		
			
				|  |  |  |  |         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]; | 
		
	
		
			
				|  |  |  |  |         AnyPromise *fetchGroupMessagesPromise = [poller pollForNewMessages]; | 
		
	
		
			
				|  |  |  |  |         [fetchGroupMessagesPromise retainUntilComplete]; | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | - (void)application:(UIApplication *)application | 
		
	
	
		
			
				
					|  |  |  | @ -1120,14 +1133,35 @@ static NSTimeInterval launchStartedAt; | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     [LKLogger print:@"[Loki] Silent push notification received; fetching messages."]; | 
		
	
		
			
				|  |  |  |  |     __block AnyPromise *job = [AppEnvironment.shared.messageFetcherJob run].then(^{ | 
		
	
		
			
				|  |  |  |  |         completionHandler(UIBackgroundFetchResultNewData); | 
		
	
		
			
				|  |  |  |  |         job = nil; | 
		
	
		
			
				|  |  |  |  |      | 
		
	
		
			
				|  |  |  |  |     NSMutableArray *promises = [NSMutableArray new]; | 
		
	
		
			
				|  |  |  |  |      | 
		
	
		
			
				|  |  |  |  |     __block AnyPromise *fetchMessagesPromise = [AppEnvironment.shared.messageFetcherJob run].then(^{ | 
		
	
		
			
				|  |  |  |  |         fetchMessagesPromise = nil; | 
		
	
		
			
				|  |  |  |  |     }).catch(^{ | 
		
	
		
			
				|  |  |  |  |         fetchMessagesPromise = nil; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     [promises addObject:fetchMessagesPromise]; | 
		
	
		
			
				|  |  |  |  |     [fetchMessagesPromise retainUntilComplete]; | 
		
	
		
			
				|  |  |  |  |      | 
		
	
		
			
				|  |  |  |  |     __block NSDictionary<NSString *, LKPublicChat *> *publicChats; | 
		
	
		
			
				|  |  |  |  |     [OWSPrimaryStorage.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { | 
		
	
		
			
				|  |  |  |  |         publicChats = [LKDatabaseUtilities getAllPublicChats:transaction]; | 
		
	
		
			
				|  |  |  |  |     }]; | 
		
	
		
			
				|  |  |  |  |     for (LKPublicChat *publicChat in publicChats) { | 
		
	
		
			
				|  |  |  |  |         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]; | 
		
	
		
			
				|  |  |  |  |         AnyPromise *fetchGroupMessagesPromise = [poller pollForNewMessages]; | 
		
	
		
			
				|  |  |  |  |         [promises addObject:fetchGroupMessagesPromise]; | 
		
	
		
			
				|  |  |  |  |         [fetchGroupMessagesPromise retainUntilComplete]; | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  |      | 
		
	
		
			
				|  |  |  |  |     PMKJoin(promises).then(^(id results) { | 
		
	
		
			
				|  |  |  |  |         completionHandler(UIBackgroundFetchResultNewData); | 
		
	
		
			
				|  |  |  |  |     }).catch(^(id error) { | 
		
	
		
			
				|  |  |  |  |         completionHandler(UIBackgroundFetchResultFailed); | 
		
	
		
			
				|  |  |  |  |         job = nil; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     [job retainUntilComplete]; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { | 
		
	
	
		
			
				
					|  |  |  | @ -1229,14 +1263,34 @@ static NSTimeInterval launchStartedAt; | 
		
	
		
			
				|  |  |  |  |     NSLog(@"[Loki] Performing background fetch."); | 
		
	
		
			
				|  |  |  |  |     [LKAnalytics.shared track:@"Performed Background Fetch"]; | 
		
	
		
			
				|  |  |  |  |     [AppReadiness runNowOrWhenAppDidBecomeReady:^{ | 
		
	
		
			
				|  |  |  |  |         __block AnyPromise *job = [AppEnvironment.shared.messageFetcherJob run].then(^{ | 
		
	
		
			
				|  |  |  |  |             completionHandler(UIBackgroundFetchResultNewData); | 
		
	
		
			
				|  |  |  |  |             job = nil; | 
		
	
		
			
				|  |  |  |  |         NSMutableArray *promises = [NSMutableArray new]; | 
		
	
		
			
				|  |  |  |  |          | 
		
	
		
			
				|  |  |  |  |         __block AnyPromise *fetchMessagesPromise = [AppEnvironment.shared.messageFetcherJob run].then(^{ | 
		
	
		
			
				|  |  |  |  |             fetchMessagesPromise = nil; | 
		
	
		
			
				|  |  |  |  |         }).catch(^{ | 
		
	
		
			
				|  |  |  |  |             fetchMessagesPromise = nil; | 
		
	
		
			
				|  |  |  |  |         }); | 
		
	
		
			
				|  |  |  |  |         [promises addObject:fetchMessagesPromise]; | 
		
	
		
			
				|  |  |  |  |         [fetchMessagesPromise retainUntilComplete]; | 
		
	
		
			
				|  |  |  |  |          | 
		
	
		
			
				|  |  |  |  |         __block NSDictionary<NSString *, LKPublicChat *> *publicChats; | 
		
	
		
			
				|  |  |  |  |         [OWSPrimaryStorage.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { | 
		
	
		
			
				|  |  |  |  |             publicChats = [LKDatabaseUtilities getAllPublicChats:transaction]; | 
		
	
		
			
				|  |  |  |  |         }]; | 
		
	
		
			
				|  |  |  |  |         for (LKPublicChat *publicChat in publicChats) { | 
		
	
		
			
				|  |  |  |  |             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]; | 
		
	
		
			
				|  |  |  |  |             AnyPromise *fetchGroupMessagesPromise = [poller pollForNewMessages]; | 
		
	
		
			
				|  |  |  |  |             [promises addObject:fetchGroupMessagesPromise]; | 
		
	
		
			
				|  |  |  |  |             [fetchGroupMessagesPromise retainUntilComplete]; | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |          | 
		
	
		
			
				|  |  |  |  |         PMKJoin(promises).then(^(id results) { | 
		
	
		
			
				|  |  |  |  |             completionHandler(UIBackgroundFetchResultNewData); | 
		
	
		
			
				|  |  |  |  |         }).catch(^(id error) { | 
		
	
		
			
				|  |  |  |  |             completionHandler(UIBackgroundFetchResultFailed); | 
		
	
		
			
				|  |  |  |  |             job = nil; | 
		
	
		
			
				|  |  |  |  |         }); | 
		
	
		
			
				|  |  |  |  |         [job retainUntilComplete]; | 
		
	
		
			
				|  |  |  |  |     }]; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
	
		
			
				
					|  |  |  | 
 |