android: parse decimal channel IDs for avatar requests and group keys

This commit is contained in:
evgeny committed 2026-10-06 17:32:43 +02:00
1 parent b3c0a2fa7b
commit eae3ef5c32
3 files changed
+15 -5

No files matched your search

@@ -9,6 +9,8 @@ object AvatarProtocol {
const val MAX_GROUP = 16 * 1024
private val filePattern = Regex("[0-9a-f]{64}\\.png")
fun validFile(file: String): Boolean = filePattern.matches(file)
// channel_id в БД/событиях — десятичный u64; avatar_server в userinfo — hex.
fun groupId(id: String): Long = id.toULongOrNull(10)?.toLong() ?: 0
fun node(id: String): Long = id.toULongOrNull(16)?.toLong() ?: 0
fun hex(id: Long): String = id.toULong().toString(16).padStart(16, '0')
fun hash(bytes: ByteArray): String = bytes.joinToString("") { "%02x".format(it.toInt() and 255) }
@@ -182,7 +182,10 @@ class AvatarRepository(context: Context) {
}
private fun requestMembers(group: String) {
if (account == 0L) start()
if (account == 0L || AvatarProtocol.node(group) == 0L) return
if (account == 0L) return
if (AvatarProtocol.groupId(group) == 0L) {
log("WARN", "Member snapshot: invalid decimal channel_id=$group"); return
}
val now = android.os.SystemClock.elapsedRealtime()
if (now - (memberRequests[group] ?: -5000) < 5000) return
memberRequests[group] = now
@@ -191,12 +194,14 @@ class AvatarRepository(context: Context) {
private suspend fun request(type: Int, group: String, server: Long = 0, owner: Long = 0, command: Int = 0,
enqueue: (Long, Long) -> Boolean): ByteArray {
val id = ids.incrementAndGet(); val gid = AvatarProtocol.node(group)
val id = ids.incrementAndGet(); val gid = AvatarProtocol.groupId(group)
require(gid != 0L) { "invalid decimal channel_id=$group" }
val result = CompletableDeferred<ByteArray>()
val operation = Pending(type, gid, server, owner, command, result)
pending[id] = operation
var complete = false
try {
log("DEBUG", "Request id=$id type=$type group=$group parsedGroup=${gid.toULong()} server=${AvatarProtocol.hex(server)}")
if (!enqueue(id, gid)) throw IOException("native enqueue failed id=$id type=$type group=$group")
val data = withTimeout(60_000) { result.await() }
complete = true
@@ -209,7 +214,10 @@ class AvatarRepository(context: Context) {
fun ensureGroup(group: String, force: Boolean = false) {
if (account == 0L) start()
if (account == 0L || AvatarProtocol.node(group) == 0L) return
if (account == 0L) return
if (AvatarProtocol.groupId(group) == 0L) {
log("WARN", "Group snapshot: invalid decimal channel_id=$group"); return
}
if ("group:$group" in loads) { if (force) dirtyGroups.add(group); return }
val current = _groups.value[group]
if (!force && current != null && !current.error &&