Simplify MarkReadReceiver#process

This commit is contained in:
andrew 2023-10-10 09:20:37 +10:30
parent 17f41d76b8
commit 3d6c7dbae4
1 changed files with 13 additions and 20 deletions

View File

@ -56,43 +56,36 @@ class MarkReadReceiver : BroadcastReceiver() {
@JvmStatic @JvmStatic
fun process(context: Context, markedReadMessages: List<MarkedMessageInfo>) { fun process(context: Context, markedReadMessages: List<MarkedMessageInfo>) {
if (markedReadMessages.isEmpty()) return
val loki = DatabaseComponent.get(context).lokiMessageDatabase() val loki = DatabaseComponent.get(context).lokiMessageDatabase()
task { task {
val hashToInfo = markedReadMessages.associateByNotNull { loki.getMessageServerHash(it.expirationInfo.id) } val hashToInfo = markedReadMessages.associateByNotNull { loki.getMessageServerHash(it.expirationInfo.id) }
if (hashToInfo.isEmpty()) return@task if (hashToInfo.isEmpty()) return@task
@Suppress("UNCHECKED_CAST") @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<String, Long> .get()["expiries"] as Map<String, Long>
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 { } fail {
Log.e(TAG, "process() disappear after read failed", it) Log.e(TAG, "process() disappear after read failed", it)
} }
if (markedReadMessages.isEmpty()) return markedReadMessages.forEach { scheduleDeletion(context, it.expirationInfo) }
for (messageInfo in markedReadMessages) {
scheduleDeletion(context, messageInfo.expirationInfo)
}
if (!isReadReceiptsEnabled(context)) return if (!isReadReceiptsEnabled(context)) return
val addressMap = Stream.of(markedReadMessages) markedReadMessages.map { it.syncMessageId }
.map { it.syncMessageId } .filter { shouldSendReadReceipt(Recipient.from(context, it.address, false)) }
.collect(Collectors.groupingBy { it.address } ) .groupBy { it.address }
.forEach { (address, messages) ->
for (address in addressMap.keys) { messages.map { it.timetamp }
val timestamps = addressMap[address]!!.map { obj: SyncMessageId -> obj.timetamp } .let(::ReadReceipt)
if (!shouldSendReadReceipt(Recipient.from(context, address, false))) { .apply { sentTimestamp = nowWithOffset }
continue .let { send(it, address) }
} }
ReadReceipt(timestamps)
.apply { sentTimestamp = nowWithOffset }
.let { send(it, address) }
}
} }
fun scheduleDeletion( fun scheduleDeletion(