From 3769f50a21b39456fb5bc9efa3ec50d49643f0c8 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Fri, 24 Jan 2020 09:48:47 +1100 Subject: [PATCH] Fix public chat deletion --- Signal/src/AppDelegate.m | 4 ++-- Signal/src/Loki/View Controllers/HomeVC.swift | 14 +++++++++++--- .../src/Loki/Database/OWSPrimaryStorage+Loki.h | 1 + .../src/Loki/Database/OWSPrimaryStorage+Loki.m | 11 +++++++++++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 2e117db37..d354f4e09 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -1602,12 +1602,12 @@ static NSTimeInterval launchStartedAt; - (LKRSSFeed *)lokiNewsFeed { - return [[LKRSSFeed alloc] initWithId:@"loki.network.feed" server:@"https://loki.network/feed/" displayName:NSLocalizedString(@"Loki News", @"") isDeletable:true]; + return [[LKRSSFeed alloc] initWithId:@"loki.network.feed" server:@"https://loki.network/feed/" displayName:@"Loki News" isDeletable:true]; } - (LKRSSFeed *)lokiMessengerUpdatesFeed { - return [[LKRSSFeed alloc] initWithId:@"loki.network.messenger-updates.feed" server:@"https://loki.network/category/messenger-updates/feed/" displayName:NSLocalizedString(@"Loki Messenger Updates", @"") isDeletable:false]; + return [[LKRSSFeed alloc] initWithId:@"loki.network.messenger-updates.feed" server:@"https://loki.network/category/messenger-updates/feed/" displayName:@"Loki Messenger Updates" isDeletable:false]; } - (void)setUpDefaultPublicChatsIfNeeded diff --git a/Signal/src/Loki/View Controllers/HomeVC.swift b/Signal/src/Loki/View Controllers/HomeVC.swift index 9213bb1fc..28e7e7078 100644 --- a/Signal/src/Loki/View Controllers/HomeVC.swift +++ b/Signal/src/Loki/View Controllers/HomeVC.swift @@ -323,12 +323,20 @@ final class HomeVC : UIViewController, UITableViewDataSource, UITableViewDelegat alert.addAction(UIAlertAction(title: NSLocalizedString("TXT_DELETE_TITLE", comment: ""), style: .destructive) { _ in guard let self = self else { return } self.editingDatabaseConnection.readWrite { transaction in + if let publicChat = publicChat { + var messageIDs: Set = [] + thread.enumerateInteractions(with: transaction) { interaction, _ in + messageIDs.insert(interaction.uniqueId!) + } + OWSPrimaryStorage.shared().updateMessageIDCollectionByPruningMessagesWithIDs(messageIDs, in: transaction) + transaction.removeObject(forKey: "\(publicChat.server).\(publicChat.channel)", inCollection: LokiPublicChatAPI.lastMessageServerIDCollection) + transaction.removeObject(forKey: "\(publicChat.server).\(publicChat.channel)", inCollection: LokiPublicChatAPI.lastDeletionServerIDCollection) + let _ = LokiPublicChatAPI.leave(publicChat.channel, on: publicChat.server) + } + thread.removeAllThreadInteractions(with: transaction) thread.remove(with: transaction) } NotificationCenter.default.post(name: .threadDeleted, object: nil, userInfo: [ "threadId" : thread.uniqueId! ]) - if let publicChat = publicChat { - let _ = LokiPublicChatAPI.leave(publicChat.channel, on: publicChat.server) - } }) alert.addAction(UIAlertAction(title: NSLocalizedString("TXT_CANCEL_TITLE", comment: ""), style: .default) { _ in }) guard let self = self else { return } diff --git a/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.h b/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.h index 698af7894..e72efba84 100644 --- a/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.h +++ b/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.h @@ -35,6 +35,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)setIDForMessageWithServerID:(NSUInteger)serverID to:(NSString *)messageID in:(YapDatabaseReadWriteTransaction *)transaction; - (NSString *_Nullable)getIDForMessageWithServerID:(NSUInteger)serverID in:(YapDatabaseReadTransaction *)transaction; +- (void)updateMessageIDCollectionByPruningMessagesWithIDs:(NSSet *)targetMessageIDs in:(YapDatabaseReadWriteTransaction *)transaction NS_SWIFT_NAME(updateMessageIDCollectionByPruningMessagesWithIDs(_:in:)); @end diff --git a/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.m b/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.m index 0f6e2781f..21bc6b45c 100644 --- a/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.m +++ b/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.m @@ -175,4 +175,15 @@ return [transaction objectForKey:key inCollection:LKMessageIDCollection]; } +- (void)updateMessageIDCollectionByPruningMessagesWithIDs:(NSSet *)targetMessageIDs in:(YapDatabaseReadWriteTransaction *)transaction { + NSMutableArray *serverIDs = [NSMutableArray new]; + [transaction enumerateRowsInCollection:LKMessageIDCollection usingBlock:^(NSString *key, id object, id metadata, BOOL *stop) { + if (![object isKindOfClass:NSString.class]) { return; } + NSString *messageID = (NSString *)object; + if (![targetMessageIDs containsObject:messageID]) { return; } + [serverIDs addObject:key]; + }]; + [transaction removeObjectsForKeys:serverIDs inCollection:LKMessageIDCollection]; +} + @end