session-ios/SessionMessagingKit/Pipelines/ReceivingPipeline.swift

41 lines
1.4 KiB
Swift
Raw Normal View History

2020-11-06 04:05:45 +01:00
import SessionUtilities
2020-11-05 23:17:05 +01:00
public enum ReceivingPipeline {
2020-11-06 06:31:56 +01:00
public enum Error : LocalizedError {
case invalidMessage
public var errorDescription: String? {
switch self {
case .invalidMessage: return "Invalid message."
}
}
}
public static func parse(_ ciphertext: Data) -> Message? {
let plaintext = ciphertext // TODO: Decryption
2020-11-06 04:05:45 +01:00
let proto: SNProtoContent
do {
2020-11-06 06:31:56 +01:00
proto = try SNProtoContent.parseData(plaintext)
2020-11-06 04:05:45 +01:00
} catch {
SNLog("Couldn't parse proto due to error: \(error).")
return nil
}
2020-11-06 06:31:56 +01:00
let message: Message? = {
if let readReceipt = ReadReceipt.fromProto(proto) { return readReceipt }
if let sessionRequest = SessionRequest.fromProto(proto) { return sessionRequest }
if let typingIndicator = TypingIndicator.fromProto(proto) { return typingIndicator }
if let closedGroupUpdate = ClosedGroupUpdate.fromProto(proto) { return closedGroupUpdate }
if let expirationTimerUpdate = ExpirationTimerUpdate.fromProto(proto) { return expirationTimerUpdate }
if let visibleMessage = VisibleMessage.fromProto(proto) { return visibleMessage }
return nil
}()
if let message = message {
guard message.isValid else { return nil }
return message
} else {
return nil
}
2020-11-06 04:05:45 +01:00
}
2020-11-05 23:17:05 +01:00
}