Implement remaining bits and pieces

This commit is contained in:
Niels Andriesse 2021-05-21 11:09:03 +10:00
parent da331b036e
commit ff96bb05a8
5 changed files with 39 additions and 12 deletions

View File

@ -602,6 +602,11 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context,
return if (threadID < 0) null else threadID return if (threadID < 0) null else threadID
} }
fun foo() {
val threadDB = DatabaseFactory.getThreadDatabase(context)
}
override fun getThreadIdForMms(mmsId: Long): Long { override fun getThreadIdForMms(mmsId: Long): Long {
val mmsDb = DatabaseFactory.getMmsDatabase(context) val mmsDb = DatabaseFactory.getMmsDatabase(context)
val cursor = mmsDb.getMessage(mmsId) val cursor = mmsDb.getMessage(mmsId)
@ -669,10 +674,15 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context,
threadDatabase.getOrCreateThreadIdFor(recipient) threadDatabase.getOrCreateThreadIdFor(recipient)
} }
if (contacts.isNotEmpty()) { if (contacts.isNotEmpty()) {
threadDatabase.notifyUpdatedFromConfig() threadDatabase.notifyConversationListListeners()
} }
} }
override fun getLastUpdated(threadID: Long): Long {
val threadDB = DatabaseFactory.getThreadDatabase(context)
return threadDB.getLastUpdated(threadID)
}
override fun getAttachmentDataUri(attachmentId: AttachmentId): Uri { override fun getAttachmentDataUri(attachmentId: AttachmentId): Uri {
return PartAuthority.getAttachmentDataUri(attachmentId) return PartAuthority.getAttachmentDataUri(attachmentId)
} }

View File

