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

chat invite links update

This commit is contained in:
InsanusMokrassar 2021-11-08 17:27:15 +06:00
parent 20b1645935
commit 10c62bf2c7
9 changed files with 496 additions and 114 deletions

View File

@ -7,6 +7,14 @@
* `Klock`: `2.4.6` -> `2.4.7` * `Klock`: `2.4.6` -> `2.4.7`
* `Ktor`: `1.6.4` -> `1.6.5` * `Ktor`: `1.6.4` -> `1.6.5`
* `MicroUtils`: `0.7.3` -> `0.8.0` * `MicroUtils`: `0.7.3` -> `0.8.0`
* `Core`:
* Replacement of simple `CreateChatInviteLink` and `EditChatInviteLink` with several new:
* `CreateChatInviteLinkSimple`
* `CreateChatInviteLinkWithLimitedMembers`
* `CreateChatInviteLinkWithJoinRequest`
* `EditChatInviteLinkSimple`
* `EditChatInviteLinkWithLimitedMembers`
* `EditChatInviteLinkWithJoinRequest`
## 0.36.1 ## 0.36.1

View File

@ -6,26 +6,66 @@ import dev.inmo.tgbotapi.requests.chat.invite_links.CreateChatInviteLink
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.abstracts.PublicChat import dev.inmo.tgbotapi.types.chat.abstracts.PublicChat
suspend fun TelegramBot.createChatInviteLink( suspend fun TelegramBot.createChatInviteLinkUnlimited(
chatId: ChatIdentifier, chatId: ChatIdentifier,
expiration: TelegramDate? = null, expiration: TelegramDate? = null
membersLimit: MembersLimit? = null ) = execute(CreateChatInviteLink.unlimited(chatId, expiration))
) = execute(CreateChatInviteLink(chatId, expiration, membersLimit))
suspend fun TelegramBot.createChatInviteLink( suspend fun TelegramBot.createChatInviteLinkUnlimited(
chat: PublicChat, chat: PublicChat,
expiration: TelegramDate? = null, expiration: TelegramDate? = null,
membersLimit: MembersLimit? = null ) = createChatInviteLinkUnlimited(chat.id, expiration)
) = createChatInviteLink(chat.id, expiration, membersLimit)
suspend fun TelegramBot.createChatInviteLink( suspend fun TelegramBot.createChatInviteLinkUnlimited(
chatId: ChatIdentifier,
expiration: DateTime
) = createChatInviteLinkUnlimited(chatId, expiration.toTelegramDate())
suspend fun TelegramBot.createChatInviteLinkUnlimited(
chat: PublicChat,
expiration: DateTime
) = createChatInviteLinkUnlimited(chat.id, expiration.toTelegramDate())
suspend fun TelegramBot.createChatInviteLinkWithLimitedMembers(
chatId: ChatIdentifier,
membersLimit: MembersLimit,
expiration: TelegramDate? = null
) = execute(CreateChatInviteLink.withLimitedMembers(chatId, membersLimit, expiration))
suspend fun TelegramBot.createChatInviteLinkWithLimitedMembers(
chat: PublicChat,
membersLimit: MembersLimit,
expiration: TelegramDate? = null,
) = createChatInviteLinkWithLimitedMembers(chat.id, membersLimit, expiration)
suspend fun TelegramBot.createChatInviteLinkWithLimitedMembers(
chatId: ChatIdentifier,
membersLimit: MembersLimit,
expiration: DateTime,
) = createChatInviteLinkWithLimitedMembers(chatId, membersLimit, expiration.toTelegramDate())
suspend fun TelegramBot.createChatInviteLinkWithLimitedMembers(
chat: PublicChat,
membersLimit: MembersLimit,
expiration: DateTime,
) = createChatInviteLinkWithLimitedMembers(chat.id, membersLimit, expiration.toTelegramDate())
suspend fun TelegramBot.createChatInviteLinkWithJoinRequest(
chatId: ChatIdentifier,
expiration: TelegramDate? = null
) = execute(CreateChatInviteLink.withJoinRequest(chatId, expiration))
suspend fun TelegramBot.createChatInviteLinkWithJoinRequest(
chat: PublicChat,
expiration: TelegramDate? = null,
) = createChatInviteLinkWithJoinRequest(chat.id, expiration)
suspend fun TelegramBot.createChatInviteLinkWithJoinRequest(
chatId: ChatIdentifier, chatId: ChatIdentifier,
expiration: DateTime, expiration: DateTime,
membersLimit: MembersLimit? = null ) = createChatInviteLinkWithJoinRequest(chatId, expiration.toTelegramDate())
) = createChatInviteLink(chatId, expiration.toTelegramDate(), membersLimit)
suspend fun TelegramBot.createChatInviteLink( suspend fun TelegramBot.createChatInviteLinkWithJoinRequest(
chat: PublicChat, chat: PublicChat,
expiration: DateTime, expiration: DateTime,
membersLimit: MembersLimit? = null ) = createChatInviteLinkWithJoinRequest(chat.id, expiration.toTelegramDate())
) = createChatInviteLink(chat.id, expiration.toTelegramDate(), membersLimit)

