From 5e0bc1bc1e2f839e89819628ba56d724112c9bc2 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 12 Oct 2018 14:08:42 -0400 Subject: [PATCH] Respond to CR. --- .../src/Messages/OWSOutgoingReceiptManager.m | 113 +++++++++--------- 1 file changed, 57 insertions(+), 56 deletions(-) diff --git a/SignalServiceKit/src/Messages/OWSOutgoingReceiptManager.m b/SignalServiceKit/src/Messages/OWSOutgoingReceiptManager.m index 54ac50782..212d9614b 100644 --- a/SignalServiceKit/src/Messages/OWSOutgoingReceiptManager.m +++ b/SignalServiceKit/src/Messages/OWSOutgoingReceiptManager.m @@ -66,7 +66,7 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa // Start processing. [AppReadiness runNowOrWhenAppIsReady:^{ - [self scheduleProcessing]; + [self process]; }]; return self; @@ -100,8 +100,7 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa } // Schedules a processing pass, unless one is already scheduled. -- (void)scheduleProcessing -{ +- (void)process { OWSAssertDebug(AppReadiness.isAppReady); dispatch_async(self.serialQueue, ^{ @@ -109,54 +108,49 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa return; } - self.isProcessing = YES; + OWSLogVerbose(@"Processing outbound receipts."); - [self process]; - }); -} + self.isProcessing = YES; -- (void)process -{ - OWSLogVerbose(@"Processing outbound receipts."); + if (!self.reachability.isReachable) { + // No network availability; abort. + self.isProcessing = NO; + return; + } - if (!self.reachability.isReachable) { - // No network availability; abort. - self.isProcessing = NO; - return; - } + NSMutableArray *sendPromises = [NSMutableArray array]; + [sendPromises addObjectsFromArray:[self sendReceiptsForReceiptType:OWSReceiptType_Delivery]]; + [sendPromises addObjectsFromArray:[self sendReceiptsForReceiptType:OWSReceiptType_Read]]; - NSMutableArray *sendPromises = [NSMutableArray array]; - [sendPromises addObjectsFromArray:[self sendReceiptsForCollection:kOutgoingDeliveryReceiptManagerCollection - receiptType:OWSReceiptType_Delivery]]; - [sendPromises addObjectsFromArray:[self sendReceiptsForCollection:kOutgoingReadReceiptManagerCollection - receiptType:OWSReceiptType_Read]]; - - if (sendPromises.count < 1) { - // No work to do; abort. - self.isProcessing = NO; - return; - } + if (sendPromises.count < 1) { + // No work to do; abort. + self.isProcessing = NO; + return; + } - AnyPromise *completionPromise = PMKJoin(sendPromises); - completionPromise.always(^() { - // Wait N seconds before conducting another pass. - // This allows time for a batch to accumulate. - // - // We want a value high enough to allow us to effectively de-duplicate - // receipts without being so high that we incur so much latency that - // the user notices. - const CGFloat kProcessingFrequencySeconds = 3.f; - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kProcessingFrequencySeconds * NSEC_PER_SEC)), - self.serialQueue, - ^{ - [self process]; - }); + AnyPromise *completionPromise = PMKJoin(sendPromises); + completionPromise.always(^() { + // Wait N seconds before conducting another pass. + // This allows time for a batch to accumulate. + // + // We want a value high enough to allow us to effectively de-duplicate + // receipts without being so high that we incur so much latency that + // the user notices. + const CGFloat kProcessingFrequencySeconds = 3.f; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kProcessingFrequencySeconds * NSEC_PER_SEC)), + self.serialQueue, + ^{ + self.isProcessing = NO; + + [self process]; + }); + }); + [completionPromise retainUntilComplete]; }); - [completionPromise retainUntilComplete]; } -- (NSArray *)sendReceiptsForCollection:(NSString *)collection receiptType:(OWSReceiptType)receiptType -{ +- (NSArray *)sendReceiptsForReceiptType:(OWSReceiptType)receiptType { + NSString *collection = [self collectionForReceiptType:receiptType]; NSMutableDictionary *> *queuedReceiptMap = [NSMutableDictionary new]; [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { @@ -200,7 +194,7 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa OWSLogInfo( @"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. resolve(@(1)); @@ -221,20 +215,17 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa { [self enqueueReceiptWithRecipientId:envelope.source timestamp:envelope.timestamp - collection:kOutgoingDeliveryReceiptManagerCollection]; + receiptType:OWSReceiptType_Delivery]; } -- (void)enqueueReadReceiptForEnvelope:(NSString *)messageAuthorId timestamp:(uint64_t)timestamp -{ - [self enqueueReceiptWithRecipientId:messageAuthorId - timestamp:timestamp - collection:kOutgoingReadReceiptManagerCollection]; +- (void)enqueueReadReceiptForEnvelope:(NSString *)messageAuthorId timestamp:(uint64_t)timestamp { + [self enqueueReceiptWithRecipientId:messageAuthorId timestamp:timestamp receiptType:OWSReceiptType_Read]; } - (void)enqueueReceiptWithRecipientId:(NSString *)recipientId timestamp:(uint64_t)timestamp - collection:(NSString *)collection -{ + receiptType:(OWSReceiptType)receiptType { + NSString *collection = [self collectionForReceiptType:receiptType]; if (recipientId.length < 1) { OWSFailDebug(@"Invalid recipient id."); @@ -254,14 +245,15 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa [transaction setObject:newTimestamps forKey:recipientId inCollection:collection]; }]; - [self scheduleProcessing]; + [self process]; }); } - (void)dequeueReceiptsWithRecipientId:(NSString *)recipientId timestamps:(NSSet *)timestamps - collection:(NSString *)collection -{ + receiptType:(OWSReceiptType)receiptType { + NSString *collection = [self collectionForReceiptType:receiptType]; + if (recipientId.length < 1) { OWSFailDebug(@"Invalid recipient id."); return; @@ -290,7 +282,16 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa { OWSAssertIsOnMainThread(); - [self scheduleProcessing]; + [self process]; +} + +- (NSString *)collectionForReceiptType:(OWSReceiptType)receiptType { + switch (receiptType) { + case OWSReceiptType_Delivery: + return kOutgoingDeliveryReceiptManagerCollection; + case OWSReceiptType_Read: + return kOutgoingReadReceiptManagerCollection; + } } @end