another.im-ios/AnotherIM/xmpp/modules/stanza/StanzaModule.swift

50 lines
1.3 KiB
Swift
Raw Normal View History

2024-06-19 15:06:39 +00:00
import Foundation
final class StanzaModule: XmppModule {
let id = "Stanza module"
2024-12-17 08:34:44 +00:00
private weak var storage: XMPPStorage?
2024-06-19 15:06:39 +00:00
2024-12-17 08:34:44 +00:00
init(_ storage: any XMPPStorage) {
2024-06-19 15:06:39 +00:00
self.storage = storage
}
func reduce(oldState: ClientState, with _: Event) -> ClientState {
oldState
}
func process(state: ClientState, with event: Event) async -> Event? {
// try to send/receive stanzas only on ready, secured, and authorized state
guard
state.isSocketSecured &&
state.authorizationStep == .authorized
else { return nil }
// receive/send stanzas from/to xml
switch event {
case .stanzaOutbound(let stanza):
await saveStanza(state, stanza, inbound: false)
return .xmlOutbound(stanza.wrapped)
case .xmlInbound(let xml):
if let stanza = Stanza(wrap: xml) {
await saveStanza(state, stanza, inbound: true)
return .stanzaInbound(stanza)
} else {
return nil
}
default:
return nil
}
}
}
// MARK: Save history
private extension StanzaModule {
func saveStanza(_ state: ClientState, _ stanza: Stanza, inbound: Bool) async {
2024-12-17 08:34:44 +00:00
guard let storage else { return }
2024-06-19 15:06:39 +00:00
print(state, stanza, inbound)
}
}