From 3d6c7dbae4e32934a395aa8c8d03da578826d268 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 10 Oct 2023 09:20:37 +1030 Subject: [PATCH] Simplify MarkReadReceiver#process --- .../notifications/MarkReadReceiver.kt | 33 ++++++++----------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/MarkReadReceiver.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/MarkReadReceiver.kt index d0ac8b63c..57b616d10 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/MarkReadReceiver.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/MarkReadReceiver.kt @@ -56,43 +56,36 @@ class MarkReadReceiver : BroadcastReceiver() { @JvmStatic fun process(context: Context, markedReadMessages: List) { + if (markedReadMessages.isEmpty()) return val loki = DatabaseComponent.get(context).lokiMessageDatabase() task { val hashToInfo = markedReadMessages.associateByNotNull { loki.getMessageServerHash(it.expirationInfo.id) } - if (hashToInfo.isEmpty()) return@task @Suppress("UNCHECKED_CAST") - val hashToExpiry = SnodeAPI.getExpiries(hashToInfo.keys.toList(), TextSecurePreferences.getLocalNumber(context)!!) + val expiries = SnodeAPI.getExpiries(hashToInfo.keys.toList(), TextSecurePreferences.getLocalNumber(context)!!) .get()["expiries"] as Map - hashToInfo.forEach { (hash, info) -> hashToExpiry[hash]?.let { scheduleDeletion(context, info.expirationInfo, it - info.expirationInfo.expireStarted) } } + hashToInfo.forEach { (hash, info) -> expiries[hash]?.let { scheduleDeletion(context, info.expirationInfo, it - info.expirationInfo.expireStarted) } } } fail { Log.e(TAG, "process() disappear after read failed", it) } - if (markedReadMessages.isEmpty()) return - for (messageInfo in markedReadMessages) { - scheduleDeletion(context, messageInfo.expirationInfo) - } + markedReadMessages.forEach { scheduleDeletion(context, it.expirationInfo) } + if (!isReadReceiptsEnabled(context)) return - val addressMap = Stream.of(markedReadMessages) - .map { it.syncMessageId } - .collect(Collectors.groupingBy { it.address } ) - - for (address in addressMap.keys) { - val timestamps = addressMap[address]!!.map { obj: SyncMessageId -> obj.timetamp } - if (!shouldSendReadReceipt(Recipient.from(context, address, false))) { - continue + markedReadMessages.map { it.syncMessageId } + .filter { shouldSendReadReceipt(Recipient.from(context, it.address, false)) } + .groupBy { it.address } + .forEach { (address, messages) -> + messages.map { it.timetamp } + .let(::ReadReceipt) + .apply { sentTimestamp = nowWithOffset } + .let { send(it, address) } } - - ReadReceipt(timestamps) - .apply { sentTimestamp = nowWithOffset } - .let { send(it, address) } - } } fun scheduleDeletion(