Handle new-style delivery receipts.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 60738b450f
commit 25c40ea3cf

@ -172,7 +172,6 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) {
- (void)updateWithCustomMessage:(NSString *)customMessage transaction:(YapDatabaseReadWriteTransaction *)transaction; - (void)updateWithCustomMessage:(NSString *)customMessage transaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)updateWithCustomMessage:(NSString *)customMessage; - (void)updateWithCustomMessage:(NSString *)customMessage;
- (void)updateWithWasDeliveredWithTransaction:(YapDatabaseReadWriteTransaction *)transaction; - (void)updateWithWasDeliveredWithTransaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)updateWithWasDelivered;
- (void)updateWithWasSentFromLinkedDeviceWithTransaction:(YapDatabaseReadWriteTransaction *)transaction; - (void)updateWithWasSentFromLinkedDeviceWithTransaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)updateWithSingleGroupRecipient:(NSString *)singleGroupRecipient - (void)updateWithSingleGroupRecipient:(NSString *)singleGroupRecipient
transaction:(YapDatabaseReadWriteTransaction *)transaction; transaction:(YapDatabaseReadWriteTransaction *)transaction;

@ -317,13 +317,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
}]; }];
} }
- (void)updateWithWasDelivered
{
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self updateWithWasDeliveredWithTransaction:transaction];
}];
}
- (void)updateWithWasSentFromLinkedDeviceWithTransaction:(YapDatabaseReadWriteTransaction *)transaction - (void)updateWithWasSentFromLinkedDeviceWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
{ {
OWSAssert(transaction); OWSAssert(transaction);

@ -186,26 +186,43 @@ NS_ASSUME_NONNULL_BEGIN
OWSAssert(envelope); OWSAssert(envelope);
OWSAssert(transaction); OWSAssert(transaction);
[self processDeliveryReceipts:envelope.source
sentTimestamps:@[
@(envelope.timestamp),
]
transaction:transaction];
}
- (void)processDeliveryReceipts:(NSString *)recipientId
sentTimestamps:(NSArray<NSNumber *> *)sentTimestamps
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssert(recipientId);
OWSAssert(sentTimestamps);
OWSAssert(transaction);
for (NSNumber *nsTimestamp in sentTimestamps) {
uint64_t timestamp = [nsTimestamp unsignedLongLongValue];
NSArray<TSOutgoingMessage *> *messages NSArray<TSOutgoingMessage *> *messages
= (NSArray<TSOutgoingMessage *> *)[TSInteraction interactionsWithTimestamp:envelope.timestamp = (NSArray<TSOutgoingMessage *> *)[TSInteraction interactionsWithTimestamp:timestamp
ofClass:[TSOutgoingMessage class] ofClass:[TSOutgoingMessage class]
withTransaction:transaction]; withTransaction:transaction];
if (messages.count < 1) { if (messages.count < 1) {
// Desktop currently sends delivery receipts for "unpersisted" messages // Desktop currently sends delivery receipts for "unpersisted" messages
// like group updates, so these errors are expected to a certain extent. // like group updates, so these errors are expected to a certain extent.
DDLogInfo(@"%@ Missing message for delivery receipt: %llu", self.tag, envelope.timestamp); DDLogInfo(@"%@ Missing message for delivery receipt: %llu", self.tag, timestamp);
} else { } else {
if (messages.count > 1) { if (messages.count > 1) {
DDLogInfo(@"%@ More than one message (%zd) for delivery receipt: %llu", DDLogInfo(
self.tag, @"%@ More than one message (%zd) for delivery receipt: %llu", self.tag, messages.count, timestamp);
messages.count,
envelope.timestamp);
} }
for (TSOutgoingMessage *outgoingMessage in messages) { for (TSOutgoingMessage *outgoingMessage in messages) {
[outgoingMessage updateWithWasDeliveredWithTransaction:transaction]; [outgoingMessage updateWithWasDeliveredWithTransaction:transaction];
} }
} }
} }
}
- (void)handleEnvelope:(OWSSignalServiceProtosEnvelope *)envelope - (void)handleEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
plaintextData:(NSData *)plaintextData plaintextData:(NSData *)plaintextData
@ -243,7 +260,7 @@ NS_ASSUME_NONNULL_BEGIN
} else if (content.hasNullMessage) { } else if (content.hasNullMessage) {
DDLogInfo(@"%@ Received null message.", self.tag); DDLogInfo(@"%@ Received null message.", self.tag);
} else if (content.hasReceiptMessage) { } else if (content.hasReceiptMessage) {
[self handleIncomingEnvelope:envelope withReceiptMessage:content.receiptMessage]; [self handleIncomingEnvelope:envelope withReceiptMessage:content.receiptMessage transaction:transaction];
} else { } else {
DDLogWarn(@"%@ Ignoring envelope. Content with no known payload", self.tag); DDLogWarn(@"%@ Ignoring envelope. Content with no known payload", self.tag);
} }
@ -340,17 +357,29 @@ NS_ASSUME_NONNULL_BEGIN
- (void)handleIncomingEnvelope:(OWSSignalServiceProtosEnvelope *)envelope - (void)handleIncomingEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
withReceiptMessage:(OWSSignalServiceProtosReceiptMessage *)receiptMessage withReceiptMessage:(OWSSignalServiceProtosReceiptMessage *)receiptMessage
transaction:(YapDatabaseReadWriteTransaction *)transaction
{ {
OWSAssert(envelope); OWSAssert(envelope);
OWSAssert(receiptMessage); OWSAssert(receiptMessage);
OWSAssert(transaction);
PBArray *messageTimestamps = receiptMessage.timestamp;
NSMutableArray<NSNumber *> *sentTimestamps = [NSMutableArray new];
for (int i = 0; i < messageTimestamps.count; i++) {
UInt64 timestamp = [messageTimestamps uint64AtIndex:i];
[sentTimestamps addObject:@(timestamp)];
}
switch (receiptMessage.type) { switch (receiptMessage.type) {
case OWSSignalServiceProtosReceiptMessageTypeDelivery: case OWSSignalServiceProtosReceiptMessageTypeDelivery:
DDLogInfo(@"%@ Ignoring receipt message with delivery receipt.", self.tag); DDLogVerbose(@"%@ Processing receipt message with delivery receipts.", self.tag);
[self processDeliveryReceipts:envelope.source sentTimestamps:sentTimestamps transaction:transaction];
return; return;
case OWSSignalServiceProtosReceiptMessageTypeRead: case OWSSignalServiceProtosReceiptMessageTypeRead:
DDLogVerbose(@"%@ Processing receipt message with read receipts.", self.tag); DDLogVerbose(@"%@ Processing receipt message with read receipts.", self.tag);
[OWSReadReceiptManager.sharedManager processReadReceiptsFromRecipient:receiptMessage envelope:envelope]; [OWSReadReceiptManager.sharedManager processReadReceiptsFromRecipientId:envelope.source
sentTimestamps:sentTimestamps
readTimestamp:envelope.timestamp];
break; break;
default: default:
DDLogInfo(@"%@ Ignoring receipt message of unknown type: %d.", self.tag, (int)receiptMessage.type); DDLogInfo(@"%@ Ignoring receipt message of unknown type: %d.", self.tag, (int)receiptMessage.type);

@ -4,8 +4,6 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@class OWSSignalServiceProtosEnvelope;
@class OWSSignalServiceProtosReceiptMessage;
@class OWSSignalServiceProtosSyncMessageRead; @class OWSSignalServiceProtosSyncMessageRead;
@class TSIncomingMessage; @class TSIncomingMessage;
@class TSOutgoingMessage; @class TSOutgoingMessage;
@ -41,8 +39,9 @@ extern NSString *const kIncomingMessageMarkedAsReadNotification;
// from a user to whom we have sent a message. // from a user to whom we have sent a message.
// //
// This method can be called from any thread. // This method can be called from any thread.
- (void)processReadReceiptsFromRecipient:(OWSSignalServiceProtosReceiptMessage *)receiptMessage - (void)processReadReceiptsFromRecipientId:(NSString *)recipientId
envelope:(OWSSignalServiceProtosEnvelope *)envelope; sentTimestamps:(NSArray<NSNumber *> *)sentTimestamps
readTimestamp:(uint64_t)readTimestamp;
- (void)applyEarlyReadReceiptsForOutgoingMessageFromLinkedDevice:(TSOutgoingMessage *)message - (void)applyEarlyReadReceiptsForOutgoingMessageFromLinkedDevice:(TSOutgoingMessage *)message
transaction:(YapDatabaseReadWriteTransaction *)transaction; transaction:(YapDatabaseReadWriteTransaction *)transaction;

@ -340,28 +340,22 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
#pragma mark - Read Receipts From Recipient #pragma mark - Read Receipts From Recipient
- (void)processReadReceiptsFromRecipient:(OWSSignalServiceProtosReceiptMessage *)receiptMessage - (void)processReadReceiptsFromRecipientId:(NSString *)recipientId
envelope:(OWSSignalServiceProtosEnvelope *)envelope sentTimestamps:(NSArray<NSNumber *> *)sentTimestamps
readTimestamp:(uint64_t)readTimestamp
{ {
OWSAssert(receiptMessage); OWSAssert(recipientId.length > 0);
OWSAssert(envelope); OWSAssert(sentTimestamps);
OWSAssert(receiptMessage.type == OWSSignalServiceProtosReceiptMessageTypeRead);
if (![self areReadReceiptsEnabled]) { if (![self areReadReceiptsEnabled]) {
DDLogInfo(@"%@ Ignoring incoming receipt message as read receipts are disabled.", self.tag); DDLogInfo(@"%@ Ignoring incoming receipt message as read receipts are disabled.", self.tag);
return; return;
} }
NSString *recipientId = envelope.source;
OWSAssert(recipientId.length > 0);
PBArray *sentTimestamps = receiptMessage.timestamp;
UInt64 readTimestamp = envelope.timestamp;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
for (int i = 0; i < sentTimestamps.count; i++) { for (NSNumber *nsSentTimestamp in sentTimestamps) {
UInt64 sentTimestamp = [sentTimestamps uint64AtIndex:i]; UInt64 sentTimestamp = [nsSentTimestamp unsignedLongLongValue];
NSArray<TSOutgoingMessage *> *messages NSArray<TSOutgoingMessage *> *messages
= (NSArray<TSOutgoingMessage *> *)[TSInteraction interactionsWithTimestamp:sentTimestamp = (NSArray<TSOutgoingMessage *> *)[TSInteraction interactionsWithTimestamp:sentTimestamp

Loading…
Cancel
Save