From f7039809a7ea6d184e0506c3a85c7b5e1cb8325a Mon Sep 17 00:00:00 2001 From: Mikunj Date: Wed, 11 Sep 2019 10:39:32 +1000 Subject: [PATCH] Stop polling upon deleting group chat. Also don't create poller if thread has been deleted. --- Signal/src/AppDelegate.m | 48 +++++++++++++++++-- .../HomeView/HomeViewController.m | 9 +++- .../Loki/Utilities/Notification+Loki.swift | 2 + 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 7d9f48d46..7bf576d2e 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -321,6 +321,9 @@ static NSTimeInterval launchStartedAt; // Loki - Observe messages received notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNewMessagesReceived:) name:NSNotification.newMessagesReceived object:nil]; + + // Loki - Observe thread deletion notifications + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleThreadDeleted:) name:NSNotification.threadDeleted object:nil]; OWSLogInfo(@"application: didFinishLaunchingWithOptions completed."); @@ -1579,26 +1582,61 @@ static NSTimeInterval launchStartedAt; - (void)createGroupChatPollersIfNeeded { - if (self.lokiPublicChatPoller == nil) { self.lokiPublicChatPoller = [[LKGroupChatPoller alloc] initForGroup:self.lokiPublicChat]; } + // Make sure thread exists + __block TSGroupThread *thread; + [OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + thread = [TSGroupThread threadWithGroupId:[self.lokiPublicChat.id dataUsingEncoding:NSUTF8StringEncoding] transaction:transaction]; + }]; + + if (thread != nil && self.lokiPublicChatPoller == nil) { + self.lokiPublicChatPoller = [[LKGroupChatPoller alloc] initForGroup:self.lokiPublicChat]; + } } - (void)createRSSFeedPollersIfNeeded { - if (self.lokiNewsFeedPoller == nil) { self.lokiNewsFeedPoller = [[LKRSSFeedPoller alloc] initForFeed:self.lokiNewsFeed]; } + // Make sure thread exists + __block TSGroupThread *lokiNewsFeedThread; + [OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + lokiNewsFeedThread = [TSGroupThread threadWithGroupId:[self.lokiNewsFeed.id dataUsingEncoding:NSUTF8StringEncoding] transaction:transaction]; + }]; + if (lokiNewsFeedThread != nil && self.lokiNewsFeedPoller == nil) { + self.lokiNewsFeedPoller = [[LKRSSFeedPoller alloc] initForFeed:self.lokiNewsFeed]; + } + + // We don't need to check thread for this as it's never deletable if (self.lokiMessengerUpdatesFeedPoller == nil) { self.lokiMessengerUpdatesFeedPoller = [[LKRSSFeedPoller alloc] initForFeed:self.lokiMessengerUpdatesFeed]; } } - (void)startGroupChatPollersIfNeeded { [self createGroupChatPollersIfNeeded]; - [self.lokiPublicChatPoller startIfNeeded]; + if (self.lokiPublicChatPoller != nil) { [self.lokiPublicChatPoller startIfNeeded]; } } - (void)startRSSFeedPollersIfNeeded { [self createRSSFeedPollersIfNeeded]; - [self.lokiNewsFeedPoller startIfNeeded]; - [self.lokiMessengerUpdatesFeedPoller startIfNeeded]; + if (self.lokiNewsFeedPoller != nil) { [self.lokiNewsFeedPoller startIfNeeded]; } + if (self.lokiMessengerUpdatesFeedPoller != nil) { [self.lokiMessengerUpdatesFeedPoller startIfNeeded]; } +} + +- (void)handleThreadDeleted:(NSNotification *)notification { + NSDictionary *userInfo = notification.userInfo; + NSString *threadId = (NSString *)userInfo[@"threadId"]; + if (threadId == nil) { return; } + + // Check if public chat was deleted + if ([threadId isEqualToString:[TSGroupThread threadIdFromGroupId:[self.lokiPublicChat.id dataUsingEncoding:NSUTF8StringEncoding]]] && self.lokiPublicChatPoller != nil) { + [self.lokiPublicChatPoller stop]; + self.lokiPublicChatPoller = nil; + } + + // Check if news feed was deleted + if ([threadId isEqualToString:[TSGroupThread threadIdFromGroupId:[self.lokiNewsFeed.id dataUsingEncoding:NSUTF8StringEncoding]]] && self.lokiNewsFeedPoller != nil) { + [self.lokiNewsFeedPoller stop]; + self.lokiNewsFeedPoller = nil; + } } @end diff --git a/Signal/src/ViewControllers/HomeView/HomeViewController.m b/Signal/src/ViewControllers/HomeView/HomeViewController.m index d92171b60..4a3162af9 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewController.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewController.m @@ -1413,6 +1413,8 @@ typedef NS_ENUM(NSInteger, HomeViewControllerSection) { - (void)deleteThread:(TSThread *)thread { [self.editingDbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + /* Loki: Orignal Code + ===================== if ([thread isKindOfClass:[TSGroupThread class]]) { TSGroupThread *groupThread = (TSGroupThread *)thread; if (groupThread.isLocalUserInGroup) { @@ -1420,10 +1422,15 @@ typedef NS_ENUM(NSInteger, HomeViewControllerSection) { return; } } + */ + // Loki: For now hard delete all groups [thread removeWithTransaction:transaction]; }]; - + + // Loki - Post notification + [[NSNotificationCenter defaultCenter] postNotificationName:NSNotification.threadDeleted object:nil userInfo:@{ @"threadId": thread.uniqueId }]; + [self updateViewState]; } diff --git a/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift b/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift index 4969f25b8..25f7bc80a 100644 --- a/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift +++ b/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift @@ -4,6 +4,7 @@ public extension Notification.Name { public static let newMessagesReceived = Notification.Name("newMessagesReceived") public static let threadFriendRequestStatusChanged = Notification.Name("threadFriendRequestStatusChanged") public static let messageFriendRequestStatusChanged = Notification.Name("messageFriendRequestStatusChanged") + public static let threadDeleted = Notification.Name("threadDeleted") } // MARK: - Obj-C @@ -13,4 +14,5 @@ public extension Notification.Name { @objc public static let newMessagesReceived = Notification.Name.newMessagesReceived.rawValue as NSString @objc public static let threadFriendRequestStatusChanged = Notification.Name.threadFriendRequestStatusChanged.rawValue as NSString @objc public static let messageFriendRequestStatusChanged = Notification.Name.messageFriendRequestStatusChanged.rawValue as NSString + @objc public static let threadDeleted = Notification.Name.threadDeleted.rawValue as NSString }