From d5e56d9c9f8be0e489b95029149d19115f7927c3 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Tue, 27 Aug 2019 15:25:37 +1000 Subject: [PATCH] Clean --- Signal/src/Loki/LokiGroupChatPoller.swift | 56 ++++++++----------- .../src/Loki/API/LokiRSSFeedParser.swift | 54 ------------------ 2 files changed, 22 insertions(+), 88 deletions(-) delete mode 100644 SignalServiceKit/src/Loki/API/LokiRSSFeedParser.swift diff --git a/Signal/src/Loki/LokiGroupChatPoller.swift b/Signal/src/Loki/LokiGroupChatPoller.swift index bfad24987..7e840770e 100644 --- a/Signal/src/Loki/LokiGroupChatPoller.swift +++ b/Signal/src/Loki/LokiGroupChatPoller.swift @@ -45,30 +45,33 @@ public final class LokiGroupChatPoller : NSObject { private func pollForNewMessages() { let group = self.group + func parseGroupMessage(body: String, timestamp: UInt64, senderDisplayName: String) { + let id = group.id.data(using: String.Encoding.utf8)! + let x1 = SSKProtoGroupContext.builder(id: id, type: .deliver) + x1.setName(group.displayName) + let x2 = SSKProtoDataMessage.builder() + x2.setTimestamp(timestamp) + x2.setGroup(try! x1.build()) + x2.setBody(body) + let x3 = SSKProtoContent.builder() + x3.setDataMessage(try! x2.build()) + let x4 = SSKProtoEnvelope.builder(type: .ciphertext, timestamp: timestamp) + x4.setSource(senderDisplayName) + x4.setSourceDevice(OWSDevicePrimaryDeviceId) + x4.setContent(try! x3.build().serializedData()) + OWSPrimaryStorage.shared().dbReadWriteConnection.readWrite { transaction in + SSKEnvironment.shared.messageManager.throws_processEnvelope(try! x4.build(), plaintextData: try! x3.build().serializedData(), wasReceivedByUD: false, transaction: transaction) + } + } switch group.kind { case .publicChat(let id): let _ = LokiGroupChatAPI.getMessages(for: id, on: group.server).done { messages in messages.reversed().forEach { message in - let id = group.id.data(using: String.Encoding.utf8)! - let x1 = SSKProtoGroupContext.builder(id: id, type: .deliver) - x1.setName(group.displayName) - let x2 = SSKProtoDataMessage.builder() - x2.setTimestamp(message.timestamp) - x2.setGroup(try! x1.build()) - x2.setBody(message.body) - let x3 = SSKProtoContent.builder() - x3.setDataMessage(try! x2.build()) - let x4 = SSKProtoEnvelope.builder(type: .ciphertext, timestamp: message.timestamp) let senderHexEncodedPublicKey = message.hexEncodedPublicKey let endIndex = senderHexEncodedPublicKey.endIndex let cutoffIndex = senderHexEncodedPublicKey.index(endIndex, offsetBy: -8) let senderDisplayName = "\(message.displayName) (...\(senderHexEncodedPublicKey[cutoffIndex.. Void)? - private var tag: Tag? - private var currentItem: Item? - private var items: [Item] = [] - - private enum Tag : String { - case item, title, description, date = "pubDate" - } - - public struct Item { - public var title: String? = nil - public var description: String? = nil - public var dateAsString: String? = nil - } - - public init(url: URL) { - self.url = url - super.init() - } - - public func parse(completion: @escaping (([Item]) -> Void)) { - guard let parser = XMLParser(contentsOf: url) else { return } - self.completion = completion - parser.delegate = self - parser.parse() - } - - public func parser(_ parser: XMLParser, didStartElement elementAsString: String, namespaceURI: String?, qualifiedName: String?, attributes: [String:String] = [:]) { - if let element = Tag(rawValue: elementAsString) { self.tag = element } - if tag == .item { currentItem = Item() } - } - - public func parser(_ parser: XMLParser, foundCharacters string: String) { - guard let element = tag else { return } - switch element { - case .title: currentItem?.title = string - case .description: currentItem?.description = string - case .date: currentItem?.dateAsString = string - default: break - } - } - - public func parser(_ parser: XMLParser, didEndElement elementAsString: String, namespaceURI: String?, qualifiedName: String?) { - guard let element = Tag(rawValue: elementAsString) else { return } - if element == .item, let currentItem = self.currentItem { items.append(currentItem) } - } - - public func parserDidEndDocument(_ parser: XMLParser) { - completion?(items) - } -}