session-ios/SessionMessagingKit/Messages/Control Messages/ReadReceipt.swift
Morgan Pretty cf66edb723 Further work on SessionMessagingKit migrations
Added migrations for contacts and started working through thread migration (have contact and closed group threads migrating)
Deprecated usage of ECKeyPair in the migrations (want to be able to remove Curve25519Kit in the future)
2022-04-06 15:43:26 +10:00

67 lines
2.2 KiB
Swift

import SessionUtilitiesKit
@objc(SNReadReceipt)
public final class ReadReceipt : ControlMessage {
@objc public var timestamps: [UInt64]?
// MARK: Initialization
public override init() { super.init() }
internal init(timestamps: [UInt64]) {
super.init()
self.timestamps = timestamps
}
// MARK: Validation
public override var isValid: Bool {
guard super.isValid else { return false }
if let timestamps = timestamps, !timestamps.isEmpty { return true }
return false
}
// MARK: Coding
public required init?(coder: NSCoder) {
super.init(coder: coder)
if let timestamps = coder.decodeObject(forKey: "messageTimestamps") as! [UInt64]? { self.timestamps = timestamps }
}
public override func encode(with coder: NSCoder) {
super.encode(with: coder)
coder.encode(timestamps, forKey: "messageTimestamps")
}
// MARK: Proto Conversion
public override class func fromProto(_ proto: SNProtoContent, sender: String) -> ReadReceipt? {
guard let receiptProto = proto.receiptMessage, receiptProto.type == .read else { return nil }
let timestamps = receiptProto.timestamp
guard !timestamps.isEmpty else { return nil }
return ReadReceipt(timestamps: timestamps)
}
public override func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? {
guard let timestamps = timestamps else {
SNLog("Couldn't construct read receipt proto from: \(self).")
return nil
}
let receiptProto = SNProtoReceiptMessage.builder(type: .read)
receiptProto.setTimestamp(timestamps)
let contentProto = SNProtoContent.builder()
do {
contentProto.setReceiptMessage(try receiptProto.build())
return try contentProto.build()
} catch {
SNLog("Couldn't construct read receipt proto from: \(self).")
return nil
}
}
// MARK: Description
public override var description: String {
"""
ReadReceipt(
timestamps: \(timestamps?.description ?? "null")
)
"""
}
}