1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-23 00:57:13 +00:00

refactor and preparing to multilevel message entities

This commit is contained in:
InsanusMokrassar 2020-01-02 23:55:26 +06:00
parent e7495468a2
commit 482d924070
36 changed files with 137 additions and 106 deletions

View File

@ -2,6 +2,17 @@
## 0.21.0 TelegramBotAPI 4.5
* Added support of strikethrough and underline
* Added `UnderlineTextSource` and `UnderlineMessageEntity`
* Added `StrikethroughTextSource` and `StrikethroughMessageEntity`
* Added support in `RawMessageEntity`
* Now `TextSource` have its `rawSource` const
* `sourceString` in `MessageEntity` now is deprecated
* All `MessageEntity` classes now have no income parameter `sourceString`
* In most part of `TextSource` classes `asMarkdownSource` and `asHtmlSource` now are getters instead if fields
* All parseMode-specific functions in `StringFormatting` now are not `infix`
* Removed constructor of `TextMentionMessageEntity`, which was deprecated previously
* Removed constructor of `TextMentionTextSource`, which was deprecated previously
* All `TelegramMediaFile` instances now have field `fileUniqueId`, which represents `file_unique_id` field from API
* Now `ChatPhoto` have two additional fields: `smallFileUniqueId` and `bigFileUniqueId`
* Now any administrator object instance have `customTitle` nullable field
@ -11,10 +22,6 @@ bot.
## 0.20.0 MPP Migration
* Added support of strikethrough and underline
* Added `UnderlineTextSource` and `UnderlineMessageEntity`
* Added `StrikethroughTextSource` and `StrikethroughMessageEntity`
* Added support in `RawMessageEntity`
* Time library change: `joda-time` -> `com.soywiz.korlibs.klock:klock`
* `Currencied` now using as `currency` value with type `String`
* For `Java` there is `Currencied#javaCurrency` extension function, which will give an old currency work way

View File

@ -1,6 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts
interface TextSource {
val rawSource: String
val asMarkdownSource: String
val asHtmlSource: String
}

View File

@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.boldMarkdown
data class BoldTextMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity, TextSource by BoldTextSource(sourceString)
override val rawSource: String
) : MessageEntity, TextSource by BoldTextSource(rawSource)

View File

@ -8,8 +8,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.commandMarkdown
data class BotCommandMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String,
private val botCommandTextSource: BotCommandTextSource = BotCommandTextSource(sourceString)
override val rawSource: String,
private val botCommandTextSource: BotCommandTextSource = BotCommandTextSource(rawSource)
) : MessageEntity, TextSource by botCommandTextSource {
val command: String
get() = botCommandTextSource.command

View File

@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.codeMarkdown
data class CodeTextMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity, TextSource by CodeTextSource(sourceString)
override val rawSource: String
) : MessageEntity, TextSource by CodeTextSource(rawSource)

View File

@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.emailMarkdown
data class EMailMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity, TextSource by EMailTextSource(sourceString)
override val rawSource: String
) : MessageEntity, TextSource by EMailTextSource(rawSource)

View File

@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagMarkdown
data class HashTagMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity, TextSource by HashTagTextSource(sourceString)
override val rawSource: String
) : MessageEntity, TextSource by HashTagTextSource(rawSource)

View File

@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.italicMarkdown
data class ItalicTextMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity, TextSource by ItalicTextSource(sourceString)
override val rawSource: String
) : MessageEntity, TextSource by ItalicTextSource(rawSource)

View File

@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown
class MentionMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity, TextSource by MentionTextSource(sourceString)
override val rawSource: String
) : MessageEntity, TextSource by MentionTextSource(rawSource)

View File

@ -5,5 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
interface MessageEntity : TextSource {
val offset: Int
val length: Int
@Deprecated("Due to opportunity to get the same string from rawSource const", ReplaceWith("rawSource"))
val sourceString: String
get() = rawSource
}

View File

@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneMarkdown
data class PhoneNumberMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity, TextSource by PhoneNumberTextSource(sourceString)
override val rawSource: String
) : MessageEntity, TextSource by PhoneNumberTextSource(rawSource)

View File

