diff --git a/src/Devices/OWSRecordTranscriptJob.m b/src/Devices/OWSRecordTranscriptJob.m index 3c4585d46..a9fbeac59 100644 --- a/src/Devices/OWSRecordTranscriptJob.m +++ b/src/Devices/OWSRecordTranscriptJob.m @@ -48,12 +48,13 @@ NS_ASSUME_NONNULL_BEGIN messagesManager:self.messagesManager]; // TODO group updates. Currently desktop doesn't support group updates, so not a problem yet. - TSOutgoingMessage *outgoingMessage = [[TSOutgoingMessage alloc] initWithTimestamp:transcript.timestamp - inThread:thread - messageBody:transcript.body - attachmentIds:attachmentsProcessor.attachmentIds - expiresInSeconds:transcript.expirationDuration - expireStartedAt:transcript.expirationStartedAt]; + TSOutgoingMessage *outgoingMessage = + [[TSOutgoingMessage alloc] initWithTimestamp:transcript.timestamp + inThread:thread + messageBody:transcript.body + attachmentIds:[attachmentsProcessor.attachmentIds mutableCopy] + expiresInSeconds:transcript.expirationDuration + expireStartedAt:transcript.expirationStartedAt]; if (transcript.isExpirationTimerUpdate) { [self.messagesManager becomeConsistentWithDisappearingConfigurationForMessage:outgoingMessage]; @@ -71,7 +72,10 @@ NS_ASSUME_NONNULL_BEGIN uint64_t textMessageTimestamp = transcript.timestamp + 1; TSOutgoingMessage *textMessage = [[TSOutgoingMessage alloc] initWithTimestamp:textMessageTimestamp inThread:thread - messageBody:transcript.body]; + messageBody:transcript.body + attachmentIds:[NSMutableArray new] + expiresInSeconds:transcript.expirationDuration + expireStartedAt:transcript.expirationStartedAt]; textMessage.messageState = TSOutgoingMessageStateDelivered; [textMessage save]; } diff --git a/src/Messages/Interactions/TSIncomingMessage.h b/src/Messages/Interactions/TSIncomingMessage.h index 55a14495d..bae0e8000 100644 --- a/src/Messages/Interactions/TSIncomingMessage.h +++ b/src/Messages/Interactions/TSIncomingMessage.h @@ -12,43 +12,6 @@ extern NSString *const TSIncomingMessageWasReadOnThisDeviceNotification; @interface TSIncomingMessage : TSMessage -/** - * Inits an incoming (non-group) message with no attachments. - * - * @param timestamp - * When the message was created in milliseconds since epoch - * @param thread - * Thread to which the message belongs - * @param body - * Body of the message - * @param attachmentIds - * The uniqueIds for the message's attachments - * - * @return initiated incoming message - */ -- (instancetype)initWithTimestamp:(uint64_t)timestamp - inThread:(nullable TSContactThread *)thread - messageBody:(nullable NSString *)body; - -/** - * Inits an incoming (non-group) message with attachments. - * - * @param timestamp - * When the message was created in milliseconds since epoch - * @param thread - * Thread to which the message belongs - * @param body - * Body of the message - * @param attachmentIds - * The uniqueIds for the message's attachments - * - * @return initiated incoming message - */ -- (instancetype)initWithTimestamp:(uint64_t)timestamp - inThread:(nullable TSContactThread *)thread - messageBody:(nullable NSString *)body - attachmentIds:(NSArray *)attachmentIds; - /** * Inits an incoming group message without attachments * @@ -64,8 +27,8 @@ extern NSString *const TSIncomingMessageWasReadOnThisDeviceNotification; * @return initiated incoming group message */ - (instancetype)initWithTimestamp:(uint64_t)timestamp - inThread:(nullable TSGroupThread *)thread - authorId:(nullable NSString *)authorId + inThread:(TSThread *)thread + authorId:(NSString *)authorId messageBody:(nullable NSString *)body; /** @@ -85,8 +48,8 @@ extern NSString *const TSIncomingMessageWasReadOnThisDeviceNotification; * @return initiated incoming group message */ - (instancetype)initWithTimestamp:(uint64_t)timestamp - inThread:(nullable TSGroupThread *)thread - authorId:(nullable NSString *)authorId + inThread:(TSThread *)thread + authorId:(NSString *)authorId messageBody:(nullable NSString *)body attachmentIds:(NSArray *)attachmentIds; @@ -109,11 +72,39 @@ extern NSString *const TSIncomingMessageWasReadOnThisDeviceNotification; * @return initiated incoming group message */ - (instancetype)initWithTimestamp:(uint64_t)timestamp - inThread:(nullable TSGroupThread *)thread - authorId:(nullable NSString *)authorId + inThread:(TSThread *)thread + authorId:(NSString *)authorId + messageBody:(nullable NSString *)body + attachmentIds:(NSArray *)attachmentIds + expiresInSeconds:(uint32_t)expiresInSeconds NS_DESIGNATED_INITIALIZER; + +- (instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; + + +/** + * For sake of a smaller API, you must specify an author id for all incoming messages + * though we technically could get the author id from a contact thread. + */ +- (instancetype)initWithTimestamp:(uint64_t)timestamp NS_UNAVAILABLE; +- (instancetype)initWithTimestamp:(uint64_t)timestamp inThread:(nullable TSThread *)thread NS_UNAVAILABLE; +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + messageBody:(nullable NSString *)body NS_UNAVAILABLE; +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + messageBody:(nullable NSString *)body + attachmentIds:(NSArray *)attachmentIds NS_UNAVAILABLE; +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + messageBody:(nullable NSString *)body + attachmentIds:(NSArray *)attachmentIds + expiresInSeconds:(uint32_t)expiresInSeconds NS_UNAVAILABLE; +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread messageBody:(nullable NSString *)body attachmentIds:(NSArray *)attachmentIds - expiresInSeconds:(uint32_t)expiresInSeconds; + expiresInSeconds:(uint32_t)expiresInSeconds + expireStartedAt:(uint64_t)expireStartedAt NS_UNAVAILABLE; /* * Find a message matching the senderId and timestamp, if any. diff --git a/src/Messages/Interactions/TSIncomingMessage.m b/src/Messages/Interactions/TSIncomingMessage.m index 0a5d05292..e83889f4f 100644 --- a/src/Messages/Interactions/TSIncomingMessage.m +++ b/src/Messages/Interactions/TSIncomingMessage.m @@ -13,91 +13,36 @@ NSString *const TSIncomingMessageWasReadOnThisDeviceNotification = @"TSIncomingM @implementation TSIncomingMessage -- (instancetype)initWithTimestamp:(uint64_t)timestamp - inThread:(nullable TSContactThread *)thread - messageBody:(nullable NSString *)body -{ - self = [super initWithTimestamp:timestamp inThread:thread messageBody:body attachmentIds:@[]]; - - if (!self) { - return self; - } - - // _authorId was nil for contact thread messages prior to 2.6.0 - _authorId = [thread contactIdentifier]; - _read = NO; - _receivedAt = [NSDate date]; - - return self; -} - -- (instancetype)initWithTimestamp:(uint64_t)timestamp - inThread:(nullable TSThread *)thread - messageBody:(nullable NSString *)body - attachmentIds:(NSArray *)attachmentIds - expiresInSeconds:(uint32_t)expiresInSeconds -{ - self = [self initWithTimestamp:timestamp - inThread:thread - messageBody:body - attachmentIds:attachmentIds - expiresInSeconds:expiresInSeconds - expireStartedAt:0]; - if (!self) { - return self; - } - - // _authorId was nil for contact thread messages prior to 2.6.0 - _authorId = [thread contactIdentifier]; - _read = NO; - _receivedAt = [NSDate date]; - - return self; -} - -- (instancetype)initWithTimestamp:(uint64_t)timestamp - inThread:(nullable TSContactThread *)thread - messageBody:(nullable NSString *)body - attachmentIds:(NSArray *)attachmentIds +- (instancetype)initWithCoder:(NSCoder *)coder { - self = [super initWithTimestamp:timestamp inThread:thread messageBody:body attachmentIds:attachmentIds]; - - if (!self) { - return self; - } - - // _authorId was nil for contact thread messages prior to 2.6.0 - _authorId = [thread contactIdentifier]; - _read = NO; - _receivedAt = [NSDate date]; - - return self; + return [super initWithCoder:coder]; } - (instancetype)initWithTimestamp:(uint64_t)timestamp - inThread:(nullable TSGroupThread *)thread - authorId:(nullable NSString *)authorId + inThread:(TSThread *)thread + authorId:(NSString *)authorId messageBody:(nullable NSString *)body { return [self initWithTimestamp:timestamp inThread:thread authorId:authorId messageBody:body attachmentIds:@[]]; } - (instancetype)initWithTimestamp:(uint64_t)timestamp - inThread:(nullable TSGroupThread *)thread - authorId:(nullable NSString *)authorId + inThread:(TSThread *)thread + authorId:(NSString *)authorId messageBody:(nullable NSString *)body attachmentIds:(NSArray *)attachmentIds { return [self initWithTimestamp:timestamp inThread:thread + authorId:authorId messageBody:body attachmentIds:attachmentIds expiresInSeconds:0]; } - (instancetype)initWithTimestamp:(uint64_t)timestamp - inThread:(nullable TSGroupThread *)thread - authorId:(nullable NSString *)authorId + inThread:(TSThread *)thread + authorId:(NSString *)authorId messageBody:(nullable NSString *)body attachmentIds:(NSArray *)attachmentIds expiresInSeconds:(uint32_t)expiresInSeconds @@ -106,7 +51,8 @@ NSString *const TSIncomingMessageWasReadOnThisDeviceNotification = @"TSIncomingM inThread:thread messageBody:body attachmentIds:attachmentIds - expiresInSeconds:expiresInSeconds]; + expiresInSeconds:expiresInSeconds + expireStartedAt:0]; if (!self) { return self; diff --git a/src/Messages/Interactions/TSOutgoingMessage.h b/src/Messages/Interactions/TSOutgoingMessage.h index 0528b3fec..3f735abea 100644 --- a/src/Messages/Interactions/TSOutgoingMessage.h +++ b/src/Messages/Interactions/TSOutgoingMessage.h @@ -17,6 +17,33 @@ typedef NS_ENUM(NSInteger, TSOutgoingMessageState) { TSOutgoingMessageStateDelivered }; +- (instancetype)initWithTimestamp:(uint64_t)timestamp; +- (instancetype)initWithTimestamp:(uint64_t)timestamp inThread:(nullable TSThread *)thread; + +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + messageBody:(nullable NSString *)body; + +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + messageBody:(nullable NSString *)body + attachmentIds:(NSMutableArray *)attachmentIds; + +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + messageBody:(nullable NSString *)body + attachmentIds:(NSMutableArray *)attachmentIds + expiresInSeconds:(uint32_t)expiresInSeconds; + +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + messageBody:(nullable NSString *)body + attachmentIds:(NSMutableArray *)attachmentIds + expiresInSeconds:(uint32_t)expiresInSeconds + expireStartedAt:(uint64_t)expireStartedAt NS_DESIGNATED_INITIALIZER; + +- (instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; + @property (nonatomic) TSOutgoingMessageState messageState; @property BOOL hasSyncedTranscript; diff --git a/src/Messages/Interactions/TSOutgoingMessage.m b/src/Messages/Interactions/TSOutgoingMessage.m index 57eebc66d..ee53f897e 100644 --- a/src/Messages/Interactions/TSOutgoingMessage.m +++ b/src/Messages/Interactions/TSOutgoingMessage.m @@ -12,11 +12,26 @@ NS_ASSUME_NONNULL_BEGIN @implementation TSOutgoingMessage +- (instancetype)initWithCoder:(NSCoder *)coder +{ + return [super initWithCoder:coder]; +} + +- (instancetype)initWithTimestamp:(uint64_t)timestamp +{ + return [self initWithTimestamp:timestamp inThread:nil]; +} + +- (instancetype)initWithTimestamp:(uint64_t)timestamp inThread:(nullable TSThread *)thread +{ + return [self initWithTimestamp:timestamp inThread:nil messageBody:nil]; +} + - (instancetype)initWithTimestamp:(uint64_t)timestamp inThread:(nullable TSThread *)thread messageBody:(nullable NSString *)body { - return [self initWithTimestamp:timestamp inThread:thread messageBody:body attachmentIds:@[]]; + return [self initWithTimestamp:timestamp inThread:thread messageBody:body attachmentIds:[NSMutableArray new]]; } - (instancetype)initWithTimestamp:(uint64_t)timestamp @@ -24,8 +39,11 @@ NS_ASSUME_NONNULL_BEGIN messageBody:(nullable NSString *)body attachmentIds:(NSMutableArray *)attachmentIds { - - return [self initWithTimestamp:timestamp inThread:thread messageBody:body attachmentIds:@[] expiresInSeconds:0]; + return [self initWithTimestamp:timestamp + inThread:thread + messageBody:body + attachmentIds:[NSMutableArray new] + expiresInSeconds:0]; } - (instancetype)initWithTimestamp:(uint64_t)timestamp @@ -35,12 +53,27 @@ NS_ASSUME_NONNULL_BEGIN expiresInSeconds:(uint32_t)expiresInSeconds { uint64_t now = [NSDate ows_millisecondTimeStamp]; + return [self initWithTimestamp:timestamp + inThread:thread + messageBody:body + attachmentIds:attachmentIds + expiresInSeconds:expiresInSeconds + expireStartedAt:now]; +} + +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + messageBody:(nullable NSString *)body + attachmentIds:(NSMutableArray *)attachmentIds + expiresInSeconds:(uint32_t)expiresInSeconds + expireStartedAt:(uint64_t)expireStartedAt +{ self = [super initWithTimestamp:timestamp inThread:thread messageBody:body attachmentIds:attachmentIds expiresInSeconds:expiresInSeconds - expireStartedAt:now]; + expireStartedAt:expireStartedAt]; if (!self) { return self; } diff --git a/src/Messages/TSMessagesManager+sendMessages.m b/src/Messages/TSMessagesManager+sendMessages.m index 4567b2e97..1895970ea 100644 --- a/src/Messages/TSMessagesManager+sendMessages.m +++ b/src/Messages/TSMessagesManager+sendMessages.m @@ -399,6 +399,7 @@ dispatch_queue_t sendingQueue() { TSIncomingMessage *incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:(outgoingMessage.timestamp + 1) inThread:cThread + authorId:[cThread contactIdentifier] messageBody:outgoingMessage.body attachmentIds:outgoingMessage.attachmentIds expiresInSeconds:outgoingMessage.expiresInSeconds]; diff --git a/src/Messages/TSMessagesManager.m b/src/Messages/TSMessagesManager.m index d8ede6af4..94b8f1da6 100644 --- a/src/Messages/TSMessagesManager.m +++ b/src/Messages/TSMessagesManager.m @@ -452,6 +452,7 @@ NS_ASSUME_NONNULL_BEGIN incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:timestamp inThread:cThread + authorId:[cThread contactIdentifier] messageBody:body attachmentIds:attachmentIds expiresInSeconds:dataMessage.expireTimer]; @@ -476,6 +477,7 @@ NS_ASSUME_NONNULL_BEGIN TSContactThread *cThread = (TSContactThread *)thread; textMessage = [[TSIncomingMessage alloc] initWithTimestamp:textMessageTimestamp inThread:cThread + authorId:[cThread contactIdentifier] messageBody:body]; } textMessage.expiresInSeconds = dataMessage.expireTimer;