mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
423 lines
14 KiB
Matlab
423 lines
14 KiB
Matlab
8 years ago
|
//
|
||
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
||
|
//
|
||
10 years ago
|
|
||
|
#import "TSThread.h"
|
||
8 years ago
|
#import "OWSReadTracking.h"
|
||
9 years ago
|
#import "TSDatabaseView.h"
|
||
10 years ago
|
#import "TSIncomingMessage.h"
|
||
8 years ago
|
#import "TSInfoMessage.h"
|
||
9 years ago
|
#import "TSInteraction.h"
|
||
9 years ago
|
#import "TSInvalidIdentityKeyReceivingErrorMessage.h"
|
||
10 years ago
|
#import "TSOutgoingMessage.h"
|
||
9 years ago
|
#import "TSStorageManager.h"
|
||
8 years ago
|
#import <YapDatabase/YapDatabase.h>
|
||
|
#import <YapDatabase/YapDatabaseTransaction.h>
|
||
10 years ago
|
|
||
9 years ago
|
NS_ASSUME_NONNULL_BEGIN
|
||
|
|
||
10 years ago
|
@interface TSThread ()
|
||
|
|
||
8 years ago
|
@property (nonatomic) NSDate *creationDate;
|
||
10 years ago
|
@property (nonatomic, copy) NSDate *archivalDate;
|
||
8 years ago
|
@property (nonatomic) NSDate *lastMessageDate;
|
||
10 years ago
|
@property (nonatomic, copy) NSString *messageDraft;
|
||
8 years ago
|
@property (atomic, nullable) NSDate *mutedUntilDate;
|
||
9 years ago
|
|
||
9 years ago
|
- (TSInteraction *)lastInteraction;
|
||
9 years ago
|
|
||
10 years ago
|
@end
|
||
|
|
||
|
@implementation TSThread
|
||
|
|
||
|
+ (NSString *)collection {
|
||
|
return @"TSThread";
|
||
|
}
|
||
|
|
||
|
- (instancetype)initWithUniqueId:(NSString *)uniqueId {
|
||
|
self = [super initWithUniqueId:uniqueId];
|
||
|
|
||
|
if (self) {
|
||
|
_archivalDate = nil;
|
||
|
_lastMessageDate = nil;
|
||
|
_creationDate = [NSDate date];
|
||
|
_messageDraft = nil;
|
||
|
}
|
||
|
|
||
|
return self;
|
||
|
}
|
||
|
|
||
9 years ago
|
- (void)removeWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
|
||
|
{
|
||
|
[super removeWithTransaction:transaction];
|
||
|
|
||
|
__block NSMutableArray<NSString *> *interactionIds = [[NSMutableArray alloc] init];
|
||
|
[self enumerateInteractionsWithTransaction:transaction
|
||
|
usingBlock:^(TSInteraction *interaction, YapDatabaseReadTransaction *transaction) {
|
||
|
[interactionIds addObject:interaction.uniqueId];
|
||
|
}];
|
||
|
|
||
|
for (NSString *interactionId in interactionIds) {
|
||
|
// This might seem redundant since we're fetching the interaction twice, once above to get the uniqueIds
|
||
|
// and then again here. The issue is we can't remove them within the enumeration (you can't mutate an
|
||
|
// enumeration source), but we also want to avoid instantiating an entire threads worth of Interaction objects
|
||
|
// at once. This way we only have a threads worth of interactionId's.
|
||
|
TSInteraction *interaction = [TSInteraction fetchObjectWithUniqueID:interactionId transaction:transaction];
|
||
|
[interaction removeWithTransaction:transaction];
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
#pragma mark To be subclassed.
|
||
|
|
||
|
- (BOOL)isGroupThread {
|
||
|
NSAssert(false, @"An abstract method on TSThread was called.");
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
9 years ago
|
// Override in ContactThread
|
||
|
- (nullable NSString *)contactIdentifier
|
||
|
{
|
||
|
return nil;
|
||
|
}
|
||
|
|
||
10 years ago
|
- (NSString *)name {
|
||
|
NSAssert(FALSE, @"Should be implemented in subclasses");
|
||
|
return nil;
|
||
|
}
|
||
|
|
||
8 years ago
|
- (NSArray<NSString *> *)recipientIdentifiers
|
||
|
{
|
||
|
NSAssert(FALSE, @"Should be implemented in subclasses");
|
||
|
return @[];
|
||
|
}
|
||
|
|
||
9 years ago
|
- (nullable UIImage *)image
|
||
|
{
|
||
10 years ago
|
return nil;
|
||
|
}
|
||
|
|
||
9 years ago
|
- (BOOL)hasSafetyNumbers
|
||
|
{
|
||
|
return NO;
|
||
|
}
|
||
|
|
||
9 years ago
|
#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];
|
||
|
}
|
||
|
|
||
9 years ago
|
/**
|
||
|
* 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
|
||
|
{
|
||
8 years ago
|
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
||
9 years ago
|
[self enumerateInteractionsWithTransaction:transaction
|
||
|
usingBlock:^(
|
||
|
TSInteraction *interaction, YapDatabaseReadTransaction *transaction) {
|
||
|
|
||
|
block(interaction);
|
||
|
}];
|
||
|
}];
|
||
|
}
|
||
|
|
||
9 years ago
|
/**
|
||
|
* 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];
|
||
|
}
|
||
|
|
||
9 years ago
|
- (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];
|
||
|
}
|
||
|
|
||
9 years ago
|
- (NSUInteger)numberOfInteractions
|
||
|
{
|
||
|
__block NSUInteger count;
|
||
8 years ago
|
[[self dbReadConnection] readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
|
||
9 years ago
|
YapDatabaseViewTransaction *interactionsByThread = [transaction ext:TSMessageDatabaseViewExtensionName];
|
||
|
count = [interactionsByThread numberOfItemsInGroup:self.uniqueId];
|
||
|
}];
|
||
|
return count;
|
||
|
}
|
||
10 years ago
|
|
||
|
- (BOOL)hasUnreadMessages {
|
||
9 years ago
|
TSInteraction *interaction = self.lastInteraction;
|
||
|
BOOL hasUnread = NO;
|
||
|
|
||
|
if ([interaction isKindOfClass:[TSIncomingMessage class]]) {
|
||
|
hasUnread = ![(TSIncomingMessage *)interaction wasRead];
|
||
|
}
|
||
10 years ago
|
|
||
|
return hasUnread;
|
||
|
}
|
||
|
|
||
8 years ago
|
- (NSArray<id<OWSReadTracking>> *)unseenMessagesWithTransaction:(YapDatabaseReadTransaction *)transaction
|
||
|
{
|
||
|
NSMutableArray<id<OWSReadTracking>> *messages = [NSMutableArray new];
|
||
8 years ago
|
[[TSDatabaseView unseenDatabaseViewExtension:transaction]
|
||
8 years ago
|
enumerateRowsInGroup:self.uniqueId
|
||
|
usingBlock:^(
|
||
|
NSString *collection, NSString *key, id object, id metadata, NSUInteger index, BOOL *stop) {
|
||
|
|
||
|
if (![object conformsToProtocol:@protocol(OWSReadTracking)]) {
|
||
8 years ago
|
OWSFail(@"%@ Unexpected object in unseen messages: %@", self.tag, object);
|
||
8 years ago
|
return;
|
||
8 years ago
|
}
|
||
|
[messages addObject:(id<OWSReadTracking>)object];
|
||
|
}];
|
||
|
|
||
|
return [messages copy];
|
||
|
}
|
||
|
|
||
8 years ago
|
- (NSArray<id<OWSReadTracking> > *)unreadMessagesWithTransaction:(YapDatabaseReadTransaction *)transaction
|
||
9 years ago
|
{
|
||
8 years ago
|
NSMutableArray<id<OWSReadTracking> > *messages = [NSMutableArray new];
|
||
9 years ago
|
[[transaction ext:TSUnreadDatabaseViewExtensionName]
|
||
10 years ago
|
enumerateRowsInGroup:self.uniqueId
|
||
|
usingBlock:^(
|
||
|
NSString *collection, NSString *key, id object, id metadata, NSUInteger index, BOOL *stop) {
|
||
9 years ago
|
|
||
8 years ago
|
if (![object conformsToProtocol:@protocol(OWSReadTracking)]) {
|
||
9 years ago
|
DDLogError(@"%@ Unexpected object in unread messages: %@", self.tag, object);
|
||
|
}
|
||
8 years ago
|
[messages addObject:(id<OWSReadTracking>)object];
|
||
10 years ago
|
}];
|
||
|
|
||
9 years ago
|
return [messages copy];
|
||
|
}
|
||
|
|
||
|
- (void)markAllAsReadWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
|
||
|
{
|
||
8 years ago
|
for (id<OWSReadTracking> message in [self unseenMessagesWithTransaction:transaction]) {
|
||
8 years ago
|
[message markAsReadWithTransaction:transaction sendReadReceipt:YES updateExpiration:YES];
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
|
// Just to be defensive, we'll also check for unread messages.
|
||
8 years ago
|
OWSAssert([self unseenMessagesWithTransaction:transaction].count < 1);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
- (TSInteraction *) lastInteraction {
|
||
|
__block TSInteraction *last;
|
||
8 years ago
|
[TSStorageManager.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
||
9 years ago
|
last = [[transaction ext:TSMessageDatabaseViewExtensionName] lastObjectInGroup:self.uniqueId];
|
||
|
}];
|
||
8 years ago
|
return last;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
- (TSInteraction *)lastInteractionForInbox
|
||
|
{
|
||
8 years ago
|
__block TSInteraction *last = nil;
|
||
8 years ago
|
[TSStorageManager.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
||
8 years ago
|
[[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;
|
||
|
}
|
||
|
}];
|
||
8 years ago
|
}];
|
||
8 years ago
|
return last;
|
||
8 years ago
|
}
|
||
|
|
||
10 years ago
|
- (NSDate *)lastMessageDate {
|
||
|
if (_lastMessageDate) {
|
||
|
return _lastMessageDate;
|
||
|
} else {
|
||
|
return _creationDate;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
- (NSString *)lastMessageLabel {
|
||
8 years ago
|
TSInteraction *interaction = self.lastInteractionForInbox;
|
||
|
if (interaction == nil) {
|
||
9 years ago
|
return @"";
|
||
|
} else {
|
||
8 years ago
|
return interaction.description;
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
// Returns YES IFF the interaction should show up in the inbox as the last message.
|
||
8 years ago
|
+ (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;
|
||
|
}
|
||
|
|
||
10 years ago
|
- (void)updateWithLastMessage:(TSInteraction *)lastMessage transaction:(YapDatabaseReadWriteTransaction *)transaction {
|
||
8 years ago
|
OWSAssert(lastMessage);
|
||
|
OWSAssert(transaction);
|
||
|
|
||
8 years ago
|
if (![self.class shouldInteractionAppearInInbox:lastMessage]) {
|
||
8 years ago
|
return;
|
||
|
}
|
||
|
|
||
8 years ago
|
NSDate *lastMessageDate = [lastMessage dateForSorting];
|
||
10 years ago
|
if (!_lastMessageDate || [lastMessageDate timeIntervalSinceDate:self.lastMessageDate] > 0) {
|
||
|
_lastMessageDate = lastMessageDate;
|
||
|
|
||
|
[self saveWithTransaction:transaction];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#pragma mark Archival
|
||
|
|
||
9 years ago
|
- (nullable NSDate *)archivalDate
|
||
|
{
|
||
10 years ago
|
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];
|
||
|
}
|
||
|
|
||
8 years ago
|
#pragma mark - Muted
|
||
|
|
||
|
- (BOOL)isMuted
|
||
|
{
|
||
|
NSDate *mutedUntilDate = self.mutedUntilDate;
|
||
|
NSDate *now = [NSDate date];
|
||
|
return (mutedUntilDate != nil &&
|
||
|
[mutedUntilDate timeIntervalSinceDate:now] > 0);
|
||
|
}
|
||
|
|
||
|
// This method does the work for the "updateWith..." methods. Please see
|
||
|
// the header for a discussion of those methods.
|
||
|
- (void)applyChangeToSelfAndLatestThread:(YapDatabaseReadWriteTransaction *)transaction
|
||
|
changeBlock:(void (^)(TSThread *))changeBlock
|
||
|
{
|
||
|
OWSAssert(transaction);
|
||
|
|
||
|
changeBlock(self);
|
||
|
|
||
|
NSString *collection = [[self class] collection];
|
||
|
TSThread *latestInstance = [transaction objectForKey:self.uniqueId inCollection:collection];
|
||
|
if (latestInstance) {
|
||
|
changeBlock(latestInstance);
|
||
|
[latestInstance saveWithTransaction:transaction];
|
||
|
} else {
|
||
|
// This message has not yet been saved.
|
||
|
[self saveWithTransaction:transaction];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
- (void)updateWithMutedUntilDate:(NSDate *)mutedUntilDate
|
||
|
{
|
||
8 years ago
|
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
||
8 years ago
|
[self applyChangeToSelfAndLatestThread:transaction
|
||
|
changeBlock:^(TSThread *thread) {
|
||
|
[thread setMutedUntilDate:mutedUntilDate];
|
||
|
}];
|
||
|
}];
|
||
|
}
|
||
|
|
||
9 years ago
|
#pragma mark - Logging
|
||
|
|
||
|
+ (NSString *)tag
|
||
|
{
|
||
|
return [NSString stringWithFormat:@"[%@]", self.class];
|
||
|
}
|
||
|
|
||
|
- (NSString *)tag
|
||
|
{
|
||
|
return self.class.tag;
|
||
|
}
|
||
|
|
||
10 years ago
|
@end
|
||
9 years ago
|
|
||
|
NS_ASSUME_NONNULL_END
|