This commit is contained in:
fmodf 2024-12-24 13:24:05 +01:00
parent e3ee92d87d
commit b8a86a17bf

View file

@ -45,7 +45,7 @@ final class RosterModule: XmppModule {
case .stanzaInbound(let stanza):
if let query = stanza.wrapped.nodes.first(where: { $0.name == "query" }), query.xmlns == "jabber:iq:roster" {
if stanza.type == .iq(.set) {
return processSet(state: state, stanza: stanza)
return await processSet(state: state, stanza: stanza)
} else if stanza.type == .iq(.result) {
return processResult(state: state, stanza: stanza)
} else {
@ -61,6 +61,13 @@ final class RosterModule: XmppModule {
}
}
// NSString* const kSubBoth = @"both";
// NSString* const kSubNone = @"none";
// NSString* const kSubTo = @"to";
// NSString* const kSubFrom = @"from";
// NSString* const kSubRemove = @"remove";
// NSString* const kAskSubscribe = @"subscribe";
private extension RosterModule {
private func update(jidStr: String, args: [String: String]) -> Event? {
print(jidStr, args)
@ -72,13 +79,35 @@ private extension RosterModule {
return nil
}
private func processSet(state: ClientState, stanza: Stanza) -> Event? {
private func processSet(state: ClientState, stanza: Stanza) async -> Event? {
// sanity check
if stanza.wrapped.attributes["from"] != state.jid.bare {
return nil
}
// get exists roster items
var existItems: [RosterItem] = []
if let data = await storage?.getRoster(jid: state.jid), let decoded = try? JSONDecoder().decode([XMLElement].self, from: data) {
existItems = decoded.compactMap { RosterItem(wrap: $0, owner: state.jid) }
}
// process
let items = stanza.wrapped
.nodes
.first(where: { $0.name == "query" })?
.nodes
.filter { $0.name == "item" } ?? []
for item in items {
guard let itemJidStr = item.attributes["jid"], let itemJid = try? JID(itemJidStr) else { continue }
let subscription = item.attributes["subscription"]
switch subscription {
case "remove":
existItems = existItems.filter { $0.jid == itemJid }
default:
continue
}
}
// according to RFC-6121 a set from server (push)
// shouyld be answered with result