Send delivery receipts.

pull/1/head
Matthew Chen 7 years ago
parent de7bffa593
commit 45d6250aee

@ -110,11 +110,11 @@ NSString *const kDeliveryReceiptManagerCollection = @"kDeliveryReceiptManagerCol
usingBlock:^(NSString *key, id object, BOOL *stop) { usingBlock:^(NSString *key, id object, BOOL *stop) {
NSString *recipientId = key; NSString *recipientId = key;
NSSet<NSNumber *> *timestamps = object; NSSet<NSNumber *> *timestamps = object;
deliveryReceiptMap[recipientId] = timestamps; deliveryReceiptMap[recipientId] = [timestamps copy];
}]; }];
}]; }];
BOOL didWork = NO; NSMutableArray<AnyPromise *> *sendPromises = [NSMutableArray array];
for (NSString *recipientId in deliveryReceiptMap) { for (NSString *recipientId in deliveryReceiptMap) {
NSSet<NSNumber *> *timestamps = deliveryReceiptMap[recipientId]; NSSet<NSNumber *> *timestamps = deliveryReceiptMap[recipientId];
@ -128,41 +128,47 @@ NSString *const kDeliveryReceiptManagerCollection = @"kDeliveryReceiptManagerCol
[OWSReceiptsForSenderMessage deliveryReceiptsForSenderMessageWithThread:thread [OWSReceiptsForSenderMessage deliveryReceiptsForSenderMessageWithThread:thread
messageTimestamps:timestamps.allObjects]; messageTimestamps:timestamps.allObjects];
[self.messageSender enqueueMessage:message AnyPromise *sendPromise = [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
success:^{ [self.messageSender enqueueMessage:message
OWSLogInfo(@"Successfully sent %lu delivery receipts to sender.", (unsigned long)timestamps.count); success:^{
} OWSLogInfo(@"Successfully sent %lu delivery receipts to sender.", (unsigned long)timestamps.count);
failure:^(NSError *error) {
OWSLogError(@"Failed to send delivery receipts to sender with error: %@", error);
}];
didWork = YES; [self dequeueDeliveryReceiptsWithRecipientId:recipientId timestamps:timestamps];
}
// Now that they've been processed, remove all enqueued delivery receipts. // The value doesn't matter, we just need any non-NSError value.
// resolve(@(1));
// NOTE: we don't need to worry about race conditions; this }
// collection will only be mutated on serialQueue. failure:^(NSError *error) {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { OWSLogError(@"Failed to send delivery receipts to sender with error: %@", error);
[transaction removeAllObjectsInCollection:kDeliveryReceiptManagerCollection];
}];
if (!didWork) { resolve(error);
}];
}];
[sendPromises addObject:sendPromise];
}
if (sendPromises.count < 1) {
// No work to do; abort.
self.isProcessing = NO; self.isProcessing = NO;
return; return;
} }
// Wait N seconds before processing delivery receipts again. AnyPromise *completionPromise = PMKJoin(sendPromises);
// This allows time for a batch to accumulate. completionPromise.always(^() {
// // Wait N seconds before processing delivery receipts again.
// We want a value high enough to allow us to effectively de-duplicate, // This allows time for a batch to accumulate.
// delivery receipts without being so high that we risk not sending delivery //
// receipts due to app exit. // We want a value high enough to allow us to effectively de-duplicate,
const CGFloat kProcessingFrequencySeconds = 3.f; // delivery receipts without being so high that we risk not sending delivery
dispatch_after( // receipts due to app exit.
dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kProcessingFrequencySeconds * NSEC_PER_SEC)), self.serialQueue, ^{ const CGFloat kProcessingFrequencySeconds = 3.f;
[self process]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kProcessingFrequencySeconds * NSEC_PER_SEC)),
}); self.serialQueue,
^{
[self process];
});
});
[completionPromise retainUntilComplete];
} }
- (void)envelopeWasReceived:(SSKProtoEnvelope *)envelope { - (void)envelopeWasReceived:(SSKProtoEnvelope *)envelope {

Loading…
Cancel
Save