session-ios/SignalServiceKit/src/Contacts/TSThread.m

402 lines
13 KiB
Mathematica
Raw Normal View History

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
2015-12-07 03:31:43 +01:00
#import "TSThread.h"
#import "NSDate+OWS.h"
#import "NSString+SSK.h"
#import "OWSPrimaryStorage.h"
#import "OWSReadTracking.h"
#import "TSDatabaseView.h"
2015-12-07 03:31:43 +01:00
#import "TSIncomingMessage.h"
#import "TSInfoMessage.h"
#import "TSInteraction.h"
#import "TSInvalidIdentityKeyReceivingErrorMessage.h"
2015-12-07 03:31:43 +01:00
#import "TSOutgoingMessage.h"
2017-04-17 21:02:53 +02:00
#import <YapDatabase/YapDatabase.h>
2015-12-07 03:31:43 +01:00
NS_ASSUME_NONNULL_BEGIN
2015-12-07 03:31:43 +01:00
@interface TSThread ()
2017-04-17 21:02:53 +02:00
@property (nonatomic) NSDate *creationDate;
2015-12-07 03:31:43 +01:00
@property (nonatomic, copy) NSDate *archivalDate;
2017-04-17 21:02:53 +02:00
@property (nonatomic) NSDate *lastMessageDate;
2015-12-07 03:31:43 +01:00
@property (nonatomic, copy) NSString *messageDraft;
2017-04-19 15:59:49 +02:00
@property (atomic, nullable) NSDate *mutedUntilDate;
2018-04-21 17:12:58 +02:00
- (TSInteraction *)lastInteractionWithTranscation:(YapDatabaseReadTransaction *)transaction;
2015-12-07 03:31:43 +01:00
@end
@implementation TSThread
+ (NSString *)collection {
return @"TSThread";
}
2017-11-15 19:15:48 +01:00
- (instancetype)initWithUniqueId:(NSString *_Nullable)uniqueId
{
2015-12-07 03:31:43 +01:00
self = [super initWithUniqueId:uniqueId];
if (self) {
_archivalDate = nil;
_lastMessageDate = nil;
_creationDate = [NSDate date];
_messageDraft = nil;
}
return self;
}
- (void)removeWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
2018-03-28 16:42:48 +02:00
[self removeAllThreadInteractionsWithTransaction:transaction];
[super removeWithTransaction:transaction];
2018-03-28 16:42:48 +02:00
}
2018-03-28 16:42:48 +02:00
- (void)removeAllThreadInteractionsWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
// We can't safely delete interactions while enumerating them, so
// we collect and delete separately.
//
// We don't want to instantiate the interactions when collecting them
// or when deleting them.
NSMutableArray<NSString *> *interactionIds = [NSMutableArray new];
YapDatabaseViewTransaction *interactionsByThread = [transaction ext:TSMessageDatabaseViewExtensionName];
OWSAssert(interactionsByThread);
[interactionsByThread
enumerateKeysInGroup:self.uniqueId
usingBlock:^(
NSString *_Nonnull collection, NSString *_Nonnull key, NSUInteger index, BOOL *_Nonnull stop) {
[interactionIds addObject:key];
}];
for (NSString *interactionId in interactionIds) {
2018-03-20 18:02:10 +01:00
// We need to fetch each interaction, since [TSInteraction removeWithTransaction:] does important work.
TSInteraction *_Nullable interaction =
[TSInteraction fetchObjectWithUniqueID:interactionId transaction:transaction];
if (!interaction) {
OWSProdLogAndFail(@"%@ couldn't load thread's interaction for deletion.", self.logTag);
continue;
}
[interaction removeWithTransaction:transaction];
}
}
2015-12-07 03:31:43 +01:00
#pragma mark To be subclassed.
- (BOOL)isGroupThread {
2017-12-19 03:17:11 +01:00
OWS_ABSTRACT_METHOD();
return NO;
2015-12-07 03:31:43 +01:00
}
// Override in ContactThread
- (nullable NSString *)contactIdentifier
{
return nil;
}
2015-12-07 03:31:43 +01:00
- (NSString *)name {
2017-12-19 03:17:11 +01:00
OWS_ABSTRACT_METHOD();
2015-12-07 03:31:43 +01:00
return nil;
}
- (NSArray<NSString *> *)recipientIdentifiers
{
2017-12-19 03:17:11 +01:00
OWS_ABSTRACT_METHOD();
return @[];
}
- (nullable UIImage *)image
{
2015-12-07 03:31:43 +01:00
return nil;
}
- (BOOL)hasSafetyNumbers
{
return NO;
}
#pragma mark Interactions
/**
* Iterate over this thread's interactions
*/
- (void)enumerateInteractionsWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
usingBlock:(void (^)(TSInteraction *interaction,
YapDatabaseReadTransaction *transaction))block
{
void (^interactionBlock)(NSString *, NSString *, id, id, NSUInteger, BOOL *) = ^void(NSString *_Nonnull collection,
NSString *_Nonnull key,
id _Nonnull object,
id _Nonnull metadata,
NSUInteger index,
BOOL *_Nonnull stop) {
TSInteraction *interaction = object;
block(interaction, transaction);
};
YapDatabaseViewTransaction *interactionsByThread = [transaction ext:TSMessageDatabaseViewExtensionName];
[interactionsByThread enumerateRowsInGroup:self.uniqueId usingBlock:interactionBlock];
}
/**
* Enumerates all the threads interactions. Note this will explode if you try to create a transaction in the block.
* If you need a transaction, use the sister method: `enumerateInteractionsWithTransaction:usingBlock`
*/
- (void)enumerateInteractionsUsingBlock:(void (^)(TSInteraction *interaction))block
{
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self enumerateInteractionsWithTransaction:transaction
usingBlock:^(
TSInteraction *interaction, YapDatabaseReadTransaction *transaction) {
block(interaction);
}];
}];
}
/**
* Useful for tests and debugging. In production use an enumeration method.
*/
- (NSArray<TSInteraction *> *)allInteractions
{
NSMutableArray<TSInteraction *> *interactions = [NSMutableArray new];
[self enumerateInteractionsUsingBlock:^(TSInteraction *_Nonnull interaction) {
[interactions addObject:interaction];
}];
return [interactions copy];
}
- (NSArray<TSInvalidIdentityKeyReceivingErrorMessage *> *)receivedMessagesForInvalidKey:(NSData *)key
{
NSMutableArray *errorMessages = [NSMutableArray new];
[self enumerateInteractionsUsingBlock:^(TSInteraction *interaction) {
if ([interaction isKindOfClass:[TSInvalidIdentityKeyReceivingErrorMessage class]]) {
TSInvalidIdentityKeyReceivingErrorMessage *error = (TSInvalidIdentityKeyReceivingErrorMessage *)interaction;
if ([[error newIdentityKey] isEqualToData:key]) {
[errorMessages addObject:(TSInvalidIdentityKeyReceivingErrorMessage *)interaction];
}
}
}];
return [errorMessages copy];
}
- (NSUInteger)numberOfInteractions
{
__block NSUInteger count;
[[self dbReadConnection] readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
YapDatabaseViewTransaction *interactionsByThread = [transaction ext:TSMessageDatabaseViewExtensionName];
count = [interactionsByThread numberOfItemsInGroup:self.uniqueId];
}];
return count;
}
2015-12-07 03:31:43 +01:00
2018-04-21 17:12:58 +02:00
- (BOOL)hasUnreadMessagesWithTransaction:(YapDatabaseReadTransaction *)transaction
{
TSInteraction *interaction = [self lastInteractionWithTransaction:transaction];
BOOL hasUnread = NO;
if ([interaction isKindOfClass:[TSIncomingMessage class]]) {
hasUnread = ![(TSIncomingMessage *)interaction wasRead];
}
2015-12-07 03:31:43 +01:00
return hasUnread;
}
- (NSArray<id<OWSReadTracking>> *)unseenMessagesWithTransaction:(YapDatabaseReadTransaction *)transaction
{
NSMutableArray<id<OWSReadTracking>> *messages = [NSMutableArray new];
[[TSDatabaseView unseenDatabaseViewExtension:transaction]
enumerateRowsInGroup:self.uniqueId
usingBlock:^(
NSString *collection, NSString *key, id object, id metadata, NSUInteger index, BOOL *stop) {
if (![object conformsToProtocol:@protocol(OWSReadTracking)]) {
2017-11-08 20:04:51 +01:00
OWSFail(@"%@ Unexpected object in unseen messages: %@", self.logTag, object);
return;
}
[messages addObject:(id<OWSReadTracking>)object];
}];
return [messages copy];
}
2018-04-21 17:12:58 +02:00
- (NSUInteger)unreadMessageCountWithTransaction:(YapDatabaseReadTransaction *)transaction
{
return [[transaction ext:TSUnreadDatabaseViewExtensionName] numberOfItemsInGroup:self.uniqueId];
}
- (void)markAllAsReadWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
for (id<OWSReadTracking> message in [self unseenMessagesWithTransaction:transaction]) {
[message markAsReadAtTimestamp:[NSDate ows_millisecondTimeStamp] sendReadReceipt:YES transaction:transaction];
}
// Just to be defensive, we'll also check for unread messages.
OWSAssert([self unseenMessagesWithTransaction:transaction].count < 1);
}
2018-04-21 17:12:58 +02:00
- (TSInteraction *)lastInteractionWithTransaction:(YapDatabaseReadTransaction *)transaction
{
return [[transaction ext:TSMessageDatabaseViewExtensionName] lastObjectInGroup:self.uniqueId];
}
- (TSInteraction *)lastInteractionForInboxWithTransaction:(YapDatabaseReadTransaction *)transaction
{
__block TSInteraction *last = nil;
[[transaction ext:TSMessageDatabaseViewExtensionName]
enumerateRowsInGroup:self.uniqueId
withOptions:NSEnumerationReverse
usingBlock:^(
NSString *collection, NSString *key, id object, id metadata, NSUInteger index, BOOL *stop) {
OWSAssert([object isKindOfClass:[TSInteraction class]]);
TSInteraction *interaction = (TSInteraction *)object;
if ([TSThread shouldInteractionAppearInInbox:interaction]) {
last = interaction;
*stop = YES;
}
}];
return last;
}
2015-12-07 03:31:43 +01:00
- (NSDate *)lastMessageDate {
if (_lastMessageDate) {
return _lastMessageDate;
} else {
return _creationDate;
}
}
2018-04-21 17:12:58 +02:00
- (NSString *)lastMessageTextWithTransaction:(YapDatabaseReadTransaction *)transaction
{
TSInteraction *interaction = [self lastInteractionForInboxWithTransaction:transaction];
if ([interaction conformsToProtocol:@protocol(OWSPreviewText)]) {
id<OWSPreviewText> previewable = (id<OWSPreviewText>)interaction;
return [previewable previewTextWithTransaction:transaction].filterStringForDisplay;
} else {
return @"";
}
2015-12-07 03:31:43 +01:00
}
// Returns YES IFF the interaction should show up in the inbox as the last message.
+ (BOOL)shouldInteractionAppearInInbox:(TSInteraction *)interaction
{
OWSAssert(interaction);
if (interaction.isDynamicInteraction) {
return NO;
}
if ([interaction isKindOfClass:[TSErrorMessage class]]) {
TSErrorMessage *errorMessage = (TSErrorMessage *)interaction;
if (errorMessage.errorType == TSErrorMessageNonBlockingIdentityChange) {
// Otherwise all group threads with the recipient will percolate to the top of the inbox, even though
// there was no meaningful interaction.
return NO;
}
} else if ([interaction isKindOfClass:[TSInfoMessage class]]) {
TSInfoMessage *infoMessage = (TSInfoMessage *)interaction;
if (infoMessage.messageType == TSInfoMessageVerificationStateChange) {
return NO;
}
}
return YES;
}
2015-12-07 03:31:43 +01:00
- (void)updateWithLastMessage:(TSInteraction *)lastMessage transaction:(YapDatabaseReadWriteTransaction *)transaction {
OWSAssert(lastMessage);
OWSAssert(transaction);
if (![self.class shouldInteractionAppearInInbox:lastMessage]) {
return;
}
self.hasEverHadMessage = YES;
NSDate *lastMessageDate = [lastMessage dateForSorting];
2015-12-07 03:31:43 +01:00
if (!_lastMessageDate || [lastMessageDate timeIntervalSinceDate:self.lastMessageDate] > 0) {
_lastMessageDate = lastMessageDate;
[self saveWithTransaction:transaction];
}
}
#pragma mark Archival
- (nullable NSDate *)archivalDate
{
2015-12-07 03:31:43 +01:00
return _archivalDate;
}
- (void)archiveThreadWithTransaction:(YapDatabaseReadWriteTransaction *)transaction {
[self archiveThreadWithTransaction:transaction referenceDate:[NSDate date]];
}
- (void)archiveThreadWithTransaction:(YapDatabaseReadWriteTransaction *)transaction referenceDate:(NSDate *)date {
[self markAllAsReadWithTransaction:transaction];
_archivalDate = date;
[self saveWithTransaction:transaction];
}
- (void)unarchiveThreadWithTransaction:(YapDatabaseReadWriteTransaction *)transaction {
_archivalDate = nil;
[self saveWithTransaction:transaction];
}
#pragma mark Drafts
- (NSString *)currentDraftWithTransaction:(YapDatabaseReadTransaction *)transaction {
TSThread *thread = [TSThread fetchObjectWithUniqueID:self.uniqueId transaction:transaction];
if (thread.messageDraft) {
return thread.messageDraft;
} else {
return @"";
}
}
- (void)setDraft:(NSString *)draftString transaction:(YapDatabaseReadWriteTransaction *)transaction {
TSThread *thread = [TSThread fetchObjectWithUniqueID:self.uniqueId transaction:transaction];
thread.messageDraft = draftString;
[thread saveWithTransaction:transaction];
}
2017-04-17 21:02:53 +02:00
#pragma mark - Muted
- (BOOL)isMuted
{
NSDate *mutedUntilDate = self.mutedUntilDate;
NSDate *now = [NSDate date];
return (mutedUntilDate != nil &&
[mutedUntilDate timeIntervalSinceDate:now] > 0);
}
2017-11-15 19:15:48 +01:00
#pragma mark - Update With... Methods
2017-04-17 21:02:53 +02:00
- (void)updateWithMutedUntilDate:(NSDate *)mutedUntilDate
{
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
2017-11-15 19:15:48 +01:00
[self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(TSThread *thread) {
[thread setMutedUntilDate:mutedUntilDate];
}];
2017-04-17 21:02:53 +02:00
}];
}
2015-12-07 03:31:43 +01:00
@end
NS_ASSUME_NONNULL_END