Add a database view for dynamic interactions.

// FREEBIE
This commit is contained in:
Matthew Chen 2017-05-23 10:12:04 -04:00
parent c7cc023541
commit 6e94b3cccb
7 changed files with 63 additions and 5 deletions

View file

@ -35,6 +35,14 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable NSDate *)receiptDateForSorting;
// "Dynamic" interactions are not messages or static events (like
// info messages, error messages, etc.). They are interactions
// created, updated and deleted by the views.
//
// These include block offers, "add to contact" offers,
// unseen message indicators, etc.
- (BOOL)isDynamicInteraction;
@end
NS_ASSUME_NONNULL_END

View file

@ -109,6 +109,11 @@ NS_ASSUME_NONNULL_BEGIN
[fetchedThread updateWithLastMessage:self transaction:transaction];
}
- (BOOL)isDynamicInteraction
{
return NO;
}
@end
NS_ASSUME_NONNULL_END

View file

@ -42,6 +42,11 @@ NS_ASSUME_NONNULL_BEGIN
return self.date;
}
- (BOOL)isDynamicInteraction
{
return YES;
}
@end
NS_ASSUME_NONNULL_END

View file

@ -44,6 +44,11 @@ NS_ASSUME_NONNULL_BEGIN
return self.date;
}
- (BOOL)isDynamicInteraction
{
return YES;
}
@end
NS_ASSUME_NONNULL_END

View file

@ -1,9 +1,5 @@
//
// TSDatabaseView.h
// TextSecureKit
//
// Created by Frederic Jacobs on 17/11/14.
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import <Foundation/Foundation.h>
@ -19,11 +15,13 @@ extern NSString *TSSecondaryDevicesGroup;
extern NSString *TSThreadDatabaseViewExtensionName;
extern NSString *TSMessageDatabaseViewExtensionName;
extern NSString *TSUnreadDatabaseViewExtensionName;
extern NSString *TSDynamicMessagesDatabaseViewExtensionName;
extern NSString *TSSecondaryDevicesDatabaseViewExtensionName;
+ (BOOL)registerThreadDatabaseView;
+ (BOOL)registerBuddyConversationDatabaseView;
+ (BOOL)registerUnreadDatabaseView;
+ (BOOL)registerDynamicMessagesDatabaseView;
+ (void)asyncRegisterSecondaryDevicesDatabaseView;
@end

View file

@ -22,6 +22,7 @@ NSString *TSSecondaryDevicesGroup = @"TSSecondaryDevicesGroup";
NSString *TSThreadDatabaseViewExtensionName = @"TSThreadDatabaseViewExtensionName";
NSString *TSMessageDatabaseViewExtensionName = @"TSMessageDatabaseViewExtensionName";
NSString *TSUnreadDatabaseViewExtensionName = @"TSUnreadDatabaseViewExtensionName";
NSString *TSDynamicMessagesDatabaseViewExtensionName = @"TSDynamicMessagesDatabaseViewExtensionName";
NSString *TSSecondaryDevicesDatabaseViewExtensionName = @"TSSecondaryDevicesDatabaseViewExtensionName";
@implementation TSDatabaseView
@ -58,6 +59,41 @@ NSString *TSSecondaryDevicesDatabaseViewExtensionName = @"TSSecondaryDevicesData
[[TSStorageManager sharedManager].database registerExtension:view withName:TSUnreadDatabaseViewExtensionName];
}
+ (BOOL)registerDynamicMessagesDatabaseView
{
YapDatabaseView *existingView =
[[TSStorageManager sharedManager].database registeredExtension:TSDynamicMessagesDatabaseViewExtensionName];
if (existingView) {
return YES;
}
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *(
YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) {
if ([object isKindOfClass:[TSInteraction class]]) {
TSInteraction *interaction = (TSInteraction *)object;
if ([interaction isDynamicInteraction]) {
return interaction.uniqueThreadId;
}
} else {
OWSAssert(0);
}
return nil;
}];
YapDatabaseViewSorting *viewSorting = [self messagesSorting];
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
options.isPersistent = YES;
options.allowedCollections =
[[YapWhitelistBlacklist alloc] initWithWhitelist:[NSSet setWithObject:[TSInteraction collection]]];
YapDatabaseView *view =
[[YapDatabaseView alloc] initWithGrouping:viewGrouping sorting:viewSorting versionTag:@"1" options:options];
return [[TSStorageManager sharedManager].database registerExtension:view
withName:TSDynamicMessagesDatabaseViewExtensionName];
}
+ (BOOL)registerThreadDatabaseView {
YapDatabaseView *threadView =
[[TSStorageManager sharedManager].database registeredExtension:TSThreadDatabaseViewExtensionName];

View file

@ -199,6 +199,7 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass";
[TSDatabaseView registerThreadDatabaseView];
[TSDatabaseView registerBuddyConversationDatabaseView];
[TSDatabaseView registerUnreadDatabaseView];
[TSDatabaseView registerDynamicMessagesDatabaseView];
[self.database registerExtension:[TSDatabaseSecondaryIndexes registerTimeStampIndex] withName:@"idx"];
// Register extensions which aren't essential for rendering threads async