@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.preMarkdown
data class PreTextMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity, TextSource by PreTextSource(sourceString)
override val rawSource: String
) : MessageEntity, TextSource by PreTextSource(rawSource)

View File

@ -29,7 +29,7 @@ internal data class RawMessageEntity(
"code" -> CodeTextMessageEntity(offset, length, sourceSubstring)
"pre" -> PreTextMessageEntity(offset, length, sourceSubstring)
"text_link" -> TextLinkMessageEntity(offset, length, sourceSubstring, url ?: throw IllegalStateException("URL must not be null for text link"))
"text_mention" -> TextMentionMessageEntity(offset, length, sourceSubstring, user as PrivateChat)
"text_mention" -> TextMentionMessageEntity(offset, length, sourceSubstring, user ?: throw IllegalStateException("User must not be null for text mention"))
"underline" -> UnderlineMessageEntity(offset, length, sourceSubstring)
"strikethrough" -> StrikethroughMessageEntity(offset, length, sourceSubstring)
else -> throw IllegalArgumentException("Unknown type of message entity")

View File

@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown
data class RegularTextMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity, TextSource by RegularTextSource(sourceString)
override val rawSource: String
) : MessageEntity, TextSource by RegularTextSource(rawSource)

View File

@ -6,5 +6,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsource
class StrikethroughMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity, TextSource by StrikethroughTextSource(sourceString)
override val rawSource: String
) : MessageEntity, TextSource by StrikethroughTextSource(rawSource)

View File

@ -8,6 +8,6 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown
data class TextLinkMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String,
override val rawSource: String,
val url: String
) : MessageEntity, TextSource by TextLinkTextSource(sourceString, url)
) : MessageEntity, TextSource by TextLinkTextSource(rawSource, url)

View File

@ -10,14 +10,6 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown
class TextMentionMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String,
override val rawSource: String,
val privateChat: PrivateChat
) : MessageEntity, TextSource by TextMentionTextSource(sourceString, privateChat) {
@Deprecated("Deprecated due to the fact that there is more common constructor")
constructor(
offset: Int,
length: Int,
sourceString: String,
user: User
) : this(offset, length, sourceString, user as PrivateChat)
}
) : MessageEntity, TextSource by TextMentionTextSource(rawSource, privateChat)

View File

@ -8,7 +8,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown
data class URLMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity, TextSource by URLTextSource(sourceString) {
val url: String = sourceString
override val rawSource: String
) : MessageEntity, TextSource by URLTextSource(rawSource) {
val url: String
get() = rawSource
}

View File

@ -6,5 +6,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsource
class UnderlineMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity, TextSource by UnderlineTextSource(sourceString)
override val rawSource: String
) : MessageEntity, TextSource by UnderlineTextSource(rawSource)

View File

@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.boldHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.boldMarkdown
class BoldTextSource(
sourceString: String
override val rawSource: String
) : TextSource {
override val asMarkdownSource: String = sourceString.boldMarkdown()
override val asHtmlSource: String = sourceString.boldHTML()
override val asMarkdownSource: String
get() = rawSource.boldMarkdown()
override val asHtmlSource: String
get() = rawSource.boldHTML()
}

View File

@ -7,12 +7,14 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.commandMarkdown
private val commandRegex = Regex("[/!][^@\\s]*")
class BotCommandTextSource(
sourceString: String
override val rawSource: String
) : TextSource {
override val asMarkdownSource: String = sourceString.commandMarkdown()
override val asHtmlSource: String = sourceString.commandHTML()
override val asMarkdownSource: String
get() = rawSource.commandMarkdown()
override val asHtmlSource: String
get() = rawSource.commandHTML()
val command: String by lazy {
commandRegex.find(sourceString) ?.value ?.substring(1) ?: sourceString.substring(1)// skip first symbol like "/" or "!"
commandRegex.find(rawSource) ?.value ?.substring(1) ?: rawSource.substring(1)// skip first symbol like "/" or "!"
}
}

View File

