// // RoomManager.swift // Chat (iOS) // // Created by Sergey Tarasov on 17.08.2022. // import Foundation import XMPPFramework import XMPPFrameworkSwift class RoomManager: NSObject { var room: XMPPRoom var storage: XMPPRoomMemoryStorage var mam: XMPPMessageArchiveManagement var messages: [XMPPMessage] = [] init(with jidString: String) { self.storage = XMPPRoomMemoryStorage() self.room = XMPPRoom(roomStorage: self.storage, jid: XMPPJID(string: jidString)!) self.mam = XMPPMessageArchiveManagement() super.init() self.room.activate(StreamManager.shared.stream) self.mam.activate(StreamManager.shared.stream) self.room.join(usingNickname: StreamManager.shared.stream.myJID?.user ?? "Test", history: nil) self.room.addDelegate(self, delegateQueue: DispatchQueue.main) self.mam.addDelegate(self, delegateQueue: DispatchQueue.main) } func fetchMessages() { self.messages.removeAll() let field = DDXMLElement(name: "field") field.addAttribute(withName: "var", stringValue: "with") field.addChild(DDXMLElement(name: "value", stringValue: self.room.roomJID.bare)) self.mam.retrieveMessageArchive(at: nil, withFields: [field], with: nil) } } extension RoomManager: XMPPRoomMemoryStorageDelegate { func xmppRoomMemoryStorage( _ sender: XMPPRoomMemoryStorage!, didReceiveMessage message: XMPPRoomMessageMemoryStorageObject!, fromOccupant occupant: XMPPRoomOccupantMemoryStorageObject!, at index: UInt, in allMessages: [Any]! ) { debugPrint(message.debugDescription) } } extension RoomManager: XMPPRoomDelegate { func xmppRoomDidJoin(_ sender: XMPPRoom) { } } extension RoomManager: XMPPMessageArchiveManagementDelegate { func xmppMessageArchiveManagement( _ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didReceiveMAMMessage message: XMPPMessage ) { if let result = message.children?.first, let forwarded = result.children?.first, let msg = forwarded.children?.first as? DDXMLElement { let xmppmsg = XMPPMessage(from: msg) if xmppmsg.isMessageWithBody { self.messages.append(xmppmsg) } } } func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFinishReceivingMessagesWith resultSet: XMPPResultSet) { } }