2017-02-07 21:09:04 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
2015-12-07 03:31:43 +01:00
|
|
|
|
|
|
|
#import "TSInteraction.h"
|
|
|
|
#import "TSDatabaseSecondaryIndexes.h"
|
|
|
|
#import "TSStorageManager+messageIDs.h"
|
2016-07-28 01:58:49 +02:00
|
|
|
#import "TSThread.h"
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2017-05-04 23:49:44 +02:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
@implementation TSInteraction
|
|
|
|
|
|
|
|
+ (instancetype)interactionForTimestamp:(uint64_t)timestamp
|
|
|
|
withTransaction:(YapDatabaseReadWriteTransaction *)transaction {
|
|
|
|
__block int counter = 0;
|
|
|
|
__block TSInteraction *interaction;
|
|
|
|
|
|
|
|
[TSDatabaseSecondaryIndexes
|
|
|
|
enumerateMessagesWithTimestamp:timestamp
|
|
|
|
withBlock:^(NSString *collection, NSString *key, BOOL *stop) {
|
|
|
|
|
2016-08-22 22:09:58 +02:00
|
|
|
if (counter != 0) {
|
|
|
|
DDLogWarn(@"The database contains two colliding timestamps at: %lld.", timestamp);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2016-08-22 22:09:58 +02:00
|
|
|
interaction = [TSInteraction fetchObjectWithUniqueID:key transaction:transaction];
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2016-08-22 22:09:58 +02:00
|
|
|
counter++;
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
usingTransaction:transaction];
|
|
|
|
|
|
|
|
return interaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *)collection {
|
|
|
|
return @"TSInteraction";
|
|
|
|
}
|
|
|
|
|
2016-08-22 22:09:58 +02:00
|
|
|
- (instancetype)initWithTimestamp:(uint64_t)timestamp inThread:(TSThread *)thread
|
|
|
|
{
|
|
|
|
self = [super initWithUniqueId:nil];
|
|
|
|
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
_timestamp = timestamp;
|
|
|
|
_uniqueThreadId = thread.uniqueId;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-02-07 21:09:04 +01:00
|
|
|
|
|
|
|
#pragma mark Thread
|
|
|
|
|
2016-08-22 22:09:58 +02:00
|
|
|
- (TSThread *)thread
|
|
|
|
{
|
|
|
|
return [TSThread fetchObjectWithUniqueID:self.uniqueThreadId];
|
|
|
|
}
|
|
|
|
|
2017-02-07 21:09:04 +01:00
|
|
|
- (void)touchThreadWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
|
|
{
|
|
|
|
TSThread *thread = [TSThread fetchObjectWithUniqueID:self.uniqueThreadId transaction:transaction];
|
|
|
|
[thread touchWithTransaction:transaction];
|
|
|
|
}
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
#pragma mark Date operations
|
|
|
|
|
|
|
|
- (uint64_t)millisecondsTimestamp {
|
|
|
|
return self.timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDate *)date {
|
|
|
|
uint64_t seconds = self.timestamp / 1000;
|
|
|
|
return [NSDate dateWithTimeIntervalSince1970:seconds];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *)stringFromTimeStamp:(uint64_t)timestamp {
|
|
|
|
return [[NSNumber numberWithUnsignedLongLong:timestamp] stringValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (uint64_t)timeStampFromString:(NSString *)string {
|
|
|
|
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
|
|
|
|
[f setNumberStyle:NSNumberFormatterNoStyle];
|
|
|
|
NSNumber *myNumber = [f numberFromString:string];
|
|
|
|
return [myNumber unsignedLongLongValue];
|
|
|
|
}
|
|
|
|
|
2017-02-16 21:39:56 +01:00
|
|
|
- (nullable NSDate *)receiptDateForSorting
|
|
|
|
{
|
|
|
|
return self.date;
|
|
|
|
}
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
- (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];
|
|
|
|
}
|
|
|
|
|
2017-05-23 16:12:04 +02:00
|
|
|
- (BOOL)isDynamicInteraction
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
@end
|
2017-05-04 23:49:44 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|