diff --git a/src/Contacts/TSThread.m b/src/Contacts/TSThread.m index aac051814..cd2b5ac71 100644 --- a/src/Contacts/TSThread.m +++ b/src/Contacts/TSThread.m @@ -246,12 +246,7 @@ NS_ASSUME_NONNULL_BEGIN } - (void)updateWithLastMessage:(TSInteraction *)lastMessage transaction:(YapDatabaseReadWriteTransaction *)transaction { - NSDate *lastMessageDate = lastMessage.date; - - if ([lastMessage isKindOfClass:[TSIncomingMessage class]]) { - TSIncomingMessage *message = (TSIncomingMessage *)lastMessage; - lastMessageDate = message.receivedAt; - } + NSDate *lastMessageDate = [lastMessage receiptDateForSorting]; if (!_lastMessageDate || [lastMessageDate timeIntervalSinceDate:self.lastMessageDate] > 0) { _lastMessageDate = lastMessageDate; diff --git a/src/Messages/Interactions/TSIncomingMessage.h b/src/Messages/Interactions/TSIncomingMessage.h index 2da3622e3..f7804ea2b 100644 --- a/src/Messages/Interactions/TSIncomingMessage.h +++ b/src/Messages/Interactions/TSIncomingMessage.h @@ -121,7 +121,6 @@ extern NSString *const TSIncomingMessageWasReadOnThisDeviceNotification; @property (nonatomic, readonly) NSString *authorId; @property (nonatomic, readonly, getter=wasRead) BOOL read; -@property (nonatomic, readonly) NSDate *receivedAt; /* * Marks a message as having been read on this device (as opposed to responding to a remote read receipt). diff --git a/src/Messages/Interactions/TSIncomingMessage.m b/src/Messages/Interactions/TSIncomingMessage.m index 5cec33496..e10acedc8 100644 --- a/src/Messages/Interactions/TSIncomingMessage.m +++ b/src/Messages/Interactions/TSIncomingMessage.m @@ -61,7 +61,8 @@ NSString *const TSIncomingMessageWasReadOnThisDeviceNotification = @"TSIncomingM _authorId = authorId; _read = NO; - _receivedAt = [NSDate date]; + + OWSAssert(self.receivedAtDate); return self; } diff --git a/src/Messages/Interactions/TSInteraction.h b/src/Messages/Interactions/TSInteraction.h index 8ec6fa1e7..6f7c4eeee 100644 --- a/src/Messages/Interactions/TSInteraction.h +++ b/src/Messages/Interactions/TSInteraction.h @@ -31,5 +31,6 @@ + (instancetype)interactionForTimestamp:(uint64_t)timestamp withTransaction:(YapDatabaseReadWriteTransaction *)transaction; +- (nullable NSDate *)receiptDateForSorting; @end diff --git a/src/Messages/Interactions/TSInteraction.m b/src/Messages/Interactions/TSInteraction.m index bab5b4c68..7d09ae755 100644 --- a/src/Messages/Interactions/TSInteraction.m +++ b/src/Messages/Interactions/TSInteraction.m @@ -86,6 +86,11 @@ return [myNumber unsignedLongLongValue]; } +- (nullable NSDate *)receiptDateForSorting +{ + return self.date; +} + - (NSString *)description { return @"Interaction description"; } diff --git a/src/Messages/Interactions/TSMessage.h b/src/Messages/Interactions/TSMessage.h index 47179e77c..65716cda9 100644 --- a/src/Messages/Interactions/TSMessage.h +++ b/src/Messages/Interactions/TSMessage.h @@ -1,5 +1,6 @@ -// Created by Frederic Jacobs on 12/11/14. -// Copyright (c) 2014 Open Whisper Systems. All rights reserved. +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// #import "TSInteraction.h" @@ -29,6 +30,9 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) { @property (nonatomic, readonly) uint64_t expiresAt; @property (nonatomic, readonly) BOOL isExpiringMessage; @property (nonatomic, readonly) BOOL shouldStartExpireTimer; +// _DO NOT_ access this property directly. You almost certainly +// want to use receiptDateForSorting instead. +@property (nonatomic, readonly) NSDate *receivedAtDate; - (instancetype)initWithTimestamp:(uint64_t)timestamp; diff --git a/src/Messages/Interactions/TSMessage.m b/src/Messages/Interactions/TSMessage.m index 4b893c7b6..92e4a784f 100644 --- a/src/Messages/Interactions/TSMessage.m +++ b/src/Messages/Interactions/TSMessage.m @@ -102,6 +102,7 @@ static const NSUInteger OWSMessageSchemaVersion = 3; _expiresInSeconds = expiresInSeconds; _expireStartedAt = expireStartedAt; [self updateExpiresAt]; + _receivedAtDate = [NSDate date]; return self; } @@ -131,7 +132,14 @@ static const NSUInteger OWSMessageSchemaVersion = 3; _attachmentIds = [NSMutableArray new]; } + if (!_receivedAtDate) { + // TSIncomingMessage.receivedAt has been superceded by TSMessage.receivedAtDate. + NSDate *receivedAt = [coder decodeObjectForKey:@"receivedAt"]; + _receivedAtDate = receivedAt; + } + _schemaVersion = OWSMessageSchemaVersion; + return self; } @@ -215,6 +223,24 @@ static const NSUInteger OWSMessageSchemaVersion = 3; return self.expiresInSeconds > 0; } +- (nullable NSDate *)receiptDateForSorting +{ + // Prefer receivedAtDate if set, otherwise fallback to date. + return self.receivedAtDate ?: self.date; +} + +#pragma mark - Logging + ++ (NSString *)tag +{ + return [NSString stringWithFormat:@"[%@]", self.class]; +} + +- (NSString *)tag +{ + return self.class.tag; +} + @end NS_ASSUME_NONNULL_END diff --git a/src/Messages/Interactions/TSOutgoingMessage.h b/src/Messages/Interactions/TSOutgoingMessage.h index ff6064da5..9751088fd 100644 --- a/src/Messages/Interactions/TSOutgoingMessage.h +++ b/src/Messages/Interactions/TSOutgoingMessage.h @@ -1,5 +1,6 @@ -// Created by Frederic Jacobs on 15/11/14. -// Copyright (c) 2014 Open Whisper Systems. All rights reserved. +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// #import "TSMessage.h" diff --git a/src/Messages/Interactions/TSOutgoingMessage.m b/src/Messages/Interactions/TSOutgoingMessage.m index e0e4c3bca..de6ff2cc8 100644 --- a/src/Messages/Interactions/TSOutgoingMessage.m +++ b/src/Messages/Interactions/TSOutgoingMessage.m @@ -1,5 +1,6 @@ -// Created by Frederic Jacobs on 15/11/14. -// Copyright (c) 2014 Open Whisper Systems. All rights reserved. +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// #import "TSOutgoingMessage.h" #import "NSDate+millisecondTimeStamp.h" @@ -86,6 +87,8 @@ NS_ASSUME_NONNULL_BEGIN self.groupMetaMessage = TSGroupMessageNone; } + OWSAssert(self.receivedAtDate); + return self; } diff --git a/src/Storage/TSDatabaseView.m b/src/Storage/TSDatabaseView.m index 055431b4d..09112389d 100644 --- a/src/Storage/TSDatabaseView.m +++ b/src/Storage/TSDatabaseView.m @@ -9,6 +9,7 @@ #import "OWSDevice.h" #import "OWSReadTracking.h" #import "TSIncomingMessage.h" +#import "TSOutgoingMessage.h" #import "TSStorageManager.h" #import "TSThread.h" @@ -263,17 +264,7 @@ NSString *TSSecondaryDevicesDatabaseViewExtensionName = @"TSSecondaryDevicesData } + (NSDate *)localTimeReceiveDateForInteraction:(TSInteraction *)interaction { - NSDate *interactionDate = interaction.date; - - if ([interaction isKindOfClass:[TSIncomingMessage class]]) { - TSIncomingMessage *message = (TSIncomingMessage *)interaction; - - if (message.receivedAt) { - interactionDate = message.receivedAt; - } - } - - return interactionDate; + return [interaction receiptDateForSorting]; } #pragma mark - Logging