Update expiry configuration
parent
b529d6d341
commit
6eba3ac8af
@ -0,0 +1,64 @@
|
||||
package org.session.libsession.messaging.jobs
|
||||
|
||||
import org.session.libsession.messaging.MessagingModuleConfiguration
|
||||
import org.session.libsession.messaging.messages.control.SyncedExpiriesMessage
|
||||
import org.session.libsession.messaging.messages.control.SyncedExpiry
|
||||
import org.session.libsession.messaging.sending_receiving.MessageSender
|
||||
import org.session.libsession.messaging.utilities.Data
|
||||
import org.session.libsession.snode.SnodeAPI
|
||||
import org.session.libsession.utilities.Address
|
||||
|
||||
class DisappearingMessagesJob(val messageIds: LongArray, val startedAtMs: Long): Job {
|
||||
|
||||
override var delegate: JobDelegate? = null
|
||||
override var id: String? = null
|
||||
override var failureCount: Int = 0
|
||||
override val maxFailureCount: Int = 1
|
||||
|
||||
override fun execute() {
|
||||
val userPublicKey = MessagingModuleConfiguration.shared.storage.getUserPublicKey() ?: return
|
||||
val module = MessagingModuleConfiguration.shared
|
||||
try {
|
||||
module.storage.getExpiringMessages(messageIds).groupBy { it.second }.forEach { (expiresInSeconds, messages) ->
|
||||
val serverHashes = messages.map { it.first }
|
||||
if (serverHashes.isEmpty()) return
|
||||
val expirationTimestamp = startedAtMs + expiresInSeconds * 1000
|
||||
val syncTarget = ""
|
||||
val syncedExpiriesMessage = SyncedExpiriesMessage()
|
||||
syncedExpiriesMessage.conversationExpiries = mapOf(
|
||||
syncTarget to serverHashes.map { serverHash -> SyncedExpiry(serverHash, expirationTimestamp) }
|
||||
)
|
||||
MessageSender.send(syncedExpiriesMessage, Address.fromSerialized(userPublicKey))
|
||||
SnodeAPI.updateExpiry(expirationTimestamp, serverHashes)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
delegate?.handleJobFailed(this, e)
|
||||
return
|
||||
}
|
||||
delegate?.handleJobSucceeded(this)
|
||||
}
|
||||
|
||||
override fun serialize(): Data = Data.Builder()
|
||||
.putLongArray(MESSAGE_IDS, messageIds)
|
||||
.putLong(STARTED_AT_MS, startedAtMs)
|
||||
.build()
|
||||
|
||||
override fun getFactoryKey(): String = KEY
|
||||
|
||||
class Factory : Job.Factory<DisappearingMessagesJob> {
|
||||
override fun create(data: Data): DisappearingMessagesJob {
|
||||
return DisappearingMessagesJob(
|
||||
data.getLongArray(MESSAGE_IDS),
|
||||
data.getLong(STARTED_AT_MS)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val KEY = "DisappearingMessagesJob"
|
||||
|
||||
private const val MESSAGE_IDS = "messageIds"
|
||||
private const val STARTED_AT_MS = "startedAtMs"
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package org.session.libsession.messaging.messages
|
||||
|
||||
import org.session.libsignal.protos.SignalServiceProtos.Content.ExpirationType
|
||||
|
||||
class ExpirationSettingsConfiguration(
|
||||
val threadId: Long = -1,
|
||||
val isEnabled: Boolean = false,
|
||||
val durationSeconds: Int = 0,
|
||||
val expirationType: ExpirationType? = null,
|
||||
val lastChangeTimestampMs: Long = 0
|
||||
)
|
@ -0,0 +1,61 @@
|
||||
package org.session.libsession.messaging.messages.control
|
||||
|
||||
import org.session.libsignal.protos.SignalServiceProtos
|
||||
import org.session.libsignal.protos.SignalServiceProtos.SyncedExpiries.SyncedConversationExpiries
|
||||
import org.session.libsignal.utilities.Log
|
||||
|
||||
class SyncedExpiriesMessage(): ControlMessage() {
|
||||
var conversationExpiries: Map<String, List<SyncedExpiry>> = emptyMap()
|
||||
|
||||
override val isSelfSendValid: Boolean = true
|
||||
|
||||
// region Validation
|
||||
override fun isValid(): Boolean {
|
||||
if (!super.isValid()) return false
|
||||
return conversationExpiries.isNotEmpty()
|
||||
}
|
||||
// endregion
|
||||
|
||||
companion object {
|
||||
const val TAG = "SyncedExpiriesMessage"
|
||||
|
||||
fun fromProto(proto: SignalServiceProtos.Content): SyncedExpiriesMessage? {
|
||||
val syncedExpiriesProto = if (proto.hasSyncedExpiries()) proto.syncedExpiries else return null
|
||||
val conversationExpiries = syncedExpiriesProto.conversationExpiriesList.associate {
|
||||
it.syncTarget to it.expiriesList.map { syncedExpiry -> SyncedExpiry.fromProto(syncedExpiry) }
|
||||
}
|
||||
return SyncedExpiriesMessage(conversationExpiries)
|
||||
}
|
||||
}
|
||||
|
||||
constructor(conversationExpiries: Map<String, List<SyncedExpiry>>) : this() {
|
||||
this.conversationExpiries = conversationExpiries
|
||||
}
|
||||
|
||||
override fun toProto(): SignalServiceProtos.Content? {
|
||||
val conversationExpiries = conversationExpiries
|
||||
if (conversationExpiries.isEmpty()) {
|
||||
Log.w(TAG, "Couldn't construct synced expiries proto from: $this")
|
||||
return null
|
||||
}
|
||||
val conversationExpiriesProto = conversationExpiries.map { (syncTarget, syncedExpiries) ->
|
||||
val expiriesProto = syncedExpiries.map(SyncedExpiry::toProto)
|
||||
val syncedConversationExpiriesProto = SyncedConversationExpiries.newBuilder()
|
||||
syncedConversationExpiriesProto.syncTarget = syncTarget
|
||||
syncedConversationExpiriesProto.addAllExpiries(expiriesProto)
|
||||
syncedConversationExpiriesProto.build()
|
||||
}
|
||||
val syncedExpiriesProto = SignalServiceProtos.SyncedExpiries.newBuilder()
|
||||
syncedExpiriesProto.addAllConversationExpiries(conversationExpiriesProto)
|
||||
val contentProto = SignalServiceProtos.Content.newBuilder()
|
||||
return try {
|
||||
contentProto.syncedExpiries = syncedExpiriesProto.build()
|
||||
setExpirationSettingsConfigIfNeeded(contentProto)
|
||||
contentProto.build()
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Couldn't construct synced expiries proto from: $this")
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package org.session.libsession.messaging.messages.control
|
||||
|
||||
import org.session.libsignal.protos.SignalServiceProtos.SyncedExpiries
|
||||
import org.session.libsignal.utilities.Log
|
||||
|
||||
class SyncedExpiry(
|
||||
var serverHash: String? = null,
|
||||
var expirationTimestamp: Long? = null
|
||||
) {
|
||||
|
||||
fun toProto(): SyncedExpiries.SyncedConversationExpiries.SyncedExpiry? {
|
||||
val syncedExpiryProto = SyncedExpiries.SyncedConversationExpiries.SyncedExpiry.newBuilder()
|
||||
serverHash?.let { syncedExpiryProto.serverHash = it }
|
||||
expirationTimestamp?.let { syncedExpiryProto.expirationTimestamp = it }
|
||||
return try {
|
||||
syncedExpiryProto.build()
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Couldn't construct synced expiry proto from: $this")
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val TAG = "SyncedExpiry"
|
||||
|
||||
@JvmStatic
|
||||
fun fromProto(proto: SyncedExpiries.SyncedConversationExpiries.SyncedExpiry): SyncedExpiry {
|
||||
val result = SyncedExpiry()
|
||||
result.serverHash = if (proto.hasServerHash()) proto.serverHash else null
|
||||
result.expirationTimestamp = if (proto.hasServerHash()) proto.expirationTimestamp else null
|
||||
return SyncedExpiry()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue