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

108 lines
3.6 KiB
Swift
Raw Normal View History

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import GRDB
2020-11-09 00:58:47 +01:00
import SessionUtilitiesKit
2020-11-05 23:17:05 +01:00
public final class ExpirationTimerUpdate: ControlMessage {
private enum CodingKeys: String, CodingKey {
case syncTarget
case duration
}
2021-02-24 05:19:50 +01:00
/// In the case of a sync message, the public key of the person the message was targeted at.
///
/// - Note: `nil` if this isn't a sync message.
public var syncTarget: String?
2020-11-06 00:59:04 +01:00
public var duration: UInt32?
2020-11-05 23:17:05 +01:00
2021-02-24 05:19:50 +01:00
public override var isSelfSendValid: Bool { true }
// MARK: - Initialization
2020-11-26 03:30:30 +01:00
public init(syncTarget: String?, duration: UInt32) {
2020-11-06 00:59:04 +01:00
super.init()
2021-02-24 05:19:50 +01:00
self.syncTarget = syncTarget
2020-11-06 00:59:04 +01:00
self.duration = duration
}
// MARK: - Validation
2020-11-17 06:23:13 +01:00
public override var isValid: Bool {
guard super.isValid else { return false }
return duration != nil
}
// MARK: - Codable
required init(from decoder: Decoder) throws {
try super.init(from: decoder)
let container: KeyedDecodingContainer<CodingKeys> = try decoder.container(keyedBy: CodingKeys.self)
syncTarget = try? container.decode(String.self, forKey: .syncTarget)
duration = try? container.decode(UInt32.self, forKey: .duration)
}
public override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container: KeyedEncodingContainer<CodingKeys> = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(syncTarget, forKey: .syncTarget)
try container.encodeIfPresent(duration, forKey: .duration)
}
2020-11-06 00:59:04 +01:00
// MARK: - Proto Conversion
public override class func fromProto(_ proto: SNProtoContent, sender: String) -> ExpirationTimerUpdate? {
2020-11-06 01:41:01 +01:00
guard let dataMessageProto = proto.dataMessage else { return nil }
2020-11-06 01:41:01 +01:00
let isExpirationTimerUpdate = (dataMessageProto.flags & UInt32(SNProtoDataMessage.SNProtoDataMessageFlags.expirationTimerUpdate.rawValue)) != 0
2020-11-06 00:59:04 +01:00
guard isExpirationTimerUpdate else { return nil }
return ExpirationTimerUpdate(
syncTarget: dataMessageProto.syncTarget,
duration: dataMessageProto.expireTimer
)
2020-11-06 00:59:04 +01:00
}
public override func toProto(_ db: Database) -> 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)
2021-02-24 05:19:50 +01:00
if let syncTarget = syncTarget { dataMessageProto.setSyncTarget(syncTarget) }
2020-12-08 03:17:02 +01:00
// Group context
do {
try setGroupContextIfNeeded(db, on: dataMessageProto)
2020-12-08 03:17:02 +01:00
} catch {
SNLog("Couldn't construct expiration timer update proto from: \(self).")
return nil
}
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-26 03:30:30 +01:00
// MARK: - Description
public var description: String {
2020-12-07 05:11:49 +01:00
"""
ExpirationTimerUpdate(
2021-05-03 07:13:18 +02:00
syncTarget: \(syncTarget ?? "null"),
2020-12-07 05:11:49 +01:00
duration: \(duration?.description ?? "null")
)
"""
}
2020-11-05 23:17:05 +01:00
}