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.
149 lines
4.0 KiB
Objective-C
149 lines
4.0 KiB
Objective-C
//
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
#import "TSInteraction.h"
|
|
#import "NSDate+OWS.h"
|
|
#import "TSDatabaseSecondaryIndexes.h"
|
|
#import "TSStorageManager+messageIDs.h"
|
|
#import "TSThread.h"
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
@implementation TSInteraction
|
|
|
|
+ (NSArray<TSInteraction *> *)interactionsWithTimestamp:(uint64_t)timestamp
|
|
ofClass:(Class)clazz
|
|
withTransaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
{
|
|
OWSAssert(timestamp > 0);
|
|
|
|
// Accept any interaction.
|
|
return [self interactionsWithTimestamp:timestamp
|
|
filter:^(TSInteraction *interaction) {
|
|
return [interaction isKindOfClass:clazz];
|
|
}
|
|
withTransaction:transaction];
|
|
}
|
|
|
|
+ (NSArray<TSInteraction *> *)interactionsWithTimestamp:(uint64_t)timestamp
|
|
filter:(BOOL (^_Nonnull)(TSInteraction *))filter
|
|
withTransaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
{
|
|
OWSAssert(timestamp > 0);
|
|
|
|
NSMutableArray<TSInteraction *> *interactions = [NSMutableArray new];
|
|
|
|
[TSDatabaseSecondaryIndexes
|
|
enumerateMessagesWithTimestamp:timestamp
|
|
withBlock:^(NSString *collection, NSString *key, BOOL *stop) {
|
|
|
|
TSInteraction *interaction =
|
|
[TSInteraction fetchObjectWithUniqueID:key transaction:transaction];
|
|
if (!filter(interaction)) {
|
|
return;
|
|
}
|
|
[interactions addObject:interaction];
|
|
}
|
|
usingTransaction:transaction];
|
|
|
|
return [interactions copy];
|
|
}
|
|
|
|
+ (NSString *)collection {
|
|
return @"TSInteraction";
|
|
}
|
|
|
|
- (instancetype)initWithTimestamp:(uint64_t)timestamp inThread:(TSThread *)thread
|
|
{
|
|
OWSAssert(timestamp > 0);
|
|
|
|
self = [super initWithUniqueId:nil];
|
|
|
|
if (!self) {
|
|
return self;
|
|
}
|
|
|
|
_timestamp = timestamp;
|
|
_uniqueThreadId = thread.uniqueId;
|
|
|
|
return self;
|
|
}
|
|
|
|
#pragma mark Thread
|
|
|
|
- (TSThread *)thread
|
|
{
|
|
return [TSThread fetchObjectWithUniqueID:self.uniqueThreadId];
|
|
}
|
|
|
|
- (void)touchThreadWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
{
|
|
TSThread *thread = [TSThread fetchObjectWithUniqueID:self.uniqueThreadId transaction:transaction];
|
|
[thread touchWithTransaction:transaction];
|
|
}
|
|
|
|
#pragma mark Date operations
|
|
|
|
- (uint64_t)millisecondsTimestamp {
|
|
return self.timestamp;
|
|
}
|
|
|
|
- (NSDate *)dateForSorting
|
|
{
|
|
return [NSDate ows_dateWithMillisecondsSince1970:self.timestampForSorting];
|
|
}
|
|
|
|
- (uint64_t)timestampForSorting
|
|
{
|
|
return self.timestamp;
|
|
}
|
|
|
|
- (NSComparisonResult)compareForSorting:(TSInteraction *)other
|
|
{
|
|
OWSAssert(other);
|
|
|
|
uint64_t timestamp1 = self.timestampForSorting;
|
|
uint64_t timestamp2 = other.timestampForSorting;
|
|
|
|
if (timestamp1 > timestamp2) {
|
|
return NSOrderedDescending;
|
|
} else if (timestamp1 < timestamp2) {
|
|
return NSOrderedAscending;
|
|
} else {
|
|
return NSOrderedSame;
|
|
}
|
|
}
|
|
|
|
- (NSString *)description {
|
|
return @"Interaction description";
|
|
}
|
|
|
|
- (void)saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction {
|
|
if (!self.uniqueId) {
|
|
self.uniqueId = [TSStorageManager getAndIncrementMessageIdWithTransaction:transaction];
|
|
}
|
|
|
|
[super saveWithTransaction:transaction];
|
|
|
|
TSThread *fetchedThread = [TSThread fetchObjectWithUniqueID:self.uniqueThreadId transaction:transaction];
|
|
|
|
[fetchedThread updateWithLastMessage:self transaction:transaction];
|
|
}
|
|
|
|
- (void)removeWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
{
|
|
[super removeWithTransaction:transaction];
|
|
|
|
[self touchThreadWithTransaction:transaction];
|
|
}
|
|
|
|
- (BOOL)isDynamicInteraction
|
|
{
|
|
return NO;
|
|
}
|
|
|
|
@end
|
|
|
|
NS_ASSUME_NONNULL_END
|