Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / WhatsAppClientInboundNotifications.kt
WhatsAppClientInboundNotifications.kt132 lines · 5.5 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp |
| 2 | |
| 3 | import com.vayunmathur.library.log.Log |
| 4 | import com.vayunmathur.communicate.data.whatsapp.call.WhatsAppCallManager |
| 5 | import com.vayunmathur.communicate.data.whatsapp.call.WhatsAppCallSignaling |
| 6 | import com.vayunmathur.communicate.data.whatsapp.encodeNode |
| 7 | import kotlinx.coroutines.launch |
| 8 | |
| 9 | private const val STALE_CALL_MINUTES = 15L |
| 10 | private const val STALE_CALL_SECONDS = STALE_CALL_MINUTES * 60 |
| 11 | private const val MS_PER_SECOND = 1000L |
| 12 | |
| 13 | /** |
| 14 | * Handle notification events (group changes, mute, pin, archive). |
| 15 | * From Go handleWAGroupInfoChange, handleWAMute, handleWAArchive, handleWAPin. |
| 16 | */ |
| 17 | internal suspend fun WhatsAppClient.handleNotification(node: WhatsAppProtocol.Node) { |
| 18 | val notifType = node.attrs["type"] ?: return |
| 19 | val from = node.attrs["from"] ?: return |
| 20 | |
| 21 | when (notifType) { |
| 22 | "w:gp2" -> handleGroupNotification(node, from) |
| 23 | "server_sync" -> handleServerSync(node) |
| 24 | "call" -> handleCallNotification(node, from) |
| 25 | "encrypt" -> handleIdentityChange(node, from) |
| 26 | "picture" -> handlePictureUpdate(node, from) |
| 27 | "account_sync" -> handleAccountSync(node) |
| 28 | "devices" -> {} // Device list update — Go ignores silently |
| 29 | "mediaretry" -> {} // Media retry — Go handles but requires bridge infra |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Handle group info change notifications. |
| 35 | * From Go handleWAGroupInfoChange. |
| 36 | */ |
| 37 | internal suspend fun WhatsAppClient.handleGroupNotification(node: WhatsAppProtocol.Node, groupJid: String) { |
| 38 | val timestamp = (node.attrs["t"]?.toLongOrNull() ?: System.currentTimeMillis() / 1000) * 1000 |
| 39 | val actor = resolveName(node.attrs["participant"] ?: groupJid) |
| 40 | val msgId = node.attrs["id"] ?: "" |
| 41 | |
| 42 | // Any group notification (being added, subject/participant changes, …) implies the |
| 43 | // group should exist locally. Fetch + publish its metadata so the conversation appears |
| 44 | // named and flagged as a group even when we were just added and have no history for it. |
| 45 | fetchAndEmitGroupInfo(groupJid) |
| 46 | |
| 47 | node.content.filterIsInstance<WhatsAppProtocol.Node>().forEach { child -> |
| 48 | val body = groupChangeBody(child) ?: return@forEach |
| 49 | eventsMutable.emit(WhatsAppEvent.IncomingMessage( |
| 50 | source = MessageSource.WHATSAPP, |
| 51 | conversationId = "wa:$groupJid", |
| 52 | messageId = msgId, |
| 53 | body = body, |
| 54 | peerName = actor, |
| 55 | peerPhone = null, |
| 56 | timestamp = timestamp, |
| 57 | )) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Respond to a server <dirty> info block with <clean> (MarkNotDirty) so the primary phone |
| 64 | * stops showing "syncing / keep WhatsApp open". Ref whatsmeow appstate.go MarkNotDirty. |
| 65 | * <iq to=s.whatsapp.net type=set xmlns=urn:xmpp:whatsapp:dirty><clean type=.. timestamp=../></iq> |
| 66 | */ |
| 67 | internal suspend fun WhatsAppClient.handleDirty(dirty: WhatsAppProtocol.Node) { |
| 68 | val type = dirty.attrs["type"] ?: return |
| 69 | val ts = dirty.attrs["timestamp"] |
| 70 | // account_sync dirty is cleared automatically by the server once we've synced; whatsmeow |
| 71 | // only explicitly cleans non-account_sync types, but cleaning is harmless and idempotent. |
| 72 | val cleanAttrs = mutableMapOf("type" to type) |
| 73 | if (ts != null) cleanAttrs["timestamp"] = ts |
| 74 | val iq = WhatsAppProtocol.Node( |
| 75 | tag = "iq", |
| 76 | attrs = mapOf( |
| 77 | "id" to generateMessageId(), "type" to "set", |
| 78 | "xmlns" to "urn:xmpp:whatsapp:dirty", "to" to "s.whatsapp.net", |
| 79 | ), |
| 80 | content = listOf(WhatsAppProtocol.Node(tag = "clean", attrs = cleanAttrs)), |
| 81 | ) |
| 82 | webSocket?.send(WhatsAppProtocol.encodeNode(iq)) |
| 83 | WhatsAppDiag.log(TAG, "cleaned dirty state: $type") |
| 84 | } |
| 85 | |
| 86 | internal suspend fun WhatsAppClient.handleServerSync(node: WhatsAppProtocol.Node) { |
| 87 | node.getChildren().filter { it.tag == "collection" }.forEach { coll -> |
| 88 | val name = coll.attrs["name"] ?: return@forEach |
| 89 | if (name in APP_STATE_COLLECTIONS) { |
| 90 | scope.launch { fetchAppStateCollection(name, fullSync = appStateVersion(name) == 0L) } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Handle incoming call notification. |
| 97 | * From Go handleWACallStart. |
| 98 | */ |
| 99 | internal suspend fun WhatsAppClient.handleCallNotification(node: WhatsAppProtocol.Node, from: String) { |
| 100 | // Route the typed stanza to the call state machine (offer/preaccept/accept/reject/ |
| 101 | // terminate/relay). This drives ringing + WebRTC media (client-to-client). |
| 102 | WhatsAppCallSignaling.parse(node)?.let { WhatsAppCallManager.onInbound(it) } |
| 103 | |
| 104 | val offer = node.getChildByTag("offer") |
| 105 | if (offer != null) { |
| 106 | // Go handleWACallStart: ignore calls older than 15 minutes |
| 107 | val callTimestamp = node.attrs["t"]?.toLongOrNull() ?: (System.currentTimeMillis() / 1000) |
| 108 | val ageSeconds = (System.currentTimeMillis() / 1000) - callTimestamp |
| 109 | if (ageSeconds > STALE_CALL_SECONDS) { |
| 110 | Log.dev(TAG, "Ignoring old call notification (${ageSeconds}s old)") |
| 111 | return |
| 112 | } |
| 113 | |
| 114 | val caller = node.attrs["participant"] ?: from |
| 115 | val callId = node.attrs["id"] ?: "" |
| 116 | val callType = offer.attrs["call-type"] ?: "voice" |
| 117 | val callLabel = when { |
| 118 | callType.contains("video") -> "video call" |
| 119 | callType.contains("group") -> "group call" |
| 120 | else -> "voice call" |
| 121 | } |
| 122 | eventsMutable.emit(WhatsAppEvent.IncomingMessage( |
| 123 | source = MessageSource.WHATSAPP, |
| 124 | conversationId = "wa:$from", |
| 125 | messageId = "call-$callId", |
| 126 | body = "\u260E Incoming $callLabel", |
| 127 | peerName = resolveName(caller), |
| 128 | peerPhone = null, |
| 129 | timestamp = callTimestamp * MS_PER_SECOND, |
| 130 | )) |
| 131 | } |
| 132 | } |