From 10c62bf2c73172a4f20fe01a4728cb6abe98ee0a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 8 Nov 2021 17:27:15 +0600 Subject: [PATCH] chat invite links update --- CHANGELOG.md | 8 + .../chat/invite_links/CreateChatInviteLink.kt | 66 ++++++-- .../chat/invite_links/EditChatInviteLink.kt | 160 ++++++++++++++---- .../chat/abstracts/ChatInviteLinkRequest.kt | 26 ++- .../chat/invite_links/CreateChatInviteLink.kt | 111 ++++++++++-- .../chat/invite_links/EditChatInviteLink.kt | 125 +++++++++++--- .../chat/invite_links/RevokeChatInviteLink.kt | 4 +- .../dev/inmo/tgbotapi/types/ChatInviteLink.kt | 108 +++++++++--- .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 2 + 9 files changed, 496 insertions(+), 114 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72fdf8602b..14b2aa67b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ * `Klock`: `2.4.6` -> `2.4.7` * `Ktor`: `1.6.4` -> `1.6.5` * `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 diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/CreateChatInviteLink.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/CreateChatInviteLink.kt index 93ed35d244..2046f51cfb 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/CreateChatInviteLink.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/CreateChatInviteLink.kt @@ -6,26 +6,66 @@ import dev.inmo.tgbotapi.requests.chat.invite_links.CreateChatInviteLink import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.chat.abstracts.PublicChat -suspend fun TelegramBot.createChatInviteLink( +suspend fun TelegramBot.createChatInviteLinkUnlimited( chatId: ChatIdentifier, - expiration: TelegramDate? = null, - membersLimit: MembersLimit? = null -) = execute(CreateChatInviteLink(chatId, expiration, membersLimit)) + expiration: TelegramDate? = null +) = execute(CreateChatInviteLink.unlimited(chatId, expiration)) -suspend fun TelegramBot.createChatInviteLink( +suspend fun TelegramBot.createChatInviteLinkUnlimited( chat: PublicChat, expiration: TelegramDate? = null, - membersLimit: MembersLimit? = null -) = createChatInviteLink(chat.id, expiration, membersLimit) +) = createChatInviteLinkUnlimited(chat.id, expiration) -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, expiration: DateTime, - membersLimit: MembersLimit? = null -) = createChatInviteLink(chatId, expiration.toTelegramDate(), membersLimit) +) = createChatInviteLinkWithJoinRequest(chatId, expiration.toTelegramDate()) -suspend fun TelegramBot.createChatInviteLink( +suspend fun TelegramBot.createChatInviteLinkWithJoinRequest( chat: PublicChat, expiration: DateTime, - membersLimit: MembersLimit? = null -) = createChatInviteLink(chat.id, expiration.toTelegramDate(), membersLimit) +) = createChatInviteLinkWithJoinRequest(chat.id, expiration.toTelegramDate()) diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/EditChatInviteLink.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/EditChatInviteLink.kt index 027a54360f..71a65dd86c 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/EditChatInviteLink.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/invite_links/EditChatInviteLink.kt @@ -6,58 +6,154 @@ import dev.inmo.tgbotapi.requests.chat.invite_links.EditChatInviteLink import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.chat.abstracts.PublicChat -suspend fun TelegramBot.editChatInviteLink( +suspend fun TelegramBot.editChatInviteLinkUnlimited( chatId: ChatIdentifier, previousLink: String, - expiration: TelegramDate? = null, - membersLimit: MembersLimit? = null -) = execute(EditChatInviteLink(chatId, previousLink, expiration, membersLimit)) + expiration: TelegramDate? = null +) = execute(EditChatInviteLink.unlimited(chatId, previousLink, expiration)) -suspend fun TelegramBot.editChatInviteLink( +suspend fun TelegramBot.editChatInviteLinkUnlimited( chat: PublicChat, previousLink: String, expiration: TelegramDate? = null, - membersLimit: MembersLimit? = null -) = editChatInviteLink(chat.id, previousLink, expiration, membersLimit) +) = editChatInviteLinkUnlimited(chat.id, previousLink, expiration) -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, previousLink: String, expiration: DateTime, - membersLimit: MembersLimit? = null -) = editChatInviteLink(chatId, previousLink, expiration.toTelegramDate(), membersLimit) +) = editChatInviteLinkWithJoinRequest(chatId, previousLink, expiration.toTelegramDate()) -suspend fun TelegramBot.editChatInviteLink( +suspend fun TelegramBot.editChatInviteLinkWithJoinRequest( chat: PublicChat, previousLink: String, expiration: DateTime, - membersLimit: MembersLimit? = null -) = editChatInviteLink(chat.id, previousLink, expiration.toTelegramDate(), membersLimit) +) = editChatInviteLinkWithJoinRequest(chat.id, previousLink, expiration.toTelegramDate()) -suspend fun TelegramBot.editChatInviteLink( - chat: ChatIdentifier, +suspend fun TelegramBot.editChatInviteLinkUnlimited( + chatId: ChatIdentifier, previousLink: ChatInviteLink, - expiration: TelegramDate? = previousLink.expirationDateTime ?.toTelegramDate(), - membersLimit: MembersLimit? = previousLink.membersLimit -) = editChatInviteLink(chat, previousLink.inviteLink, expiration, membersLimit) + expiration: TelegramDate? = null +) = editChatInviteLinkUnlimited(chatId, previousLink.inviteLink, expiration) -suspend fun TelegramBot.editChatInviteLink( - chat: ChatIdentifier, - previousLink: ChatInviteLink, - expiration: DateTime, - membersLimit: MembersLimit? = previousLink.membersLimit -) = editChatInviteLink(chat, previousLink.inviteLink, expiration, membersLimit) - -suspend fun TelegramBot.editChatInviteLink( +suspend fun TelegramBot.editChatInviteLinkUnlimited( chat: PublicChat, previousLink: ChatInviteLink, - expiration: TelegramDate? = previousLink.expirationDateTime ?.toTelegramDate(), - membersLimit: MembersLimit? = previousLink.membersLimit -) = editChatInviteLink(chat, previousLink.inviteLink, expiration, membersLimit) + expiration: TelegramDate? = null, +) = editChatInviteLinkUnlimited(chat.id, previousLink, expiration) -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, previousLink: ChatInviteLink, expiration: DateTime, - membersLimit: MembersLimit? = previousLink.membersLimit -) = editChatInviteLink(chat, previousLink.inviteLink, expiration, membersLimit) +) = editChatInviteLinkWithJoinRequest(chat.id, previousLink, expiration.toTelegramDate()) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/abstracts/ChatInviteLinkRequest.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/abstracts/ChatInviteLinkRequest.kt index a45d428a8d..e58ce0ec81 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/abstracts/ChatInviteLinkRequest.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/abstracts/ChatInviteLinkRequest.kt @@ -5,16 +5,26 @@ import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest import dev.inmo.tgbotapi.types.* import kotlinx.serialization.DeserializationStrategy -interface ChatInviteLinkRequest : SimpleRequest { +interface ChatInviteLinkRequest : SimpleRequest { val chatId: ChatIdentifier - - override val resultDeserializer: DeserializationStrategy - get() = CommonInviteLink.serializer() } -interface KnownChatInviteLinkRequest : ChatInviteLinkRequest { + +interface KnownChatInviteLinkRequest : ChatInviteLinkRequest { val inviteLink: String } -interface EditChatInviteLinkRequest : ChatInviteLinkRequest { - val expireDate: DateTime? - val membersLimit: MembersLimit? + +interface LimitedMembersChatInviteLinkRequest : ChatInviteLinkRequest { + val membersLimit: MembersLimit + + override val resultDeserializer: DeserializationStrategy + get() = ChatInviteLinkWithLimitedMembers.serializer() +} + +interface WithJoinRequestChatInviteLinkRequest : ChatInviteLinkRequest { + override val resultDeserializer: DeserializationStrategy + get() = ChatInviteLinkWithJoinRequest.serializer() +} + +interface EditChatInviteLinkRequest : ChatInviteLinkRequest { + val expireDate: DateTime? } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/CreateChatInviteLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/CreateChatInviteLink.kt index 4a87ef49c7..0e1ac7184d 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/CreateChatInviteLink.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/CreateChatInviteLink.kt @@ -1,31 +1,108 @@ package dev.inmo.tgbotapi.requests.chat.invite_links 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 kotlinx.serialization.* +sealed interface CreateChatInviteLink : EditChatInviteLinkRequest { + 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 -data class CreateChatInviteLink( +data class CreateChatInviteLinkUnlimited( @SerialName(chatIdField) override val chatId: ChatIdentifier, @SerialName(expireDateField) - private val expirationUnixTimeStamp: TelegramDate? = null, - @SerialName(memberLimitField) - override val membersLimit: MembersLimit? = null -) : EditChatInviteLinkRequest { - override val expireDate: DateTime? - get() = expirationUnixTimeStamp ?.asDate + override val expirationUnixTimeStamp: TelegramDate? = null, +) : CreateChatInviteLink { override val requestSerializer: SerializationStrategy<*> get() = serializer() - - override fun method(): String = "createChatInviteLink" + override val resultDeserializer: DeserializationStrategy + get() = ChatInviteLinkUnlimited.serializer() } -fun CreateChatInviteLink( - chatId: ChatId, - expireDate: DateTime, - membersLimit: MembersLimit? = null -): CreateChatInviteLink = CreateChatInviteLink( - chatId, expireDate.toTelegramDate(), membersLimit -) +/** + * Represent [https://core.telegram.org/bots/api#createchatinvitelink] request WITH `member_limit` + * and WITHOUT `creates_join_request` + * + * @see CreateChatInviteLink.withLimitedMembers + * @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, 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, WithJoinRequestChatInviteLinkRequest { + @Required + @SerialName(createsJoinRequestField) + private val createsJoinRequest: Boolean = true + + override val requestSerializer: SerializationStrategy<*> + get() = serializer() +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/EditChatInviteLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/EditChatInviteLink.kt index fdeb3b55e1..d164a36091 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/EditChatInviteLink.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/EditChatInviteLink.kt @@ -1,35 +1,122 @@ package dev.inmo.tgbotapi.requests.chat.invite_links import com.soywiz.klock.DateTime -import dev.inmo.tgbotapi.requests.chat.abstracts.EditChatInviteLinkRequest -import dev.inmo.tgbotapi.requests.chat.abstracts.KnownChatInviteLinkRequest +import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest +import dev.inmo.tgbotapi.requests.chat.abstracts.* import dev.inmo.tgbotapi.types.* import kotlinx.serialization.* +sealed interface EditChatInviteLink : EditChatInviteLinkRequest, KnownChatInviteLinkRequest { + 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 -data class EditChatInviteLink( +data class EditChatInviteLinkUnlimited( @SerialName(chatIdField) override val chatId: ChatIdentifier, @SerialName(inviteLinkField) override val inviteLink: String, @SerialName(expireDateField) - private val expirationUnixTimeStamp: TelegramDate? = null, - @SerialName(memberLimitField) - override val membersLimit: MembersLimit? = null -) : EditChatInviteLinkRequest, KnownChatInviteLinkRequest { - override val expireDate: DateTime? - get() = expirationUnixTimeStamp ?.asDate + override val expirationUnixTimeStamp: TelegramDate? = null, +) : EditChatInviteLink { override val requestSerializer: SerializationStrategy<*> get() = serializer() - - override fun method(): String = "editChatInviteLink" + override val resultDeserializer: DeserializationStrategy + get() = ChatInviteLinkUnlimited.serializer() } -fun EditChatInviteLink( - chatId: ChatIdentifier, - inviteLink: String, - expireDate: DateTime, - membersLimit: MembersLimit? = null -): EditChatInviteLink = EditChatInviteLink( - chatId, inviteLink, expireDate.toTelegramDate(), membersLimit -) +/** + * Represent [https://core.telegram.org/bots/api#editchatinvitelink] request WITH `member_limit` + * and WITHOUT `creates_join_request` + * + * @see EditChatInviteLink.withLimitedMembers + * @see EditChatInviteLinkUnlimited + * @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, + 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, + WithJoinRequestChatInviteLinkRequest { + @Required + @SerialName(createsJoinRequestField) + private val createsJoinRequest: Boolean = true + + override val requestSerializer: SerializationStrategy<*> + get() = serializer() +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/RevokeChatInviteLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/RevokeChatInviteLink.kt index 31a8993666..fb6c77bf5a 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/RevokeChatInviteLink.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/RevokeChatInviteLink.kt @@ -10,9 +10,11 @@ data class RevokeChatInviteLink( override val chatId: ChatIdentifier, @SerialName(inviteLinkField) override val inviteLink: String -) : KnownChatInviteLinkRequest { +) : KnownChatInviteLinkRequest { override val requestSerializer: SerializationStrategy<*> get() = serializer() + override val resultDeserializer: DeserializationStrategy + get() = SecondaryChatInviteLink.serializer() override fun method(): String = "revokeChatInviteLink" } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt index e7771b2ad0..e868c76c2d 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt @@ -1,6 +1,7 @@ package dev.inmo.tgbotapi.types import com.soywiz.klock.DateTime +import dev.inmo.tgbotapi.CommonAbstracts.WithUser import dev.inmo.tgbotapi.utils.RiskFeature import kotlinx.serialization.* import kotlinx.serialization.descriptors.SerialDescriptor @@ -20,7 +21,11 @@ private data class RawChatInviteLink( @SerialName(expireDateField) val expirationDateTime: TelegramDate? = null, @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( @@ -29,17 +34,36 @@ private fun ChatInviteLink.toRawChatInviteLink() = RawChatInviteLink( isPrimary, isRevoked, expirationDateTime ?.toTelegramDate(), - membersLimit + (this as? ChatInviteLinkWithLimitedMembers) ?.membersLimit, + this is ChatInviteLinkWithJoinRequest, + (this as? ChatInviteLinkWithJoinRequest) ?.leftToReview ) @Serializable(ChatInviteLinkSerializer::class) -sealed class ChatInviteLink { - abstract val inviteLink: String - abstract val creator: User - abstract val isPrimary: Boolean - abstract val isRevoked: Boolean - abstract val expirationDateTime: DateTime? - abstract val membersLimit: MembersLimit? +sealed interface ChatInviteLink : WithUser { + val inviteLink: String + val creator: User + val isPrimary: Boolean + get() = this is PrimaryInviteLink + val isRevoked: Boolean + val expirationDateTime: DateTime? + + override val user: User + get() = creator + + companion object { + fun serializer(): KSerializer = ChatInviteLinkSerializer + } +} + +@Serializable(ChatInviteLinkSerializer::class) +sealed interface SecondaryChatInviteLink : ChatInviteLink { + override val isPrimary: Boolean + get() = false + + companion object { + fun serializer(): KSerializer = ChatInviteLinkSerializer as KSerializer + } } @Serializable @@ -52,17 +76,47 @@ data class PrimaryInviteLink( override val isRevoked: Boolean = false, @SerialName(expireDateField) private val expireDate: TelegramDate? = null, - @SerialName(memberLimitField) - override val membersLimit: MembersLimit? = null -) : ChatInviteLink() { - override val isPrimary: Boolean - get() = true +) : ChatInviteLink { override val expirationDateTime: DateTime? get() = expireDate ?.asDate } @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) override val inviteLink: String, @SerialName(creatorField) @@ -71,11 +125,7 @@ data class CommonInviteLink( override val isRevoked: Boolean = false, @SerialName(expireDateField) private val expireDate: TelegramDate? = null, - @SerialName(memberLimitField) - override val membersLimit: MembersLimit? = null -) : ChatInviteLink() { - override val isPrimary: Boolean - get() = false +) : SecondaryChatInviteLink { override val expirationDateTime: DateTime? get() = expireDate ?.asDate } @@ -89,11 +139,21 @@ object ChatInviteLinkSerializer : KSerializer { val deserializedRaw = RawChatInviteLink.serializer().deserialize(decoder) return deserializedRaw.run { when { - deserializedRaw.isPrimary -> PrimaryInviteLink( - inviteLink, creator, isRevoked, expirationDateTime, membersLimit + isPrimary -> PrimaryInviteLink( + inviteLink, creator, isRevoked, expirationDateTime ) - else -> CommonInviteLink( - inviteLink, creator, isRevoked, expirationDateTime, membersLimit + createsJoinRequest == true -> { + ChatInviteLinkWithJoinRequest( + inviteLink, creator, pendingJoinRequestCount ?: 0, isRevoked, expirationDateTime + ) + } + membersLimit != null -> { + ChatInviteLinkWithLimitedMembers( + inviteLink, creator, membersLimit, isRevoked, expirationDateTime + ) + } + else -> ChatInviteLinkUnlimited( + inviteLink, creator, isRevoked, expirationDateTime ) } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index 8a9ba6d089..7d864b5820 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -177,6 +177,8 @@ const val messageAutoDeleteTimeField = "message_auto_delete_time" const val isPrimaryField = "is_primary" const val isRevokedField = "is_revoked" 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 requestContactField = "request_contact"