|
| 1 | +import Auth |
| 2 | +import Foundation |
| 3 | +import GRDB |
| 4 | +import InlineProtocol |
| 5 | +import Logger |
| 6 | +import MultipartFormDataKit |
| 7 | +import RealtimeAPI |
| 8 | + |
| 9 | +public struct TransactionDeleteMessage: Transaction { |
| 10 | + // Properties |
| 11 | + var messageIds: [Int64] |
| 12 | + var peerId: Peer |
| 13 | + var chatId: Int64 |
| 14 | + |
| 15 | + // Config |
| 16 | + public var id = UUID().uuidString |
| 17 | + var config = TransactionConfig.default |
| 18 | + var date = Date() |
| 19 | + |
| 20 | + public init(messageIds: [Int64], peerId: Peer, chatId: Int64) { |
| 21 | + self.messageIds = messageIds |
| 22 | + self.peerId = peerId |
| 23 | + self.chatId = chatId |
| 24 | + } |
| 25 | + |
| 26 | + // Methods |
| 27 | + func optimistic() { |
| 28 | + Log.shared.debug("Optimistic delete message \(messageIds) \(peerId) \(chatId)") |
| 29 | + do { |
| 30 | + try AppDatabase.shared.dbWriter.write { db in |
| 31 | + let chat = try Chat.fetchOne(db, id: chatId) |
| 32 | + |
| 33 | + let prevChatLastMsgId = chat?.lastMsgId |
| 34 | + |
| 35 | + for messageId in messageIds { |
| 36 | + try Message.filter(Column("messageId") == messageId && Column("chatId") == chatId).deleteAll(db) |
| 37 | + } |
| 38 | + |
| 39 | + // Update last message |
| 40 | + for messageId in messageIds { |
| 41 | + guard prevChatLastMsgId == messageId else { continue } |
| 42 | + |
| 43 | + let previousMessage = try Message |
| 44 | + .filter(Column("chatId") == chat?.id) |
| 45 | + .order(Column("date").desc) |
| 46 | + .limit(1, offset: 1) |
| 47 | + .fetchOne(db) |
| 48 | + |
| 49 | + var updatedChat = chat |
| 50 | + updatedChat?.lastMsgId = previousMessage?.messageId |
| 51 | + try updatedChat?.save(db) |
| 52 | + } |
| 53 | + } |
| 54 | + } catch { |
| 55 | + Log.shared.error("Failed to delete message \(error)") |
| 56 | + } |
| 57 | + |
| 58 | + DispatchQueue.main.async { |
| 59 | + MessagesPublisher.shared.messagesDeleted(messageIds: messageIds, peer: peerId) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + func execute() async throws -> [InlineProtocol.Update] { |
| 64 | + let result = try await Realtime.shared.invoke( |
| 65 | + .deleteMessages, |
| 66 | + input: .deleteMessages(DeleteMessagesInput.with { |
| 67 | + $0.peerID = peerId.toInputPeer() |
| 68 | + $0.messageIds = messageIds |
| 69 | + }) |
| 70 | + ) |
| 71 | + |
| 72 | + guard case let .deleteMessages(response) = result else { |
| 73 | + throw DeleteMessageError.failed |
| 74 | + } |
| 75 | + |
| 76 | + return response.updates |
| 77 | + } |
| 78 | + |
| 79 | + func shouldRetryOnFail(error: Error) -> Bool { |
| 80 | + if let error = error as? RealtimeAPIError { |
| 81 | + switch error { |
| 82 | + case let .rpcError(_, _, code): |
| 83 | + switch code { |
| 84 | + case 400, 401: |
| 85 | + return false |
| 86 | + |
| 87 | + default: |
| 88 | + return true |
| 89 | + } |
| 90 | + default: |
| 91 | + return true |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return true |
| 96 | + } |
| 97 | + |
| 98 | + func didSucceed(result: [InlineProtocol.Update]) async { |
| 99 | + await Realtime.shared.updates.applyBatch(updates: result) |
| 100 | + } |
| 101 | + |
| 102 | + func didFail(error: Error?) async { |
| 103 | + Log.shared.error("Failed to delete message", error: error) |
| 104 | + |
| 105 | + try? await AppDatabase.shared.dbWriter.write { db in |
| 106 | + var chat = try Chat.fetchOne(db, id: chatId) |
| 107 | + } |
| 108 | + |
| 109 | + DispatchQueue.main.async { |
| 110 | + MessagesPublisher.shared.messagesDeleted(messageIds: messageIds, peer: peerId) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + func rollback() async { |
| 115 | + let _ = try? await AppDatabase.shared.dbWriter.write { db in |
| 116 | + var chat = try Chat.fetchOne(db, id: chatId) |
| 117 | + |
| 118 | + let prevChatLastMsgId = chat?.lastMsgId |
| 119 | + |
| 120 | + for messageId in messageIds { |
| 121 | + try Message.filter(Column("messageId") == messageId && Column("chatId") == chatId).deleteAll(db) |
| 122 | + } |
| 123 | + |
| 124 | + // Update last message |
| 125 | + for messageId in messageIds { |
| 126 | + guard prevChatLastMsgId == messageId else { continue } |
| 127 | + |
| 128 | + let previousMessage = try Message |
| 129 | + .filter(Column("chatId") == chat?.id) |
| 130 | + .order(Column("date").desc) |
| 131 | + .limit(1, offset: 1) |
| 132 | + .fetchOne(db) |
| 133 | + |
| 134 | + var updatedChat = chat |
| 135 | + updatedChat?.lastMsgId = previousMessage?.messageId |
| 136 | + try updatedChat?.save(db) |
| 137 | + |
| 138 | + break |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + enum DeleteMessageError: Error { |
| 144 | + case failed |
| 145 | + } |
| 146 | +} |
0 commit comments