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/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..13557e32f 100644 --- a/SignalServiceKit/src/Storage/FullTextSearchFinder.swift +++ b/SignalServiceKit/src/Storage/FullTextSearchFinder.swift @@ -194,7 +194,39 @@ 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 + } + let dbConnection = OWSPrimaryStorage.shared().dbReadConnection + 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 + } + guard let text = attachmentStream.readOversizeText() else { + owsFail("Could not load oversize text attachment") + return + } + oversizeText = text + }) + return oversizeText } private class func indexContent(object: Any) -> String? { @@ -236,6 +268,8 @@ public class FullTextSearchFinder: NSObject { } private class var dbExtensionConfig: YapDatabaseFullTextSearch { + SwiftAssertIsOnMainThread(#function) + let contentColumnName = "content" let handler = YapDatabaseFullTextSearchHandler.withObjectBlock { (dict: NSMutableDictionary, _: String, _: String, object: Any) in