Fix group chat notifications

pull/79/head
Niels Andriesse 5 years ago
parent 34dfe740fc
commit 213d154472

@ -1097,12 +1097,25 @@ static NSTimeInterval launchStartedAt;
} }
[LKLogger print:@"[Loki] Silent push notification received; fetching messages."]; [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(^{ }).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 - (void)application:(UIApplication *)application
@ -1120,14 +1133,35 @@ static NSTimeInterval launchStartedAt;
} }
[LKLogger print:@"[Loki] Silent push notification received; fetching messages."]; [LKLogger print:@"[Loki] Silent push notification received; fetching messages."];
__block AnyPromise *job = [AppEnvironment.shared.messageFetcherJob run].then(^{
completionHandler(UIBackgroundFetchResultNewData); NSMutableArray *promises = [NSMutableArray new];
job = nil;
__block AnyPromise *fetchMessagesPromise = [AppEnvironment.shared.messageFetcherJob run].then(^{
fetchMessagesPromise = nil;
}).catch(^{ }).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); completionHandler(UIBackgroundFetchResultFailed);
job = nil;
}); });
[job retainUntilComplete];
} }
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
@ -1229,14 +1263,34 @@ static NSTimeInterval launchStartedAt;
NSLog(@"[Loki] Performing background fetch."); NSLog(@"[Loki] Performing background fetch.");
[LKAnalytics.shared track:@"Performed Background Fetch"]; [LKAnalytics.shared track:@"Performed Background Fetch"];
[AppReadiness runNowOrWhenAppDidBecomeReady:^{ [AppReadiness runNowOrWhenAppDidBecomeReady:^{
__block AnyPromise *job = [AppEnvironment.shared.messageFetcherJob run].then(^{ NSMutableArray *promises = [NSMutableArray new];
completionHandler(UIBackgroundFetchResultNewData);
job = nil; __block AnyPromise *fetchMessagesPromise = [AppEnvironment.shared.messageFetcherJob run].then(^{
fetchMessagesPromise = nil;
}).catch(^{ }).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); completionHandler(UIBackgroundFetchResultFailed);
job = nil;
}); });
[job retainUntilComplete];
}]; }];
} }

@ -1,3 +1,4 @@
import PromiseKit
// TODO: Move away from polling // TODO: Move away from polling
@ -47,10 +48,15 @@ public final class LokiPublicChatPoller : NSObject {
} }
// MARK: Polling // MARK: Polling
private func pollForNewMessages() { @objc(pollForNewMessages)
public func objc_pollForNewMessages() -> AnyPromise {
AnyPromise.from(pollForNewMessages())
}
public func pollForNewMessages() -> Promise<Void> {
let publicChat = self.publicChat let publicChat = self.publicChat
let userHexEncodedPublicKey = self.userHexEncodedPublicKey let userHexEncodedPublicKey = self.userHexEncodedPublicKey
let _ = 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
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()

Loading…
Cancel
Save