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

WhatsAppGroupBodies.kt71 lines · 2.9 KiBRaw
1package com.vayunmathur.communicate.data.whatsapp
2
3/**
4 * Group-notification bodies (split from WhatsAppClientInbound.kt for file length).
5 * Behavior identical, call sites unchanged.
6 */
7
8/** Human-readable body for one group-change child, or null when unhandled. */
9internal fun groupChangeBody(child: WhatsAppProtocol.Node): String? {
10 return when (child.tag) {
11 "subject" -> subjectBody(child)
12 "description" -> descriptionBody(child)
13 "add", "remove", "promote", "demote" -> membershipBody(child)
14 // Go wrapGroupInfoChange: ephemeral setting
15 "ephemeral" -> ephemeralBody(child)
16 // Go wrapGroupInfoChange: announce mode
17 "announce" -> announceBody(child)
18 // Go wrapGroupInfoChange: locked (restrict edit to admins)
19 "locked" -> lockedBody(child)
20 // Go wrapGroupInfoChange: link/unlink community
21 "link" -> "[Group linked: ${child.attrs["link_type"]}]"
22 "unlink" -> "[Group unlinked: ${child.attrs["unlink_type"]}]"
23 else -> null
24 }
25}
26
27/** Subject-change body. */
28private fun subjectBody(child: WhatsAppProtocol.Node): String? {
29 val newName = child.attrs["subject"] ?: child.data?.let { String(it, Charsets.UTF_8) }
30 return newName?.let { "[Group name changed to: $it]" }
31}
32
33/** Description-change body. */
34private fun descriptionBody(child: WhatsAppProtocol.Node): String {
35 val newDesc = child.data?.let { String(it, Charsets.UTF_8) } ?: ""
36 return "[Group description changed: $newDesc]"
37}
38
39/** Membership-change body. */
40private fun membershipBody(child: WhatsAppProtocol.Node): String {
41 val participants = child.content.filterIsInstance<WhatsAppProtocol.Node>()
42 .mapNotNull { it.attrs["jid"] }
43 return "[Group: ${participants.joinToString()} ${child.tag}ed]"
44}
45
46/** Announce-mode body. */
47private fun announceBody(child: WhatsAppProtocol.Node): String {
48 val isAnnounce = child.attrs["announce"] == "true" || child.attrs["value"] == "on"
49 return if (isAnnounce) "[Only admins can send messages now]"
50 else "[All participants can send messages now]"
51}
52
53/** Locked-state body. */
54private fun lockedBody(child: WhatsAppProtocol.Node): String {
55 val isLocked = child.attrs["locked"] == "true" || child.attrs["value"] == "on"
56 return if (isLocked) "[Only admins can edit group info now]"
57 else "[All participants can edit group info now]"
58}
59
60/** Ephemeral-setting change body. */
61internal fun ephemeralBody(child: WhatsAppProtocol.Node): String {
62 val expiration = child.attrs["expiration"]?.toLongOrNull() ?: 0
63 if (expiration <= 0) return "[Disappearing messages turned off]"
64 val duration = when {
65 expiration >= SECONDS_PER_WEEK -> "${expiration / SECONDS_PER_WEEK} week(s)"
66 expiration >= SECONDS_PER_DAY -> "${expiration / SECONDS_PER_DAY} day(s)"
67 expiration >= SECONDS_PER_HOUR -> "${expiration / SECONDS_PER_HOUR} hour(s)"
68 else -> "$expiration seconds"
69 }
70 return "[Disappearing messages set to $duration]"
71}