Rework message sender to assume thread is non-nil.

This commit is contained in:
Matthew Chen 2019-07-24 10:06:34 -03:00
parent adf13de1d7
commit 2a609791bf
3 changed files with 49 additions and 30 deletions

View File

@ -17,9 +17,10 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithTimestamp:(uint64_t)timestamp NS_UNAVAILABLE;
- (instancetype)initWithThread:(TSThread *)thread
outgoingMessage:(TSOutgoingMessage *)message
isRecipientUpdate:(BOOL)isRecipientUpdate NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithLocalThread:(TSThread *)localThread
messageThread:(TSThread *)messageThread
outgoingMessage:(TSOutgoingMessage *)message
isRecipientUpdate:(BOOL)isRecipientUpdate NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
@end

View File

@ -40,14 +40,17 @@ NS_ASSUME_NONNULL_BEGIN
@implementation OWSOutgoingSentMessageTranscript
- (instancetype)initWithThread:(TSThread *)thread
outgoingMessage:(TSOutgoingMessage *)message
isRecipientUpdate:(BOOL)isRecipientUpdate
- (instancetype)initWithLocalThread:(TSThread *)localThread
messageThread:(TSThread *)messageThread
outgoingMessage:(TSOutgoingMessage *)message
isRecipientUpdate:(BOOL)isRecipientUpdate
{
OWSAssertDebug(message);
OWSAssertDebug(message != nil);
OWSAssertDebug(localThread != nil);
OWSAssertDebug(messageThread != nil);
// The sync message's timestamp must match the original outgoing message's timestamp.
self = [super initWithTimestamp:message.timestamp thread:thread];
self = [super initWithTimestamp:message.timestamp thread:localThread];
if (!self) {
return self;
@ -56,7 +59,6 @@ NS_ASSUME_NONNULL_BEGIN
_message = message;
_isRecipientUpdate = isRecipientUpdate;
TSThread *_Nullable messageThread = message.threadWithSneakyTransaction;
if ([messageThread isKindOfClass:[TSContactThread class]]) {
TSContactThread *contactThread = (TSContactThread *)messageThread;
_sentRecipientAddress = contactThread.contactAddress;

View File

@ -852,24 +852,30 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
{
__block TSThread *_Nullable thread = nil;
[self.databaseStorage writeWithBlock:^(SDSAnyWriteTransaction *transaction) {
thread = [message threadWithTransaction:transaction];
OWSAssertDebug(thread != nil);
// For some legacy sync messages, thread may be nil.
// In this case, we should try to use the "local" thread.
BOOL isSyncMessage = [message isKindOfClass:[OWSOutgoingSyncMessage class]];
if (thread == nil && isSyncMessage) {
thread = [TSAccountManager getOrCreateLocalThreadWithTransaction:transaction];
if (thread == nil) {
OWSFailDebug(@"Could not restore thread for sync message.");
} else {
OWSLogInfo(@"Thread restored for sync message.");
}
}
thread = [self threadForMessage:message transaction:transaction];
}];
return thread;
}
- (nullable TSThread *)threadForMessage:(TSMessage *)message transaction:(SDSAnyWriteTransaction *)transaction
{
TSThread *_Nullable thread = [message threadWithTransaction:transaction];
OWSAssertDebug(thread != nil);
// For some legacy sync messages, thread may be nil.
// In this case, we should try to use the "local" thread.
BOOL isSyncMessage = [message isKindOfClass:[OWSOutgoingSyncMessage class]];
if (thread == nil && isSyncMessage) {
thread = [TSAccountManager getOrCreateLocalThreadWithTransaction:transaction];
if (thread == nil) {
OWSFailDebug(@"Could not restore thread for sync message.");
} else {
OWSLogInfo(@"Thread restored for sync message.");
}
}
return thread;
}
- (void)unregisteredRecipient:(SignalRecipient *)recipient
message:(TSOutgoingMessage *)message
thread:(TSThread *)thread
@ -1524,25 +1530,35 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
failure:(RetryableFailureHandler)failure
{
SignalServiceAddress *localAddress = self.tsAccountManager.localAddress;
__block TSThread *_Nullable thread;
// After sending a message to its "message thread",
// we send a sync transcript to the "local thread".
__block TSThread *_Nullable localThread;
__block TSThread *_Nullable messageThread;
__block SignalRecipient *recipient;
[self.databaseStorage writeWithBlock:^(SDSAnyWriteTransaction *transaction) {
thread = [TSAccountManager getOrCreateLocalThreadWithTransaction:transaction];
localThread = [TSAccountManager getOrCreateLocalThreadWithTransaction:transaction];
messageThread = [self threadForMessage:message transaction:transaction];
recipient = [SignalRecipient markRecipientAsRegisteredAndGet:localAddress transaction:transaction];
}];
if (thread == nil) {
if (localThread == nil) {
OWSFailDebug(@"Missing local thread.");
return;
}
if (messageThread == nil) {
OWSFailDebug(@"Missing message thread.");
return;
}
OWSOutgoingSentMessageTranscript *sentMessageTranscript =
[[OWSOutgoingSentMessageTranscript alloc] initWithThread:thread
outgoingMessage:message
isRecipientUpdate:isRecipientUpdate];
[[OWSOutgoingSentMessageTranscript alloc] initWithLocalThread:localThread
messageThread:messageThread
outgoingMessage:message
isRecipientUpdate:isRecipientUpdate];
OWSMessageSend *messageSend = [[OWSMessageSend alloc] initWithMessage:sentMessageTranscript
thread:thread
thread:localThread
recipient:recipient
senderCertificate:nil
udAccess:nil