View File

@ -6,58 +6,154 @@ import dev.inmo.tgbotapi.requests.chat.invite_links.EditChatInviteLink
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.abstracts.PublicChat import dev.inmo.tgbotapi.types.chat.abstracts.PublicChat
suspend fun TelegramBot.editChatInviteLink( suspend fun TelegramBot.editChatInviteLinkUnlimited(
chatId: ChatIdentifier, chatId: ChatIdentifier,
previousLink: String, previousLink: String,
expiration: TelegramDate? = null, expiration: TelegramDate? = null
membersLimit: MembersLimit? = null ) = execute(EditChatInviteLink.unlimited(chatId, previousLink, expiration))
) = execute(EditChatInviteLink(chatId, previousLink, expiration, membersLimit))
suspend fun TelegramBot.editChatInviteLink( suspend fun TelegramBot.editChatInviteLinkUnlimited(
chat: PublicChat, chat: PublicChat,
previousLink: String, previousLink: String,
expiration: TelegramDate? = null, expiration: TelegramDate? = null,
membersLimit: MembersLimit? = null ) = editChatInviteLinkUnlimited(chat.id, previousLink, expiration)
) = editChatInviteLink(chat.id, previousLink, expiration, membersLimit)
suspend fun TelegramBot.editChatInviteLink( suspend fun TelegramBot.editChatInviteLinkUnlimited(
chatId: ChatIdentifier,
previousLink: String,
expiration: DateTime
) = editChatInviteLinkUnlimited(chatId, previousLink, expiration.toTelegramDate())
suspend fun TelegramBot.editChatInviteLinkUnlimited(
chat: PublicChat,
previousLink: String,
expiration: DateTime
) = editChatInviteLinkUnlimited(chat.id, previousLink, expiration.toTelegramDate())
suspend fun TelegramBot.editChatInviteLinkWithLimitedMembers(
chatId: ChatIdentifier,
previousLink: String,
membersLimit: MembersLimit,
expiration: TelegramDate? = null
) = execute(EditChatInviteLink.withLimitedMembers(chatId, previousLink, membersLimit, expiration))
suspend fun TelegramBot.editChatInviteLinkWithLimitedMembers(
chat: PublicChat,
previousLink: String,
membersLimit: MembersLimit,
expiration: TelegramDate? = null,
) = editChatInviteLinkWithLimitedMembers(chat.id, previousLink, membersLimit, expiration)
suspend fun TelegramBot.editChatInviteLinkWithLimitedMembers(
chatId: ChatIdentifier,
previousLink: String,
membersLimit: MembersLimit,
expiration: DateTime,
) = editChatInviteLinkWithLimitedMembers(chatId, previousLink, membersLimit, expiration.toTelegramDate())
suspend fun TelegramBot.editChatInviteLinkWithLimitedMembers(
chat: PublicChat,
previousLink: String,
membersLimit: MembersLimit,
expiration: DateTime,
) = editChatInviteLinkWithLimitedMembers(chat.id, previousLink, membersLimit, expiration.toTelegramDate())
suspend fun TelegramBot.editChatInviteLinkWithJoinRequest(
chatId: ChatIdentifier,
previousLink: String,
expiration: TelegramDate? = null
) = execute(EditChatInviteLink.withJoinRequest(chatId, previousLink, expiration))
suspend fun TelegramBot.editChatInviteLinkWithJoinRequest(
chat: PublicChat,
previousLink: String,
expiration: TelegramDate? = null,
) = editChatInviteLinkWithJoinRequest(chat.id, previousLink, expiration)
suspend fun TelegramBot.editChatInviteLinkWithJoinRequest(
chatId: ChatIdentifier, chatId: ChatIdentifier,
previousLink: String, previousLink: String,
expiration: DateTime, expiration: DateTime,
membersLimit: MembersLimit? = null ) = editChatInviteLinkWithJoinRequest(chatId, previousLink, expiration.toTelegramDate())
) = editChatInviteLink(chatId, previousLink, expiration.toTelegramDate(), membersLimit)
suspend fun TelegramBot.editChatInviteLink( suspend fun TelegramBot.editChatInviteLinkWithJoinRequest(
chat: PublicChat, chat: PublicChat,
previousLink: String, previousLink: String,
expiration: DateTime, expiration: DateTime,
membersLimit: MembersLimit? = null ) = editChatInviteLinkWithJoinRequest(chat.id, previousLink, expiration.toTelegramDate())
) = editChatInviteLink(chat.id, previousLink, expiration.toTelegramDate(), membersLimit)
suspend fun TelegramBot.editChatInviteLink( suspend fun TelegramBot.editChatInviteLinkUnlimited(
chat: ChatIdentifier, chatId: ChatIdentifier,
previousLink: ChatInviteLink, previousLink: ChatInviteLink,
expiration: TelegramDate? = previousLink.expirationDateTime ?.toTelegramDate(), expiration: TelegramDate? = null
membersLimit: MembersLimit? = previousLink.membersLimit ) = editChatInviteLinkUnlimited(chatId, previousLink.inviteLink, expiration)
) = editChatInviteLink(chat, previousLink.inviteLink, expiration, membersLimit)
suspend fun TelegramBot.editChatInviteLink( suspend fun TelegramBot.editChatInviteLinkUnlimited(
chat: ChatIdentifier,
previousLink: ChatInviteLink,
expiration: DateTime,
membersLimit: MembersLimit? = previousLink.membersLimit
) = editChatInviteLink(chat, previousLink.inviteLink, expiration, membersLimit)
suspend fun TelegramBot.editChatInviteLink(
chat: PublicChat, chat: PublicChat,
previousLink: ChatInviteLink, previousLink: ChatInviteLink,
expiration: TelegramDate? = previousLink.expirationDateTime ?.toTelegramDate(), expiration: TelegramDate? = null,
membersLimit: MembersLimit? = previousLink.membersLimit ) = editChatInviteLinkUnlimited(chat.id, previousLink, expiration)
) = editChatInviteLink(chat, previousLink.inviteLink, expiration, membersLimit)
suspend fun TelegramBot.editChatInviteLink( suspend fun TelegramBot.editChatInviteLinkUnlimited(
chatId: ChatIdentifier,
previousLink: ChatInviteLink,
expiration: DateTime
) = editChatInviteLinkUnlimited(chatId, previousLink, expiration.toTelegramDate())
suspend fun TelegramBot.editChatInviteLinkUnlimited(
chat: PublicChat,
previousLink: ChatInviteLink,
expiration: DateTime
) = editChatInviteLinkUnlimited(chat.id, previousLink, expiration.toTelegramDate())
suspend fun TelegramBot.editChatInviteLinkWithLimitedMembers(
chatId: ChatIdentifier,
previousLink: ChatInviteLink,
membersLimit: MembersLimit,
expiration: TelegramDate? = null
) = editChatInviteLinkWithLimitedMembers(chatId, previousLink.inviteLink, membersLimit, expiration)
suspend fun TelegramBot.editChatInviteLinkWithLimitedMembers(
chat: PublicChat,
previousLink: ChatInviteLink,
membersLimit: MembersLimit,
expiration: TelegramDate? = null,
) = editChatInviteLinkWithLimitedMembers(chat.id, previousLink, membersLimit, expiration)
suspend fun TelegramBot.editChatInviteLinkWithLimitedMembers(
chatId: ChatIdentifier,
previousLink: ChatInviteLink,
membersLimit: MembersLimit,
expiration: DateTime,
) = editChatInviteLinkWithLimitedMembers(chatId, previousLink, membersLimit, expiration.toTelegramDate())
suspend fun TelegramBot.editChatInviteLinkWithLimitedMembers(
chat: PublicChat,
previousLink: ChatInviteLink,
membersLimit: MembersLimit,
expiration: DateTime,
) = editChatInviteLinkWithLimitedMembers(chat.id, previousLink, membersLimit, expiration.toTelegramDate())
suspend fun TelegramBot.editChatInviteLinkWithJoinRequest(
chatId: ChatIdentifier,
previousLink: ChatInviteLink,
expiration: TelegramDate? = null
) = editChatInviteLinkWithJoinRequest(chatId, previousLink.inviteLink, expiration)
suspend fun TelegramBot.editChatInviteLinkWithJoinRequest(
chat: PublicChat,
previousLink: ChatInviteLink,
expiration: TelegramDate? = null,
) = editChatInviteLinkWithJoinRequest(chat.id, previousLink, expiration)
suspend fun TelegramBot.editChatInviteLinkWithJoinRequest(
chatId: ChatIdentifier,
previousLink: ChatInviteLink,
expiration: DateTime,
) = editChatInviteLinkWithJoinRequest(chatId, previousLink, expiration.toTelegramDate())
suspend fun TelegramBot.editChatInviteLinkWithJoinRequest(
chat: PublicChat, chat: PublicChat,
previousLink: ChatInviteLink, previousLink: ChatInviteLink,
expiration: DateTime, expiration: DateTime,
membersLimit: MembersLimit? = previousLink.membersLimit ) = editChatInviteLinkWithJoinRequest(chat.id, previousLink, expiration.toTelegramDate())
) = editChatInviteLink(chat, previousLink.inviteLink, expiration, membersLimit)

