From 654ef890494d060fd2027820c424098608ac4936 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 12 Apr 2017 10:26:47 -0400 Subject: [PATCH] Rework outgoing message state. // FREEBIE --- src/Devices/OWSRecordTranscriptJob.m | 1 + src/Messages/Interactions/TSOutgoingMessage.h | 20 +++++++++++++++---- src/Messages/Interactions/TSOutgoingMessage.m | 19 ++++++++++++++---- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/Devices/OWSRecordTranscriptJob.m b/src/Devices/OWSRecordTranscriptJob.m index d6940e9ae..bafea8376 100644 --- a/src/Devices/OWSRecordTranscriptJob.m +++ b/src/Devices/OWSRecordTranscriptJob.m @@ -86,6 +86,7 @@ NS_ASSUME_NONNULL_BEGIN attachmentIds:[NSMutableArray new] expiresInSeconds:transcript.expirationDuration expireStartedAt:transcript.expirationStartedAt]; + // Since updateWithWasDelivered is a new message, updateWithWasDelivered will save it. [textMessage updateWithWasDelivered]; } } diff --git a/src/Messages/Interactions/TSOutgoingMessage.h b/src/Messages/Interactions/TSOutgoingMessage.h index 71e510cda..4a72464c1 100644 --- a/src/Messages/Interactions/TSOutgoingMessage.h +++ b/src/Messages/Interactions/TSOutgoingMessage.h @@ -109,18 +109,30 @@ typedef NS_ENUM(NSInteger, TSOutgoingMessageState) { filename:(nullable NSString *)filename; // TSOutgoingMessage are updated from many threads. We don't want to save -// our local copy since it may be out of date. Instead, we use these -// "updateWith..." methods to: +// our local copy (this instance) since it may be out of date. Instead, we +// use these "updateWith..." methods to: // -// a) Update a property of the local copy. +// a) Update a property of this instance. // b) Load an up-to-date instance of this model from from the data store. // c) Update and save that fresh instance. +// d) If this message hasn't yet been saved, save this local instance. +// +// After "updateWith...": +// +// a) An updated copy of this message will always have been saved in the +// data store. +// b) The local property on this instance will always have been updated. +// c) Other properties on this instance may be out of date. +// +// All mutable properties of this class have been made read-only to +// prevent accidentally modifying them directly. // // This isn't a perfect arrangement, but in practice this will prevent -// data loss. +// data loss and will resolve all known issues. - (void)updateWithMessageState:(TSOutgoingMessageState)messageState; - (void)updateWithSendingError:(NSError *)error; - (void)updateWithHasSyncedTranscript:(BOOL)hasSyncedTranscript; +- (void)updateWithCustomMessage:(NSString *)customMessage transaction:(YapDatabaseReadWriteTransaction *)transaction; - (void)updateWithCustomMessage:(NSString *)customMessage; - (void)updateWithWasDeliveredWithTransaction:(YapDatabaseReadWriteTransaction *)transaction; - (void)updateWithWasDelivered; diff --git a/src/Messages/Interactions/TSOutgoingMessage.m b/src/Messages/Interactions/TSOutgoingMessage.m index c868ac376..080e8f109 100644 --- a/src/Messages/Interactions/TSOutgoingMessage.m +++ b/src/Messages/Interactions/TSOutgoingMessage.m @@ -189,6 +189,8 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec - (void)updateWithSendingError:(NSError *)error { + OWSAssert(error); + [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self applyChangeToSelfAndLatest:transaction changeBlock:^(TSOutgoingMessage *message) { @@ -218,19 +220,28 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec }]; } +- (void)updateWithCustomMessage:(NSString *)customMessage transaction:(YapDatabaseReadWriteTransaction *)transaction +{ + OWSAssert(customMessage); + OWSAssert(transaction); + + [self applyChangeToSelfAndLatest:transaction + changeBlock:^(TSOutgoingMessage *message) { + [message setCustomMessage:customMessage]; + }]; +} + - (void)updateWithCustomMessage:(NSString *)customMessage { [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [self applyChangeToSelfAndLatest:transaction - changeBlock:^(TSOutgoingMessage *message) { - [message setCustomMessage:customMessage]; - }]; + [self updateWithCustomMessage:customMessage transaction:transaction]; }]; } - (void)updateWithWasDeliveredWithTransaction:(YapDatabaseReadWriteTransaction *)transaction { OWSAssert(transaction); + [self applyChangeToSelfAndLatest:transaction changeBlock:^(TSOutgoingMessage *message) { [message setWasDelivered:YES];