From c1ce9b133da209533040cfeed6fdce73d3e6b7c0 Mon Sep 17 00:00:00 2001 From: fmodf Date: Sun, 14 Jul 2024 19:08:43 +0200 Subject: [PATCH] wip --- .../AppCore/Actions/ConversationActions.swift | 2 + .../Middlewares/DatabaseMiddleware.swift | 38 +++++++++++++++++++ .../AppCore/Middlewares/FileMiddleware.swift | 7 ---- .../Middlewares/SharingMiddleware.swift | 13 +++++++ 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/ConversationsClassic/AppCore/Actions/ConversationActions.swift b/ConversationsClassic/AppCore/Actions/ConversationActions.swift index 9294677..6bb8b32 100644 --- a/ConversationsClassic/AppCore/Actions/ConversationActions.swift +++ b/ConversationsClassic/AppCore/Actions/ConversationActions.swift @@ -5,4 +5,6 @@ enum ConversationAction: Codable { case sendMessage(from: String, to: String, body: String) case setReplyText(String) + + case sendMediaMessages(from: String, to: String, messagesIds: [String], localFilesNames: [String]) } diff --git a/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift index 837f3fc..5d9ff3e 100644 --- a/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift @@ -355,6 +355,44 @@ final class DatabaseMiddleware { } .eraseToAnyPublisher() + case .conversationAction(.sendMediaMessages(let from, let to, let messageIds, let localFilesNames)): + return Future { promise in + Task(priority: .background) { [weak self] in + guard let database = self?.database else { + promise(.success(.databaseAction(.storeMessageFailed(reason: L10n.Global.Error.genericDbError))) + ) + return + } + do { + for (index, id) in messageIds.enumerated() { + let message = Message( + id: id, + type: .chat, + contentType: .attachment, + from: from, + to: to, + body: nil, + subject: nil, + thread: nil, + oobUrl: nil, + date: Date(), + pending: true, + sentError: false, + attachmentLocalName: localFilesNames[index] + ) + try database._db.write { db in + try message.insert(db) + } + } + promise(.success(.empty)) + } catch { + promise(.success(.databaseAction(.storeMessageFailed(reason: error.localizedDescription))) + ) + } + } + } + .eraseToAnyPublisher() + default: return Empty().eraseToAnyPublisher() } diff --git a/ConversationsClassic/AppCore/Middlewares/FileMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/FileMiddleware.swift index af12982..8994c39 100644 --- a/ConversationsClassic/AppCore/Middlewares/FileMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/FileMiddleware.swift @@ -85,13 +85,6 @@ final class FileMiddleware { } .eraseToAnyPublisher() - case .fileAction(.galleryItemsCopiedForUploading(let newMessageIds, let localNames)): - print("!!!!!!") - print(newMessageIds) - print(localNames) - print("!!!!!!") - return Empty().eraseToAnyPublisher() - default: return Empty().eraseToAnyPublisher() } diff --git a/ConversationsClassic/AppCore/Middlewares/SharingMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/SharingMiddleware.swift index 070e087..3a88081 100644 --- a/ConversationsClassic/AppCore/Middlewares/SharingMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/SharingMiddleware.swift @@ -69,6 +69,19 @@ final class SharingMiddleware { print("Camera captured: \(media.count)") return Empty().eraseToAnyPublisher() + case .fileAction(.galleryItemsCopiedForUploading(let newMessageIds, let localNames)): + if let chat = state.conversationsState.currentChat { + return Just(.conversationAction(.sendMediaMessages( + from: chat.account, + to: chat.participant, + messagesIds: newMessageIds, + localFilesNames: localNames + ))) + .eraseToAnyPublisher() + } else { + return Empty().eraseToAnyPublisher() + } + case .sharingAction(.shareLocation(let lat, let lon)): if let chat = state.conversationsState.currentChat { let msg = "geo:\(lat),\(lon)"