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 {
NSDate *lastMessageDate = lastMessage.date;
if ([lastMessage isKindOfClass:[TSIncomingMessage class]]) {
TSIncomingMessage *message = (TSIncomingMessage *)lastMessage;
lastMessageDate = message.receivedAt;
if ([lastMessage isKindOfClass:[TSMessage class]]) {
TSMessage *message = (TSMessage *)lastMessage;
if ([message bestReceivedAtDate]) {
lastMessageDate = [message bestReceivedAtDate];
}
}
if (!_lastMessageDate || [lastMessageDate timeIntervalSinceDate:self.lastMessageDate] > 0) {

@ -121,6 +121,12 @@ extern NSString *const TSIncomingMessageWasReadOnThisDeviceNotification;
@property (nonatomic, readonly) NSString *authorId;
@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;
/*

@ -61,7 +61,8 @@ NSString *const TSIncomingMessageWasReadOnThisDeviceNotification = @"TSIncomingM
_authorId = authorId;
_read = NO;
_receivedAt = [NSDate date];
OWSAssert(self.receivedAtDate);
return self;
}
@ -134,6 +135,18 @@ NSString *const TSIncomingMessageWasReadOnThisDeviceNotification = @"TSIncomingM
[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
+ (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"
@ -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 bestReceivedAtDate instead.
@property (nonatomic, readonly) NSDate *receivedAtDate;
- (instancetype)initWithTimestamp:(uint64_t)timestamp;
@ -60,6 +64,11 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) {
- (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
NS_ASSUME_NONNULL_END

@ -102,6 +102,7 @@ static const NSUInteger OWSMessageSchemaVersion = 3;
_expiresInSeconds = expiresInSeconds;
_expireStartedAt = expireStartedAt;
[self updateExpiresAt];
_receivedAtDate = [NSDate date];
return self;
}
@ -132,6 +133,10 @@ static const NSUInteger OWSMessageSchemaVersion = 3;
}
_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;
}
@ -215,6 +220,11 @@ static const NSUInteger OWSMessageSchemaVersion = 3;
return self.expiresInSeconds > 0;
}
- (nullable NSDate *)bestReceivedAtDate
{
return self.receivedAtDate;
}
@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"

@ -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;
}

@ -9,6 +9,7 @@
#import "OWSDevice.h"
#import "OWSReadTracking.h"
#import "TSIncomingMessage.h"
#import "TSOutgoingMessage.h"
#import "TSStorageManager.h"
#import "TSThread.h"
@ -265,11 +266,10 @@ 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;
if ([interaction isKindOfClass:[TSMessage class]]) {
TSMessage *message = (TSMessage *)interaction;
if ([message bestReceivedAtDate]) {
interactionDate = [message bestReceivedAtDate];
}
}

Loading…
Cancel
Save