From aea4330bca6b094e517a75c5910a5e63e1882e02 Mon Sep 17 00:00:00 2001 From: fmodf Date: Tue, 6 Aug 2024 13:15:13 +0200 Subject: [PATCH] wip --- .../Database+Martin/Database+Martin.swift | 7 + .../Database+MartinChannelManager.swift | 21 +++ .../Database+MartinChatManager.swift | 72 +++++++++++ .../Database+MartinRoomManager.swift | 105 +++++++++++++++ .../Database+MartinRosterManager.swift} | 122 ------------------ .../Database/Database+Migrations.swift | 3 + .../AppCore/Models/Room.swift | 3 + 7 files changed, 211 insertions(+), 122 deletions(-) create mode 100644 ConversationsClassic/AppCore/Database/Database+Martin/Database+Martin.swift create mode 100644 ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinChannelManager.swift create mode 100644 ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinChatManager.swift create mode 100644 ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinRoomManager.swift rename ConversationsClassic/AppCore/Database/{Database+Martin.swift => Database+Martin/Database+MartinRosterManager.swift} (57%) diff --git a/ConversationsClassic/AppCore/Database/Database+Martin/Database+Martin.swift b/ConversationsClassic/AppCore/Database/Database+Martin/Database+Martin.swift new file mode 100644 index 0000000..aec3ef4 --- /dev/null +++ b/ConversationsClassic/AppCore/Database/Database+Martin/Database+Martin.swift @@ -0,0 +1,7 @@ +import Foundation +import GRDB +import Martin + +extension Database: MartinsManager {} + +// Check specific implementation in Database+Martin* files diff --git a/ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinChannelManager.swift b/ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinChannelManager.swift new file mode 100644 index 0000000..bac242c --- /dev/null +++ b/ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinChannelManager.swift @@ -0,0 +1,21 @@ +import Foundation +import GRDB +import Martin + +extension Database: Martin.ChannelManager { + func channels(for _: Martin.Context) -> [any Martin.ChannelProtocol] { + [] + } + + func createChannel(for _: Martin.Context, with _: Martin.BareJID, participantId _: String, nick _: String?, state _: Martin.ChannelState) -> Martin.ConversationCreateResult { + .none + } + + func channel(for _: Martin.Context, with _: Martin.BareJID) -> (any Martin.ChannelProtocol)? { + nil + } + + func close(channel _: any Martin.ChannelProtocol) -> Bool { + false + } +} diff --git a/ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinChatManager.swift b/ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinChatManager.swift new file mode 100644 index 0000000..6f795f9 --- /dev/null +++ b/ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinChatManager.swift @@ -0,0 +1,72 @@ +import Foundation +import GRDB +import Martin + +extension Database: Martin.ChatManager { + func chats(for context: Martin.Context) -> [any Martin.ChatProtocol] { + do { + let chats: [Chat] = try _db.read { db in + try Chat.filter(Column("account") == context.userBareJid.stringValue).fetchAll(db) + } + return chats.map { chat in + Martin.ChatBase(context: context, jid: BareJID(chat.participant)) + } + } catch { + logIt(.error, "Error fetching chats: \(error.localizedDescription)") + return [] + } + } + + func chat(for context: Martin.Context, with: Martin.BareJID) -> (any Martin.ChatProtocol)? { + do { + let chat: Chat? = try _db.read { db in + try Chat + .filter(Column("account") == context.userBareJid.stringValue) + .filter(Column("participant") == with.stringValue) + .fetchOne(db) + } + if chat != nil { + return Martin.ChatBase(context: context, jid: with) + } else { + return nil + } + } catch { + logIt(.error, "Error fetching chat: \(error.localizedDescription)") + return nil + } + } + + func createChat(for context: Martin.Context, with: Martin.BareJID) -> (any Martin.ChatProtocol)? { + do { + let chat: Chat? = try _db.read { db in + try Chat + .filter(Column("account") == context.userBareJid.stringValue) + .filter(Column("participant") == with.stringValue) + .fetchOne(db) + } + if chat != nil { + return Martin.ChatBase(context: context, jid: with) + } else { + let chat = Chat( + id: UUID().uuidString, + account: context.userBareJid.stringValue, + participant: with.stringValue, + type: .chat + ) + try _db.write { db in + try chat.save(db) + } + return Martin.ChatBase(context: context, jid: with) + } + } catch { + logIt(.error, "Error fetching chat: \(error.localizedDescription)") + return nil + } + } + + func close(chat: any Martin.ChatProtocol) -> Bool { + // not used in Martin library for now + print("Closing chat: \(chat)") + return false + } +} diff --git a/ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinRoomManager.swift b/ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinRoomManager.swift new file mode 100644 index 0000000..6a399c4 --- /dev/null +++ b/ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinRoomManager.swift @@ -0,0 +1,105 @@ +import Foundation +import GRDB +import Martin + +extension Database: Martin.RoomManager { + func rooms(for context: Martin.Context) -> [any Martin.RoomProtocol] { + do { + let rooms: [Room] = try _db.read { db in + try Room.filter(Column("account") == context.userBareJid.stringValue).fetchAll(db) + } + return rooms.map { room in + Martin.RoomBase( + context: context, + jid: context.userBareJid, + nickname: room.nickname, + password: room.password, + dispatcher: QueueDispatcher(label: "room-\(room.id)") + ) + } + } catch { + logIt(.error, "Error fetching channels: \(error.localizedDescription)") + return [] + } + } + + func room(for context: Martin.Context, with roomJid: Martin.BareJID) -> (any Martin.RoomProtocol)? { + do { + let room: Room? = try _db.read { db in + try Room + .filter(Column("account") == context.userBareJid.stringValue) + .filter(Column("id") == roomJid.stringValue) + .fetchOne(db) + } + if let room { + return Martin.RoomBase( + context: context, + jid: context.userBareJid, + nickname: room.nickname, + password: room.password, + dispatcher: QueueDispatcher(label: "room-\(room.id)") + ) + } else { + return nil + } + } catch { + logIt(.error, "Error fetching room: \(error.localizedDescription)") + return nil + } + } + + func createRoom(for context: Martin.Context, with roomJid: Martin.BareJID, nickname: String, password: String?) -> (any Martin.RoomProtocol)? { + do { + let room: Room? = try _db.read { db in + try Room + .filter(Column("account") == context.userBareJid.stringValue) + .filter(Column("id") == roomJid.stringValue) + .fetchOne(db) + } + if let room { + return Martin.RoomBase( + context: context, + jid: context.userBareJid, + nickname: room.nickname, + password: room.password, + dispatcher: QueueDispatcher(label: "room-\(room.id)") + ) + } else { + let room = Room( + id: roomJid.stringValue, + account: context.userBareJid.stringValue, + nickname: nickname, + password: password + ) + try _db.write { db in + try room.save(db) + } + return Martin.RoomBase( + context: context, + jid: context.userBareJid, + nickname: nickname, + password: password, + dispatcher: QueueDispatcher(label: "room-\(room.id)") + ) + } + } catch { + logIt(.error, "Error fetching room: \(error.localizedDescription)") + return nil + } + } + + func close(room: any Martin.RoomProtocol) -> Bool { + do { + try _db.write { db in + try Room + .filter(Column("account") == room.context?.userBareJid.stringValue ?? "") + .filter(Column("id") == room.jid.stringValue) + .deleteAll(db) + } + return true + } catch { + logIt(.error, "Error closing room: \(error.localizedDescription)") + return false + } + } +} diff --git a/ConversationsClassic/AppCore/Database/Database+Martin.swift b/ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinRosterManager.swift similarity index 57% rename from ConversationsClassic/AppCore/Database/Database+Martin.swift rename to ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinRosterManager.swift index d463018..194dc29 100644 --- a/ConversationsClassic/AppCore/Database/Database+Martin.swift +++ b/ConversationsClassic/AppCore/Database/Database+Martin/Database+MartinRosterManager.swift @@ -2,9 +2,6 @@ import Foundation import GRDB import Martin -extension Database: MartinsManager {} - -// MARK: - Martin's roster manager extension Database: Martin.RosterManager { func clear(for context: Martin.Context) { print("Clearing roster for context: \(context)") @@ -154,122 +151,3 @@ extension Database: Martin.RosterManager { func initialize(context _: Martin.Context) {} func deinitialize(context _: Martin.Context) {} } - -// MARK: - Martin's chats manager -extension Database: Martin.ChatManager { - func chats(for context: Martin.Context) -> [any Martin.ChatProtocol] { - do { - let chats: [Chat] = try _db.read { db in - try Chat.filter(Column("account") == context.userBareJid.stringValue).fetchAll(db) - } - return chats.map { chat in - Martin.ChatBase(context: context, jid: BareJID(chat.participant)) - } - } catch { - logIt(.error, "Error fetching chats: \(error.localizedDescription)") - return [] - } - } - - func chat(for context: Martin.Context, with: Martin.BareJID) -> (any Martin.ChatProtocol)? { - do { - let chat: Chat? = try _db.read { db in - try Chat - .filter(Column("account") == context.userBareJid.stringValue) - .filter(Column("participant") == with.stringValue) - .fetchOne(db) - } - if chat != nil { - return Martin.ChatBase(context: context, jid: with) - } else { - return nil - } - } catch { - logIt(.error, "Error fetching chat: \(error.localizedDescription)") - return nil - } - } - - func createChat(for context: Martin.Context, with: Martin.BareJID) -> (any Martin.ChatProtocol)? { - do { - let chat: Chat? = try _db.read { db in - try Chat - .filter(Column("account") == context.userBareJid.stringValue) - .filter(Column("participant") == with.stringValue) - .fetchOne(db) - } - if chat != nil { - return Martin.ChatBase(context: context, jid: with) - } else { - let chat = Chat( - id: UUID().uuidString, - account: context.userBareJid.stringValue, - participant: with.stringValue, - type: .chat - ) - try _db.write { db in - try chat.save(db) - } - return Martin.ChatBase(context: context, jid: with) - } - } catch { - logIt(.error, "Error fetching chat: \(error.localizedDescription)") - return nil - } - } - - func close(chat: any Martin.ChatProtocol) -> Bool { - // not used in Martin library for now - print("Closing chat: \(chat)") - return false - } -} - -// MARK: - Martin's rooms manager -extension Database: Martin.RoomManager { - func rooms(for _: Martin.Context) -> [any Martin.RoomProtocol] { - [] - } - - func room(for _: Martin.Context, with _: Martin.BareJID) -> (any Martin.RoomProtocol)? { - nil - } - - func createRoom(for _: Martin.Context, with _: Martin.BareJID, nickname _: String, password _: String?) -> (any Martin.RoomProtocol)? { - nil - } - - func close(room _: any Martin.RoomProtocol) -> Bool { - false - } -} - -// MARK: - Martin's channels manager -extension Database: Martin.ChannelManager { - func channels(for _: Martin.Context) -> [any Martin.ChannelProtocol] { - [] - // do { - // let channels: [Channel] = try _db.read { db in - // try Channel.filter(Column("account") == context.userBareJid.stringValue).fetchAll(db) - // } - // return channels.map { channel in - // Martin.ChannelBase(context: context, channelJid: channel.channel, participantId: , nickname: , state: ) - // } - // } catch { - // logIt(.error, "Error fetching channels: \(error.localizedDescription)") - // return [] - // } - } - - func createChannel(for _: Martin.Context, with _: Martin.BareJID, participantId _: String, nick _: String?, state _: Martin.ChannelState) -> Martin.ConversationCreateResult { - .none - } - - func channel(for _: Martin.Context, with _: Martin.BareJID) -> (any Martin.ChannelProtocol)? { - nil - } - - func close(channel _: any Martin.ChannelProtocol) -> Bool { - false - } -} diff --git a/ConversationsClassic/AppCore/Database/Database+Migrations.swift b/ConversationsClassic/AppCore/Database/Database+Migrations.swift index 7c8b47d..c0336d3 100644 --- a/ConversationsClassic/AppCore/Database/Database+Migrations.swift +++ b/ConversationsClassic/AppCore/Database/Database+Migrations.swift @@ -71,6 +71,9 @@ extension Database { // rooms try db.create(table: "rooms", options: [.ifNotExists]) { table in table.column("id", .text).notNull().primaryKey().unique(onConflict: .replace) + table.column("account", .text).notNull() + table.column("nickname", .text).notNull() + table.column("password", .text) } // channels diff --git a/ConversationsClassic/AppCore/Models/Room.swift b/ConversationsClassic/AppCore/Models/Room.swift index b72cae6..583fd8b 100644 --- a/ConversationsClassic/AppCore/Models/Room.swift +++ b/ConversationsClassic/AppCore/Models/Room.swift @@ -6,6 +6,9 @@ import SwiftUI // MARK: - Account struct Room: DBStorable { var id: String + var account: String + var nickname: String + var password: String? } extension Room {