2020-11-27 05:56:16 +01:00
|
|
|
package org.session.libsession.messaging.messages.control
|
|
|
|
|
2023-11-28 03:17:27 +01:00
|
|
|
import network.loki.messenger.libsession_util.util.ExpiryMode
|
2021-04-26 03:14:45 +02:00
|
|
|
import org.session.libsession.messaging.MessagingModuleConfiguration
|
2022-12-08 03:38:53 +01:00
|
|
|
import org.session.libsession.messaging.messages.ExpirationConfiguration
|
2021-03-11 00:23:16 +01:00
|
|
|
import org.session.libsession.messaging.messages.visible.VisibleMessage
|
2021-05-18 01:44:06 +02:00
|
|
|
import org.session.libsignal.protos.SignalServiceProtos
|
2023-08-03 09:37:21 +02:00
|
|
|
import org.session.libsignal.utilities.Log
|
2020-11-25 02:06:41 +01:00
|
|
|
|
2023-11-10 03:22:35 +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.
|
|
|
|
*/
|
2023-11-28 03:17:27 +01:00
|
|
|
data class ExpirationTimerUpdate(var expiryMode: ExpiryMode, var syncTarget: String? = null) : ControlMessage() {
|
2020-11-27 06:41:21 +01:00
|
|
|
|
2021-03-31 08:14:51 +02:00
|
|
|
override val isSelfSendValid: Boolean = true
|
|
|
|
|
2020-11-27 06:41:21 +01:00
|
|
|
companion object {
|
|
|
|
const val TAG = "ExpirationTimerUpdate"
|
|
|
|
|
|
|
|
fun fromProto(proto: SignalServiceProtos.Content): ExpirationTimerUpdate? {
|
2021-03-09 07:26:29 +01:00
|
|
|
val dataMessageProto = if (proto.hasDataMessage()) proto.dataMessage else return null
|
2023-08-03 09:37:21 +02:00
|
|
|
val isExpirationTimerUpdate = dataMessageProto.flags.and(
|
|
|
|
SignalServiceProtos.DataMessage.Flags.EXPIRATION_TIMER_UPDATE_VALUE
|
|
|
|
) != 0
|
2020-11-27 06:41:21 +01:00
|
|
|
if (!isExpirationTimerUpdate) return null
|
2021-04-06 08:05:59 +02:00
|
|
|
val syncTarget = dataMessageProto.syncTarget
|
2023-11-28 03:17:27 +01:00
|
|
|
val duration: Int = if (proto.hasExpirationTimer()) proto.expirationTimer else dataMessageProto.expireTimer
|
|
|
|
val type = proto.expirationType.takeIf { duration > 0 }
|
|
|
|
val expiryMode = when (type) {
|
|
|
|
SignalServiceProtos.Content.ExpirationType.DELETE_AFTER_SEND -> ExpiryMode.AfterSend(duration.toLong())
|
|
|
|
SignalServiceProtos.Content.ExpirationType.DELETE_AFTER_READ -> ExpiryMode.AfterRead(duration.toLong())
|
|
|
|
else -> ExpiryMode.NONE
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExpirationTimerUpdate(expiryMode, syncTarget)
|
2020-11-27 06:41:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 05:56:16 +01:00
|
|
|
override fun toProto(): SignalServiceProtos.Content? {
|
2020-11-27 06:41:21 +01:00
|
|
|
val dataMessageProto = SignalServiceProtos.DataMessage.newBuilder()
|
|
|
|
dataMessageProto.flags = SignalServiceProtos.DataMessage.Flags.EXPIRATION_TIMER_UPDATE_VALUE
|
2023-11-28 03:17:27 +01:00
|
|
|
dataMessageProto.expireTimer = expiryMode.expirySeconds.toInt()
|
2021-04-06 08:05:59 +02:00
|
|
|
// Sync target
|
|
|
|
if (syncTarget != null) {
|
|
|
|
dataMessageProto.syncTarget = syncTarget
|
|
|
|
}
|
2021-03-11 00:23:16 +01:00
|
|
|
// Group context
|
2021-04-26 03:14:45 +02:00
|
|
|
if (MessagingModuleConfiguration.shared.storage.isClosedGroup(recipient!!)) {
|
2021-03-11 00:23:16 +01:00
|
|
|
try {
|
|
|
|
setGroupContext(dataMessageProto)
|
|
|
|
} catch(e: Exception) {
|
|
|
|
Log.w(VisibleMessage.TAG, "Couldn't construct visible message proto from: $this")
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
2022-11-22 02:08:58 +01:00
|
|
|
return try {
|
2023-11-10 03:22:35 +01:00
|
|
|
SignalServiceProtos.Content.newBuilder().apply {
|
2023-11-28 03:17:27 +01:00
|
|
|
expirationType
|
|
|
|
expirationTimer
|
2023-11-10 03:22:35 +01:00
|
|
|
dataMessage = dataMessageProto.build()
|
|
|
|
setExpirationConfigurationIfNeeded(threadID)
|
|
|
|
}.build()
|
2020-11-27 06:41:21 +01:00
|
|
|
} 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-27 06:41:21 +01:00
|
|
|
}
|
2020-11-27 05:56:16 +01:00
|
|
|
}
|
2020-11-25 02:06:41 +01:00
|
|
|
}
|