Partially implement sending pipeline
This commit is contained in:
parent
fac757e4fc
commit
d735568e94
|
@ -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).")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<Void> {
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -185,9 +185,9 @@ public enum SnodeAPI {
|
|||
}
|
||||
|
||||
// MARK: Sending
|
||||
public static func sendMessage(_ message: Message) -> Promise<Set<RawResponsePromise>> {
|
||||
public static func sendMessage(_ message: SnodeMessage) -> Promise<Set<RawResponsePromise>> {
|
||||
let (promise, seal) = Promise<Set<RawResponsePromise>>.pending()
|
||||
let publicKey = message.recipientPublicKey
|
||||
let publicKey = message.recipient
|
||||
Threading.workQueue.async {
|
||||
getTargetSnodes(for: publicKey).map2 { targetSnodes in
|
||||
let parameters = message.toJSON()
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import Foundation
|
||||
import PromiseKit
|
||||
import SessionUtilities
|
||||
|
||||
public enum HTTP {
|
||||
private static let sslURLSession = URLSession(configuration: .ephemeral)
|
||||
|
|
Loading…
Reference in New Issue