refactor: use the shared config expiry mode types instead of custom protobufs so we can remove a variable and simplify checks across two variables

This commit is contained in:
0x330a 2023-07-28 11:39:12 +10:00
parent 7f4db64a5a
commit 19083e2ce7
4 changed files with 43 additions and 20 deletions

View File

@ -4,7 +4,7 @@ import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import org.session.libsession.messaging.messages.ExpirationConfiguration
import org.session.libsession.messaging.messages.ExpirationDatabaseConfiguration
import org.session.libsession.messaging.messages.ExpirationDatabaseMetadata
import org.session.libsession.utilities.GroupUtil.CLOSED_GROUP_PREFIX
import org.session.libsession.utilities.GroupUtil.OPEN_GROUP_INBOX_PREFIX
import org.session.libsession.utilities.GroupUtil.OPEN_GROUP_PREFIX
@ -43,19 +43,19 @@ class ExpirationConfigurationDatabase(context: Context, helper: SQLCipherOpenHel
AND EXISTS (SELECT ${RecipientDatabase.EXPIRE_MESSAGES} FROM ${RecipientDatabase.TABLE_NAME} WHERE ${ThreadDatabase.TABLE_NAME}.${ThreadDatabase.ADDRESS} = ${RecipientDatabase.TABLE_NAME}.${RecipientDatabase.ADDRESS} AND ${RecipientDatabase.EXPIRE_MESSAGES} > 0)
""".trimIndent()
private fun readExpirationConfiguration(cursor: Cursor): ExpirationDatabaseConfiguration {
return ExpirationDatabaseConfiguration(
private fun readExpirationConfiguration(cursor: Cursor): ExpirationDatabaseMetadata {
return ExpirationDatabaseMetadata(
threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID)),
updatedTimestampMs = cursor.getLong(cursor.getColumnIndexOrThrow(UPDATED_TIMESTAMP_MS))
)
}
}
fun getExpirationConfiguration(threadId: Long): ExpirationDatabaseConfiguration? {
fun getExpirationConfiguration(threadId: Long): ExpirationDatabaseMetadata? {
val query = "$THREAD_ID = ?"
val args = arrayOf("$threadId")
val configurations: MutableList<ExpirationDatabaseConfiguration> = mutableListOf()
val configurations: MutableList<ExpirationDatabaseMetadata> = mutableListOf()
readableDatabase.query(TABLE_NAME, null, query, args, null, null, null).use { cursor ->
while (cursor.moveToNext()) {

View File

@ -1672,21 +1672,29 @@ open class Storage(context: Context, helper: SQLCipherOpenHelper, private val co
override fun getExpirationConfiguration(threadId: Long): ExpirationConfiguration? {
val recipient = getRecipientForThread(threadId) ?: return null
val dbExpirationMetadata = DatabaseComponent.get(context).expirationConfigurationDatabase().getExpirationConfiguration(threadId) ?: return null
return if (recipient.isContactRecipient && recipient.address.serialize().startsWith(IdPrefix.STANDARD.value)) {
// read it from contacts config if exists
configFactory.contacts?.get(recipient.address.serialize())?.let { contact ->
val mode = contact.expiryMode
ExpirationConfiguration(threadId, mode.expirySeconds,)
contact.expiryMode
ExpirationConfiguration(
threadId,
mode,
dbExpirationMetadata.updatedTimestampMs
)
}
} else if (recipient.isClosedGroupRecipient) {
// read it from group config if exists
val groupPublicKey = GroupUtil.doubleDecodeGroupId(recipient.address.serialize())
configFactory.userGroups?.getLegacyGroupInfo(groupPublicKey)?.let {
configFactory.userGroups?.getLegacyGroupInfo(groupPublicKey)?.let { group ->
val expiry = group.disappearingTimer
ExpirationConfiguration(
threadId,
if (expiry != 0L) ExpiryMode.AfterRead(expiry) else ExpiryMode.NONE,
dbExpirationMetadata.updatedTimestampMs
)
}
} else null
return DatabaseComponent.get(context).expirationConfigurationDatabase().getExpirationConfiguration(threadId)
}
override fun setExpirationConfiguration(config: ExpirationConfiguration) {

View File

@ -38,16 +38,23 @@ import org.session.libsignal.protos.SignalServiceProtos.Content.ExpirationType;
import org.session.libsignal.utilities.Log;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo;
import org.thoughtcrime.securesms.database.MmsDatabase;
import org.thoughtcrime.securesms.database.SmsDatabase;
import org.thoughtcrime.securesms.database.Storage;
import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.dependencies.DatabaseComponent;
import org.thoughtcrime.securesms.mms.MmsException;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;
/**
* Get the response text from the Wearable Device and sends an message as a reply
*/
@AndroidEntryPoint
public class RemoteReplyReceiver extends BroadcastReceiver {
public static final String TAG = RemoteReplyReceiver.class.getSimpleName();
@ -55,6 +62,15 @@ public class RemoteReplyReceiver extends BroadcastReceiver {
public static final String ADDRESS_EXTRA = "address";
public static final String REPLY_METHOD = "reply_method";
@Inject
ThreadDatabase threadDatabase;
@Inject
MmsDatabase mmsDatabase;
@Inject
SmsDatabase smsDatabase;
@Inject
Storage storage;
@SuppressLint("StaticFieldLeak")
@Override
public void onReceive(final Context context, Intent intent) {
@ -76,19 +92,19 @@ public class RemoteReplyReceiver extends BroadcastReceiver {
@Override
protected Void doInBackground(Void... params) {
Recipient recipient = Recipient.from(context, address, false);
ThreadDatabase threadDatabase = DatabaseComponent.get(context).threadDatabase();
long threadId = threadDatabase.getOrCreateThreadIdFor(recipient);
VisibleMessage message = new VisibleMessage();
message.setSentTimestamp(SnodeAPI.getNowWithOffset());
message.setText(responseText.toString());
ExpirationConfiguration config = DatabaseComponent.get(context).expirationConfigurationDatabase().getExpirationConfiguration(threadId);
ExpirationConfiguration config = storage.getExpirationConfiguration(threadId);
long expiresInMillis = config == null ? 0 : config.getDurationSeconds() * 1000L;
long expireStartedAt = config.getExpirationType() == ExpirationType.DELETE_AFTER_SEND ? message.getSentTimestamp() : 0L;
switch (replyMethod) {
case GroupMessage: {
OutgoingMediaMessage reply = OutgoingMediaMessage.from(message, recipient, Collections.emptyList(), null, null, expiresInMillis, 0);
try {
DatabaseComponent.get(context).mmsDatabase().insertMessageOutbox(reply, threadId, false, null, true);
mmsDatabase.insertMessageOutbox(reply, threadId, false, null, true);
MessageSender.send(message, address);
} catch (MmsException e) {
Log.w(TAG, e);
@ -97,7 +113,7 @@ public class RemoteReplyReceiver extends BroadcastReceiver {
}
case SecureMessage: {
OutgoingTextMessage reply = OutgoingTextMessage.from(message, recipient, expiresInMillis, expireStartedAt);
DatabaseComponent.get(context).smsDatabase().insertMessageOutbox(threadId, reply, false, System.currentTimeMillis(), null, true);
smsDatabase.insertMessageOutbox(threadId, reply, false, System.currentTimeMillis(), null, true);
MessageSender.send(message, address);
break;
}

View File

@ -1,14 +1,13 @@
package org.session.libsession.messaging.messages
import org.session.libsignal.protos.SignalServiceProtos.Content.ExpirationType
import network.loki.messenger.libsession_util.util.ExpiryMode
data class ExpirationConfiguration(
val threadId: Long = -1,
val durationSeconds: Int = 0,
val expirationType: ExpirationType? = null,
val expirationType: ExpiryMode? = null,
val updatedTimestampMs: Long = 0
) {
val isEnabled = durationSeconds > 0 && expirationType != null
val isEnabled = expirationType != null && expirationType.expirySeconds > 0
companion object {
val isNewConfigEnabled = false /* TODO: System.currentTimeMillis() > 1_676_851_200_000 // 13/02/2023 */
@ -16,7 +15,7 @@ data class ExpirationConfiguration(
}
}
data class ExpirationDatabaseConfiguration(
data class ExpirationDatabaseMetadata(
val threadId: Long = -1,
val updatedTimestampMs: Long
)