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,54 +108,49 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa
return; return;
} }
self.isProcessing = YES; OWSLogVerbose(@"Processing outbound receipts.");
[self process]; self.isProcessing = YES;
});
}
- (void)process if (!self.reachability.isReachable) {
{ // No network availability; abort.
OWSLogVerbose(@"Processing outbound receipts."); self.isProcessing = NO;
return;
}
if (!self.reachability.isReachable) { NSMutableArray<AnyPromise *> *sendPromises = [NSMutableArray array];
// No network availability; abort. [sendPromises addObjectsFromArray:[self sendReceiptsForReceiptType:OWSReceiptType_Delivery]];
self.isProcessing = NO; [sendPromises addObjectsFromArray:[self sendReceiptsForReceiptType:OWSReceiptType_Read]];
return;
}
NSMutableArray<AnyPromise *> *sendPromises = [NSMutableArray array]; if (sendPromises.count < 1) {
[sendPromises addObjectsFromArray:[self sendReceiptsForCollection:kOutgoingDeliveryReceiptManagerCollection // No work to do; abort.
receiptType:OWSReceiptType_Delivery]]; self.isProcessing = NO;
[sendPromises addObjectsFromArray:[self sendReceiptsForCollection:kOutgoingReadReceiptManagerCollection return;
receiptType:OWSReceiptType_Read]]; }
if (sendPromises.count < 1) {
// No work to do; abort.
self.isProcessing = NO;
return;
}
AnyPromise *completionPromise = PMKJoin(sendPromises); AnyPromise *completionPromise = PMKJoin(sendPromises);
completionPromise.always(^() { completionPromise.always(^() {
// Wait N seconds before conducting another pass. // Wait N seconds before conducting another pass.
// This allows time for a batch to accumulate. // This allows time for a batch to accumulate.
// //
// We want a value high enough to allow us to effectively de-duplicate // 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 // receipts without being so high that we incur so much latency that
// the user notices. // the user notices.
const CGFloat kProcessingFrequencySeconds = 3.f; const CGFloat kProcessingFrequencySeconds = 3.f;
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 process]; self.isProcessing = NO;
});
[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