mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			58 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Swift
		
	
			
		
		
	
	
			58 lines
		
	
	
		
			1.9 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) -> 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() -> 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
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |