Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / rcs / e2e / RcsPendingGroups.kt

RcsPendingGroups.kt48 lines · 2.1 KiBRaw
1package com.vayunmathur.communicate.data.rcs.e2e
2
3import com.vayunmathur.communicate.data.rcs.RcsFeature
4
5/**
6 * Pending encrypted-group setups awaiting member key packages.
7 *
8 * When the user creates an encrypted group before every member's key package
9 * is cached, the conversation id + member set is parked here. Each inbound
10 * key package re-checks pending sets; once all members are cached, the group
11 * is created and the entry removed. In-memory like [RcsPeerKeys] (v1 scope).
12 */
13object RcsPendingGroups {
14 private data class Pending(val conversationId: String, val members: Set<String>)
15
16 private val pending = java.util.concurrent.ConcurrentHashMap<String, Pending>()
17
18 /** Park [conversationId] with [members] until all key packages arrive. */
19 fun add(conversationId: String, members: Collection<String>) {
20 if (!RcsFeature.enabled || conversationId.isBlank() || members.isEmpty()) return
21 pending[conversationId] = Pending(conversationId, members.toSet())
22 }
23
24 /** Drop the pending entry (group created or abandoned). */
25 fun remove(conversationId: String) {
26 pending.remove(conversationId)
27 }
28
29 /**
30 * Pending entries whose member set includes [peerE164] and whose every
31 * member now has a cached key package. Callers create the group for each
32 * returned conversation id, then [remove] it.
33 */
34 fun readyFor(peerE164: String): List<Pair<String, Map<String, ByteArray>>> {
35 if (!RcsFeature.enabled) return emptyList()
36 return pending.values.mapNotNull { p ->
37 if (!p.members.contains(peerE164)) return@mapNotNull null
38 val packages = p.members.associateWith { RcsPeerKeys.get(it) }
39 if (packages.values.any { it == null }) return@mapNotNull null
40 @Suppress("UNCHECKED_CAST")
41 p.conversationId to (packages as Map<String, ByteArray>)
42 }
43 }
44
45 /** True when [conversationId] is parked awaiting keys ("waiting" banner). */
46 fun isPending(conversationId: String): Boolean =
47 RcsFeature.enabled && pending.containsKey(conversationId)
48}