This commit is contained in:
fmodf 2025-01-13 04:11:24 +01:00
parent 3b92aa7606
commit 8b6273b573
2 changed files with 65 additions and 24 deletions

View file

@ -116,3 +116,16 @@ private extension Stanza {
print("Something went wrong! with \(wrapped.stringRepresentation.prettyStr)") print("Something went wrong! with \(wrapped.stringRepresentation.prettyStr)")
} }
} }
final actor StanzaRegistry {
var stanzas: [Stanza] = []
func enqueue(_ stanza: Stanza) {
stanzas.append(stanza)
}
func deuque(for stanzaId: String) -> Stanza? {
guard let index = stanzas.firstIndex(where: { $0.id == stanzaId }) else { return nil }
return stanzas.remove(at: index)
}
}

View file

@ -2,11 +2,13 @@
// XEP-0237 // XEP-0237
import Foundation import Foundation
// TODO: implement error catching
final class RosterModule: XmppModule { final class RosterModule: XmppModule {
let id = "Roseter module" let id = "Roseter module"
private weak var storage: (any XMPPStorage)? private weak var storage: (any XMPPStorage)?
private var isVerSupported = false private var isVerSupported = false
private let registry = StanzaRegistry()
init(_ storage: any XMPPStorage) { init(_ storage: any XMPPStorage) {
self.storage = storage self.storage = storage
@ -46,6 +48,7 @@ final class RosterModule: XmppModule {
) )
) )
if let req { if let req {
await registry.enqueue(req)
return .stanzaOutbound(req) return .stanzaOutbound(req)
} else { } else {
return nil return nil
@ -75,6 +78,7 @@ final class RosterModule: XmppModule {
) )
) )
if let req { if let req {
await registry.enqueue(req)
return .stanzaOutbound(req) return .stanzaOutbound(req)
} else { } else {
return nil return nil
@ -100,6 +104,7 @@ final class RosterModule: XmppModule {
) )
) )
if let req { if let req {
await registry.enqueue(req)
return .stanzaOutbound(req) return .stanzaOutbound(req)
} else { } else {
return nil return nil
@ -112,30 +117,8 @@ final class RosterModule: XmppModule {
await storage?.setRosterVer(jid: state.jid, version: ver) await storage?.setRosterVer(jid: state.jid, version: ver)
} }
// get items from stanza // process stanza
var items: [XMLElement] = [] return await processInbound(stanza: stanza)
switch stanza.type {
case .iq(.set):
break
// return await processSet(state: state, stanza: stanza)
case .iq(.result):
break
// return await processResult(state: state, stanza: stanza)
case .iq(.error):
// handle errors here
// TODO: implement error catching
break
default:
break
}
// process items
// result
return nil
} else { } else {
return nil return nil
} }
@ -146,6 +129,51 @@ final class RosterModule: XmppModule {
} }
} }
private extension RosterModule {
func processInbound(stanza: Stanza) async -> Event? {
switch stanza.type {
case .iq(.set):
return nil
case .iq(.error):
return nil
case .iq(.result):
return nil
default:
return nil
}
// get items from stanza
// var items: [XMLElement] = []
// switch stanza.type {
// case .iq(.set):
// break
// // return await processSet(state: state, stanza: stanza)
//
// case .iq(.result):
// let stanzaItems = stanza.wrapped
// .nodes
// .first(where: { $0.name == "query" })?
// .nodes
// .filter { $0.name == "item" } ?? []
// items.append(contentsOf: stanzaItems)
//
// case .iq(.error):
// // handle errors here
// break
//
// default:
// break
// }
// process items
// result
//
}
}
// private extension RosterModule { // private extension RosterModule {
// private func update(state: ClientState, jidStr: String, args: [String: String]) async -> Event? { // private func update(state: ClientState, jidStr: String, args: [String: String]) async -> Event? {
// print(state, jidStr, args) // print(state, jidStr, args)