diff --git a/AnotherXMPP/models/Stanza.swift b/AnotherXMPP/models/Stanza.swift index d2cbf3c..c72e80f 100644 --- a/AnotherXMPP/models/Stanza.swift +++ b/AnotherXMPP/models/Stanza.swift @@ -116,3 +116,16 @@ private extension Stanza { 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) + } +} diff --git a/AnotherXMPP/modules/roster/RosterModule.swift b/AnotherXMPP/modules/roster/RosterModule.swift index ed1d661..70eaaea 100644 --- a/AnotherXMPP/modules/roster/RosterModule.swift +++ b/AnotherXMPP/modules/roster/RosterModule.swift @@ -2,11 +2,13 @@ // XEP-0237 import Foundation +// TODO: implement error catching final class RosterModule: XmppModule { let id = "Roseter module" private weak var storage: (any XMPPStorage)? private var isVerSupported = false + private let registry = StanzaRegistry() init(_ storage: any XMPPStorage) { self.storage = storage @@ -46,6 +48,7 @@ final class RosterModule: XmppModule { ) ) if let req { + await registry.enqueue(req) return .stanzaOutbound(req) } else { return nil @@ -75,6 +78,7 @@ final class RosterModule: XmppModule { ) ) if let req { + await registry.enqueue(req) return .stanzaOutbound(req) } else { return nil @@ -100,6 +104,7 @@ final class RosterModule: XmppModule { ) ) if let req { + await registry.enqueue(req) return .stanzaOutbound(req) } else { return nil @@ -112,30 +117,8 @@ final class RosterModule: XmppModule { await storage?.setRosterVer(jid: state.jid, version: ver) } - // get items from stanza - var items: [XMLElement] = [] - 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 + // process stanza + return await processInbound(stanza: stanza) } else { 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 func update(state: ClientState, jidStr: String, args: [String: String]) async -> Event? { // print(state, jidStr, args)