@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.codeHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.codeMarkdown
class CodeTextSource(
sourceString: String
override val rawSource: String
) : TextSource {
override val asMarkdownSource: String = sourceString.codeMarkdown()
override val asHtmlSource: String = sourceString.codeHTML()
override val asMarkdownSource: String
get() = rawSource.codeMarkdown()
override val asHtmlSource: String
get() = rawSource.codeHTML()
}

View File

@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.emailHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.emailMarkdown
class EMailTextSource(
sourceString: String
override val rawSource: String
) : TextSource {
override val asMarkdownSource: String = sourceString.emailMarkdown()
override val asHtmlSource: String = sourceString.emailHTML()
override val asMarkdownSource: String
get() = rawSource.emailMarkdown()
override val asHtmlSource: String
get() = rawSource.emailHTML()
}

View File

@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagMarkdown
class HashTagTextSource(
sourceString: String
override val rawSource: String
) : TextSource {
override val asMarkdownSource: String = sourceString.hashTagMarkdown()
override val asHtmlSource: String = sourceString.hashTagHTML()
override val asMarkdownSource: String
get() = rawSource.hashTagMarkdown()
override val asHtmlSource: String
get() = rawSource.hashTagHTML()
}

View File

@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.italicHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.italicMarkdown
class ItalicTextSource(
sourceString: String
override val rawSource: String
) : TextSource {
override val asMarkdownSource: String = sourceString.italicMarkdown()
override val asHtmlSource: String = sourceString.italicHTML()
override val asMarkdownSource: String
get() = rawSource.italicMarkdown()
override val asHtmlSource: String
get() = rawSource.italicHTML()
}

View File

@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown
class MentionTextSource(
sourceString: String
override val rawSource: String
) : TextSource {
override val asMarkdownSource: String = sourceString.mentionMarkdown()
override val asHtmlSource: String = sourceString.mentionHTML()
override val asMarkdownSource: String
get() = rawSource.mentionMarkdown()
override val asHtmlSource: String
get() = rawSource.mentionHTML()
}

View File

@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneMarkdown
class PhoneNumberTextSource(
sourceString: String
override val rawSource: String
) : TextSource {
override val asMarkdownSource: String = sourceString.phoneMarkdown()
override val asHtmlSource: String = sourceString.phoneHTML()
override val asMarkdownSource: String
get() = rawSource.phoneMarkdown()
override val asHtmlSource: String
get() = rawSource.phoneHTML()
}

View File

@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.preHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.preMarkdown
class PreTextSource(
sourceString: String
override val rawSource: String
) : TextSource {
override val asMarkdownSource: String = sourceString.preMarkdown()
override val asHtmlSource: String = sourceString.preHTML()
override val asMarkdownSource: String
get() = rawSource.preMarkdown()
override val asHtmlSource: String
get() = rawSource.preHTML()
}

View File

@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toHtml
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown
class RegularTextSource(
sourceString: String
override val rawSource: String
) : TextSource {
override val asMarkdownSource: String = sourceString.toMarkdown()
override val asHtmlSource: String = sourceString.toHtml()
override val asMarkdownSource: String
get() = rawSource.toMarkdown()
override val asHtmlSource: String
get() = rawSource.toHtml()
}

View File

@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.strikethroughHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.strikethroughMarkdown
class StrikethroughTextSource(
sourceString: String
override val rawSource: String
) : TextSource {
override val asHtmlSource: String = sourceString.strikethroughHTML()
override val asMarkdownSource: String = sourceString.strikethroughMarkdown()
override val asHtmlSource: String
get() = rawSource.strikethroughHTML()
override val asMarkdownSource: String
get() = rawSource.strikethroughMarkdown()
}

View File

@ -5,9 +5,9 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown
class TextLinkTextSource(
sourceString: String,
override val rawSource: String,
url: String
) : TextSource {
override val asMarkdownSource: String = sourceString.linkMarkdown(url)
override val asHtmlSource: String = sourceString.linkHTML(url)
override val asMarkdownSource: String = rawSource.linkMarkdown(url)
override val asHtmlSource: String = rawSource.linkHTML(url)
}

View File

