From b8a86a17bfea53b1fad041eaab20c8f57a1bf4b8 Mon Sep 17 00:00:00 2001 From: fmodf Date: Tue, 24 Dec 2024 13:24:05 +0100 Subject: [PATCH] wip --- AnotherXMPP/modules/roster/RosterModule.swift | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/AnotherXMPP/modules/roster/RosterModule.swift b/AnotherXMPP/modules/roster/RosterModule.swift index a7cfa47..9c23ff3 100644 --- a/AnotherXMPP/modules/roster/RosterModule.swift +++ b/AnotherXMPP/modules/roster/RosterModule.swift @@ -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