session-android/libsession/src/main/java/org/session/libsession/messaging/messages/control/ExpirationTimerUpdate.kt

78 lines
2.9 KiB
Kotlin
Raw Normal View History

package org.session.libsession.messaging.messages.control
2021-04-26 03:14:45 +02:00
import org.session.libsession.messaging.MessagingModuleConfiguration
import org.session.libsession.messaging.messages.visible.VisibleMessage
2021-05-18 01:12:33 +02:00
import org.session.libsignal.utilities.Log
2021-05-18 01:44:06 +02:00
import org.session.libsignal.protos.SignalServiceProtos
2020-11-25 02:06:41 +01:00
class ExpirationTimerUpdate() : ControlMessage() {
2021-05-12 06:01:57 +02: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.
*/
2021-03-02 02:24:09 +01:00
var syncTarget: String? = null
var duration: Int? = 0
override val isSelfSendValid: Boolean = true
2021-05-12 06:01:57 +02:00
override fun isValid(): Boolean {
if (!super.isValid()) return false
return duration != null
}
companion object {
const val TAG = "ExpirationTimerUpdate"
fun fromProto(proto: SignalServiceProtos.Content): ExpirationTimerUpdate? {
val dataMessageProto = if (proto.hasDataMessage()) proto.dataMessage else return null
2021-03-02 02:24:09 +01:00
val isExpirationTimerUpdate = dataMessageProto.flags.and(SignalServiceProtos.DataMessage.Flags.EXPIRATION_TIMER_UPDATE_VALUE) != 0
if (!isExpirationTimerUpdate) return null
val syncTarget = dataMessageProto.syncTarget
val duration = dataMessageProto.expireTimer
return ExpirationTimerUpdate(syncTarget, duration)
}
}
2021-06-29 05:26:33 +02:00
constructor(duration: Int) : this() {
2021-05-12 06:48:13 +02:00
this.syncTarget = null
this.duration = duration
}
internal constructor(syncTarget: String, duration: Int) : this() {
this.syncTarget = syncTarget
this.duration = duration
}
override fun toProto(): SignalServiceProtos.Content? {
val duration = duration
if (duration == null) {
Log.w(TAG, "Couldn't construct expiration timer update proto from: $this")
return null
}
val dataMessageProto = SignalServiceProtos.DataMessage.newBuilder()
dataMessageProto.flags = SignalServiceProtos.DataMessage.Flags.EXPIRATION_TIMER_UPDATE_VALUE
dataMessageProto.expireTimer = duration
// Sync target
if (syncTarget != null) {
dataMessageProto.syncTarget = syncTarget
}
// Group context
2021-04-26 03:14:45 +02:00
if (MessagingModuleConfiguration.shared.storage.isClosedGroup(recipient!!)) {
try {
setGroupContext(dataMessageProto)
} catch(e: Exception) {
Log.w(VisibleMessage.TAG, "Couldn't construct visible message proto from: $this")
return null
}
}
val contentProto = SignalServiceProtos.Content.newBuilder()
2022-11-22 02:08:58 +01:00
return try {
contentProto.dataMessage = dataMessageProto.build()
2022-11-22 02:08:58 +01:00
setExpirationConfigurationIfNeeded(contentProto)
contentProto.build()
} catch (e: Exception) {
Log.w(TAG, "Couldn't construct expiration timer update proto from: $this")
2022-11-22 02:08:58 +01:00
null
}
}
2020-11-25 02:06:41 +01:00
}