diff --git a/lib/src/services/api/mediafiles/download.service.dart b/lib/src/services/api/mediafiles/download.service.dart index 42a4195..8ebf018 100644 --- a/lib/src/services/api/mediafiles/download.service.dart +++ b/lib/src/services/api/mediafiles/download.service.dart @@ -23,10 +23,44 @@ Future tryDownloadAllMediaFiles({bool force = false}) async { await twonlyDB.mediaFilesDao.getAllMediaFilesPendingDownload(); for (final mediaFile in mediaFiles) { - await startDownloadMedia(mediaFile, force); + if (await canMediaFileBeDownloaded(mediaFile)) { + await startDownloadMedia(mediaFile, force); + } } } +Future canMediaFileBeDownloaded(MediaFile mediaFile) async { + final messages = + await twonlyDB.messagesDao.getMessagesByMediaId(mediaFile.mediaId); + + // Verify that the sender of the original image / message does still exists. + // If not delete the message as it can not be downloaded from the server anymore. + + if (messages.length != 1) { + Log.error('A media for download must have one original message.'); + return false; + } + + if (messages.first.senderId == null) { + Log.error('A media for download must have a sender id.'); + return false; + } + + final contact = + await twonlyDB.contactsDao.getContactById(messages.first.senderId!); + + if (contact == null || contact.accountDeleted) { + Log.info( + 'Sender does not exists anymore. Delete media file and message.', + ); + await twonlyDB.mediaFilesDao.deleteMediaFile(mediaFile.mediaId); + await twonlyDB.messagesDao.deleteMessagesById(messages.first.messageId); + return false; + } + + return true; +} + enum DownloadMediaTypes { video, image, @@ -90,11 +124,9 @@ Future handleDownloadStatusUpdate(TaskStatusUpdate update) async { failed = false; } else { failed = true; - if (update.responseStatusCode != null) { - Log.error( - 'Got invalid response status code: ${update.responseStatusCode}', - ); - } + Log.error( + 'Got invalid response status code: ${update.responseStatusCode}', + ); } } else { Log.info('Got ${update.status} for $mediaId'); diff --git a/lib/src/services/api/messages.dart b/lib/src/services/api/messages.dart index 6db4af4..ea3bafc 100644 --- a/lib/src/services/api/messages.dart +++ b/lib/src/services/api/messages.dart @@ -74,6 +74,14 @@ Future<(Uint8List, Uint8List?)?> tryToSendCompleteMessage({ } receiptId = receipt.receiptId; + final contact = + await twonlyDB.contactsDao.getContactById(receipt.contactId); + if (contact == null || contact.accountDeleted) { + Log.warn('Will not send message again as user does not exist anymore.'); + await twonlyDB.receiptsDao.deleteReceipt(receiptId); + return null; + } + if (!onlyReturnEncryptedData && receipt.ackByServerAt != null && receipt.markForRetry == null) { @@ -177,9 +185,6 @@ Future<(Uint8List, Uint8List?)?> tryToSendCompleteMessage({ if (receiptId != null) { await twonlyDB.receiptsDao.deleteReceipt(receiptId); } - if (receipt != null) { - await twonlyDB.receiptsDao.deleteReceipt(receipt.receiptId); - } } return null; }