From e8d0d9ecc0258e41055783f4636ed9de582f4085 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 13 Jun 2018 17:35:22 -0400 Subject: [PATCH 1/2] Index oversize text for search. --- .../Messages/Attachments/TSAttachmentStream.h | 3 ++ .../Messages/Attachments/TSAttachmentStream.m | 21 +++++++++ .../src/Storage/FullTextSearchFinder.swift | 47 ++++++++++++++++++- 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.h b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.h index a14f48801..e1bf40fd6 100644 --- a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.h +++ b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.h @@ -58,6 +58,9 @@ NS_ASSUME_NONNULL_BEGIN - (BOOL)writeData:(NSData *)data error:(NSError **)error; - (BOOL)writeDataSource:(DataSource *)dataSource; +- (BOOL)isOversizeText; +- (nullable NSString *)readOversizeText; + + (void)deleteAttachments; + (NSString *)attachmentsFolder; diff --git a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m index 1936915c8..ce04045d1 100644 --- a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m +++ b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m @@ -672,6 +672,27 @@ NS_ASSUME_NONNULL_BEGIN return [OWSBackupFragment fetchObjectWithUniqueID:self.lazyRestoreFragmentId]; } +- (BOOL)isOversizeText +{ + return [self.contentType isEqualToString:OWSMimeTypeOversizeTextMessage]; +} + +- (nullable NSString *)readOversizeText +{ + if (!self.isOversizeText) { + OWSFail(@"%@ oversize text attachment has unexpected content type.", self.logTag); + return nil; + } + NSError *error; + NSData *_Nullable data = [self readDataFromFileWithError:&error]; + if (error || !data) { + OWSFail(@"%@ could not read oversize text attachment: %@.", self.logTag, error); + return nil; + } + NSString *_Nullable string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + return string; +} + #pragma mark - Update With... Methods - (void)markForLazyRestoreWithFragment:(OWSBackupFragment *)lazyRestoreFragment diff --git a/SignalServiceKit/src/Storage/FullTextSearchFinder.swift b/SignalServiceKit/src/Storage/FullTextSearchFinder.swift index ab1042aad..3e40c2db1 100644 --- a/SignalServiceKit/src/Storage/FullTextSearchFinder.swift +++ b/SignalServiceKit/src/Storage/FullTextSearchFinder.swift @@ -194,9 +194,48 @@ public class FullTextSearchFinder: NSObject { } private static let messageIndexer: SearchIndexer = SearchIndexer { (message: TSMessage) in - return message.body ?? "" + if let body = message.body, body.count > 0 { + return body + } + if let oversizeText = oversizeText(forMessage: message) { + return oversizeText + } + return "" } + private static func oversizeText(forMessage message: TSMessage) -> String? { + guard message.hasAttachments() else { + return nil + } + guard let dbConnection = dbConnection else { + owsFail("Could not load attachment for search indexing.") + return nil + } + var oversizeText: String? + dbConnection.read({ (transaction) in + guard let attachment = message.attachment(with: transaction) else { + owsFail("Could not load attachment for search indexing.") + return + } + guard let attachmentStream = attachment as? TSAttachmentStream else { + return + } + guard attachmentStream.isOversizeText() else { + return + } + Logger.verbose("attachmentStream: \(attachmentStream.contentType)") + Logger.flush() + guard let text = attachmentStream.readOversizeText() else { + owsFail("Could not load oversize text attachment") + return + } + oversizeText = text + }) + return oversizeText + } + + private static var dbConnection: YapDatabaseConnection? + private class func indexContent(object: Any) -> String? { if let groupThread = object as? TSGroupThread { return self.groupThreadIndexer.index(groupThread) @@ -236,6 +275,12 @@ public class FullTextSearchFinder: NSObject { } private class var dbExtensionConfig: YapDatabaseFullTextSearch { + SwiftAssertIsOnMainThread(#function) + + if dbConnection == nil { + dbConnection = OWSPrimaryStorage.shared().newDatabaseConnection() + } + let contentColumnName = "content" let handler = YapDatabaseFullTextSearchHandler.withObjectBlock { (dict: NSMutableDictionary, _: String, _: String, object: Any) in From 40e5bcc23fc7086617042846147c57e87b9d9a0a Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 14 Jun 2018 16:56:22 -0400 Subject: [PATCH 2/2] Respond to CR. --- .../contacts/SystemContactsFetcher.swift | 1 - .../src/Storage/FullTextSearchFinder.swift | 13 +------------ 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/SignalMessaging/contacts/SystemContactsFetcher.swift b/SignalMessaging/contacts/SystemContactsFetcher.swift index bf81a13a2..12178f179 100644 --- a/SignalMessaging/contacts/SystemContactsFetcher.swift +++ b/SignalMessaging/contacts/SystemContactsFetcher.swift @@ -214,7 +214,6 @@ public class SystemContactsFetcher: NSObject { self.contactStoreAdapter.requestAccess { (granted, error) in if let error = error { Logger.error("\(self.TAG) error fetching contacts: \(error)") - Logger.flush() completion(error) return } diff --git a/SignalServiceKit/src/Storage/FullTextSearchFinder.swift b/SignalServiceKit/src/Storage/FullTextSearchFinder.swift index 3e40c2db1..13557e32f 100644 --- a/SignalServiceKit/src/Storage/FullTextSearchFinder.swift +++ b/SignalServiceKit/src/Storage/FullTextSearchFinder.swift @@ -207,10 +207,7 @@ public class FullTextSearchFinder: NSObject { guard message.hasAttachments() else { return nil } - guard let dbConnection = dbConnection else { - owsFail("Could not load attachment for search indexing.") - return nil - } + let dbConnection = OWSPrimaryStorage.shared().dbReadConnection var oversizeText: String? dbConnection.read({ (transaction) in guard let attachment = message.attachment(with: transaction) else { @@ -223,8 +220,6 @@ public class FullTextSearchFinder: NSObject { guard attachmentStream.isOversizeText() else { return } - Logger.verbose("attachmentStream: \(attachmentStream.contentType)") - Logger.flush() guard let text = attachmentStream.readOversizeText() else { owsFail("Could not load oversize text attachment") return @@ -234,8 +229,6 @@ public class FullTextSearchFinder: NSObject { return oversizeText } - private static var dbConnection: YapDatabaseConnection? - private class func indexContent(object: Any) -> String? { if let groupThread = object as? TSGroupThread { return self.groupThreadIndexer.index(groupThread) @@ -277,10 +270,6 @@ public class FullTextSearchFinder: NSObject { private class var dbExtensionConfig: YapDatabaseFullTextSearch { SwiftAssertIsOnMainThread(#function) - if dbConnection == nil { - dbConnection = OWSPrimaryStorage.shared().newDatabaseConnection() - } - let contentColumnName = "content" let handler = YapDatabaseFullTextSearchHandler.withObjectBlock { (dict: NSMutableDictionary, _: String, _: String, object: Any) in