@ -64,7 +64,7 @@ public class ThreadDatabase extends Database {
private static final String TAG = ThreadDatabase.class.getSimpleName(); private static final String TAG = ThreadDatabase.class.getSimpleName();
private Map<Long, Address> addressCache = new HashMap<>(); private final Map<Long, Address> addressCache = new HashMap<>();
public static final String TABLE_NAME = "thread"; public static final String TABLE_NAME = "thread";
public static final String ID = "_id"; public static final String ID = "_id";
@ -404,6 +404,21 @@ public class ThreadDatabase extends Database {
} }
} }
public Long getLastUpdated(long threadId) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[]{DATE}, ID_WHERE, new String[]{String.valueOf(threadId)}, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
return cursor.getLong(0);
}
return -1L;
} finally {
if (cursor != null) cursor.close();
}
}
public void deleteConversation(long threadId) { public void deleteConversation(long threadId) {
DatabaseFactory.getSmsDatabase(context).deleteThread(threadId); DatabaseFactory.getSmsDatabase(context).deleteThread(threadId);
DatabaseFactory.getMmsDatabase(context).deleteThread(threadId); DatabaseFactory.getMmsDatabase(context).deleteThread(threadId);
@ -471,7 +486,6 @@ public class ThreadDatabase extends Database {
} }
public @Nullable Recipient getRecipientForThreadId(long threadId) { public @Nullable Recipient getRecipientForThreadId(long threadId) {
// Loki - Cache the address
if (addressCache.containsKey(threadId) && addressCache.get(threadId) != null) { if (addressCache.containsKey(threadId) && addressCache.get(threadId) != null) {
return Recipient.from(context, addressCache.get(threadId), false); return Recipient.from(context, addressCache.get(threadId), false);
} }
@ -505,10 +519,6 @@ public class ThreadDatabase extends Database {
notifyConversationListeners(threadId); notifyConversationListeners(threadId);
} }
public void notifyUpdatedFromConfig() {
notifyConversationListListeners();
}
public boolean update(long threadId, boolean unarchive) { public boolean update(long threadId, boolean unarchive) {
MmsSmsDatabase mmsSmsDatabase = DatabaseFactory.getMmsSmsDatabase(context); MmsSmsDatabase mmsSmsDatabase = DatabaseFactory.getMmsSmsDatabase(context);
long count = mmsSmsDatabase.getConversationCount(threadId); long count = mmsSmsDatabase.getConversationCount(threadId);

View File

@ -140,6 +140,7 @@ interface StorageProtocol {
fun getOrCreateThreadIdFor(publicKey: String, groupPublicKey: String?, openGroupID: String?): Long fun getOrCreateThreadIdFor(publicKey: String, groupPublicKey: String?, openGroupID: String?): Long
fun getThreadIdFor(address: Address): Long? fun getThreadIdFor(address: Address): Long?
fun getThreadIdForMms(mmsId: Long): Long fun getThreadIdForMms(mmsId: Long): Long
fun getLastUpdated(threadID: Long): Long
// Session Request // Session Request
fun getSessionRequestSentTimestamp(publicKey: String): Long? fun getSessionRequestSentTimestamp(publicKey: String): Long?

View File

@ -5,9 +5,9 @@ import org.session.libsession.database.MessageDataProvider
import org.session.libsession.database.StorageProtocol import org.session.libsession.database.StorageProtocol
class MessagingModuleConfiguration( class MessagingModuleConfiguration(
val context: Context, val context: Context,
val storage: StorageProtocol, val storage: StorageProtocol,
val messageDataProvider: MessageDataProvider val messageDataProvider: MessageDataProvider
) { ) {
companion object { companion object {

View File

@ -7,9 +7,11 @@ import org.session.libsession.messaging.MessagingModuleConfiguration
import org.session.libsession.messaging.jobs.JobQueue import org.session.libsession.messaging.jobs.JobQueue
import org.session.libsession.messaging.jobs.MessageReceiveJob import org.session.libsession.messaging.jobs.MessageReceiveJob
import org.session.libsession.snode.SnodeAPI import org.session.libsession.snode.SnodeAPI
import org.session.libsession.utilities.GroupUtil
import org.session.libsignal.crypto.getRandomElementOrNull import org.session.libsignal.crypto.getRandomElementOrNull
import org.session.libsignal.utilities.Log import org.session.libsignal.utilities.Log
import org.session.libsignal.utilities.successBackground import org.session.libsignal.utilities.successBackground
import java.util.*
import java.util.concurrent.Executors import java.util.concurrent.Executors
import java.util.concurrent.ScheduledFuture import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -71,9 +73,13 @@ class ClosedGroupPollerV2 {
if (!isPolling(groupPublicKey)) { return } if (!isPolling(groupPublicKey)) { return }
// Get the received date of the last message in the thread. If we don't have any messages yet, pick some // Get the received date of the last message in the thread. If we don't have any messages yet, pick some
// reasonable fake time interval to use instead. // reasonable fake time interval to use instead.
val timeSinceLastMessage = 5 * 60 * 1000 // TODO: Implement val storage = MessagingModuleConfiguration.shared.storage
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
val threadID = storage.getThreadID(groupID)?.toLongOrNull() ?: return
val lastUpdated = storage.getLastUpdated(threadID)
val timeSinceLastMessage = if (lastUpdated != -1L) Date().time - lastUpdated else 5 * 60 * 1000
val minPollInterval = Companion.minPollInterval val minPollInterval = Companion.minPollInterval
val limit = 12 * 60 * 60 * 1000 val limit: Long = 12 * 60 * 60 * 1000
val a = (Companion.maxPollInterval - minPollInterval).toDouble() / limit.toDouble() val a = (Companion.maxPollInterval - minPollInterval).toDouble() / limit.toDouble()
val nextPollInterval = a * min(timeSinceLastMessage, limit) + minPollInterval val nextPollInterval = a * min(timeSinceLastMessage, limit) + minPollInterval
Log.d("Loki", "Next poll interval for closed group with public key: $groupPublicKey is ${nextPollInterval / 1000} s.") Log.d("Loki", "Next poll interval for closed group with public key: $groupPublicKey is ${nextPollInterval / 1000} s.")