View File

@ -5,16 +5,26 @@ import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.DeserializationStrategy
interface ChatInviteLinkRequest : SimpleRequest<CommonInviteLink> { interface ChatInviteLinkRequest<R : SecondaryChatInviteLink> : SimpleRequest<R> {
val chatId: ChatIdentifier val chatId: ChatIdentifier
override val resultDeserializer: DeserializationStrategy<CommonInviteLink>
get() = CommonInviteLink.serializer()
} }
interface KnownChatInviteLinkRequest : ChatInviteLinkRequest {
interface KnownChatInviteLinkRequest<R : SecondaryChatInviteLink> : ChatInviteLinkRequest<R> {
val inviteLink: String val inviteLink: String
} }
interface EditChatInviteLinkRequest : ChatInviteLinkRequest {
val expireDate: DateTime? interface LimitedMembersChatInviteLinkRequest : ChatInviteLinkRequest<ChatInviteLinkWithLimitedMembers> {
val membersLimit: MembersLimit? val membersLimit: MembersLimit
override val resultDeserializer: DeserializationStrategy<ChatInviteLinkWithLimitedMembers>
get() = ChatInviteLinkWithLimitedMembers.serializer()
}
interface WithJoinRequestChatInviteLinkRequest : ChatInviteLinkRequest<ChatInviteLinkWithJoinRequest> {
override val resultDeserializer: DeserializationStrategy<ChatInviteLinkWithJoinRequest>
get() = ChatInviteLinkWithJoinRequest.serializer()
}
interface EditChatInviteLinkRequest<R : SecondaryChatInviteLink> : ChatInviteLinkRequest<R> {
val expireDate: DateTime?
} }

