2024-06-19 15:15:27 +00:00
|
|
|
import Combine
|
|
|
|
import Foundation
|
|
|
|
import GRDB
|
|
|
|
|
2024-07-13 01:29:46 +00:00
|
|
|
// swiftlint:disable:next type_body_length
|
2024-06-19 15:15:27 +00:00
|
|
|
final class DatabaseMiddleware {
|
|
|
|
static let shared = DatabaseMiddleware()
|
|
|
|
private let database = Database.shared
|
|
|
|
private var cancellables: Set<AnyCancellable> = []
|
2024-06-26 08:00:59 +00:00
|
|
|
private var conversationCancellables: Set<AnyCancellable> = []
|
2024-06-19 15:15:27 +00:00
|
|
|
|
|
|
|
private init() {
|
|
|
|
// Database changes
|
|
|
|
ValueObservation
|
|
|
|
.tracking(Roster.fetchAll)
|
|
|
|
.publisher(in: database._db, scheduling: .immediate)
|
|
|
|
.sink { _ in
|
|
|
|
// Handle completion
|
|
|
|
} receiveValue: { rosters in
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
store.dispatch(.databaseAction(.storedRostersLoaded(rosters: rosters)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
|
|
|
ValueObservation
|
|
|
|
.tracking(Chat.fetchAll)
|
|
|
|
.publisher(in: database._db, scheduling: .immediate)
|
|
|
|
.sink { _ in
|
|
|
|
// Handle completion
|
|
|
|
} receiveValue: { chats in
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
store.dispatch(.databaseAction(.storedChatsLoaded(chats: chats)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
|
2024-07-14 10:08:51 +00:00
|
|
|
// swiftlint:disable:next function_body_length cyclomatic_complexity
|
2024-06-19 15:15:27 +00:00
|
|
|
func middleware(state _: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
|
|
|
|
switch action {
|
2024-06-26 08:00:59 +00:00
|
|
|
// MARK: Accounts
|
2024-06-19 15:15:27 +00:00
|
|
|
case .startAction(.loadStoredAccounts):
|
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
Task(priority: .background) { [weak self] in
|
|
|
|
guard let database = self?.database else {
|
|
|
|
promise(.success(.databaseAction(.loadingStoredAccountsFailed)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
try database._db.read { db in
|
|
|
|
let accounts = try Account.fetchAll(db)
|
|
|
|
promise(.success(.databaseAction(.storedAccountsLoaded(accounts: accounts))))
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
promise(.success(.databaseAction(.loadingStoredAccountsFailed)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
|
|
|
case .accountsAction(.makeAccountPermanent(let account)):
|
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
Task(priority: .background) { [weak self] in
|
|
|
|
guard let database = self?.database else {
|
|
|
|
promise(.success(.databaseAction(.updateAccountFailed)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
try database._db.write { db in
|
|
|
|
// make permanent and store to database
|
|
|
|
var acc = account
|
|
|
|
acc.isTemp = false
|
|
|
|
try acc.insert(db)
|
|
|
|
|
|
|
|
// Re-Fetch all accounts
|
|
|
|
let accounts = try Account.fetchAll(db)
|
|
|
|
|
|
|
|
// Use the accounts
|
|
|
|
promise(.success(.databaseAction(.storedAccountsLoaded(accounts: accounts))))
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
promise(.success(.databaseAction(.updateAccountFailed)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
2024-06-26 08:00:59 +00:00
|
|
|
// MARK: Rosters
|
2024-06-19 15:15:27 +00:00
|
|
|
case .rostersAction(.markRosterAsLocallyDeleted(let ownerJID, let contactJID)):
|
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
Task(priority: .background) { [weak self] in
|
|
|
|
guard let database = self?.database else {
|
|
|
|
promise(.success(.rostersAction(.rosterDeletingFailed(reason: L10n.Global.Error.genericDbError))))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
_ = try database._db.write { db in
|
|
|
|
try Roster
|
|
|
|
.filter(Column("bareJid") == ownerJID)
|
|
|
|
.filter(Column("contactBareJid") == contactJID)
|
|
|
|
.updateAll(db, Column("locallyDeleted").set(to: true))
|
|
|
|
}
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: roster \(contactJID) for account \(ownerJID) marked as locally deleted")))
|
2024-06-19 15:15:27 +00:00
|
|
|
} catch {
|
|
|
|
promise(.success(.rostersAction(.rosterDeletingFailed(reason: L10n.Global.Error.genericDbError))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
|
|
|
case .rostersAction(.unmarkRosterAsLocallyDeleted(let ownerJID, let contactJID)):
|
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
Task(priority: .background) { [weak self] in
|
|
|
|
guard let database = self?.database else {
|
|
|
|
promise(.success(.rostersAction(.rosterDeletingFailed(reason: L10n.Global.Error.genericDbError))))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
_ = try database._db.write { db in
|
|
|
|
try Roster
|
|
|
|
.filter(Column("bareJid") == ownerJID)
|
|
|
|
.filter(Column("contactBareJid") == contactJID)
|
|
|
|
.updateAll(db, Column("locallyDeleted").set(to: false))
|
|
|
|
}
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: roster \(contactJID) for account \(ownerJID) unmarked as locally deleted")))
|
2024-06-19 15:15:27 +00:00
|
|
|
} catch {
|
|
|
|
promise(.success(.rostersAction(.rosterDeletingFailed(reason: L10n.Global.Error.genericDbError))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
2024-06-26 08:00:59 +00:00
|
|
|
// MARK: Chats
|
2024-06-20 05:43:49 +00:00
|
|
|
case .chatsAction(.createNewChat(let accountJid, let participantJid)):
|
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
Task(priority: .background) { [weak self] in
|
|
|
|
guard let database = self?.database else {
|
|
|
|
promise(.success(.chatsAction(.chatCreationFailed(reason: L10n.Global.Error.genericDbError))))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
try database._db.write { db in
|
|
|
|
let chat = Chat(
|
|
|
|
id: UUID().uuidString,
|
|
|
|
account: accountJid,
|
|
|
|
participant: participantJid,
|
|
|
|
type: .chat
|
|
|
|
)
|
|
|
|
try chat.insert(db)
|
|
|
|
promise(.success(.chatsAction(.chatCreated(chat: chat))))
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
promise(.success(.chatsAction(.chatCreationFailed(reason: L10n.Global.Error.genericDbError))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
2024-06-26 08:00:59 +00:00
|
|
|
// MARK: Conversation and messages
|
|
|
|
case .conversationAction(.makeConversationActive(let chat, _)):
|
2024-06-26 09:29:30 +00:00
|
|
|
subscribeToMessages(chat: chat)
|
|
|
|
return Empty().eraseToAnyPublisher()
|
2024-06-26 08:00:59 +00:00
|
|
|
|
2024-06-24 13:28:26 +00:00
|
|
|
case .xmppAction(.xmppMessageReceived(let message)):
|
2024-06-26 08:00:59 +00:00
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
Task(priority: .background) { [weak self] in
|
|
|
|
guard let database = self?.database else {
|
2024-06-26 09:29:30 +00:00
|
|
|
promise(.success(.databaseAction(.storeMessageFailed(reason: L10n.Global.Error.genericDbError))))
|
|
|
|
return
|
|
|
|
}
|
2024-07-02 07:22:28 +00:00
|
|
|
guard message.contentType != .typing, message.body != nil else {
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: message \(message.id) received as 'typing...' or message body is nil")))
|
2024-06-26 08:00:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
try database._db.write { db in
|
|
|
|
try message.insert(db)
|
|
|
|
}
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: message \(message.id) stored in db")))
|
2024-06-26 08:00:59 +00:00
|
|
|
} catch {
|
2024-06-26 09:29:30 +00:00
|
|
|
promise(.success(.databaseAction(.storeMessageFailed(reason: error.localizedDescription))))
|
2024-06-26 08:00:59 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-24 13:28:26 +00:00
|
|
|
}
|
2024-06-26 08:00:59 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
|
|
|
case .conversationAction(.sendMessage(let from, let to, let body)):
|
2024-06-26 10:26:04 +00:00
|
|
|
return Future<AppAction, Never> { 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 {
|
|
|
|
let message = Message(
|
|
|
|
id: UUID().uuidString,
|
|
|
|
type: .chat,
|
|
|
|
contentType: .text,
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
body: body,
|
|
|
|
subject: nil,
|
|
|
|
thread: nil,
|
|
|
|
oobUrl: nil,
|
|
|
|
date: Date(),
|
|
|
|
pending: true,
|
|
|
|
sentError: false
|
|
|
|
)
|
|
|
|
try database._db.write { db in
|
|
|
|
try message.insert(db)
|
|
|
|
}
|
|
|
|
promise(.success(.xmppAction(.xmppMessageSent(message))))
|
|
|
|
} catch {
|
|
|
|
promise(.success(.databaseAction(.storeMessageFailed(reason: error.localizedDescription))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
|
|
|
case .xmppAction(.xmppMessageSendSuccess(let msgId)):
|
|
|
|
// mark message as pending false and sentError false
|
|
|
|
return Future<AppAction, Never> { 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 {
|
|
|
|
_ = try database._db.write { db in
|
|
|
|
try Message
|
|
|
|
.filter(Column("id") == msgId)
|
|
|
|
.updateAll(db, Column("pending").set(to: false), Column("sentError").set(to: false))
|
|
|
|
}
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: message \(msgId) marked in db as sent")))
|
2024-06-26 10:26:04 +00:00
|
|
|
} catch {
|
|
|
|
promise(.success(.databaseAction(.storeMessageFailed(reason: error.localizedDescription)))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
|
|
|
case .xmppAction(.xmppMessageSendFailed(let msgId)):
|
|
|
|
// mark message as pending false and sentError true
|
|
|
|
return Future<AppAction, Never> { 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 {
|
|
|
|
_ = try database._db.write { db in
|
|
|
|
try Message
|
|
|
|
.filter(Column("id") == msgId)
|
|
|
|
.updateAll(db, Column("pending").set(to: false), Column("sentError").set(to: true))
|
|
|
|
}
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: message \(msgId) marked in db as failed to send")))
|
2024-06-26 10:26:04 +00:00
|
|
|
} catch {
|
2024-07-12 11:43:14 +00:00
|
|
|
promise(.success(.databaseAction(.storeMessageFailed(reason: error.localizedDescription))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
2024-07-13 01:29:46 +00:00
|
|
|
// MARK: Attachments
|
2024-07-14 08:52:15 +00:00
|
|
|
case .fileAction(.downloadAttachmentFile(let id, _)):
|
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
Task(priority: .background) { [weak self] in
|
|
|
|
guard let database = self?.database else {
|
|
|
|
promise(.success(.databaseAction(.updateAttachmentFailed(id: id, reason: L10n.Global.Error.genericDbError)))
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
_ = try database._db.write { db in
|
|
|
|
try Message
|
|
|
|
.filter(Column("id") == id)
|
|
|
|
.updateAll(db, Column("attachmentDownloadFailed").set(to: false))
|
|
|
|
}
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: message \(id) marked in db as starting downloading attachment")))
|
2024-07-14 08:52:15 +00:00
|
|
|
} catch {
|
|
|
|
promise(.success(.databaseAction(.updateAttachmentFailed(id: id, reason: error.localizedDescription)))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
2024-07-13 01:29:46 +00:00
|
|
|
case .fileAction(.downloadingAttachmentFileFailed(let id, _)):
|
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
Task(priority: .background) { [weak self] in
|
|
|
|
guard let database = self?.database else {
|
|
|
|
promise(.success(.databaseAction(.updateAttachmentFailed(id: id, reason: L10n.Global.Error.genericDbError)))
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
_ = try database._db.write { db in
|
2024-07-13 13:38:15 +00:00
|
|
|
try Message
|
2024-07-13 01:29:46 +00:00
|
|
|
.filter(Column("id") == id)
|
2024-07-13 14:41:56 +00:00
|
|
|
.updateAll(db, Column("attachmentDownloadFailed").set(to: true))
|
2024-07-13 01:29:46 +00:00
|
|
|
}
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: message \(id) marked in db as failed to download attachment")))
|
2024-07-13 01:29:46 +00:00
|
|
|
} catch {
|
|
|
|
promise(.success(.databaseAction(.updateAttachmentFailed(id: id, reason: error.localizedDescription)))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
2024-07-14 10:08:51 +00:00
|
|
|
case .fileAction(.attachmentFileDownloaded(let id, let localName)):
|
2024-07-12 11:43:14 +00:00
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
Task(priority: .background) { [weak self] in
|
|
|
|
guard let database = self?.database else {
|
2024-07-13 01:29:46 +00:00
|
|
|
promise(.success(.databaseAction(.updateAttachmentFailed(id: id, reason: L10n.Global.Error.genericDbError)))
|
|
|
|
)
|
2024-07-12 11:43:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
_ = try database._db.write { db in
|
2024-07-13 13:38:15 +00:00
|
|
|
try Message
|
2024-07-12 11:43:14 +00:00
|
|
|
.filter(Column("id") == id)
|
2024-07-14 10:08:51 +00:00
|
|
|
.updateAll(db, Column("attachmentLocalName").set(to: localName), Column("attachmentDownloadFailed").set(to: false))
|
2024-07-12 11:43:14 +00:00
|
|
|
}
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: message \(id) marked in db as downloaded attachment")))
|
2024-07-12 11:43:14 +00:00
|
|
|
} catch {
|
2024-07-13 01:29:46 +00:00
|
|
|
promise(.success(.databaseAction(.updateAttachmentFailed(id: id, reason: error.localizedDescription)))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
2024-07-14 10:08:51 +00:00
|
|
|
case .fileAction(.attachmentThumbnailCreated(let id, let thumbnailName)):
|
2024-07-13 01:29:46 +00:00
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
Task(priority: .background) { [weak self] in
|
|
|
|
guard let database = self?.database else {
|
|
|
|
promise(.success(.databaseAction(.updateAttachmentFailed(id: id, reason: L10n.Global.Error.genericDbError)))
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
_ = try database._db.write { db in
|
2024-07-13 13:38:15 +00:00
|
|
|
try Message
|
2024-07-13 01:29:46 +00:00
|
|
|
.filter(Column("id") == id)
|
2024-07-14 10:08:51 +00:00
|
|
|
.updateAll(db, Column("attachmentThumbnailName").set(to: thumbnailName))
|
2024-07-13 01:29:46 +00:00
|
|
|
}
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: message \(id) marked in db as thumbnail created")))
|
2024-07-13 01:29:46 +00:00
|
|
|
} catch {
|
|
|
|
promise(.success(.databaseAction(.updateAttachmentFailed(id: id, reason: error.localizedDescription)))
|
|
|
|
)
|
2024-06-26 10:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
2024-07-14 17:08:43 +00:00
|
|
|
|
2024-07-14 19:22:46 +00:00
|
|
|
// MARK: Sharing
|
2024-07-14 17:08:43 +00:00
|
|
|
case .conversationAction(.sendMediaMessages(let from, let to, let messageIds, let localFilesNames)):
|
|
|
|
return Future<AppAction, Never> { 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,
|
2024-07-14 19:22:46 +00:00
|
|
|
attachmentType: localFilesNames[index].attachmentType,
|
2024-07-14 17:08:43 +00:00
|
|
|
attachmentLocalName: localFilesNames[index]
|
|
|
|
)
|
|
|
|
try database._db.write { db in
|
|
|
|
try message.insert(db)
|
|
|
|
}
|
|
|
|
}
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: messages with sharings stored in db")))
|
2024-07-14 17:08:43 +00:00
|
|
|
} catch {
|
|
|
|
promise(.success(.databaseAction(.storeMessageFailed(reason: error.localizedDescription)))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
2024-07-14 19:22:46 +00:00
|
|
|
|
2024-07-16 12:36:57 +00:00
|
|
|
case .sharingAction(.retrySharing(let id)):
|
|
|
|
return Future<AppAction, Never> { 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 {
|
|
|
|
_ = try database._db.write { db in
|
|
|
|
try Message
|
|
|
|
.filter(Column("id") == id)
|
|
|
|
.updateAll(db, Column("pending").set(to: true), Column("sentError").set(to: false))
|
|
|
|
}
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: message \(id) with shares marked in db as pending to send")))
|
2024-07-16 12:36:57 +00:00
|
|
|
} catch {
|
|
|
|
promise(.success(.databaseAction(.storeMessageFailed(reason: error.localizedDescription)))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
2024-07-16 13:01:27 +00:00
|
|
|
case .xmppAction(.xmppSharingUploadSuccess(let messageId, let remotePath)):
|
2024-07-14 19:22:46 +00:00
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
Task(priority: .background) { [weak self] in
|
|
|
|
guard let database = self?.database else {
|
|
|
|
promise(.success(.databaseAction(.updateAttachmentFailed(id: messageId, reason: L10n.Global.Error.genericDbError)))
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
_ = try database._db.write { db in
|
|
|
|
try Message
|
|
|
|
.filter(Column("id") == messageId)
|
2024-07-16 12:36:57 +00:00
|
|
|
.updateAll(db, Column("attachmentRemotePath").set(to: remotePath), Column("pending").set(to: false), Column("sentError").set(to: false))
|
2024-07-14 19:22:46 +00:00
|
|
|
}
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: shared file uploaded and message \(messageId) marked in db as sent")))
|
2024-07-14 19:22:46 +00:00
|
|
|
} catch {
|
|
|
|
promise(.success(.databaseAction(.updateAttachmentFailed(id: messageId, reason: error.localizedDescription)))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
2024-07-16 13:01:27 +00:00
|
|
|
case .xmppAction(.xmppSharingUploadFailed(let messageId, _)):
|
2024-07-14 19:22:46 +00:00
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
Task(priority: .background) { [weak self] in
|
|
|
|
guard let database = self?.database else {
|
|
|
|
promise(.success(.databaseAction(.updateAttachmentFailed(id: messageId, reason: L10n.Global.Error.genericDbError)))
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
_ = try database._db.write { db in
|
|
|
|
try Message
|
|
|
|
.filter(Column("id") == messageId)
|
|
|
|
.updateAll(db, Column("pending").set(to: false), Column("sentError").set(to: true))
|
|
|
|
}
|
2024-07-16 14:05:15 +00:00
|
|
|
promise(.success(.info("DatabaseMiddleware: shared file upload failed and message \(messageId) marked in db as failed to send")))
|
2024-07-14 19:22:46 +00:00
|
|
|
} catch {
|
|
|
|
promise(.success(.databaseAction(.updateAttachmentFailed(id: messageId, reason: error.localizedDescription)))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
2024-06-24 13:28:26 +00:00
|
|
|
|
2024-06-19 15:15:27 +00:00
|
|
|
default:
|
|
|
|
return Empty().eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-26 09:29:30 +00:00
|
|
|
|
|
|
|
private extension DatabaseMiddleware {
|
|
|
|
func subscribeToMessages(chat: Chat) {
|
|
|
|
conversationCancellables = []
|
|
|
|
ValueObservation
|
|
|
|
.tracking(
|
|
|
|
Message
|
|
|
|
.filter(
|
2024-07-02 06:32:23 +00:00
|
|
|
(Column("to") == chat.account && Column("from") == chat.participant) ||
|
2024-06-26 09:29:30 +00:00
|
|
|
(Column("from") == chat.account && Column("to") == chat.participant)
|
|
|
|
)
|
2024-07-01 10:05:39 +00:00
|
|
|
.order(Column("date").desc)
|
2024-06-26 09:29:30 +00:00
|
|
|
.fetchAll
|
|
|
|
)
|
|
|
|
.publisher(in: database._db, scheduling: .immediate)
|
2024-06-26 10:26:04 +00:00
|
|
|
.sink { _ in
|
2024-06-26 09:29:30 +00:00
|
|
|
} receiveValue: { messages in
|
2024-07-11 15:46:57 +00:00
|
|
|
// messages
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
store.dispatch(.conversationAction(.messagesUpdated(messages: messages)))
|
|
|
|
}
|
2024-06-26 09:29:30 +00:00
|
|
|
}
|
|
|
|
.store(in: &conversationCancellables)
|
|
|
|
}
|
|
|
|
}
|