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

Modern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / WhatsAppClientChats.kt

WhatsAppClientChats.kt208 lines · 7.9 KiBRaw
1package com.vayunmathur.communicate.data.whatsapp
2
3import com.vayunmathur.communicate.data.whatsapp.WhatsAppClient.State
4import com.vayunmathur.communicate.data.whatsapp.buildLeaveGroup
5import com.vayunmathur.communicate.data.whatsapp.encodeNode
6import com.vayunmathur.communicate.data.whatsapp.generateMessageId
7
8/**
9 * Delete a chat, optionally leaving group, with AppState mutations.
10 * From Go HandleMatrixDeleteChat.
11 */
12/** True when the socket can carry a group-leave stanza right now. */
13private fun WhatsAppClient.canLeaveGroup(): Boolean =
14 stateMutable.value is State.Connected && webSocket != null
15
16suspend fun WhatsAppClient.deleteChat(conversationId: String, leaveGroup: Boolean = true): Boolean {
17 val jid = extractJid(conversationId) ?: return false
18 val ws = webSocket
19
20 if (leaveGroup && jid.contains("@g.us") && canLeaveGroup()) {
21 val id = WhatsAppProtocol.generateMessageId(authData?.wid)
22 val node = WhatsAppProtocol.buildLeaveGroup(jid, id)
23 ws?.send(WhatsAppProtocol.encodeNode(node))
24 }
25
26 // Query last message timestamp for the delete anchor
27 val conversation = db?.conversationDao()?.getConversation(jid)
28 val lastMsgTimestamp = conversation?.lastMessageTimestamp ?: 0L
29
30 // Push AppState delete mutation (Go HandleMatrixDeleteChat PatchDelete)
31 if (stateMutable.value is State.Connected && ws != null) {
32 val patchAttrs = mutableMapOf("jid" to jid, "action" to "delete")
33 if (lastMsgTimestamp > 0) patchAttrs["messageTimestamp"] = lastMsgTimestamp.toString()
34
35 val patchNode = WhatsAppProtocol.Node(
36 tag = "iq",
37 attrs = mapOf(
38 "id" to WhatsAppProtocol.generateMessageId(authData?.wid),
39 "type" to "set",
40 "xmlns" to "w:sync:app:state",
41 "to" to "s.whatsapp.net",
42 ),
43 content = listOf(
44 WhatsAppProtocol.Node(
45 tag = "sync",
46 content = listOf(
47 WhatsAppProtocol.Node(
48 tag = "collection",
49 attrs = mapOf("name" to "regular"),
50 content = listOf(
51 WhatsAppProtocol.Node(
52 tag = "patch",
53 attrs = patchAttrs,
54 )
55 ),
56 )
57 ),
58 )
59 ),
60 )
61 ws.send(WhatsAppProtocol.encodeNode(patchNode))
62 }
63
64 db?.conversationDao()?.delete(jid)
65 eventsMutable.emit(WhatsAppEvent.ConversationDeleted(source, conversationId))
66 return true
67}
68
69suspend fun WhatsAppClient.sendNewThread(recipientJid: String, body: String): String? {
70 if (stateMutable.value !is State.Connected) return null
71 val ws = webSocket ?: return null
72 val jid = if (recipientJid.contains("@")) recipientJid else "[email protected]"
73 val id = WhatsAppProtocol.generateMessageId(authData?.wid)
74 val node = buildEncryptedTextNode(jid, id, body) ?: return null
75 pendingMessageIDs.add(id)
76 val sent = ws.send(WhatsAppProtocol.encodeNode(node))
77 if (!sent) pendingMessageIDs.remove(id)
78 return if (sent) "wa:$jid" else null
79}
80
81suspend fun WhatsAppClient.deleteThread(conversationId: String): Boolean {
82 val jid = extractJid(conversationId) ?: return false
83 db?.conversationDao()?.delete(jid)
84 eventsMutable.emit(WhatsAppEvent.ConversationDeleted(source, conversationId))
85 return true
86}
87
88suspend fun WhatsAppClient.markChatUnread(conversationId: String, unread: Boolean) {
89 if (stateMutable.value !is State.Connected) return
90 val ws = webSocket ?: return
91 val chatJid = extractJid(conversationId) ?: return
92
93 val patchNode = WhatsAppProtocol.Node(
94 tag = "iq",
95 attrs = mapOf(
96 "id" to WhatsAppProtocol.generateMessageId(authData?.wid),
97 "type" to "set",
98 "xmlns" to "w:sync:app:state",
99 "to" to "s.whatsapp.net",
100 ),
101 content = listOf(
102 WhatsAppProtocol.Node(
103 tag = "sync",
104 content = listOf(
105 WhatsAppProtocol.Node(
106 tag = "collection",
107 attrs = mapOf("name" to "regular"),
108 content = listOf(
109 WhatsAppProtocol.Node(
110 tag = "patch",
111 attrs = mapOf(
112 "jid" to chatJid,
113 "action" to "markRead",
114 ),
115 data = if (unread) "true".toByteArray() else "false".toByteArray(),
116 )
117 ),
118 )
119 ),
120 )
121 ),
122 )
123 ws.send(WhatsAppProtocol.encodeNode(patchNode))
124}
125
126suspend fun WhatsAppClient.setMute(conversationId: String, muteUntilMs: Long) {
127 if (stateMutable.value !is State.Connected) return
128 val ws = webSocket ?: return
129 val chatJid = extractJid(conversationId) ?: return
130
131 val patchNode = WhatsAppProtocol.Node(
132 tag = "iq",
133 attrs = mapOf(
134 "id" to WhatsAppProtocol.generateMessageId(authData?.wid),
135 "type" to "set",
136 "xmlns" to "w:sync:app:state",
137 "to" to "s.whatsapp.net",
138 ),
139 content = listOf(
140 WhatsAppProtocol.Node(
141 tag = "sync",
142 content = listOf(
143 WhatsAppProtocol.Node(
144 tag = "collection",
145 attrs = mapOf("name" to "regular"),
146 content = listOf(
147 WhatsAppProtocol.Node(
148 tag = "patch",
149 attrs = mapOf(
150 "jid" to chatJid,
151 "action" to "mute",
152 ),
153 data = muteUntilMs.toString().toByteArray(),
154 )
155 ),
156 )
157 ),
158 )
159 ),
160 )
161 ws.send(WhatsAppProtocol.encodeNode(patchNode))
162 val conv = db?.conversationDao()?.getConversation(chatJid)
163 if (conv != null) {
164 db?.conversationDao()?.upsert(conv.copy(muteEndTime = muteUntilMs))
165 }
166}
167
168suspend fun WhatsAppClient.togglePin(conversationId: String, pinned: Boolean) {
169 if (stateMutable.value !is State.Connected) return
170 val ws = webSocket ?: return
171 val chatJid = extractJid(conversationId) ?: return
172
173 val patchNode = WhatsAppProtocol.Node(
174 tag = "iq",
175 attrs = mapOf(
176 "id" to WhatsAppProtocol.generateMessageId(authData?.wid),
177 "type" to "set",
178 "xmlns" to "w:sync:app:state",
179 "to" to "s.whatsapp.net",
180 ),
181 content = listOf(
182 WhatsAppProtocol.Node(
183 tag = "sync",
184 content = listOf(
185 WhatsAppProtocol.Node(
186 tag = "collection",
187 attrs = mapOf("name" to "regular"),
188 content = listOf(
189 WhatsAppProtocol.Node(
190 tag = "patch",
191 attrs = mapOf(
192 "jid" to chatJid,
193 "action" to "pin",
194 ),
195 data = if (pinned) "true".toByteArray() else "false".toByteArray(),
196 )
197 ),
198 )
199 ),
200 )
201 ),
202 )
203 ws.send(WhatsAppProtocol.encodeNode(patchNode))
204 val conv = db?.conversationDao()?.getConversation(chatJid)
205 if (conv != null) {
206 db?.conversationDao()?.upsert(conv.copy(pinned = pinned))
207 }
208}