From 69dea7548696fa4f1c82c98c420089d4b1b33c89 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Tue, 20 Jul 2021 14:06:23 +1000 Subject: [PATCH] potentially fix the conversation stuck at unread --- SessionMessagingKit/Jobs/AttachmentDownloadJob.swift | 5 +++++ SessionMessagingKit/Messages/Signal/TSIncomingMessage.m | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/SessionMessagingKit/Jobs/AttachmentDownloadJob.swift b/SessionMessagingKit/Jobs/AttachmentDownloadJob.swift index 980582e83..f7a281b20 100644 --- a/SessionMessagingKit/Jobs/AttachmentDownloadJob.swift +++ b/SessionMessagingKit/Jobs/AttachmentDownloadJob.swift @@ -86,6 +86,11 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject self.handlePermanentFailure(error: error) } else if let error = error as? OnionRequestAPI.Error, case .httpRequestFailedAtDestination(let statusCode, _, _) = error, statusCode == 400 { + // Otherwise, the attachment will show a state of downloading forever, + // and the message won't be able to be marked as read. + storage.write(with: { transaction in + storage.setAttachmentState(to: .failed, for: pointer, associatedWith: self.tsMessageID, using: transaction) + }, completion: { }) // This usually indicates a file that has expired on the server, so there's no need to retry. self.handlePermanentFailure(error: error) } else { diff --git a/SessionMessagingKit/Messages/Signal/TSIncomingMessage.m b/SessionMessagingKit/Messages/Signal/TSIncomingMessage.m index cdf88f736..6d5d17ab7 100644 --- a/SessionMessagingKit/Messages/Signal/TSIncomingMessage.m +++ b/SessionMessagingKit/Messages/Signal/TSIncomingMessage.m @@ -147,6 +147,11 @@ NS_ASSUME_NONNULL_BEGIN BOOL areAllAttachmentsDownloaded = YES; for (NSString *attachmentId in self.attachmentIds) { TSAttachment *attachment = [TSAttachment fetchObjectWithUniqueID:attachmentId transaction:transaction]; + // If the attachment download is failed, we can mark this message as read. + // Otherwise, this message will never be marked as read. + if ([attachment isKindOfClass:[TSAttachmentPointer class]] && ((TSAttachmentPointer *)attachment).state == TSAttachmentPointerStateFailed) { + continue; + } areAllAttachmentsDownloaded = areAllAttachmentsDownloaded && attachment.isDownloaded; if (!areAllAttachmentsDownloaded) break; }