session-ios/SignalServiceKit/src/Storage/TSDatabaseView.m

451 lines
18 KiB
Mathematica
Raw Normal View History

2015-12-07 03:31:43 +01:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
2015-12-07 03:31:43 +01:00
//
#import "TSDatabaseView.h"
#import "OWSDevice.h"
#import "OWSReadTracking.h"
2018-03-20 22:22:19 +01:00
#import "TSAttachment.h"
#import "TSAttachmentPointer.h"
2015-12-07 03:31:43 +01:00
#import "TSIncomingMessage.h"
#import "TSInvalidIdentityKeyErrorMessage.h"
#import "TSOutgoingMessage.h"
2015-12-07 03:31:43 +01:00
#import "TSThread.h"
2017-12-12 16:31:05 +01:00
#import <YapDatabase/YapDatabaseAutoView.h>
2017-11-29 17:44:58 +01:00
#import <YapDatabase/YapDatabaseCrossProcessNotification.h>
2017-12-12 16:31:05 +01:00
#import <YapDatabase/YapDatabaseViewTypes.h>
2015-12-07 03:31:43 +01:00
NSString *const TSInboxGroup = @"TSInboxGroup";
NSString *const TSArchiveGroup = @"TSArchiveGroup";
2015-12-07 03:31:43 +01:00
NSString *const TSUnreadIncomingMessagesGroup = @"TSUnreadIncomingMessagesGroup";
NSString *const TSSecondaryDevicesGroup = @"TSSecondaryDevicesGroup";
// YAPDB BUG: when changing from non-persistent to persistent view, we had to rename TSThreadDatabaseViewExtensionName
// -> TSThreadDatabaseViewExtensionName2 to work around https://github.com/yapstudios/YapDatabase/issues/324
NSString *const TSThreadDatabaseViewExtensionName = @"TSThreadDatabaseViewExtensionName2";
2018-09-28 01:40:51 +02:00
NSString *const TSMessageDatabaseViewExtensionName = @"TSMessageDatabaseViewExtensionName";
NSString *const TSThreadOutgoingMessageDatabaseViewExtensionName = @"TSThreadOutgoingMessageDatabaseViewExtensionName";
NSString *const TSUnreadDatabaseViewExtensionName = @"TSUnreadDatabaseViewExtensionName";
NSString *const TSUnseenDatabaseViewExtensionName = @"TSUnseenDatabaseViewExtensionName";
NSString *const TSThreadSpecialMessagesDatabaseViewExtensionName = @"TSThreadSpecialMessagesDatabaseViewExtensionName";
NSString *const TSSecondaryDevicesDatabaseViewExtensionName = @"TSSecondaryDevicesDatabaseViewExtensionName";
2018-03-20 22:22:19 +01:00
NSString *const TSLazyRestoreAttachmentsDatabaseViewExtensionName
= @"TSLazyRestoreAttachmentsDatabaseViewExtensionName";
NSString *const TSLazyRestoreAttachmentsGroup = @"TSLazyRestoreAttachmentsGroup";
2018-01-29 19:40:16 +01:00
@interface OWSStorage (TSDatabaseView)
- (BOOL)registerExtension:(YapDatabaseExtension *)extension withName:(NSString *)extensionName;
@end
#pragma mark -
2015-12-07 03:31:43 +01:00
@implementation TSDatabaseView
+ (void)registerCrossProcessNotifier:(OWSStorage *)storage
2017-11-29 17:44:58 +01:00
{
OWSAssertDebug(storage);
2017-11-29 17:44:58 +01:00
// I don't think the identifier and name of this extension matter for our purposes,
// so long as they don't conflict with any other extension names.
YapDatabaseExtension *extension =
[[YapDatabaseCrossProcessNotification alloc] initWithIdentifier:@"SignalCrossProcessNotifier"];
[storage registerExtension:extension withName:@"SignalCrossProcessNotifier"];
2017-11-29 17:44:58 +01:00
}
+ (void)registerMessageDatabaseViewWithName:(NSString *)viewName
viewGrouping:(YapDatabaseViewGrouping *)viewGrouping
version:(NSString *)version
storage:(OWSStorage *)storage
{
2017-12-19 17:38:25 +01:00
OWSAssertIsOnMainThread();
OWSAssertDebug(viewName.length > 0);
OWSAssertDebug((viewGrouping));
OWSAssertDebug(storage);
YapDatabaseView *existingView = [storage registeredExtension:viewName];
if (existingView) {
2018-08-27 16:29:51 +02:00
OWSFailDebug(@"Registered database view twice: %@", viewName);
return;
2015-12-07 03:31:43 +01:00
}
YapDatabaseViewSorting *viewSorting = [self messagesSorting];
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
options.isPersistent = YES;
options.allowedCollections =
[[YapWhitelistBlacklist alloc] initWithWhitelist:[NSSet setWithObject:[TSInteraction collection]]];
2017-12-12 16:31:05 +01:00
YapDatabaseView *view = [[YapDatabaseAutoView alloc] initWithGrouping:viewGrouping
sorting:viewSorting
versionTag:version
options:options];
2018-01-30 18:39:27 +01:00
[storage asyncRegisterExtension:view withName:viewName];
}
+ (void)asyncRegisterUnreadDatabaseView:(OWSStorage *)storage
{
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *(
YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) {
if ([object conformsToProtocol:@protocol(OWSReadTracking)]) {
id<OWSReadTracking> possiblyRead = (id<OWSReadTracking>)object;
if (!possiblyRead.wasRead && possiblyRead.shouldAffectUnreadCounts) {
return possiblyRead.uniqueThreadId;
}
}
return nil;
}];
2015-12-07 03:31:43 +01:00
[self registerMessageDatabaseViewWithName:TSUnreadDatabaseViewExtensionName
viewGrouping:viewGrouping
2018-09-28 01:40:51 +02:00
version:@"1"
storage:storage];
}
+ (void)asyncRegisterUnseenDatabaseView:(OWSStorage *)storage
{
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *(
YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) {
if ([object conformsToProtocol:@protocol(OWSReadTracking)]) {
id<OWSReadTracking> possiblyRead = (id<OWSReadTracking>)object;
if (!possiblyRead.wasRead) {
return possiblyRead.uniqueThreadId;
}
}
return nil;
}];
[self registerMessageDatabaseViewWithName:TSUnseenDatabaseViewExtensionName
viewGrouping:viewGrouping
2018-09-28 01:40:51 +02:00
version:@"1"
storage:storage];
2015-12-07 03:31:43 +01:00
}
+ (void)asyncRegisterThreadSpecialMessagesDatabaseView:(OWSStorage *)storage
{
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *(
YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) {
if (![object isKindOfClass:[TSInteraction class]]) {
OWSFailDebug(@"Unexpected entity %@ in collection: %@", [object class], collection);
return nil;
}
TSInteraction *interaction = (TSInteraction *)object;
if ([interaction isDynamicInteraction]) {
return interaction.uniqueThreadId;
} else if ([object isKindOfClass:[TSInvalidIdentityKeyErrorMessage class]]) {
2017-05-26 17:00:37 +02:00
return interaction.uniqueThreadId;
2017-05-26 18:59:20 +02:00
} else if ([object isKindOfClass:[TSErrorMessage class]]) {
TSErrorMessage *errorMessage = (TSErrorMessage *)object;
if (errorMessage.errorType == TSErrorMessageNonBlockingIdentityChange) {
return errorMessage.uniqueThreadId;
}
}
return nil;
}];
[self registerMessageDatabaseViewWithName:TSThreadSpecialMessagesDatabaseViewExtensionName
viewGrouping:viewGrouping
2018-09-28 01:40:51 +02:00
version:@"1"
storage:storage];
}
+ (void)asyncRegisterThreadInteractionsDatabaseView:(OWSStorage *)storage
{
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *(
YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) {
if (![object isKindOfClass:[TSInteraction class]]) {
OWSFailDebug(@"Unexpected entity %@ in collection: %@", [object class], collection);
return nil;
}
TSInteraction *interaction = (TSInteraction *)object;
return interaction.uniqueThreadId;
}];
[self registerMessageDatabaseViewWithName:TSMessageDatabaseViewExtensionName
viewGrouping:viewGrouping
2018-09-28 01:40:51 +02:00
version:@"1"
storage:storage];
}
+ (void)asyncRegisterThreadOutgoingMessagesDatabaseView:(OWSStorage *)storage
{
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *(
YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) {
if ([object isKindOfClass:[TSOutgoingMessage class]]) {
return ((TSOutgoingMessage *)object).uniqueThreadId;
}
return nil;
}];
[self registerMessageDatabaseViewWithName:TSThreadOutgoingMessageDatabaseViewExtensionName
viewGrouping:viewGrouping
2018-09-28 01:40:51 +02:00
version:@"2"
storage:storage];
}
+ (void)asyncRegisterThreadDatabaseView:(OWSStorage *)storage
{
YapDatabaseView *threadView = [storage registeredExtension:TSThreadDatabaseViewExtensionName];
2015-12-07 03:31:43 +01:00
if (threadView) {
2018-08-27 16:29:51 +02:00
OWSFailDebug(@"Registered database view twice: %@", TSThreadDatabaseViewExtensionName);
return;
2015-12-07 03:31:43 +01:00
}
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *(
YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) {
if (![object isKindOfClass:[TSThread class]]) {
OWSFailDebug(@"Unexpected entity %@ in collection: %@", [object class], collection);
return nil;
}
TSThread *thread = (TSThread *)object;
if (thread.isGroupThread) {
// Do nothing; we never hide group threads.
} else if (thread.hasEverHadMessage) {
// Do nothing; we never hide threads that have ever had a message.
} else {
YapDatabaseViewTransaction *viewTransaction = [transaction ext:TSMessageDatabaseViewExtensionName];
OWSAssertDebug(viewTransaction);
NSUInteger threadMessageCount = [viewTransaction numberOfItemsInGroup:thread.uniqueId];
if (threadMessageCount < 1) {
return nil;
}
}
2018-09-28 01:40:51 +02:00
if (thread.archivalDate) {
return ([self threadShouldBeInInbox:thread]) ? TSInboxGroup : TSArchiveGroup;
} else if (thread.archivalDate) {
return TSArchiveGroup;
} else {
return TSInboxGroup;
}
}];
2015-12-07 03:31:43 +01:00
YapDatabaseViewSorting *viewSorting = [self threadSorting];
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
2017-07-06 04:31:58 +02:00
options.isPersistent = YES;
2015-12-07 03:31:43 +01:00
options.allowedCollections =
[[YapWhitelistBlacklist alloc] initWithWhitelist:[NSSet setWithObject:[TSThread collection]]];
YapDatabaseView *databaseView =
2018-09-28 01:40:51 +02:00
[[YapDatabaseAutoView alloc] initWithGrouping:viewGrouping sorting:viewSorting versionTag:@"3" options:options];
[storage asyncRegisterExtension:databaseView withName:TSThreadDatabaseViewExtensionName];
2015-12-07 03:31:43 +01:00
}
2018-09-28 01:40:51 +02:00
/**
* Determines whether a thread belongs to the archive or inbox
*
* @param thread TSThread
*
* @return Inbox if true, Archive if false
*/
+ (BOOL)threadShouldBeInInbox:(TSThread *)thread {
NSDate *lastMessageDate = thread.lastMessageDate;
NSDate *archivalDate = thread.archivalDate;
if (lastMessageDate && archivalDate) { // this is what is called
return ([lastMessageDate timeIntervalSinceDate:archivalDate] > 0)
? YES
: NO; // if there hasn't been a new message since the archive date, it's in the archive. an issue is
// that empty threads are always given with a lastmessage date of the present on every launch
} else if (archivalDate) {
return NO;
}
return YES;
}
2015-12-07 03:31:43 +01:00
+ (YapDatabaseViewSorting *)threadSorting {
return [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(YapDatabaseReadTransaction *transaction,
NSString *group,
NSString *collection1,
NSString *key1,
id object1,
NSString *collection2,
NSString *key2,
id object2) {
if (![object1 isKindOfClass:[TSThread class]]) {
OWSFailDebug(@"Unexpected entity %@ in collection: %@", [object1 class], collection1);
return NSOrderedSame;
}
if (![object2 isKindOfClass:[TSThread class]]) {
OWSFailDebug(@"Unexpected entity %@ in collection: %@", [object2 class], collection2);
return NSOrderedSame;
}
TSThread *thread1 = (TSThread *)object1;
TSThread *thread2 = (TSThread *)object2;
if ([group isEqualToString:TSArchiveGroup] || [group isEqualToString:TSInboxGroup]) {
2018-09-28 01:40:51 +02:00
return [thread1.lastMessageDate compare:thread2.lastMessageDate];
}
return NSOrderedSame;
2015-12-07 03:31:43 +01:00
}];
}
+ (YapDatabaseViewSorting *)messagesSorting {
return [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(YapDatabaseReadTransaction *transaction,
NSString *group,
NSString *collection1,
NSString *key1,
id object1,
NSString *collection2,
NSString *key2,
id object2) {
if (![object1 isKindOfClass:[TSInteraction class]]) {
OWSFailDebug(@"Unexpected entity %@ in collection: %@", [object1 class], collection1);
return NSOrderedSame;
}
if (![object2 isKindOfClass:[TSInteraction class]]) {
OWSFailDebug(@"Unexpected entity %@ in collection: %@", [object2 class], collection2);
return NSOrderedSame;
}
TSInteraction *message1 = (TSInteraction *)object1;
TSInteraction *message2 = (TSInteraction *)object2;
return [message1 compareForSorting:message2];
2015-12-07 03:31:43 +01:00
}];
}
+ (void)asyncRegisterSecondaryDevicesDatabaseView:(OWSStorage *)storage
{
2018-03-20 22:22:19 +01:00
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *_Nullable(
YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) {
if (![object isKindOfClass:[OWSDevice class]]) {
OWSFailDebug(@"Unexpected entity %@ in collection: %@", [object class], collection);
return nil;
}
OWSDevice *device = (OWSDevice *)object;
if (![device isPrimaryDevice]) {
return TSSecondaryDevicesGroup;
2018-03-20 22:22:19 +01:00
}
return nil;
}];
YapDatabaseViewSorting *viewSorting = [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(
YapDatabaseReadTransaction *transaction,
NSString *group,
NSString *collection1,
NSString *key1,
id object1,
NSString *collection2,
NSString *key2,
id object2) {
if (![object1 isKindOfClass:[OWSDevice class]]) {
OWSFailDebug(@"Unexpected entity %@ in collection: %@", [object1 class], collection1);
return NSOrderedSame;
}
if (![object2 isKindOfClass:[OWSDevice class]]) {
OWSFailDebug(@"Unexpected entity %@ in collection: %@", [object2 class], collection2);
return NSOrderedSame;
}
OWSDevice *device1 = (OWSDevice *)object1;
OWSDevice *device2 = (OWSDevice *)object2;
return [device2.createdAt compare:device1.createdAt];
}];
YapDatabaseViewOptions *options = [YapDatabaseViewOptions new];
options.isPersistent = YES;
NSSet *deviceCollection = [NSSet setWithObject:[OWSDevice collection]];
options.allowedCollections = [[YapWhitelistBlacklist alloc] initWithWhitelist:deviceCollection];
YapDatabaseView *view =
2017-12-12 16:31:05 +01:00
[[YapDatabaseAutoView alloc] initWithGrouping:viewGrouping sorting:viewSorting versionTag:@"3" options:options];
[storage asyncRegisterExtension:view withName:TSSecondaryDevicesDatabaseViewExtensionName];
}
2018-03-20 22:22:19 +01:00
+ (void)asyncRegisterLazyRestoreAttachmentsDatabaseView:(OWSStorage *)storage
{
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *_Nullable(
YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) {
if (![object isKindOfClass:[TSAttachment class]]) {
OWSFailDebug(@"Unexpected entity %@ in collection: %@", [object class], collection);
2018-03-20 22:22:19 +01:00
return nil;
}
if (![object isKindOfClass:[TSAttachmentPointer class]]) {
2018-03-20 22:22:19 +01:00
return nil;
}
TSAttachmentPointer *attachmentPointer = (TSAttachmentPointer *)object;
if (attachmentPointer.lazyRestoreFragment) {
2018-03-20 22:22:19 +01:00
return TSLazyRestoreAttachmentsGroup;
} else {
return nil;
}
}];
YapDatabaseViewSorting *viewSorting =
[YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(YapDatabaseReadTransaction *transaction,
NSString *group,
NSString *collection1,
NSString *key1,
id object1,
NSString *collection2,
NSString *key2,
id object2) {
if (![object1 isKindOfClass:[TSAttachmentPointer class]]) {
OWSFailDebug(@"Unexpected entity %@ in collection: %@", [object1 class], collection1);
return NSOrderedSame;
}
if (![object2 isKindOfClass:[TSAttachmentPointer class]]) {
OWSFailDebug(@"Unexpected entity %@ in collection: %@", [object2 class], collection2);
return NSOrderedSame;
}
2018-03-20 22:22:19 +01:00
// Specific ordering doesn't matter; we just need a stable ordering.
TSAttachmentPointer *attachmentPointer1 = (TSAttachmentPointer *)object1;
TSAttachmentPointer *attachmentPointer2 = (TSAttachmentPointer *)object2;
return [attachmentPointer1.uniqueId compare:attachmentPointer2.uniqueId];
}];
2018-03-20 22:22:19 +01:00
YapDatabaseViewOptions *options = [YapDatabaseViewOptions new];
options.isPersistent = YES;
options.allowedCollections =
[[YapWhitelistBlacklist alloc] initWithWhitelist:[NSSet setWithObject:[TSAttachment collection]]];
YapDatabaseView *view =
[[YapDatabaseAutoView alloc] initWithGrouping:viewGrouping sorting:viewSorting versionTag:@"4" options:options];
[storage asyncRegisterExtension:view withName:TSLazyRestoreAttachmentsDatabaseViewExtensionName];
2018-03-20 22:22:19 +01:00
}
+ (id)unseenDatabaseViewExtension:(YapDatabaseReadTransaction *)transaction
{
OWSAssertDebug(transaction);
id result = [transaction ext:TSUnseenDatabaseViewExtensionName];
if (!result) {
result = [transaction ext:TSUnreadDatabaseViewExtensionName];
OWSAssertDebug(result);
}
return result;
}
+ (id)threadOutgoingMessageDatabaseView:(YapDatabaseReadTransaction *)transaction
{
OWSAssertDebug(transaction);
id result = [transaction ext:TSThreadOutgoingMessageDatabaseViewExtensionName];
OWSAssertDebug(result);
return result;
}
+ (id)threadSpecialMessagesDatabaseView:(YapDatabaseReadTransaction *)transaction
{
OWSAssertDebug(transaction);
id result = [transaction ext:TSThreadSpecialMessagesDatabaseViewExtensionName];
OWSAssertDebug(result);
return result;
}
2015-12-07 03:31:43 +01:00
@end