From d735568e9441acfdb060b35b3735224410cbbb6e Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 6 Nov 2020 13:46:06 +1100 Subject: [PATCH] Partially implement sending pipeline --- .../Control Message/ClosedGroupUpdate.swift | 6 +-- .../Control Message/ReadReceipt.swift | 2 + .../Pipelines/SendingPipeline.swift | 44 +++++++++++++++++-- SessionSnodeKit/Message.swift | 29 +++++++----- SessionSnodeKit/SnodeAPI.swift | 4 +- SessionUtilities/HTTP.swift | 1 - 6 files changed, 66 insertions(+), 20 deletions(-) diff --git a/SessionMessagingKit/Messages/Control Message/ClosedGroupUpdate.swift b/SessionMessagingKit/Messages/Control Message/ClosedGroupUpdate.swift index 0cf32e6c8..9f83cede2 100644 --- a/SessionMessagingKit/Messages/Control Message/ClosedGroupUpdate.swift +++ b/SessionMessagingKit/Messages/Control Message/ClosedGroupUpdate.swift @@ -109,9 +109,9 @@ public final class ClosedGroupUpdate : ControlMessage { closedGroupUpdate.setSenderKeys([ try senderKey.toProto() ]) } let contentProto = SNProtoContent.builder() - let dataMessage = SNProtoDataMessage.builder() - dataMessage.setClosedGroupUpdate(try closedGroupUpdate.build()) - contentProto.setDataMessage(try dataMessage.build()) + let dataMessageProto = SNProtoDataMessage.builder() + dataMessageProto.setClosedGroupUpdate(try closedGroupUpdate.build()) + contentProto.setDataMessage(try dataMessageProto.build()) return try contentProto.build() } catch { SNLog("Couldn't construct closed group update proto from: \(self).") diff --git a/SessionMessagingKit/Messages/Control Message/ReadReceipt.swift b/SessionMessagingKit/Messages/Control Message/ReadReceipt.swift index c5d4f23d1..a46bfd313 100644 --- a/SessionMessagingKit/Messages/Control Message/ReadReceipt.swift +++ b/SessionMessagingKit/Messages/Control Message/ReadReceipt.swift @@ -13,10 +13,12 @@ public final class ReadReceipt : ControlMessage { // MARK: Coding public required init?(coder: NSCoder) { super.init(coder: coder) + if let timestamps = coder.decodeObject(forKey: "timestamps") as! [UInt64]? { self.timestamps = timestamps } } public override func encode(with coder: NSCoder) { super.encode(with: coder) + coder.encode(timestamps, forKey: "timestamps") } // MARK: Proto Conversion diff --git a/SessionMessagingKit/Pipelines/SendingPipeline.swift b/SessionMessagingKit/Pipelines/SendingPipeline.swift index c3f3961e9..6827f3671 100644 --- a/SessionMessagingKit/Pipelines/SendingPipeline.swift +++ b/SessionMessagingKit/Pipelines/SendingPipeline.swift @@ -1,8 +1,44 @@ +import PromiseKit +import SessionSnodeKit +import SessionUtilities public enum SendingPipeline { - // 1. Convert message to proto - // 2. Convert proto to binary - // 3. Encrypt - // 4. Send to snode + public enum Destination { + case contact(publicKey: String) + case closedGroup(publicKey: String) + case openGroup(channel: UInt64, server: String) + } + + public enum Error : LocalizedError { + case protoConversionFailed + case protoSerializationFailed + + public var errorDescription: String? { + switch self { + case .protoConversionFailed: return "Couldn't convert message to proto." + case .protoSerializationFailed: return "Couldn't serialize proto." + } + } + } + + public static func send(_ message: Message, to destination: Destination) -> Promise { + guard let proto = message.toProto() else { return Promise(error: Error.protoConversionFailed) } + let data: Data + do { + data = try proto.serializedData() + } catch { + SNLog("Couldn't serialize proto due to error: \(error).") + return Promise(error: Error.protoSerializationFailed) + } + // TODO: Encryption + let recipient = "" + let base64EncodedData = data.base64EncodedString() + let ttl: UInt64 = 2 * 24 * 60 * 60 * 1000 + let timestamp: UInt64 = 0 + let nonce = "" + let snodeMessage = SnodeMessage(recipient: recipient, data: base64EncodedData, ttl: ttl, timestamp: timestamp, nonce: nonce) + let _ = SnodeAPI.sendMessage(snodeMessage) + return Promise.value(()) + } } diff --git a/SessionSnodeKit/Message.swift b/SessionSnodeKit/Message.swift index d7a3099ba..01ed7ab37 100644 --- a/SessionSnodeKit/Message.swift +++ b/SessionSnodeKit/Message.swift @@ -1,9 +1,9 @@ import PromiseKit import SessionUtilities -public struct Message { +public struct SnodeMessage { /// The hex encoded public key of the recipient. - let recipientPublicKey: String + let recipient: String /// The content of the message. let data: LosslessStringConvertible /// The time to live for the message in milliseconds. @@ -11,16 +11,25 @@ public struct Message { /// When the proof of work was calculated. /// /// - Note: Expressed as milliseconds since 00:00:00 UTC on 1 January 1970. - let timestamp: UInt64? = nil + let timestamp: UInt64 /// The base 64 encoded proof of work. - let nonce: String? = nil + let nonce: String + + public init(recipient: String, data: LosslessStringConvertible, ttl: UInt64, timestamp: UInt64, nonce: String) { + self.recipient = recipient + self.data = data + self.ttl = ttl + self.timestamp = timestamp + self.nonce = nonce + } public func toJSON() -> JSON { - var result = [ "pubKey" : recipientPublicKey, "data" : data.description, "ttl" : String(ttl) ] - if let timestamp = timestamp, let nonce = nonce { - result["timestamp"] = String(timestamp) - result["nonce"] = nonce - } - return result + return [ + "pubKey" : recipient, + "data" : data.description, + "ttl" : String(ttl), + "timestamp" : String(timestamp), + "nonce" : nonce + ] } } diff --git a/SessionSnodeKit/SnodeAPI.swift b/SessionSnodeKit/SnodeAPI.swift index edae9f476..46b4a9511 100644 --- a/SessionSnodeKit/SnodeAPI.swift +++ b/SessionSnodeKit/SnodeAPI.swift @@ -185,9 +185,9 @@ public enum SnodeAPI { } // MARK: Sending - public static func sendMessage(_ message: Message) -> Promise> { + public static func sendMessage(_ message: SnodeMessage) -> Promise> { let (promise, seal) = Promise>.pending() - let publicKey = message.recipientPublicKey + let publicKey = message.recipient Threading.workQueue.async { getTargetSnodes(for: publicKey).map2 { targetSnodes in let parameters = message.toJSON() diff --git a/SessionUtilities/HTTP.swift b/SessionUtilities/HTTP.swift index 0f81181fa..cf4ab2d94 100644 --- a/SessionUtilities/HTTP.swift +++ b/SessionUtilities/HTTP.swift @@ -1,6 +1,5 @@ import Foundation import PromiseKit -import SessionUtilities public enum HTTP { private static let sslURLSession = URLSession(configuration: .ephemeral)