Fix Message#expiryMode de/serialisation
parent
cb0327ecb2
commit
4c7485f53d
@ -1,59 +1,53 @@
|
||||
package org.session.libsession.messaging.messages.control
|
||||
|
||||
import network.loki.messenger.libsession_util.util.ExpiryMode
|
||||
import org.session.libsession.messaging.MessagingModuleConfiguration
|
||||
import org.session.libsession.messaging.messages.copyExpiration
|
||||
import org.session.libsession.messaging.messages.visible.VisibleMessage
|
||||
import org.session.libsignal.protos.SignalServiceProtos
|
||||
import org.session.libsignal.protos.SignalServiceProtos.DataMessage.Flags.EXPIRATION_TIMER_UPDATE_VALUE
|
||||
import org.session.libsignal.utilities.Log
|
||||
|
||||
/** 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.
|
||||
*/
|
||||
data class ExpirationTimerUpdate(var syncTarget: String? = null) : ControlMessage() {
|
||||
data class ExpirationTimerUpdate(var syncTarget: String? = null, val isGroup: Boolean = false) : ControlMessage() {
|
||||
override val isSelfSendValid: Boolean = true
|
||||
|
||||
companion object {
|
||||
const val TAG = "ExpirationTimerUpdate"
|
||||
private val storage = MessagingModuleConfiguration.shared.storage
|
||||
|
||||
fun fromProto(proto: SignalServiceProtos.Content): ExpirationTimerUpdate? {
|
||||
val dataMessageProto = if (proto.hasDataMessage()) proto.dataMessage else return null
|
||||
val isExpirationTimerUpdate = dataMessageProto.flags.and(
|
||||
SignalServiceProtos.DataMessage.Flags.EXPIRATION_TIMER_UPDATE_VALUE
|
||||
) != 0
|
||||
if (!isExpirationTimerUpdate) return null
|
||||
|
||||
return ExpirationTimerUpdate(dataMessageProto.syncTarget)
|
||||
.copyExpiration(proto)
|
||||
}
|
||||
fun fromProto(proto: SignalServiceProtos.Content): ExpirationTimerUpdate? =
|
||||
proto.dataMessage?.takeIf { it.flags and EXPIRATION_TIMER_UPDATE_VALUE != 0 }?.run {
|
||||
ExpirationTimerUpdate(syncTarget, hasGroup()).copyExpiration(proto)
|
||||
}
|
||||
}
|
||||
|
||||
override fun toProto(): SignalServiceProtos.Content? {
|
||||
val dataMessageProto = SignalServiceProtos.DataMessage.newBuilder()
|
||||
dataMessageProto.flags = SignalServiceProtos.DataMessage.Flags.EXPIRATION_TIMER_UPDATE_VALUE
|
||||
dataMessageProto.expireTimer = expiryMode.expirySeconds.toInt()
|
||||
// Sync target
|
||||
if (syncTarget != null) {
|
||||
dataMessageProto.syncTarget = syncTarget
|
||||
val dataMessageProto = SignalServiceProtos.DataMessage.newBuilder().apply {
|
||||
flags = EXPIRATION_TIMER_UPDATE_VALUE
|
||||
expireTimer = expiryMode.expirySeconds.toInt()
|
||||
}
|
||||
// Sync target
|
||||
syncTarget?.let { dataMessageProto.syncTarget = it }
|
||||
// Group context
|
||||
if (MessagingModuleConfiguration.shared.storage.isClosedGroup(recipient!!)) {
|
||||
if (storage.isClosedGroup(recipient!!)) {
|
||||
try {
|
||||
setGroupContext(dataMessageProto)
|
||||
} catch(e: Exception) {
|
||||
Log.w(VisibleMessage.TAG, "Couldn't construct visible message proto from: $this")
|
||||
Log.w(TAG, "Couldn't construct visible message proto from: $this", e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
return try {
|
||||
SignalServiceProtos.Content.newBuilder().apply {
|
||||
dataMessage = dataMessageProto.build()
|
||||
setExpirationConfigurationIfNeeded(threadID)
|
||||
}.build()
|
||||
SignalServiceProtos.Content.newBuilder()
|
||||
.setDataMessage(dataMessageProto)
|
||||
.applyExpiryMode()
|
||||
.build()
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Couldn't construct expiration timer update proto from: $this")
|
||||
Log.w(TAG, "Couldn't construct expiration timer update proto from: $this", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue