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 {