Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / WhatsAppFormatText.kt
WhatsAppFormatText.kt177 lines · 6.6 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.whatsapp.proto.WhatsAppE2EProto |
| 4 | |
| 5 | /** |
| 6 | * Message formatting (split from WhatsAppProtocolParsing.kt for file length). |
| 7 | * Behavior identical, call sites unchanged. |
| 8 | */ |
| 9 | |
| 10 | internal fun WhatsAppProtocol.formatDisappearingTimer(seconds: Int): String { |
| 11 | return when { |
| 12 | seconds >= SECONDS_90_DAYS -> "90 days" |
| 13 | seconds >= SECONDS_PER_WEEK -> "7 days" |
| 14 | seconds >= SECONDS_PER_DAY -> "${seconds / SECONDS_PER_DAY} days" |
| 15 | seconds >= SECONDS_PER_HOUR -> "${seconds / SECONDS_PER_HOUR} hours" |
| 16 | seconds >= SECONDS_PER_MINUTE -> "${seconds / SECONDS_PER_MINUTE} minutes" |
| 17 | else -> "$seconds seconds" |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | internal fun WhatsAppProtocol.extractContextInfo( |
| 22 | e2eMessage: WhatsAppE2EProto.Message |
| 23 | ): ContextInfoResult { |
| 24 | val ctx = when { |
| 25 | e2eMessage.hasExtendedTextMessage() -> e2eMessage.extendedTextMessage.contextInfo |
| 26 | e2eMessage.hasImageMessage() -> e2eMessage.imageMessage.contextInfo |
| 27 | e2eMessage.hasVideoMessage() -> e2eMessage.videoMessage.contextInfo |
| 28 | e2eMessage.hasAudioMessage() -> e2eMessage.audioMessage.contextInfo |
| 29 | e2eMessage.hasDocumentMessage() -> e2eMessage.documentMessage.contextInfo |
| 30 | e2eMessage.hasStickerMessage() -> e2eMessage.stickerMessage.contextInfo |
| 31 | e2eMessage.hasLocationMessage() -> e2eMessage.locationMessage.contextInfo |
| 32 | e2eMessage.hasContactMessage() -> e2eMessage.contactMessage.contextInfo |
| 33 | else -> null |
| 34 | } ?: return ContextInfoResult() |
| 35 | |
| 36 | return ContextInfoResult( |
| 37 | isForwarded = ctx.isForwarded, |
| 38 | forwardingScore = ctx.forwardingScore, |
| 39 | replyToId = ctx.stanzaId.ifEmpty { null }, |
| 40 | mentionedJids = ctx.mentionedJidList.orEmpty(), |
| 41 | ) |
| 42 | } |
| 43 | |
| 44 | // WA text formatting regexes (from Go wa-text.go) |
| 45 | private val waBoldRegex = Regex("(?<=[\\s>_~]|^)\\*(.+?)\\*(?=[^a-zA-Z\\d]|$)") |
| 46 | private val waItalicRegex = Regex("(?<=[\\s>~*]|^)_(.+?)_(?=[^a-zA-Z\\d]|$)") |
| 47 | private val waStrikethroughRegex = Regex("(?<=[\\s>_*]|^)~(.+?)~(?=[^a-zA-Z\\d]|$)") |
| 48 | private val waInlineCodeRegex = Regex("(?<=[\\s>_*~]|^)`(.+?)`(?=[^a-zA-Z\\d]|$)") |
| 49 | private val waOrderedListRegex = Regex("(?m)^(\\d{1,2})\\. ") |
| 50 | private val waBulletedListRegex = Regex("(?m)^( *)\\* ") |
| 51 | private val waBlockquoteRegex = Regex("(?m)^> ") |
| 52 | private val waInlineURLRegex = Regex("\\[(.+?)]\\((.+?)\\)") |
| 53 | |
| 54 | fun WhatsAppProtocol.convertWAFormattingToHtml(text: String): String { |
| 55 | val sb = StringBuilder() |
| 56 | var remaining = text |
| 57 | while (true) { |
| 58 | val start = remaining.indexOf("```") |
| 59 | val end = if (start == -1) -1 else remaining.indexOf("```", start + FENCE_LENGTH) |
| 60 | if (start == -1 || end == -1) break |
| 61 | val before = remaining.substring(0, start) |
| 62 | val code = remaining.substring(start + FENCE_LENGTH, end) |
| 63 | remaining = remaining.substring(end + FENCE_LENGTH) |
| 64 | sb.append(formatInlineWA(before)) |
| 65 | if (code.contains('\n')) { |
| 66 | sb.append("<pre><code>").append(escapeHtml(code)).append("</code></pre>") |
| 67 | } else { |
| 68 | sb.append("<code>").append(escapeHtml(code)).append("</code>") |
| 69 | } |
| 70 | } |
| 71 | sb.append(formatInlineWA(remaining)) |
| 72 | return sb.toString() |
| 73 | } |
| 74 | |
| 75 | private fun WhatsAppProtocol.escapeHtml(text: String): String = |
| 76 | text.replace("&", "&").replace("<", "<").replace(">", ">") |
| 77 | |
| 78 | private fun WhatsAppProtocol.formatInlineWA(text: String): String { |
| 79 | var result = escapeHtml(text) |
| 80 | |
| 81 | // Blockquotes (Go parseWAFormattingToHTML blockquote handling) |
| 82 | result = processBlockquotes(result) |
| 83 | |
| 84 | // Ordered lists (Go orderedListRegex) |
| 85 | result = processOrderedLists(result) |
| 86 | |
| 87 | // Bulleted lists — must come after bold since * is used for both |
| 88 | result = processBulletedLists(result) |
| 89 | |
| 90 | result = waBoldRegex.replace(result) { "<b>${it.groupValues[1]}</b>" } |
| 91 | result = waItalicRegex.replace(result) { "<i>${it.groupValues[1]}</i>" } |
| 92 | result = waStrikethroughRegex.replace(result) { "<s>${it.groupValues[1]}</s>" } |
| 93 | result = waInlineCodeRegex.replace(result) { "<code>${it.groupValues[1]}</code>" } |
| 94 | |
| 95 | // Inline URLs (Go inlineURLRegex) |
| 96 | result = waInlineURLRegex.replace(result) { "<a href=\"${it.groupValues[2]}\">${it.groupValues[1]}</a>" } |
| 97 | |
| 98 | result = result.replace("\n", "<br>") |
| 99 | return result |
| 100 | } |
| 101 | |
| 102 | private fun WhatsAppProtocol.processBlockquotes(text: String): String { |
| 103 | val lines = text.split("\n") |
| 104 | val result = StringBuilder() |
| 105 | var inBlockquote = false |
| 106 | for (line in lines) { |
| 107 | if (line.startsWith("> ")) { |
| 108 | if (!inBlockquote) { |
| 109 | result.append("<blockquote>") |
| 110 | inBlockquote = true |
| 111 | } else { |
| 112 | result.append("<br>") |
| 113 | } |
| 114 | result.append(line.removePrefix("> ")) |
| 115 | } else { |
| 116 | if (inBlockquote) { |
| 117 | result.append("</blockquote>") |
| 118 | inBlockquote = false |
| 119 | } |
| 120 | if (result.isNotEmpty()) result.append("\n") |
| 121 | result.append(line) |
| 122 | } |
| 123 | } |
| 124 | if (inBlockquote) result.append("</blockquote>") |
| 125 | return result.toString() |
| 126 | } |
| 127 | |
| 128 | private fun WhatsAppProtocol.processOrderedLists(text: String): String { |
| 129 | val lines = text.split("\n") |
| 130 | val result = StringBuilder() |
| 131 | var inList = false |
| 132 | for (line in lines) { |
| 133 | val match = Regex("^(\\d{1,2})\\. (.*)").find(line) |
| 134 | if (match != null) { |
| 135 | val listNumber = match.groupValues[1].toIntOrNull() ?: 1 |
| 136 | if (!inList) { |
| 137 | result.append("<ol start=\"$listNumber\">") |
| 138 | inList = true |
| 139 | } |
| 140 | result.append("<li value=\"$listNumber\">").append(match.groupValues[2]).append("</li>") |
| 141 | } else { |
| 142 | if (inList) { |
| 143 | result.append("</ol>") |
| 144 | inList = false |
| 145 | } |
| 146 | if (result.isNotEmpty()) result.append("\n") |
| 147 | result.append(line) |
| 148 | } |
| 149 | } |
| 150 | if (inList) result.append("</ol>") |
| 151 | return result.toString() |
| 152 | } |
| 153 | |
| 154 | private fun WhatsAppProtocol.processBulletedLists(text: String): String { |
| 155 | val lines = text.split("\n") |
| 156 | val result = StringBuilder() |
| 157 | var inList = false |
| 158 | for (line in lines) { |
| 159 | val match = Regex("^(?:\\* |- )(.*)").find(line) |
| 160 | if (match != null) { |
| 161 | if (!inList) { |
| 162 | result.append("<ul>") |
| 163 | inList = true |
| 164 | } |
| 165 | result.append("<li>").append(match.groupValues[1]).append("</li>") |
| 166 | } else { |
| 167 | if (inList) { |
| 168 | result.append("</ul>") |
| 169 | inList = false |
| 170 | } |
| 171 | if (result.isNotEmpty()) result.append("\n") |
| 172 | result.append(line) |
| 173 | } |
| 174 | } |
| 175 | if (inList) result.append("</ul>") |
| 176 | return result.toString() |
| 177 | } |