Respond to CR.

pull/1/head
Matthew Chen 7 years ago
parent 3e85c8c023
commit 5e0bc1bc1e

@ -66,7 +66,7 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa
// Start processing. // Start processing.
[AppReadiness runNowOrWhenAppIsReady:^{ [AppReadiness runNowOrWhenAppIsReady:^{
[self scheduleProcessing]; [self process];
}]; }];
return self; return self;
@ -100,8 +100,7 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa
} }
// Schedules a processing pass, unless one is already scheduled. // Schedules a processing pass, unless one is already scheduled.
- (void)scheduleProcessing - (void)process {
{
OWSAssertDebug(AppReadiness.isAppReady); OWSAssertDebug(AppReadiness.isAppReady);
dispatch_async(self.serialQueue, ^{ dispatch_async(self.serialQueue, ^{
@ -109,16 +108,10 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa
return; return;
} }
self.isProcessing = YES;
[self process];
});
}
- (void)process
{
OWSLogVerbose(@"Processing outbound receipts."); OWSLogVerbose(@"Processing outbound receipts.");
self.isProcessing = YES;
if (!self.reachability.isReachable) { if (!self.reachability.isReachable) {
// No network availability; abort. // No network availability; abort.
self.isProcessing = NO; self.isProcessing = NO;
@ -126,10 +119,8 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa
} }
NSMutableArray<AnyPromise *> *sendPromises = [NSMutableArray array]; NSMutableArray<AnyPromise *> *sendPromises = [NSMutableArray array];
[sendPromises addObjectsFromArray:[self sendReceiptsForCollection:kOutgoingDeliveryReceiptManagerCollection [sendPromises addObjectsFromArray:[self sendReceiptsForReceiptType:OWSReceiptType_Delivery]];
receiptType:OWSReceiptType_Delivery]]; [sendPromises addObjectsFromArray:[self sendReceiptsForReceiptType:OWSReceiptType_Read]];
[sendPromises addObjectsFromArray:[self sendReceiptsForCollection:kOutgoingReadReceiptManagerCollection
receiptType:OWSReceiptType_Read]];
if (sendPromises.count < 1) { if (sendPromises.count < 1) {
// No work to do; abort. // No work to do; abort.
@ -149,14 +140,17 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kProcessingFrequencySeconds * NSEC_PER_SEC)), dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kProcessingFrequencySeconds * NSEC_PER_SEC)),
self.serialQueue, self.serialQueue,
^{ ^{
self.isProcessing = NO;
[self process]; [self process];
}); });
}); });
[completionPromise retainUntilComplete]; [completionPromise retainUntilComplete];
});
} }
- (NSArray<AnyPromise *> *)sendReceiptsForCollection:(NSString *)collection receiptType:(OWSReceiptType)receiptType - (NSArray<AnyPromise *> *)sendReceiptsForReceiptType:(OWSReceiptType)receiptType {
{ NSString *collection = [self collectionForReceiptType:receiptType];
NSMutableDictionary<NSString *, NSSet<NSNumber *> *> *queuedReceiptMap = [NSMutableDictionary new]; NSMutableDictionary<NSString *, NSSet<NSNumber *> *> *queuedReceiptMap = [NSMutableDictionary new];
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
@ -200,7 +194,7 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa
OWSLogInfo( OWSLogInfo(
@"Successfully sent %lu %@ receipts to sender.", (unsigned long)timestamps.count, receiptName); @"Successfully sent %lu %@ receipts to sender.", (unsigned long)timestamps.count, receiptName);
[self dequeueReceiptsWithRecipientId:recipientId timestamps:timestamps collection:collection]; [self dequeueReceiptsWithRecipientId:recipientId timestamps:timestamps receiptType:receiptType];
// The value doesn't matter, we just need any non-NSError value. // The value doesn't matter, we just need any non-NSError value.
resolve(@(1)); resolve(@(1));
@ -221,20 +215,17 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa
{ {
[self enqueueReceiptWithRecipientId:envelope.source [self enqueueReceiptWithRecipientId:envelope.source
timestamp:envelope.timestamp timestamp:envelope.timestamp
collection:kOutgoingDeliveryReceiptManagerCollection]; receiptType:OWSReceiptType_Delivery];
} }
- (void)enqueueReadReceiptForEnvelope:(NSString *)messageAuthorId timestamp:(uint64_t)timestamp - (void)enqueueReadReceiptForEnvelope:(NSString *)messageAuthorId timestamp:(uint64_t)timestamp {
{ [self enqueueReceiptWithRecipientId:messageAuthorId timestamp:timestamp receiptType:OWSReceiptType_Read];
[self enqueueReceiptWithRecipientId:messageAuthorId
timestamp:timestamp
collection:kOutgoingReadReceiptManagerCollection];
} }
- (void)enqueueReceiptWithRecipientId:(NSString *)recipientId - (void)enqueueReceiptWithRecipientId:(NSString *)recipientId
timestamp:(uint64_t)timestamp timestamp:(uint64_t)timestamp
collection:(NSString *)collection receiptType:(OWSReceiptType)receiptType {
{ NSString *collection = [self collectionForReceiptType:receiptType];
if (recipientId.length < 1) { if (recipientId.length < 1) {
OWSFailDebug(@"Invalid recipient id."); OWSFailDebug(@"Invalid recipient id.");
@ -254,14 +245,15 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa
[transaction setObject:newTimestamps forKey:recipientId inCollection:collection]; [transaction setObject:newTimestamps forKey:recipientId inCollection:collection];
}]; }];
[self scheduleProcessing]; [self process];
}); });
} }
- (void)dequeueReceiptsWithRecipientId:(NSString *)recipientId - (void)dequeueReceiptsWithRecipientId:(NSString *)recipientId
timestamps:(NSSet<NSNumber *> *)timestamps timestamps:(NSSet<NSNumber *> *)timestamps
collection:(NSString *)collection receiptType:(OWSReceiptType)receiptType {
{ NSString *collection = [self collectionForReceiptType:receiptType];
if (recipientId.length < 1) { if (recipientId.length < 1) {
OWSFailDebug(@"Invalid recipient id."); OWSFailDebug(@"Invalid recipient id.");
return; return;
@ -290,7 +282,16 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa
{ {
OWSAssertIsOnMainThread(); OWSAssertIsOnMainThread();
[self scheduleProcessing]; [self process];
}
- (NSString *)collectionForReceiptType:(OWSReceiptType)receiptType {
switch (receiptType) {
case OWSReceiptType_Delivery:
return kOutgoingDeliveryReceiptManagerCollection;
case OWSReceiptType_Read:
return kOutgoingReadReceiptManagerCollection;
}
} }
@end @end

Loading…
Cancel
Save