1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +00:00

reworking of links extension

This commit is contained in:
InsanusMokrassar 2020-11-16 13:22:08 +06:00
parent af1d6d6f1a
commit 6650dd9cfe
3 changed files with 71 additions and 34 deletions

View File

@ -10,9 +10,7 @@ import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.* import kotlinx.serialization.json.*
@Serializable(UserSerializer::class) @Serializable(UserSerializer::class)
sealed class User : PrivateChat { sealed class User : PrivateChat
abstract override val id: UserId
}
@Serializable @Serializable
data class CommonUser( data class CommonUser(

View File

@ -1,10 +1,13 @@
package dev.inmo.tgbotapi.types.chat.abstracts package dev.inmo.tgbotapi.types.chat.abstracts
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.UserId
import dev.inmo.tgbotapi.types.chat.PreviewChatSerializer import dev.inmo.tgbotapi.types.chat.PreviewChatSerializer
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable(PreviewChatSerializer::class) @Serializable(PreviewChatSerializer::class)
interface PrivateChat : Chat, UsernameChat { interface PrivateChat : Chat, UsernameChat {
override val id: UserId
val firstName: String val firstName: String
val lastName: String val lastName: String
} }

View File

@ -1,13 +1,20 @@
package dev.inmo.tgbotapi.extensions.utils.formatting package dev.inmo.tgbotapi.extensions.utils.formatting
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.MessageEntity.textsources.link
import dev.inmo.tgbotapi.types.ParseMode.* import dev.inmo.tgbotapi.types.ParseMode.*
import dev.inmo.tgbotapi.types.chat.abstracts.PrivateChat import dev.inmo.tgbotapi.types.chat.abstracts.*
import dev.inmo.tgbotapi.types.chat.abstracts.UsernameChat
import dev.inmo.tgbotapi.types.chat.abstracts.extended.ExtendedChat import dev.inmo.tgbotapi.types.chat.abstracts.extended.ExtendedChat
import dev.inmo.tgbotapi.types.chat.abstracts.extended.ExtendedPublicChat
import dev.inmo.tgbotapi.types.message.abstracts.Message
private const val internalLinkBeginning = "https://t.me" private const val internalLinkBeginning = "https://t.me"
fun makeLink(username: String) = "$internalLinkBeginning/$username"
inline val Username.link
get() = makeLink(username)
inline fun makeLink(username: Username) = username.link
fun makeLinkToMessage( fun makeLinkToMessage(
username: String, username: String,
messageId: MessageIdentifier messageId: MessageIdentifier
@ -24,8 +31,12 @@ fun makeLinkToMessage(
private val linkIdRedundantPartRegex = Regex("^-100") private val linkIdRedundantPartRegex = Regex("^-100")
private val usernameBeginSymbolRegex = Regex("^@") private val usernameBeginSymbolRegex = Regex("^@")
/**
* Link which can be used as by any user to get access to [Message]. Returns null in case when there are no
* known way to build link (for [PrivateChat]s, for example)
*/
fun makeLinkToMessage( fun makeLinkToMessage(
chat: ExtendedChat, chat: Chat,
messageId: MessageIdentifier messageId: MessageIdentifier
): String? { ): String? {
return when { return when {
@ -43,8 +54,55 @@ fun makeLinkToMessage(
} }
} }
/**
* @see makeLinkToMessage
*/
val Message.link: String?
get() = makeLinkToMessage(
chat,
messageId
)
/**
* Link which can be used as by any user to get access to [Chat]. Returns null in case when there are no
* known way to build link
*/
val Chat.link: String?
get() {
if (this is UsernameChat) {
username ?.username ?.let { return it }
}
if (this is ExtendedPublicChat) {
inviteLink ?.let { return it }
}
if (this is PrivateChat) {
return id.link
}
return null
}
private const val stickerSetAddingLinkPrefix = "$internalLinkBeginning/addstickers" private const val stickerSetAddingLinkPrefix = "$internalLinkBeginning/addstickers"
val StickerSetName.stickerSetLink
get() = link(this, "$stickerSetAddingLinkPrefix/$this")
/**
* @return Link for adding of sticker set with name [stickerSetName] with formatting for [MarkdownV2]
*/
@Deprecated("Use extension `stickerSetLink` + getting of `asMarkdownV2Source` property")
fun makeLinkToAddStickerSetInMarkdownV2(
stickerSetName: StickerSetName
) = stickerSetName.stickerSetLink.asMarkdownV2Source
/**
* @return Link for adding of sticker set with name [stickerSetName] with formatting for [Markdown]
*/
@Deprecated("Use extension `stickerSetLink` + getting of `asMarkdownSource` property")
fun makeLinkToAddStickerSetInMarkdown(stickerSetName: StickerSetName) = stickerSetName.stickerSetLink.asMarkdownSource
/**
* @return Link for adding of sticker set with name [stickerSetName] with formatting for [HTML]
*/
@Deprecated("Use extension `stickerSetLink` + getting of `asHtmlSource` property")
fun makeLinkToAddStickerSetInHtml(stickerSetName: StickerSetName) = stickerSetName.stickerSetLink.asHtmlSource
/** /**
* Create a link for adding of sticker set with name [stickerSetName]. Was added thanks to user Djaler and based on * Create a link for adding of sticker set with name [stickerSetName]. Was added thanks to user Djaler and based on
* https://github.com/Djaler/evil-bot/blob/master/src/main/kotlin/com/github/djaler/evilbot/utils/StickerUtils.kt#L6-L8 * https://github.com/Djaler/evil-bot/blob/master/src/main/kotlin/com/github/djaler/evilbot/utils/StickerUtils.kt#L6-L8
@ -53,34 +111,12 @@ private const val stickerSetAddingLinkPrefix = "$internalLinkBeginning/addsticke
* @see [makeLinkToAddStickerSetInMarkdown] * @see [makeLinkToAddStickerSetInMarkdown]
* @see [makeLinkToAddStickerSetInHtml] * @see [makeLinkToAddStickerSetInHtml]
*/ */
@Deprecated("Use extension `stickerSetLink` + getting of required property")
fun makeLinkToAddStickerSet( fun makeLinkToAddStickerSet(
stickerSetName: StickerSetName, stickerSetName: StickerSetName,
parseMode: ParseMode parseMode: ParseMode
) = (stickerSetName to "$stickerSetAddingLinkPrefix/$stickerSetName").link( ) = when (parseMode) {
parseMode MarkdownParseMode -> makeLinkToAddStickerSetInMarkdown(stickerSetName)
) MarkdownV2ParseMode -> makeLinkToAddStickerSetInMarkdownV2(stickerSetName)
HTMLParseMode -> makeLinkToAddStickerSetInHtml(stickerSetName)
/** }
* @return Link for adding of sticker set with name [stickerSetName] with formatting for [MarkdownV2]
*/
fun makeLinkToAddStickerSetInMarkdownV2(stickerSetName: StickerSetName) =
makeLinkToAddStickerSet(
stickerSetName,
MarkdownV2
)
/**
* @return Link for adding of sticker set with name [stickerSetName] with formatting for [Markdown]
*/
fun makeLinkToAddStickerSetInMarkdown(stickerSetName: StickerSetName) =
makeLinkToAddStickerSet(
stickerSetName,
Markdown
)
/**
* @return Link for adding of sticker set with name [stickerSetName] with formatting for [HTML]
*/
fun makeLinkToAddStickerSetInHtml(stickerSetName: StickerSetName) =
makeLinkToAddStickerSet(
stickerSetName,
HTML
)