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 / whatsapp / WhatsAppEvent.kt

WhatsAppEvent.kt292 lines · 10.1 KiBRaw
1package com.vayunmathur.communicate.data.whatsapp
2
3/**
4 * communicate-native event surface for the WhatsApp primary client. Replaces the messages-module
5 * `GMEvent` bus: the ported [WhatsAppClient] emits these instead, so `communicate` has no dependency
6 * on `messages`. The message-bearing subtypes mirror the original `GMEvent` shapes verbatim (so the
7 * port is a mechanical rename), plus primary-client additions: [StateChanged], [HistorySync],
8 * [PresenceUpdate].
9 */
10sealed interface WhatsAppEvent {
11 val source: MessageSource
12
13 /** Transport/login lifecycle change. */
14 data class StateChanged(
15 override val source: MessageSource = MessageSource.WHATSAPP,
16 val state: WhatsAppState,
17 val detail: String? = null,
18 ) : WhatsAppEvent
19
20 /** A conversation-list row changed: latest preview, unread count and peer metadata. */
21 data class ConversationUpdate(
22 override val source: MessageSource = MessageSource.WHATSAPP,
23 val conversationId: String,
24 val peerName: String?,
25 val peerPhone: String?,
26 val avatarUrl: String?,
27 val lastPreview: String?,
28 val lastTimestamp: Long,
29 val unreadCount: Int,
30 val isGroup: Boolean = false,
31 val participantCount: Int = 0,
32 val conversationType: String? = null,
33 val outgoingId: String? = null,
34 val serviceData: String? = null,
35 val isMessageRequest: Boolean = false,
36 ) : WhatsAppEvent
37
38 /** A message row changed (new, edited or status-updated) in a conversation. */
39 data class MessageUpdate(
40 override val source: MessageSource = MessageSource.WHATSAPP,
41 val conversationId: String,
42 val messageId: String,
43 val body: String,
44 val outgoing: Boolean,
45 val timestamp: Long,
46 val senderName: String?,
47 val senderId: String? = null,
48 val reactionsJson: String? = null,
49 val mediaData: ByteArray? = null,
50 val mediaMime: String? = null,
51 val mediaName: String? = null,
52 val statusType: String? = null,
53 val serviceData: String? = null,
54 val attachments: List<MessageAttachment> = emptyList(),
55 ) : WhatsAppEvent
56
57 /** Inbound message (was GMEvent.IncomingMessage). */
58 data class IncomingMessage(
59 override val source: MessageSource = MessageSource.WHATSAPP,
60 val conversationId: String,
61 val messageId: String,
62 val body: String,
63 val peerName: String?,
64 val peerPhone: String?,
65 val timestamp: Long,
66 val senderName: String? = null,
67 val senderId: String? = null,
68 val attachments: List<MessageAttachment> = emptyList(),
69 val serviceData: String? = null,
70 val pollQuestion: String? = null,
71 val pollOptions: List<String> = emptyList(),
72 ) : WhatsAppEvent
73
74 /** Message revoked/deleted (was GMEvent.MessageDeleted). */
75 data class MessageDeleted(
76 override val source: MessageSource = MessageSource.WHATSAPP,
77 val messageId: String,
78 val conversationId: String? = null,
79 val timestamp: Long = 0L,
80 ) : WhatsAppEvent
81
82 /** A message was edited; the row now shows the replacement body. */
83 data class MessageEdited(
84 override val source: MessageSource = MessageSource.WHATSAPP,
85 val conversationId: String? = null,
86 val messageId: String,
87 val newBody: String,
88 val timestamp: Long = 0L,
89 ) : WhatsAppEvent
90
91 /** A delivery or read receipt for one of our sent messages. */
92 data class ReadReceipt(
93 override val source: MessageSource = MessageSource.WHATSAPP,
94 val conversationId: String,
95 val messageId: String? = null,
96 val senderId: String? = null,
97 val timestampMs: Long = 0L,
98 val timestamp: Long = 0L,
99 val isDelivery: Boolean = false,
100 ) : WhatsAppEvent
101
102 /** A conversation was deleted. */
103 data class ConversationDeleted(
104 override val source: MessageSource = MessageSource.WHATSAPP,
105 val conversationId: String,
106 ) : WhatsAppEvent
107
108 /** Typing indicator (was GMEvent.TypingIndicator). */
109 data class TypingIndicator(
110 override val source: MessageSource = MessageSource.WHATSAPP,
111 val conversationId: String,
112 val senderId: String,
113 val isTyping: Boolean,
114 ) : WhatsAppEvent
115
116 /** An emoji reaction was added to a message. */
117 data class ReactionReceived(
118 override val source: MessageSource = MessageSource.WHATSAPP,
119 val conversationId: String,
120 val messageId: String,
121 val senderId: String,
122 val emoji: String,
123 ) : WhatsAppEvent
124
125 /** An emoji reaction was removed from a message. */
126 data class ReactionRemoved(
127 override val source: MessageSource = MessageSource.WHATSAPP,
128 val conversationId: String,
129 val messageId: String,
130 val senderId: String,
131 ) : WhatsAppEvent
132
133 /** A vote was cast on a poll message. */
134 data class PollVote(
135 override val source: MessageSource = MessageSource.WHATSAPP,
136 val conversationId: String,
137 val pollMessageId: String,
138 val voterId: String,
139 val optionNames: List<String>,
140 ) : WhatsAppEvent
141
142 /** A conversation (group) was renamed. */
143 data class ConversationNameChanged(
144 override val source: MessageSource = MessageSource.WHATSAPP,
145 val conversationId: String,
146 val newName: String,
147 ) : WhatsAppEvent
148
149 /** A conversation avatar changed. */
150 data class ConversationAvatarChanged(
151 override val source: MessageSource = MessageSource.WHATSAPP,
152 val conversationId: String,
153 val avatarUrl: String?,
154 ) : WhatsAppEvent
155
156 /** A participant joined a group conversation. */
157 data class ParticipantAdded(
158 override val source: MessageSource = MessageSource.WHATSAPP,
159 val conversationId: String,
160 val participantId: String,
161 ) : WhatsAppEvent
162
163 /** A participant left or was removed from a group conversation. */
164 data class ParticipantRemoved(
165 override val source: MessageSource = MessageSource.WHATSAPP,
166 val conversationId: String,
167 val participantId: String,
168 ) : WhatsAppEvent
169
170 /** A conversation mute setting changed; muteExpireTimeMs is when the mute lapses. */
171 data class MuteSettingChanged(
172 override val source: MessageSource = MessageSource.WHATSAPP,
173 val conversationId: String,
174 val muteExpireTimeMs: Long,
175 ) : WhatsAppEvent
176
177 /** A message request arrived from an unknown sender. */
178 data class MessageRequestReceived(
179 override val source: MessageSource = MessageSource.WHATSAPP,
180 val conversationId: String,
181 ) : WhatsAppEvent
182
183 /** Sending a message failed. */
184 data class SendFailed(
185 override val source: MessageSource = MessageSource.WHATSAPP,
186 val conversationId: String,
187 val messageId: String? = null,
188 val tmpId: String? = null,
189 val errorMessage: String,
190 ) : WhatsAppEvent
191
192 /** An inbound message could not be decrypted (session or identity problem). */
193 data class DecryptionError(
194 override val source: MessageSource = MessageSource.WHATSAPP,
195 val conversationId: String,
196 val senderAci: String,
197 val senderDeviceId: Int,
198 val timestamp: Long,
199 val errorMessage: String? = null,
200 ) : WhatsAppEvent
201
202 /** The primary line was logged out server-side (was GMEvent.SourceLoggedOut). */
203 data class SourceLoggedOut(
204 override val source: MessageSource = MessageSource.WHATSAPP,
205 val reason: String? = null,
206 ) : WhatsAppEvent
207
208 /** Presence/last-seen update for a peer. */
209 data class PresenceUpdate(
210 override val source: MessageSource = MessageSource.WHATSAPP,
211 val conversationId: String,
212 val isOnline: Boolean,
213 val lastSeen: Long = 0L,
214 ) : WhatsAppEvent
215
216 /** A batch of history from the server's initial/on-demand history sync. */
217 data class HistorySync(
218 override val source: MessageSource = MessageSource.WHATSAPP,
219 val conversations: List<WhatsAppHistoryConversation>,
220 ) : WhatsAppEvent
221
222 // ---- Calling (Phase D/E) ----
223
224 /** An inbound call offer arrived (ringing). */
225 data class CallOffer(
226 override val source: MessageSource = MessageSource.WHATSAPP,
227 val callId: String,
228 val from: String,
229 val callCreator: String,
230 val isVideo: Boolean,
231 val peerName: String? = null,
232 val timestamp: Long = System.currentTimeMillis(),
233 ) : WhatsAppEvent
234
235 /** A call's lifecycle phase changed (dialing/ringing/active/…). [phase] is a WhatsAppCallPhase name. */
236 data class CallStateChanged(
237 override val source: MessageSource = MessageSource.WHATSAPP,
238 val callId: String,
239 val phase: String,
240 val isVideo: Boolean = false,
241 ) : WhatsAppEvent
242
243 /** A call ended. [reason] mirrors the terminate reason (or a local cause). */
244 data class CallEnded(
245 override val source: MessageSource = MessageSource.WHATSAPP,
246 val callId: String,
247 val reason: String,
248 val durationSeconds: Long = 0L,
249 ) : WhatsAppEvent
250}
251
252/** Transport/login lifecycle states for the primary client. */
253enum class WhatsAppState {
254 Disconnected,
255 Connecting,
256 Registering,
257 AwaitingCode,
258 Connected,
259 Syncing,
260 Ready,
261}
262
263/** Decoded media descriptor (keys + url) for on-demand download. */
264data class WhatsAppMediaInfo(
265 val url: String,
266 val directPath: String?,
267 val mediaKey: ByteArray,
268 val mimeType: String?,
269 val fileLength: Long,
270 val fileSha256: ByteArray?,
271 val fileEncSha256: ByteArray?,
272 val mediaType: String,
273)
274
275/** One conversation's worth of history-sync payload. */
276data class WhatsAppHistoryConversation(
277 val conversationId: String,
278 val name: String?,
279 val isGroup: Boolean,
280 val messages: List<WhatsAppHistoryMessage>,
281)
282
283/** One message inside a history-sync conversation payload. */
284data class WhatsAppHistoryMessage(
285 val messageId: String,
286 val body: String,
287 val timestamp: Long,
288 val outgoing: Boolean,
289 val senderId: String?,
290 val senderName: String?,
291 val serviceData: String? = null,
292)