View File

@ -1,31 +1,108 @@
package dev.inmo.tgbotapi.requests.chat.invite_links package dev.inmo.tgbotapi.requests.chat.invite_links
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import dev.inmo.tgbotapi.requests.chat.abstracts.EditChatInviteLinkRequest import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.requests.chat.abstracts.*
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.* import kotlinx.serialization.*
sealed interface CreateChatInviteLink<R : SecondaryChatInviteLink> : EditChatInviteLinkRequest<R> {
val expirationUnixTimeStamp: TelegramDate?
override val expireDate: DateTime?
get() = expirationUnixTimeStamp ?.asDate
override fun method(): String = "createChatInviteLink"
companion object {
fun unlimited(
chatId: ChatIdentifier,
expirationUnixTimeStamp: TelegramDate? = null,
) = CreateChatInviteLinkUnlimited(chatId, expirationUnixTimeStamp)
fun withLimitedMembers(
chatId: ChatIdentifier,
membersLimit: MembersLimit,
expirationUnixTimeStamp: TelegramDate? = null,
) = CreateChatInviteLinkWithLimitedMembers(chatId, membersLimit, expirationUnixTimeStamp)
fun withJoinRequest(
chatId: ChatIdentifier,
expirationUnixTimeStamp: TelegramDate? = null,
) = CreateChatInviteLinkWithJoinRequest(chatId, expirationUnixTimeStamp)
fun unlimited(
chatId: ChatIdentifier,
expiration: DateTime? = null,
) = unlimited(chatId, expiration?.toTelegramDate())
fun withLimitedMembers(
chatId: ChatIdentifier,
membersLimit: MembersLimit,
expiration: DateTime? = null,
) = withLimitedMembers(chatId, membersLimit, expiration?.toTelegramDate())
fun withJoinRequest(
chatId: ChatIdentifier,
expiration: DateTime? = null,
) = withJoinRequest(chatId, expiration?.toTelegramDate())
}
}
/**
* Represent [https://core.telegram.org/bots/api#createchatinvitelink] request WITHOUT `member_limit`
* and `creates_join_request`
*
* @see CreateChatInviteLink.unlimited
* @see CreateChatInviteLinkWithLimitedMembers
* @see CreateChatInviteLinkWithJoinRequest
*/
@Serializable @Serializable
data class CreateChatInviteLink( data class CreateChatInviteLinkUnlimited(
@SerialName(chatIdField) @SerialName(chatIdField)
override val chatId: ChatIdentifier, override val chatId: ChatIdentifier,
@SerialName(expireDateField) @SerialName(expireDateField)
private val expirationUnixTimeStamp: TelegramDate? = null, override val expirationUnixTimeStamp: TelegramDate? = null,
@SerialName(memberLimitField) ) : CreateChatInviteLink<ChatInviteLinkUnlimited> {
override val membersLimit: MembersLimit? = null
) : EditChatInviteLinkRequest {
override val expireDate: DateTime?
get() = expirationUnixTimeStamp ?.asDate
override val requestSerializer: SerializationStrategy<*> override val requestSerializer: SerializationStrategy<*>
get() = serializer() get() = serializer()
override val resultDeserializer: DeserializationStrategy<ChatInviteLinkUnlimited>
override fun method(): String = "createChatInviteLink" get() = ChatInviteLinkUnlimited.serializer()
} }
fun CreateChatInviteLink( /**
chatId: ChatId, * Represent [https://core.telegram.org/bots/api#createchatinvitelink] request WITH `member_limit`
expireDate: DateTime, * and WITHOUT `creates_join_request`
membersLimit: MembersLimit? = null *
): CreateChatInviteLink = CreateChatInviteLink( * @see CreateChatInviteLink.withLimitedMembers
chatId, expireDate.toTelegramDate(), membersLimit * @see CreateChatInviteLinkUnlimited
) * @see CreateChatInviteLinkWithJoinRequest
*/
@Serializable
data class CreateChatInviteLinkWithLimitedMembers(
@SerialName(chatIdField)
override val chatId: ChatIdentifier,
@SerialName(memberLimitField)
override val membersLimit: MembersLimit,
@SerialName(expireDateField)
override val expirationUnixTimeStamp: TelegramDate? = null,
) : CreateChatInviteLink<ChatInviteLinkWithLimitedMembers>, LimitedMembersChatInviteLinkRequest {
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}
/**
* Represent [https://core.telegram.org/bots/api#createchatinvitelink] request WITHOUT `member_limit`
* and WITH `creates_join_request`
*
* @see CreateChatInviteLink.withJoinRequest
* @see CreateChatInviteLinkUnlimited
* @see CreateChatInviteLinkWithLimitedMembers
*/
@Serializable
data class CreateChatInviteLinkWithJoinRequest(
@SerialName(chatIdField)
override val chatId: ChatIdentifier,
@SerialName(expireDateField)
override val expirationUnixTimeStamp: TelegramDate? = null,
) : CreateChatInviteLink<ChatInviteLinkWithJoinRequest>, WithJoinRequestChatInviteLinkRequest {
@Required
@SerialName(createsJoinRequestField)
private val createsJoinRequest: Boolean = true
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}

View File

@ -1,35 +1,122 @@
package dev.inmo.tgbotapi.requests.chat.invite_links package dev.inmo.tgbotapi.requests.chat.invite_links
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import dev.inmo.tgbotapi.requests.chat.abstracts.EditChatInviteLinkRequest import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.requests.chat.abstracts.KnownChatInviteLinkRequest import dev.inmo.tgbotapi.requests.chat.abstracts.*
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.* import kotlinx.serialization.*
sealed interface EditChatInviteLink<R : SecondaryChatInviteLink> : EditChatInviteLinkRequest<R>, KnownChatInviteLinkRequest<R> {
val expirationUnixTimeStamp: TelegramDate?
override val expireDate: DateTime?
get() = expirationUnixTimeStamp ?.asDate
override fun method(): String = "editChatInviteLink"
companion object {
fun unlimited(
chatId: ChatIdentifier,
inviteLink: String,
expirationUnixTimeStamp: TelegramDate? = null,
) = EditChatInviteLinkUnlimited(chatId, inviteLink, expirationUnixTimeStamp)
fun withLimitedMembers(
chatId: ChatIdentifier,
inviteLink: String,
membersLimit: MembersLimit,
expirationUnixTimeStamp: TelegramDate? = null,
) = EditChatInviteLinkWithLimitedMembers(chatId, inviteLink, membersLimit, expirationUnixTimeStamp)
fun withJoinRequest(
chatId: ChatIdentifier,
inviteLink: String,
expirationUnixTimeStamp: TelegramDate? = null,
) = EditChatInviteLinkWithJoinRequest(chatId, inviteLink, expirationUnixTimeStamp)
fun unlimited(
chatId: ChatIdentifier,
inviteLink: String,
expiration: DateTime? = null,
) = unlimited(chatId, inviteLink, expiration ?.toTelegramDate())
fun withLimitedMembers(
chatId: ChatIdentifier,
inviteLink: String,
membersLimit: MembersLimit,
expiration: DateTime? = null,
) = withLimitedMembers(chatId, inviteLink, membersLimit, expiration ?.toTelegramDate())
fun withJoinRequest(
chatId: ChatIdentifier,
inviteLink: String,
expiration: DateTime? = null,
) = withJoinRequest(chatId, inviteLink, expiration ?.toTelegramDate())
}
}
/**
* Represent [https://core.telegram.org/bots/api#editchatinvitelink] request WITHOUT `member_limit`
* and `creates_join_request`
*
* @see EditChatInviteLink.unlimited
* @see EditChatInviteLinkWithLimitedMembers
* @see EditChatInviteLinkWithJoinRequest
*/
@Serializable @Serializable
data class EditChatInviteLink( data class EditChatInviteLinkUnlimited(
@SerialName(chatIdField) @SerialName(chatIdField)
override val chatId: ChatIdentifier, override val chatId: ChatIdentifier,
@SerialName(inviteLinkField) @SerialName(inviteLinkField)
override val inviteLink: String, override val inviteLink: String,
@SerialName(expireDateField) @SerialName(expireDateField)
private val expirationUnixTimeStamp: TelegramDate? = null, override val expirationUnixTimeStamp: TelegramDate? = null,
@SerialName(memberLimitField) ) : EditChatInviteLink<ChatInviteLinkUnlimited> {
override val membersLimit: MembersLimit? = null
) : EditChatInviteLinkRequest, KnownChatInviteLinkRequest {
override val expireDate: DateTime?
get() = expirationUnixTimeStamp ?.asDate
override val requestSerializer: SerializationStrategy<*> override val requestSerializer: SerializationStrategy<*>
get() = serializer() get() = serializer()
override val resultDeserializer: DeserializationStrategy<ChatInviteLinkUnlimited>
override fun method(): String = "editChatInviteLink" get() = ChatInviteLinkUnlimited.serializer()
} }
fun EditChatInviteLink( /**
chatId: ChatIdentifier, * Represent [https://core.telegram.org/bots/api#editchatinvitelink] request WITH `member_limit`
inviteLink: String, * and WITHOUT `creates_join_request`
expireDate: DateTime, *
membersLimit: MembersLimit? = null * @see EditChatInviteLink.withLimitedMembers
): EditChatInviteLink = EditChatInviteLink( * @see EditChatInviteLinkUnlimited
chatId, inviteLink, expireDate.toTelegramDate(), membersLimit * @see EditChatInviteLinkWithJoinRequest
) */
@Serializable
data class EditChatInviteLinkWithLimitedMembers(
@SerialName(chatIdField)
override val chatId: ChatIdentifier,
@SerialName(inviteLinkField)
override val inviteLink: String,
@SerialName(memberLimitField)
override val membersLimit: MembersLimit,
@SerialName(expireDateField)
override val expirationUnixTimeStamp: TelegramDate? = null,
) : EditChatInviteLink<ChatInviteLinkWithLimitedMembers>,
LimitedMembersChatInviteLinkRequest {
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}
/**
* Represent [https://core.telegram.org/bots/api#editchatinvitelink] request WITHOUT `member_limit`
* and WITH `creates_join_request`
*
* @see EditChatInviteLink.withJoinRequest
* @see EditChatInviteLinkUnlimited
* @see EditChatInviteLinkWithLimitedMembers
*/
@Serializable
data class EditChatInviteLinkWithJoinRequest(
@SerialName(chatIdField)
override val chatId: ChatIdentifier,
@SerialName(inviteLinkField)
override val inviteLink: String,
@SerialName(expireDateField)
override val expirationUnixTimeStamp: TelegramDate? = null,
) : EditChatInviteLink<ChatInviteLinkWithJoinRequest>,
WithJoinRequestChatInviteLinkRequest {
@Required
@SerialName(createsJoinRequestField)
private val createsJoinRequest: Boolean = true
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}

View File

@ -10,9 +10,11 @@ data class RevokeChatInviteLink(
override val chatId: ChatIdentifier, override val chatId: ChatIdentifier,
@SerialName(inviteLinkField) @SerialName(inviteLinkField)
override val inviteLink: String override val inviteLink: String
) : KnownChatInviteLinkRequest { ) : KnownChatInviteLinkRequest<SecondaryChatInviteLink> {
override val requestSerializer: SerializationStrategy<*> override val requestSerializer: SerializationStrategy<*>
get() = serializer() get() = serializer()
override val resultDeserializer: DeserializationStrategy<SecondaryChatInviteLink>
get() = SecondaryChatInviteLink.serializer()
override fun method(): String = "revokeChatInviteLink" override fun method(): String = "revokeChatInviteLink"
} }

View File

@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.types package dev.inmo.tgbotapi.types
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import dev.inmo.tgbotapi.CommonAbstracts.WithUser
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.descriptors.SerialDescriptor
@ -20,7 +21,11 @@ private data class RawChatInviteLink(
@SerialName(expireDateField) @SerialName(expireDateField)
val expirationDateTime: TelegramDate? = null, val expirationDateTime: TelegramDate? = null,
@SerialName(memberLimitField) @SerialName(memberLimitField)
val membersLimit: MembersLimit ?= null val membersLimit: MembersLimit ?= null,
@SerialName(createsJoinRequestField)
val createsJoinRequest: Boolean? = null,
@SerialName(pendingJoinRequestCountField)
val pendingJoinRequestCount: MembersLimit ?= null
) )
private fun ChatInviteLink.toRawChatInviteLink() = RawChatInviteLink( private fun ChatInviteLink.toRawChatInviteLink() = RawChatInviteLink(
@ -29,17 +34,36 @@ private fun ChatInviteLink.toRawChatInviteLink() = RawChatInviteLink(
isPrimary, isPrimary,
isRevoked, isRevoked,
expirationDateTime ?.toTelegramDate(), expirationDateTime ?.toTelegramDate(),
membersLimit (this as? ChatInviteLinkWithLimitedMembers) ?.membersLimit,
this is ChatInviteLinkWithJoinRequest,
(this as? ChatInviteLinkWithJoinRequest) ?.leftToReview
) )
@Serializable(ChatInviteLinkSerializer::class) @Serializable(ChatInviteLinkSerializer::class)
sealed class ChatInviteLink { sealed interface ChatInviteLink : WithUser {
abstract val inviteLink: String val inviteLink: String
abstract val creator: User val creator: User
abstract val isPrimary: Boolean val isPrimary: Boolean
abstract val isRevoked: Boolean get() = this is PrimaryInviteLink
abstract val expirationDateTime: DateTime? val isRevoked: Boolean
abstract val membersLimit: MembersLimit? val expirationDateTime: DateTime?
override val user: User
get() = creator
companion object {
fun serializer(): KSerializer<ChatInviteLink> = ChatInviteLinkSerializer
}
}
@Serializable(ChatInviteLinkSerializer::class)
sealed interface SecondaryChatInviteLink : ChatInviteLink {
override val isPrimary: Boolean
get() = false
companion object {
fun serializer(): KSerializer<SecondaryChatInviteLink> = ChatInviteLinkSerializer as KSerializer<SecondaryChatInviteLink>
}
} }
@Serializable @Serializable
@ -52,17 +76,47 @@ data class PrimaryInviteLink(
override val isRevoked: Boolean = false, override val isRevoked: Boolean = false,
@SerialName(expireDateField) @SerialName(expireDateField)
private val expireDate: TelegramDate? = null, private val expireDate: TelegramDate? = null,
@SerialName(memberLimitField) ) : ChatInviteLink {
override val membersLimit: MembersLimit? = null
) : ChatInviteLink() {
override val isPrimary: Boolean
get() = true
override val expirationDateTime: DateTime? override val expirationDateTime: DateTime?
get() = expireDate ?.asDate get() = expireDate ?.asDate
} }
@Serializable @Serializable
data class CommonInviteLink( data class ChatInviteLinkWithJoinRequest(
@SerialName(inviteLinkField)
override val inviteLink: String,
@SerialName(creatorField)
override val creator: User,
@SerialName(pendingJoinRequestCountField)
val leftToReview: Int,
@SerialName(isRevokedField)
override val isRevoked: Boolean = false,
@SerialName(expireDateField)
private val expireDate: TelegramDate? = null
) : SecondaryChatInviteLink {
override val expirationDateTime: DateTime?
get() = expireDate ?.asDate
}
@Serializable
data class ChatInviteLinkWithLimitedMembers(
@SerialName(inviteLinkField)
override val inviteLink: String,
@SerialName(creatorField)
override val creator: User,
@SerialName(memberLimitField)
val membersLimit: MembersLimit,
@SerialName(isRevokedField)
override val isRevoked: Boolean = false,
@SerialName(expireDateField)
private val expireDate: TelegramDate? = null,
) : SecondaryChatInviteLink {
override val expirationDateTime: DateTime?
get() = expireDate ?.asDate
}
@Serializable
data class ChatInviteLinkUnlimited(
@SerialName(inviteLinkField) @SerialName(inviteLinkField)
override val inviteLink: String, override val inviteLink: String,
@SerialName(creatorField) @SerialName(creatorField)
@ -71,11 +125,7 @@ data class CommonInviteLink(
override val isRevoked: Boolean = false, override val isRevoked: Boolean = false,
@SerialName(expireDateField) @SerialName(expireDateField)
private val expireDate: TelegramDate? = null, private val expireDate: TelegramDate? = null,
@SerialName(memberLimitField) ) : SecondaryChatInviteLink {
override val membersLimit: MembersLimit? = null
) : ChatInviteLink() {
override val isPrimary: Boolean
get() = false
override val expirationDateTime: DateTime? override val expirationDateTime: DateTime?
get() = expireDate ?.asDate get() = expireDate ?.asDate
} }
@ -89,11 +139,21 @@ object ChatInviteLinkSerializer : KSerializer<ChatInviteLink> {
val deserializedRaw = RawChatInviteLink.serializer().deserialize(decoder) val deserializedRaw = RawChatInviteLink.serializer().deserialize(decoder)
return deserializedRaw.run { return deserializedRaw.run {
when { when {
deserializedRaw.isPrimary -> PrimaryInviteLink( isPrimary -> PrimaryInviteLink(
inviteLink, creator, isRevoked, expirationDateTime, membersLimit inviteLink, creator, isRevoked, expirationDateTime
) )
else -> CommonInviteLink( createsJoinRequest == true -> {
inviteLink, creator, isRevoked, expirationDateTime, membersLimit ChatInviteLinkWithJoinRequest(
inviteLink, creator, pendingJoinRequestCount ?: 0, isRevoked, expirationDateTime
)
}
membersLimit != null -> {
ChatInviteLinkWithLimitedMembers(
inviteLink, creator, membersLimit, isRevoked, expirationDateTime
)
}
else -> ChatInviteLinkUnlimited(
inviteLink, creator, isRevoked, expirationDateTime
) )
} }
} }

View File

@ -177,6 +177,8 @@ const val messageAutoDeleteTimeField = "message_auto_delete_time"
const val isPrimaryField = "is_primary" const val isPrimaryField = "is_primary"
const val isRevokedField = "is_revoked" const val isRevokedField = "is_revoked"
const val expireDateField = "expire_date" const val expireDateField = "expire_date"
const val createsJoinRequestField = "creates_join_request"
const val pendingJoinRequestCountField = "pending_join_request_count"
const val memberLimitField = "member_limit" const val memberLimitField = "member_limit"
const val requestContactField = "request_contact" const val requestContactField = "request_contact"