diff --git a/src/Messages/Interactions/TSErrorMessage.h b/src/Messages/Interactions/TSErrorMessage.h index 7504400a2..66a4ec71a 100644 --- a/src/Messages/Interactions/TSErrorMessage.h +++ b/src/Messages/Interactions/TSErrorMessage.h @@ -19,7 +19,7 @@ typedef NS_ENUM(int32_t, TSErrorMessageType) { TSErrorMessageUnknownContactBlockOffer, }; --(instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; +- (instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; - (instancetype)initWithTimestamp:(uint64_t)timestamp inThread:(TSThread *)thread failedMessageType:(TSErrorMessageType)errorMessageType NS_DESIGNATED_INITIALIZER; diff --git a/src/Messages/Interactions/TSErrorMessage.m b/src/Messages/Interactions/TSErrorMessage.m index 2ffdd3485..74562f9d7 100644 --- a/src/Messages/Interactions/TSErrorMessage.m +++ b/src/Messages/Interactions/TSErrorMessage.m @@ -25,7 +25,8 @@ messageBody:nil attachmentIds:@[] expiresInSeconds:0 - expireStartedAt:0]; + expireStartedAt:0 + groupMetaMessage:TSGroupMessageNone]; if (!self) { return self; diff --git a/src/Messages/Interactions/TSIncomingMessage.m b/src/Messages/Interactions/TSIncomingMessage.m index c91ea7cce..a84b4ebc6 100644 --- a/src/Messages/Interactions/TSIncomingMessage.m +++ b/src/Messages/Interactions/TSIncomingMessage.m @@ -47,7 +47,8 @@ NSString *const TSIncomingMessageWasReadOnThisDeviceNotification = @"TSIncomingM messageBody:body attachmentIds:attachmentIds expiresInSeconds:expiresInSeconds - expireStartedAt:0]; + expireStartedAt:0 + groupMetaMessage:TSGroupMessageNone]; if (!self) { return self; diff --git a/src/Messages/Interactions/TSInfoMessage.h b/src/Messages/Interactions/TSInfoMessage.h index 62ea47be3..d4f631e46 100644 --- a/src/Messages/Interactions/TSInfoMessage.h +++ b/src/Messages/Interactions/TSInfoMessage.h @@ -19,8 +19,8 @@ typedef NS_ENUM(NSInteger, TSInfoMessageType) { + (instancetype)userNotRegisteredMessageInThread:(TSThread *)thread; -@property TSInfoMessageType messageType; -@property NSString *customMessage; +@property (atomic, readonly) TSInfoMessageType messageType; +@property (atomic, readonly) NSString *customMessage; - (instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; diff --git a/src/Messages/Interactions/TSInfoMessage.m b/src/Messages/Interactions/TSInfoMessage.m index 9411aa409..9df6642c6 100644 --- a/src/Messages/Interactions/TSInfoMessage.m +++ b/src/Messages/Interactions/TSInfoMessage.m @@ -23,7 +23,8 @@ NS_ASSUME_NONNULL_BEGIN messageBody:nil attachmentIds:@[] expiresInSeconds:0 - expireStartedAt:0]; + expireStartedAt:0 + groupMetaMessage:TSGroupMessageNone]; if (!self) { return self; diff --git a/src/Messages/Interactions/TSInteraction.m b/src/Messages/Interactions/TSInteraction.m index 7d09ae755..9b028cb02 100644 --- a/src/Messages/Interactions/TSInteraction.m +++ b/src/Messages/Interactions/TSInteraction.m @@ -104,7 +104,6 @@ TSThread *fetchedThread = [TSThread fetchObjectWithUniqueID:self.uniqueThreadId transaction:transaction]; - [fetchedThread updateWithLastMessage:self transaction:transaction]; } diff --git a/src/Messages/Interactions/TSMessage.h b/src/Messages/Interactions/TSMessage.h index 65716cda9..b749b629e 100644 --- a/src/Messages/Interactions/TSMessage.h +++ b/src/Messages/Interactions/TSMessage.h @@ -24,7 +24,7 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) { @property (nonatomic, readonly) NSMutableArray *attachmentIds; @property (nullable, nonatomic) NSString *body; -@property (nonatomic) TSGroupMetaMessage groupMetaMessage; +@property (atomic, readonly) TSGroupMetaMessage groupMetaMessage; @property (nonatomic) uint32_t expiresInSeconds; @property (nonatomic) uint64_t expireStartedAt; @property (nonatomic, readonly) uint64_t expiresAt; @@ -58,7 +58,15 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) { messageBody:(nullable NSString *)body attachmentIds:(NSArray *)attachmentIds expiresInSeconds:(uint32_t)expiresInSeconds - expireStartedAt:(uint64_t)expireStartedAt NS_DESIGNATED_INITIALIZER; + expireStartedAt:(uint64_t)expireStartedAt; + +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + messageBody:(nullable NSString *)body + attachmentIds:(NSArray *)attachmentIds + expiresInSeconds:(uint32_t)expiresInSeconds + expireStartedAt:(uint64_t)expireStartedAt + groupMetaMessage:(TSGroupMetaMessage)groupMetaMessage NS_DESIGNATED_INITIALIZER; - (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; diff --git a/src/Messages/Interactions/TSMessage.m b/src/Messages/Interactions/TSMessage.m index e98b6b063..b3cb2a078 100644 --- a/src/Messages/Interactions/TSMessage.m +++ b/src/Messages/Interactions/TSMessage.m @@ -8,6 +8,7 @@ #import "TSAttachment.h" #import "TSAttachmentPointer.h" #import "TSThread.h" +#import #import NS_ASSUME_NONNULL_BEGIN @@ -35,8 +36,12 @@ static const NSUInteger OWSMessageSchemaVersion = 3; */ @property (nonatomic, readonly) NSUInteger schemaVersion; +@property (atomic) TSGroupMetaMessage groupMetaMessage; + @end +#pragma mark - + @implementation TSMessage - (instancetype)initWithTimestamp:(uint64_t)timestamp @@ -88,6 +93,23 @@ static const NSUInteger OWSMessageSchemaVersion = 3; attachmentIds:(NSArray *)attachmentIds expiresInSeconds:(uint32_t)expiresInSeconds expireStartedAt:(uint64_t)expireStartedAt +{ + return [self initWithTimestamp:timestamp + inThread:thread + messageBody:body + attachmentIds:attachmentIds + expiresInSeconds:expiresInSeconds + expireStartedAt:expireStartedAt + groupMetaMessage:TSGroupMessageNone]; +} + +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + messageBody:(nullable NSString *)body + attachmentIds:(NSArray *)attachmentIds + expiresInSeconds:(uint32_t)expiresInSeconds + expireStartedAt:(uint64_t)expireStartedAt + groupMetaMessage:(TSGroupMetaMessage)groupMetaMessage { self = [super initWithTimestamp:timestamp inThread:thread]; @@ -103,6 +125,7 @@ static const NSUInteger OWSMessageSchemaVersion = 3; _expireStartedAt = expireStartedAt; [self updateExpiresAt]; _receivedAtDate = [NSDate date]; + _groupMetaMessage = groupMetaMessage; return self; } diff --git a/src/Messages/Interactions/TSOutgoingMessage.h b/src/Messages/Interactions/TSOutgoingMessage.h index 4a72464c1..f58f116d1 100644 --- a/src/Messages/Interactions/TSOutgoingMessage.h +++ b/src/Messages/Interactions/TSOutgoingMessage.h @@ -33,6 +33,10 @@ typedef NS_ENUM(NSInteger, TSOutgoingMessageState) { inThread:(nullable TSThread *)thread messageBody:(nullable NSString *)body; +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + groupMetaMessage:(TSGroupMetaMessage)groupMetaMessage; + - (instancetype)initWithTimestamp:(uint64_t)timestamp inThread:(nullable TSThread *)thread messageBody:(nullable NSString *)body @@ -49,7 +53,15 @@ typedef NS_ENUM(NSInteger, TSOutgoingMessageState) { messageBody:(nullable NSString *)body attachmentIds:(NSMutableArray *)attachmentIds expiresInSeconds:(uint32_t)expiresInSeconds - expireStartedAt:(uint64_t)expireStartedAt NS_DESIGNATED_INITIALIZER; + expireStartedAt:(uint64_t)expireStartedAt; + +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + messageBody:(nullable NSString *)body + attachmentIds:(NSMutableArray *)attachmentIds + expiresInSeconds:(uint32_t)expiresInSeconds + expireStartedAt:(uint64_t)expireStartedAt + groupMetaMessage:(TSGroupMetaMessage)groupMetaMessage NS_DESIGNATED_INITIALIZER; - (instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; diff --git a/src/Messages/Interactions/TSOutgoingMessage.m b/src/Messages/Interactions/TSOutgoingMessage.m index 080e8f109..c72f3e957 100644 --- a/src/Messages/Interactions/TSOutgoingMessage.m +++ b/src/Messages/Interactions/TSOutgoingMessage.m @@ -110,13 +110,46 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec attachmentIds:(NSMutableArray *)attachmentIds expiresInSeconds:(uint32_t)expiresInSeconds expireStartedAt:(uint64_t)expireStartedAt +{ + TSGroupMetaMessage groupMetaMessage + = ([thread isKindOfClass:[TSGroupThread class]] ? TSGroupMessageDeliver : TSGroupMessageNone); + return [self initWithTimestamp:timestamp + inThread:thread + messageBody:body + attachmentIds:attachmentIds + expiresInSeconds:expiresInSeconds + expireStartedAt:expireStartedAt + groupMetaMessage:groupMetaMessage]; +} + +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + groupMetaMessage:(TSGroupMetaMessage)groupMetaMessage +{ + return [self initWithTimestamp:timestamp + inThread:thread + messageBody:@"" + attachmentIds:[NSMutableArray new] + expiresInSeconds:0 + expireStartedAt:0 + groupMetaMessage:groupMetaMessage]; +} + +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + messageBody:(nullable NSString *)body + attachmentIds:(NSMutableArray *)attachmentIds + expiresInSeconds:(uint32_t)expiresInSeconds + expireStartedAt:(uint64_t)expireStartedAt + groupMetaMessage:(TSGroupMetaMessage)groupMetaMessage { self = [super initWithTimestamp:timestamp inThread:thread messageBody:body attachmentIds:attachmentIds expiresInSeconds:expiresInSeconds - expireStartedAt:expireStartedAt]; + expireStartedAt:expireStartedAt + groupMetaMessage:groupMetaMessage]; if (!self) { return self; } @@ -125,11 +158,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec _sentRecipients = [NSArray new]; _hasSyncedTranscript = NO; - if ([thread isKindOfClass:[TSGroupThread class]]) { - self.groupMetaMessage = TSGroupMessageDeliver; - } else { - self.groupMetaMessage = TSGroupMessageNone; - } _attachmentFilenameMap = [NSMutableDictionary new]; OWSAssert(self.receivedAtDate); @@ -169,15 +197,15 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec #pragma mark - Update Methods -- (void)applyChangeToSelfAndLatest:(YapDatabaseReadWriteTransaction *)transaction - changeBlock:(void (^)(TSOutgoingMessage *))changeBlock +- (void)applyChangeToSelfAndLatestOutgoingMessage:(YapDatabaseReadWriteTransaction *)transaction + changeBlock:(void (^)(TSOutgoingMessage *))changeBlock { OWSAssert(transaction); changeBlock(self); - TSOutgoingMessage *latestMessage = - [transaction objectForKey:self.uniqueId inCollection:[TSOutgoingMessage collection]]; + NSString *collection = [[self class] collection]; + TSOutgoingMessage *latestMessage = [transaction objectForKey:self.uniqueId inCollection:collection]; if (latestMessage) { changeBlock(latestMessage); [latestMessage saveWithTransaction:transaction]; @@ -192,31 +220,31 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec OWSAssert(error); [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [self applyChangeToSelfAndLatest:transaction - changeBlock:^(TSOutgoingMessage *message) { - [message setMessageState:TSOutgoingMessageStateUnsent]; - [message setMostRecentFailureText:error.localizedDescription]; - }]; + [self applyChangeToSelfAndLatestOutgoingMessage:transaction + changeBlock:^(TSOutgoingMessage *message) { + [message setMessageState:TSOutgoingMessageStateUnsent]; + [message setMostRecentFailureText:error.localizedDescription]; + }]; }]; } - (void)updateWithMessageState:(TSOutgoingMessageState)messageState { [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [self applyChangeToSelfAndLatest:transaction - changeBlock:^(TSOutgoingMessage *message) { - [message setMessageState:messageState]; - }]; + [self applyChangeToSelfAndLatestOutgoingMessage:transaction + changeBlock:^(TSOutgoingMessage *message) { + [message setMessageState:messageState]; + }]; }]; } - (void)updateWithHasSyncedTranscript:(BOOL)hasSyncedTranscript { [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [self applyChangeToSelfAndLatest:transaction - changeBlock:^(TSOutgoingMessage *message) { - [message setHasSyncedTranscript:hasSyncedTranscript]; - }]; + [self applyChangeToSelfAndLatestOutgoingMessage:transaction + changeBlock:^(TSOutgoingMessage *message) { + [message setHasSyncedTranscript:hasSyncedTranscript]; + }]; }]; } @@ -225,10 +253,10 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec OWSAssert(customMessage); OWSAssert(transaction); - [self applyChangeToSelfAndLatest:transaction - changeBlock:^(TSOutgoingMessage *message) { - [message setCustomMessage:customMessage]; - }]; + [self applyChangeToSelfAndLatestOutgoingMessage:transaction + changeBlock:^(TSOutgoingMessage *message) { + [message setCustomMessage:customMessage]; + }]; } - (void)updateWithCustomMessage:(NSString *)customMessage @@ -242,10 +270,10 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec { OWSAssert(transaction); - [self applyChangeToSelfAndLatest:transaction - changeBlock:^(TSOutgoingMessage *message) { - [message setWasDelivered:YES]; - }]; + [self applyChangeToSelfAndLatestOutgoingMessage:transaction + changeBlock:^(TSOutgoingMessage *message) { + [message setWasDelivered:YES]; + }]; } - (void)updateWithWasDelivered @@ -297,10 +325,10 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec - (void)updateWithSentRecipient:(NSString *)contactId transaction:(YapDatabaseReadWriteTransaction *)transaction { OWSAssert(transaction); - [self applyChangeToSelfAndLatest:transaction - changeBlock:^(TSOutgoingMessage *message) { - [message addSentRecipient:contactId]; - }]; + [self applyChangeToSelfAndLatestOutgoingMessage:transaction + changeBlock:^(TSOutgoingMessage *message) { + [message addSentRecipient:contactId]; + }]; } - (void)updateWithSentRecipient:(NSString *)contactId