session-ios/SessionMessagingKit/Messages/Control Message/ExpirationTimerUpdate.swift

51 lines
1.9 KiB
Swift
Raw Normal View History

2020-11-06 00:59:04 +01:00
import SessionUtilities
2020-11-05 23:17:05 +01:00
@objc(SNExpirationTimerUpdate)
public final class ExpirationTimerUpdate : ControlMessage {
2020-11-06 00:59:04 +01:00
public var duration: UInt32?
2020-11-05 23:17:05 +01:00
2020-11-06 00:59:04 +01:00
// MARK: Initialization
2020-11-06 01:41:01 +01:00
init(duration: UInt32) {
2020-11-06 00:59:04 +01:00
super.init()
self.duration = duration
}
// MARK: Coding
public required init?(coder: NSCoder) {
super.init(coder: coder)
if let duration = coder.decodeObject(forKey: "duration") as! UInt32? { self.duration = duration }
}
public override func encode(with coder: NSCoder) {
super.encode(with: coder)
coder.encode(duration, forKey: "duration")
}
// MARK: Proto Conversion
public override class func fromProto(_ proto: SNProtoContent) -> ExpirationTimerUpdate? {
2020-11-06 01:41:01 +01:00
guard let dataMessageProto = proto.dataMessage else { return nil }
let isExpirationTimerUpdate = (dataMessageProto.flags & UInt32(SNProtoDataMessage.SNProtoDataMessageFlags.expirationTimerUpdate.rawValue)) != 0
2020-11-06 00:59:04 +01:00
guard isExpirationTimerUpdate else { return nil }
2020-11-06 01:41:01 +01:00
let duration = dataMessageProto.expireTimer
return ExpirationTimerUpdate(duration: duration)
2020-11-06 00:59:04 +01:00
}
public override func toProto() -> SNProtoContent? {
2020-11-06 01:41:01 +01:00
guard let duration = duration else {
SNLog("Couldn't construct expiration timer update proto from: \(self).")
return nil
}
let dataMessageProto = SNProtoDataMessage.builder()
dataMessageProto.setFlags(UInt32(SNProtoDataMessage.SNProtoDataMessageFlags.expirationTimerUpdate.rawValue))
dataMessageProto.setExpireTimer(duration)
2020-11-06 00:59:04 +01:00
let contentProto = SNProtoContent.builder()
do {
2020-11-06 01:41:01 +01:00
contentProto.setDataMessage(try dataMessageProto.build())
2020-11-06 00:59:04 +01:00
return try contentProto.build()
} catch {
SNLog("Couldn't construct expiration timer update proto from: \(self).")
return nil
}
}
2020-11-05 23:17:05 +01:00
}