Rework outgoing message state.

pull/1/head
Matthew Chen 8 years ago
parent 9275c67818
commit 9e6062f21c

@ -51,10 +51,12 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) {
@class OWSSignalServiceProtosDataMessageBuilder; @class OWSSignalServiceProtosDataMessageBuilder;
@class SignalRecipient; @class SignalRecipient;
@interface TSOutgoingMessageRecipientState : NSObject @interface TSOutgoingMessageRecipientState : MTLModel
@property (atomic, readonly) OWSOutgoingMessageRecipientState state; @property (atomic, readonly) OWSOutgoingMessageRecipientState state;
// This property should only be set if state == .sent.
@property (atomic, nullable, readonly) NSNumber *deliveryTimestamp; @property (atomic, nullable, readonly) NSNumber *deliveryTimestamp;
// This property should only be set if state == .sent.
@property (atomic, nullable, readonly) NSNumber *readTimestamp; @property (atomic, nullable, readonly) NSNumber *readTimestamp;
@end @end

@ -6,6 +6,7 @@
#import "NSDate+OWS.h" #import "NSDate+OWS.h"
#import "OWSMessageSender.h" #import "OWSMessageSender.h"
#import "OWSOutgoingSyncMessage.h" #import "OWSOutgoingSyncMessage.h"
#import "OWSPrimaryStorage.h"
#import "OWSSignalServiceProtos.pb.h" #import "OWSSignalServiceProtos.pb.h"
#import "ProtoBuf+OWS.h" #import "ProtoBuf+OWS.h"
#import "SignalRecipient.h" #import "SignalRecipient.h"
@ -107,7 +108,16 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
NSMutableDictionary<NSString *, TSOutgoingMessageRecipientState *> *recipientStateMap = [NSMutableDictionary new]; NSMutableDictionary<NSString *, TSOutgoingMessageRecipientState *> *recipientStateMap = [NSMutableDictionary new];
// Our default recipient list is the current thread members. // Our default recipient list is the current thread members.
NSArray<NSString *> *recipientIds = [self.thread recipientIdentifiers]; __block NSArray<NSString *> *recipientIds = @[];
// To avoid deadlock while migrating these records, we use a dedicated
// migration connection. For legacy records (created more than ~9 months
// before the migration), we need to infer the recipient list for this
// message from the current thread membership. This inference isn't
// always accurate, so not using the same connection for both reads is
// acceptable.
[TSOutgoingMessage.dbMigrationConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
recipientIds = [[self threadWithTransaction:transaction] recipientIdentifiers];
}];
if (sentRecipients) { if (sentRecipients) {
// If we have a `sentRecipients` list, prefer that as it is more accurate. // If we have a `sentRecipients` list, prefer that as it is more accurate.
recipientIds = sentRecipients; recipientIds = sentRecipients;
@ -146,8 +156,16 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
recipientStateMap[recipientId] = recipientState; recipientStateMap[recipientId] = recipientState;
} }
self.recipientStateMap = [recipientStateMap copy]; self.recipientStateMap = [recipientStateMap copy];
}
[self updateMessageState]; + (YapDatabaseConnection *)dbMigrationConnection
{
static YapDatabaseConnection *connection = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
connection = [[OWSPrimaryStorage sharedManager] newDatabaseConnection];
});
return connection;
} }
+ (instancetype)outgoingMessageInThread:(nullable TSThread *)thread + (instancetype)outgoingMessageInThread:(nullable TSThread *)thread
@ -258,16 +276,9 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
} }
self.recipientStateMap = [recipientStateMap copy]; self.recipientStateMap = [recipientStateMap copy];
[self updateMessageState];
return self; return self;
} }
- (void)updateMessageState
{
// self.messageState = [TSOutgoingMessage messageStateForRecipientStates:self.recipientStateMap.allValues];
}
- (TSOutgoingMessageState)messageState - (TSOutgoingMessageState)messageState
{ {
return [TSOutgoingMessage messageStateForRecipientStates:self.recipientStateMap.allValues]; return [TSOutgoingMessage messageStateForRecipientStates:self.recipientStateMap.allValues];
@ -462,7 +473,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
recipientState.state = OWSOutgoingMessageRecipientStateFailed; recipientState.state = OWSOutgoingMessageRecipientStateFailed;
} }
} }
[message updateMessageState];
}]; }];
} }
@ -479,7 +489,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
recipientState.state = OWSOutgoingMessageRecipientStateSending; recipientState.state = OWSOutgoingMessageRecipientStateSending;
} }
} }
[message updateMessageState];
}]; }];
} }
@ -515,8 +524,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
OWSAssert(recipientId.length > 0); OWSAssert(recipientId.length > 0);
OWSAssert(transaction); OWSAssert(transaction);
// TODO: I suspect we're double-calling this method.
[self applyChangeToSelfAndLatestCopy:transaction [self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(TSOutgoingMessage *message) { changeBlock:^(TSOutgoingMessage *message) {
TSOutgoingMessageRecipientState *_Nullable recipientState TSOutgoingMessageRecipientState *_Nullable recipientState
@ -526,7 +533,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
return; return;
} }
recipientState.state = OWSOutgoingMessageRecipientStateSent; recipientState.state = OWSOutgoingMessageRecipientStateSent;
[message updateMessageState];
}]; }];
} }
@ -544,7 +550,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
return; return;
} }
recipientState.state = OWSOutgoingMessageRecipientStateSkipped; recipientState.state = OWSOutgoingMessageRecipientStateSkipped;
[message updateMessageState];
}]; }];
} }
@ -567,7 +572,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
} }
recipientState.state = OWSOutgoingMessageRecipientStateSent; recipientState.state = OWSOutgoingMessageRecipientStateSent;
recipientState.deliveryTimestamp = deliveryTimestamp; recipientState.deliveryTimestamp = deliveryTimestamp;
[message updateMessageState];
}]; }];
} }
@ -590,7 +594,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
} }
recipientState.state = OWSOutgoingMessageRecipientStateSent; recipientState.state = OWSOutgoingMessageRecipientStateSent;
recipientState.readTimestamp = @(readTimestamp); recipientState.readTimestamp = @(readTimestamp);
[message updateMessageState];
}]; }];
} }
@ -608,7 +611,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
} }
} }
[message setIsFromLinkedDevice:YES]; [message setIsFromLinkedDevice:YES];
[message updateMessageState];
}]; }];
} }
@ -626,7 +628,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
[message setRecipientStateMap:@{ [message setRecipientStateMap:@{
singleGroupRecipient : recipientState, singleGroupRecipient : recipientState,
}]; }];
[message updateMessageState];
}]; }];
} }
@ -668,7 +669,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
break; break;
} }
} }
[message updateMessageState];
}]; }];
} }
#pragma mark - #pragma mark -

@ -593,11 +593,6 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
thread:thread thread:thread
attempts:OWSMessageSenderRetryAttempts attempts:OWSMessageSenderRetryAttempts
success:^{ success:^{
DDLogInfo(@"%@ Marking group message as sent to recipient: %@", self.logTag, recipient.uniqueId);
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
// Mark this recipient as "sent".
[message updateWithSentRecipient:recipient.uniqueId transaction:transaction];
}];
[futureSource trySetResult:@1]; [futureSource trySetResult:@1];
} }
failure:^(NSError *error) { failure:^(NSError *error) {

Loading…
Cancel
Save