From 2e8e931551ec8c845fb4011cb3f087bd06bb3184 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Mon, 9 Aug 2021 16:52:42 +1000 Subject: [PATCH] minor refactor --- .../MessageReceiver+Handling.swift | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift index 3c516cea7..7fd6be707 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift @@ -226,20 +226,24 @@ extension MessageReceiver { guard message.sender == message.author else { return } let userPublicKey = getUserHexEncodedPublicKey() let transaction = transaction as! YapDatabaseReadWriteTransaction - if let author = message.author, let timestamp = message.timestamp, - let messageToDelete = userPublicKey == message.sender ? TSOutgoingMessage.find(withTimestamp: timestamp) : TSIncomingMessage.find(withAuthorId: author, timestamp: timestamp, transaction: transaction) { - if let incomingMessage = messageToDelete as? TSIncomingMessage { - incomingMessage.markAsReadNow(withSendReadReceipt: false, transaction: transaction) - if let notificationIdentifier = incomingMessage.notificationIdentifier, !notificationIdentifier.isEmpty { - UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [notificationIdentifier]) - UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [notificationIdentifier]) - + if let author = message.author, let timestamp = message.timestamp { + let localMessage: TSMessage? + if userPublicKey == message.sender { localMessage = TSOutgoingMessage.find(withTimestamp: timestamp) } + else { localMessage = TSIncomingMessage.find(withAuthorId: author, timestamp: timestamp, transaction: transaction) } + + if let messageToDelete = localMessage { + if let incomingMessage = messageToDelete as? TSIncomingMessage { + incomingMessage.markAsReadNow(withSendReadReceipt: false, transaction: transaction) + if let notificationIdentifier = incomingMessage.notificationIdentifier, !notificationIdentifier.isEmpty { + UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [notificationIdentifier]) + UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [notificationIdentifier]) + } } + if let serverHash = messageToDelete.serverHash { + SnodeAPI.deleteMessage(publicKey: author, serverHashes: [serverHash]).retainUntilComplete() + } + messageToDelete.updateForDeletion(with: transaction) } - if let serverHash = messageToDelete.serverHash { - SnodeAPI.deleteMessage(publicKey: author, serverHashes: [serverHash]).retainUntilComplete() - } - messageToDelete.updateForDeletion(with: transaction) } }