From b9bf1ebfa5fb355610554148597c87abe52bf8f3 Mon Sep 17 00:00:00 2001 From: fmodf Date: Mon, 16 Dec 2024 14:11:46 +0100 Subject: [PATCH] wip --- AnotherIM/xmpp/models/Roster.swift | 29 ++++++++++++ AnotherIM/xmpp/models/Stanza.swift | 45 ++++++++++--------- .../xmpp/modules/roster/RosterModule.swift | 2 +- 3 files changed, 55 insertions(+), 21 deletions(-) create mode 100644 AnotherIM/xmpp/models/Roster.swift diff --git a/AnotherIM/xmpp/models/Roster.swift b/AnotherIM/xmpp/models/Roster.swift new file mode 100644 index 0000000..5997d1a --- /dev/null +++ b/AnotherIM/xmpp/models/Roster.swift @@ -0,0 +1,29 @@ +import Foundation + +enum RosterSubsriptionType: String { + case both + case from + case none + case remove + case to + + var isFrom: Bool { + switch self { + case .from, .both: + return true + + case .none, .to, .remove: + return false + } + } + + var isTo: Bool { + switch self { + case .to, .both: + return true + + case .none, .from, .remove: + return false + } + } +} diff --git a/AnotherIM/xmpp/models/Stanza.swift b/AnotherIM/xmpp/models/Stanza.swift index fd67ae7..d2cbf3c 100644 --- a/AnotherIM/xmpp/models/Stanza.swift +++ b/AnotherIM/xmpp/models/Stanza.swift @@ -73,12 +73,32 @@ struct Stanza { } } -// MARK: Init +// MARK: Init IQ Stanza extension Stanza { - static func iqGet(jid: String? = nil, payload: XMLElement) -> Stanza? { - var attributes = ["id": XMLElement.randomId, "type": "get"] - if let jid { - attributes["from"] = jid + static func iqGet(payload: XMLElement) -> Stanza? { + buildIq(direction: "get", from: nil, to: nil, payload: payload) + } + + static func iqGet(from: String, payload: XMLElement) -> Stanza? { + buildIq(direction: "get", from: from, to: nil, payload: payload) + } + + static func iqSet(payload: XMLElement) -> Stanza? { + buildIq(direction: "set", from: nil, to: nil, payload: payload) + } + + static func iqSet(from: String, payload: XMLElement) -> Stanza? { + buildIq(direction: "set", from: from, to: nil, payload: payload) + } + + // build iq stanza + private static func buildIq(direction: String, from: String?, to: String?, payload: XMLElement) -> Stanza? { + var attributes = ["id": XMLElement.randomId, "type": direction] + if let from { + attributes["from"] = from + } + if let to { + attributes["to"] = to } let req = XMLElement( name: "iq", @@ -89,21 +109,6 @@ extension Stanza { ) return Stanza(wrap: req) } - - static func iqSet(jid: String? = nil, payload: XMLElement) -> Stanza? { - var attributes = ["id": XMLElement.randomId, "type": "get"] - if let jid { - attributes["from"] = jid - } - let req = XMLElement( - name: "iq", - xmlns: nil, - attributes: ["type": "set", "id": XMLElement.randomId], - content: nil, - nodes: [payload] - ) - return Stanza(wrap: req) - } } private extension Stanza { diff --git a/AnotherIM/xmpp/modules/roster/RosterModule.swift b/AnotherIM/xmpp/modules/roster/RosterModule.swift index 4b16ea6..6df5a43 100644 --- a/AnotherIM/xmpp/modules/roster/RosterModule.swift +++ b/AnotherIM/xmpp/modules/roster/RosterModule.swift @@ -14,7 +14,7 @@ final class RosterModule: XmppModule { case .requestRoster: // TODO: check version! - let req = Stanza.iqGet(jid: state.jid.full, payload: XMLElement(name: "query", xmlns: "jabber:iq:roster", attributes: [:], content: nil, nodes: [])) + let req = Stanza.iqGet(from: state.jid.full, payload: XMLElement(name: "query", xmlns: "jabber:iq:roster", attributes: [:], content: nil, nodes: [])) if let req { return .stanzaOutbound(req) } else {