@ -7,15 +7,9 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown
class TextMentionTextSource(
sourceString: String,
override val rawSource: String,
privateChat: PrivateChat
) : TextSource {
@Deprecated("Deprecated due to the fact that there is more common constructor")
constructor(
sourceString: String,
user: User
) : this(sourceString, user as PrivateChat)
override val asMarkdownSource: String = sourceString.mentionMarkdown(privateChat.id)
override val asHtmlSource: String = sourceString.mentionHTML(privateChat.id)
override val asMarkdownSource: String = rawSource.mentionMarkdown(privateChat.id)
override val asHtmlSource: String = rawSource.mentionHTML(privateChat.id)
}

View File

@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown
class URLTextSource(
sourceString: String
override val rawSource: String
) : TextSource{
override val asMarkdownSource: String = sourceString.linkMarkdown(sourceString)
override val asHtmlSource: String = sourceString.linkHTML(sourceString)
override val asMarkdownSource: String
get() = rawSource.linkMarkdown(rawSource)
override val asHtmlSource: String
get() = rawSource.linkHTML(rawSource)
}

View File

@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.underlineHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.underlineMarkdown
class UnderlineTextSource(
sourceString: String
override val rawSource: String
) : TextSource {
override val asMarkdownSource: String = sourceString.underlineMarkdown()
override val asHtmlSource: String = sourceString.underlineHTML()
override val asMarkdownSource: String
get() = rawSource.underlineMarkdown()
override val asHtmlSource: String
get() = rawSource.underlineHTML()
}

View File

@ -14,12 +14,14 @@ const val htmlBoldControl = "b"
const val htmlItalicControl = "i"
const val htmlCodeControl = "code"
const val htmlPreControl = "pre"
const val htmlUnderlineControl = "u"
const val htmlStrikethroughControl = "s"
private infix fun String.markdownDefault(controlSymbol: String) = "$controlSymbol$this$controlSymbol"
private infix fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>$this</$controlSymbol>"
private fun String.markdownDefault(controlSymbol: String) = "$controlSymbol$this$controlSymbol"
private fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>$this</$controlSymbol>"
infix fun String.linkMarkdown(link: String): String = "[$this]($link)"
infix fun String.linkHTML(link: String): String = "<a href=\"$link\">$this</a>"
fun String.linkMarkdown(link: String): String = "[$this]($link)"
fun String.linkHTML(link: String): String = "<a href=\"$link\">$this</a>"
fun String.boldMarkdown(): String = markdownDefault(markdownBoldControl)
@ -45,32 +47,32 @@ fun String.emailHTML(): String = linkHTML("mailto://$this")
* Crutch for support of strikethrough in default markdown. Simply add modifier, but it will not look like correct
*/
fun String.strikethroughMarkdown(): String = map { it + "\u0336" }.joinToString("")
fun String.strikethroughHTML(): String = htmlDefault("s")
fun String.strikethroughHTML(): String = htmlDefault(htmlStrikethroughControl)
/**
* Crutch for support of underline in default markdown. Simply add modifier, but it will not look like correct
*/
fun String.underlineMarkdown(): String = map { it + "\u0347" }.joinToString("")
fun String.underlineHTML(): String = htmlDefault("u")
fun String.underlineHTML(): String = htmlDefault(htmlUnderlineControl)
private inline infix fun String.mention(adapt: String.() -> String): String = if (startsWith("@")) {
private inline fun String.mention(adapt: String.() -> String): String = if (startsWith("@")) {
this
} else {
"@${adapt()}"
}
private inline infix fun String.hashTag(adapt: String.() -> String): String = if (startsWith("#")) {
private inline fun String.hashTag(adapt: String.() -> String): String = if (startsWith("#")) {
this
} else {
"#${adapt()}"
}
infix fun String.mentionMarkdown(userId: UserId): String = linkMarkdown(userId.link)
infix fun String.mentionHTML(userId: UserId): String = linkHTML(userId.link)
fun String.mentionMarkdown(userId: UserId): String = linkMarkdown(userId.link)
fun String.mentionHTML(userId: UserId): String = linkHTML(userId.link)
fun String.mentionMarkdown(): String = mention(String::toMarkdown)

View File

@ -7,6 +7,12 @@ fun String.toMarkdown(): String {
).replace(
"_",
"\\_"
).replace(
"`",
"\\`"
).replace(
"[",
"\\["
)
}