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 / WhatsAppServiceData.kt

WhatsAppServiceData.kt54 lines · 1.8 KiBRaw
1package com.vayunmathur.communicate.data.whatsapp
2
3import kotlinx.serialization.Serializable
4import kotlinx.serialization.encodeToString
5import kotlinx.serialization.json.Json
6
7/**
8 * Rich-feature payload serialized into `SmsMessage.serviceData` (and the cached-message row) so the
9 * WhatsApp line can carry reactions, polls, edits, revokes, quoted replies and group metadata
10 * through the SIM/GV-shaped models without changing those paths. The UI ([ConversationScreen])
11 * parses this back out to render the extras.
12 */
13@Serializable
14data class WhatsAppServiceData(
15 val senderName: String? = null,
16 val senderJid: String? = null,
17 val reactions: List<Reaction> = emptyList(),
18 val isEdited: Boolean = false,
19 val isRevoked: Boolean = false,
20 // Poll
21 val pollQuestion: String? = null,
22 val pollOptions: List<PollOptionData> = emptyList(),
23 // Quoted reply
24 val quotedMessageId: String? = null,
25 val quotedBody: String? = null,
26 val quotedSender: String? = null,
27 // Group
28 val isGroup: Boolean = false,
29 val groupParticipantCount: Int = 0,
30 // Media
31 val mediaUrl: String? = null,
32 val mediaMime: String? = null,
33 val mediaName: String? = null,
34) {
35 fun serialize(): String = JSON.encodeToString(this)
36
37 /** The JSON codec and parser entry point for [WhatsAppServiceData]. */
38 companion object {
39 private val JSON = Json { ignoreUnknownKeys = true; encodeDefaults = true }
40
41 fun parse(json: String?): WhatsAppServiceData? =
42 if (json.isNullOrBlank()) null else runCatching {
43 JSON.decodeFromString<WhatsAppServiceData>(json)
44 }.getOrNull()
45 }
46}
47
48/** One option of a WhatsApp poll, with its running vote tally. */
49@Serializable
50data class PollOptionData(
51 val name: String,
52 val voteCount: Int = 0,
53 val voters: List<String> = emptyList(),
54)