Add “received at” timestamp to all TSMessages so that they may be sorted properly.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 5ed95c4782
commit 7bd4d26532

@ -248,9 +248,11 @@ NS_ASSUME_NONNULL_BEGIN
- (void)updateWithLastMessage:(TSInteraction *)lastMessage transaction:(YapDatabaseReadWriteTransaction *)transaction { - (void)updateWithLastMessage:(TSInteraction *)lastMessage transaction:(YapDatabaseReadWriteTransaction *)transaction {
NSDate *lastMessageDate = lastMessage.date; NSDate *lastMessageDate = lastMessage.date;
if ([lastMessage isKindOfClass:[TSIncomingMessage class]]) { if ([lastMessage isKindOfClass:[TSMessage class]]) {
TSIncomingMessage *message = (TSIncomingMessage *)lastMessage; TSMessage *message = (TSMessage *)lastMessage;
lastMessageDate = message.receivedAt; if ([message bestReceivedAtDate]) {
lastMessageDate = [message bestReceivedAtDate];
}
} }
if (!_lastMessageDate || [lastMessageDate timeIntervalSinceDate:self.lastMessageDate] > 0) { if (!_lastMessageDate || [lastMessageDate timeIntervalSinceDate:self.lastMessageDate] > 0) {

@ -121,6 +121,12 @@ extern NSString *const TSIncomingMessageWasReadOnThisDeviceNotification;
@property (nonatomic, readonly) NSString *authorId; @property (nonatomic, readonly) NSString *authorId;
@property (nonatomic, readonly, getter=wasRead) BOOL read; @property (nonatomic, readonly, getter=wasRead) BOOL read;
// _DO NOT_ access this property directly. You almost certainly
// want to use bestReceivedAtDate instead.
//
// This property has been superceded by TSMessage.receivedAtData.
// This property only exists for backwards compatability with messages
// received before TSMessage.receivedAtData was added.
@property (nonatomic, readonly) NSDate *receivedAt; @property (nonatomic, readonly) NSDate *receivedAt;
/* /*

@ -61,7 +61,8 @@ NSString *const TSIncomingMessageWasReadOnThisDeviceNotification = @"TSIncomingM
_authorId = authorId; _authorId = authorId;
_read = NO; _read = NO;
_receivedAt = [NSDate date];
OWSAssert(self.receivedAtDate);
return self; return self;
} }
@ -134,6 +135,18 @@ NSString *const TSIncomingMessageWasReadOnThisDeviceNotification = @"TSIncomingM
[self touchThreadWithTransaction:transaction]; [self touchThreadWithTransaction:transaction];
} }
- (nullable NSDate *)bestReceivedAtDate
{
NSDate *result = [super bestReceivedAtDate];
if (!result) {
// For backward compatibility with messages received before
// TSMessage.receivedAtData was added, honor TSIncomingMessage.receivedAt
// if TSMessage.receivedAtData is not set.
result = self.receivedAt;
}
return result;
}
#pragma mark - Logging #pragma mark - Logging
+ (NSString *)tag + (NSString *)tag

@ -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" #import "TSInteraction.h"
@ -29,6 +30,9 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) {
@property (nonatomic, readonly) uint64_t expiresAt; @property (nonatomic, readonly) uint64_t expiresAt;
@property (nonatomic, readonly) BOOL isExpiringMessage; @property (nonatomic, readonly) BOOL isExpiringMessage;
@property (nonatomic, readonly) BOOL shouldStartExpireTimer; @property (nonatomic, readonly) BOOL shouldStartExpireTimer;
// _DO NOT_ access this property directly. You almost certainly
// want to use bestReceivedAtDate instead.
@property (nonatomic, readonly) NSDate *receivedAtDate;
- (instancetype)initWithTimestamp:(uint64_t)timestamp; - (instancetype)initWithTimestamp:(uint64_t)timestamp;
@ -60,6 +64,11 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) {
- (BOOL)hasAttachments; - (BOOL)hasAttachments;
// This message should return TSMessage.receivedAtDate for most messages.
// For messages received before TSMessage.receivedAtDate was added, this
// will try to return TSIncomingMessage.receivedAt.
- (nullable NSDate *)bestReceivedAtDate;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -102,6 +102,7 @@ static const NSUInteger OWSMessageSchemaVersion = 3;
_expiresInSeconds = expiresInSeconds; _expiresInSeconds = expiresInSeconds;
_expireStartedAt = expireStartedAt; _expireStartedAt = expireStartedAt;
[self updateExpiresAt]; [self updateExpiresAt];
_receivedAtDate = [NSDate date];
return self; return self;
} }
@ -132,6 +133,10 @@ static const NSUInteger OWSMessageSchemaVersion = 3;
} }
_schemaVersion = OWSMessageSchemaVersion; _schemaVersion = OWSMessageSchemaVersion;
// We _DO NOT_ set _receivedAt_ in this constructor. We don't want to
// set the receivedAt time for old messages in the data store.
return self; return self;
} }
@ -215,6 +220,11 @@ static const NSUInteger OWSMessageSchemaVersion = 3;
return self.expiresInSeconds > 0; return self.expiresInSeconds > 0;
} }
- (nullable NSDate *)bestReceivedAtDate
{
return self.receivedAtDate;
}
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -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" #import "TSMessage.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 "TSOutgoingMessage.h" #import "TSOutgoingMessage.h"
#import "NSDate+millisecondTimeStamp.h" #import "NSDate+millisecondTimeStamp.h"
@ -86,6 +87,8 @@ NS_ASSUME_NONNULL_BEGIN
self.groupMetaMessage = TSGroupMessageNone; self.groupMetaMessage = TSGroupMessageNone;
} }
OWSAssert(self.receivedAtDate);
return self; return self;
} }

@ -9,6 +9,7 @@
#import "OWSDevice.h" #import "OWSDevice.h"
#import "OWSReadTracking.h" #import "OWSReadTracking.h"
#import "TSIncomingMessage.h" #import "TSIncomingMessage.h"
#import "TSOutgoingMessage.h"
#import "TSStorageManager.h" #import "TSStorageManager.h"
#import "TSThread.h" #import "TSThread.h"
@ -265,11 +266,10 @@ NSString *TSSecondaryDevicesDatabaseViewExtensionName = @"TSSecondaryDevicesData
+ (NSDate *)localTimeReceiveDateForInteraction:(TSInteraction *)interaction { + (NSDate *)localTimeReceiveDateForInteraction:(TSInteraction *)interaction {
NSDate *interactionDate = interaction.date; NSDate *interactionDate = interaction.date;
if ([interaction isKindOfClass:[TSIncomingMessage class]]) { if ([interaction isKindOfClass:[TSMessage class]]) {
TSIncomingMessage *message = (TSIncomingMessage *)interaction; TSMessage *message = (TSMessage *)interaction;
if ([message bestReceivedAtDate]) {
if (message.receivedAt) { interactionDate = [message bestReceivedAtDate];
interactionDate = message.receivedAt;
} }
} }

Loading…
Cancel
Save