This commit is contained in:
fmodf 2024-08-06 13:15:13 +02:00
parent 59f802d385
commit aea4330bca
7 changed files with 211 additions and 122 deletions

View file

@ -0,0 +1,7 @@
import Foundation
import GRDB
import Martin
extension Database: MartinsManager {}
// Check specific implementation in Database+Martin* files

View file

@ -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<any Martin.ChannelProtocol> {
.none
}
func channel(for _: Martin.Context, with _: Martin.BareJID) -> (any Martin.ChannelProtocol)? {
nil
}
func close(channel _: any Martin.ChannelProtocol) -> Bool {
false
}
}

View file

@ -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
}
}

View file

@ -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
}
}
}

View file

@ -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<any Martin.ChannelProtocol> {
.none
}
func channel(for _: Martin.Context, with _: Martin.BareJID) -> (any Martin.ChannelProtocol)? {
nil
}
func close(channel _: any Martin.ChannelProtocol) -> Bool {
false
}
}

View file

@ -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

View file

@ -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 {