Validate messages after receiving
This commit is contained in:
parent
001b85353e
commit
52ed49d61e
|
@ -20,7 +20,7 @@ public final class ClosedGroupUpdate : ControlMessage {
|
|||
}
|
||||
|
||||
// MARK: Validation
|
||||
public override var isValidForSending: Bool { kind != nil }
|
||||
public override var isValid: Bool { kind != nil }
|
||||
|
||||
// MARK: Coding
|
||||
public required init?(coder: NSCoder) {
|
||||
|
|
|
@ -11,7 +11,7 @@ public final class ExpirationTimerUpdate : ControlMessage {
|
|||
}
|
||||
|
||||
// MARK: Validation
|
||||
public override var isValidForSending: Bool { duration != nil }
|
||||
public override var isValid: Bool { duration != nil }
|
||||
|
||||
// MARK: Coding
|
||||
public required init?(coder: NSCoder) {
|
||||
|
|
|
@ -11,7 +11,7 @@ public final class ReadReceipt : ControlMessage {
|
|||
}
|
||||
|
||||
// MARK: Validation
|
||||
public override var isValidForSending: Bool {
|
||||
public override var isValid: Bool {
|
||||
if let timestamps = timestamps, !timestamps.isEmpty { return true }
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ public final class SessionRequest : ControlMessage {
|
|||
}
|
||||
|
||||
// MARK: Validation
|
||||
public override var isValidForSending: Bool { preKeyBundle != nil }
|
||||
public override var isValid: Bool { preKeyBundle != nil }
|
||||
|
||||
// MARK: Coding
|
||||
public required init?(coder: NSCoder) {
|
||||
|
|
|
@ -24,7 +24,7 @@ public final class TypingIndicator : ControlMessage {
|
|||
}
|
||||
|
||||
// MARK: Validation
|
||||
public override var isValidForSending: Bool { kind != nil }
|
||||
public override var isValid: Bool { kind != nil }
|
||||
|
||||
// MARK: Initialization
|
||||
internal init(kind: Kind) {
|
||||
|
|
|
@ -11,7 +11,7 @@ public class Message : NSObject, NSCoding { // Not a protocol for YapDatabase co
|
|||
public override init() { }
|
||||
|
||||
// MARK: Validation
|
||||
public var isValidForSending: Bool { true }
|
||||
public var isValid: Bool { true }
|
||||
|
||||
// MARK: Coding
|
||||
public required init?(coder: NSCoder) {
|
||||
|
|
|
@ -13,7 +13,7 @@ public final class VisibleMessage : Message {
|
|||
public override init() { super.init() }
|
||||
|
||||
// MARK: Validation
|
||||
public override var isValidForSending: Bool {
|
||||
public override var isValid: Bool {
|
||||
if !attachmentIDs.isEmpty { return true }
|
||||
if let text = text?.trimmingCharacters(in: .whitespacesAndNewlines), !text.isEmpty { return true }
|
||||
return false
|
||||
|
|
|
@ -2,22 +2,39 @@ import SessionUtilities
|
|||
|
||||
public enum ReceivingPipeline {
|
||||
|
||||
public static func parse(_ data: Data) -> Message? {
|
||||
// TODO: Decryption
|
||||
// TODO: Validation
|
||||
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
|
||||
let proto: SNProtoContent
|
||||
do {
|
||||
proto = try SNProtoContent.parseData(data)
|
||||
proto = try SNProtoContent.parseData(plaintext)
|
||||
} catch {
|
||||
SNLog("Couldn't parse proto due to error: \(error).")
|
||||
return nil
|
||||
}
|
||||
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
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ public enum SendingPipeline {
|
|||
}
|
||||
|
||||
public static func send(_ message: Message, to destination: Destination) -> Promise<Void> {
|
||||
guard message.isValidForSending else { return Promise(error: Error.invalidMessage) }
|
||||
guard message.isValid else { return Promise(error: Error.invalidMessage) }
|
||||
guard let proto = message.toProto() else { return Promise(error: Error.protoConversionFailed) }
|
||||
let plaintext: Data
|
||||
do {
|
||||
|
|
Loading…
Reference in New Issue