tgbotapi/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt

193 lines
6.2 KiB
Kotlin
Raw Normal View History

package dev.inmo.tgbotapi.types
2023-05-27 12:19:14 +00:00
import korlibs.time.DateTime
2022-05-01 16:13:40 +00:00
import dev.inmo.tgbotapi.abstracts.WithUser
2022-04-21 18:16:41 +00:00
import dev.inmo.tgbotapi.types.chat.User
2021-06-03 17:34:11 +00:00
import dev.inmo.tgbotapi.utils.RiskFeature
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
@Serializable
private data class RawChatInviteLink(
@SerialName(inviteLinkField)
val inviteLink: String,
@SerialName(creatorField)
val creator: User,
@SerialName(isPrimaryField)
val isPrimary: Boolean,
@SerialName(isRevokedField)
val isRevoked: Boolean,
2021-11-08 12:00:43 +00:00
@SerialName(nameField)
val name: String? = null,
@SerialName(expireDateField)
val expirationDateTime: TelegramDate? = null,
@SerialName(memberLimitField)
2021-11-08 11:27:15 +00:00
val membersLimit: MembersLimit ?= null,
@SerialName(createsJoinRequestField)
val createsJoinRequest: Boolean? = null,
@SerialName(pendingJoinRequestCountField)
val pendingJoinRequestCount: MembersLimit ?= null
)
private fun ChatInviteLink.toRawChatInviteLink() = RawChatInviteLink(
inviteLink,
creator,
isPrimary,
isRevoked,
2021-11-08 12:00:43 +00:00
(this as? SecondaryChatInviteLink) ?.name,
expirationDateTime ?.toTelegramDate(),
2021-11-08 11:27:15 +00:00
(this as? ChatInviteLinkWithLimitedMembers) ?.membersLimit,
this is ChatInviteLinkWithJoinRequest,
(this as? ChatInviteLinkWithJoinRequest) ?.leftToReview
)
2021-11-11 06:11:58 +00:00
/**
* Base interface for all chat invite links. See inheritors for more info or official [docs](https://core.telegram.org/bots/api#chatinvitelink)
*/
@Serializable(ChatInviteLinkSerializer::class)
2021-11-08 11:27:15 +00:00
sealed interface ChatInviteLink : WithUser {
val inviteLink: String
val creator: User
val isPrimary: Boolean
get() = this is PrimaryInviteLink
val isRevoked: Boolean
val expirationDateTime: DateTime?
2021-11-08 12:00:43 +00:00
val name: String?
2021-11-08 11:27:15 +00:00
override val user: User
get() = creator
}
2021-11-11 06:11:58 +00:00
/**
* Base interface for all [ChatInviteLink]s which are NOT [PrimaryInviteLink]
*/
2021-11-08 11:27:15 +00:00
@Serializable(ChatInviteLinkSerializer::class)
sealed interface SecondaryChatInviteLink : ChatInviteLink {
override val isPrimary: Boolean
get() = false
}
2021-11-11 06:11:58 +00:00
/**
* Primary invite link in the chat for this bot
*/
@Serializable
data class PrimaryInviteLink(
@SerialName(inviteLinkField)
override val inviteLink: String,
@SerialName(creatorField)
override val creator: User,
@SerialName(isRevokedField)
override val isRevoked: Boolean = false,
@SerialName(expireDateField)
private val expireDate: TelegramDate? = null,
2021-11-08 11:27:15 +00:00
) : ChatInviteLink {
override val expirationDateTime: DateTime?
get() = expireDate ?.asDate
2021-11-08 12:00:43 +00:00
override val name: String?
get() = null
2021-11-08 11:27:15 +00:00
}
2021-11-11 06:11:58 +00:00
/**
* Represent [SecondaryChatInviteLink] which will require an aprovement from one of the administrators
*
* @see ChatJoinRequest
* @see dev.inmo.tgbotapi.types.update.ChatJoinRequestUpdate
*/
2021-11-08 11:27:15 +00:00
@Serializable
data class ChatInviteLinkWithJoinRequest(
@SerialName(inviteLinkField)
override val inviteLink: String,
@SerialName(creatorField)
override val creator: User,
2021-11-08 12:00:43 +00:00
@SerialName(nameField)
override val name: String? = null,
2021-11-08 11:27:15 +00:00
@SerialName(pendingJoinRequestCountField)
2021-11-08 12:00:43 +00:00
val leftToReview: Int = 0,
2021-11-08 11:27:15 +00:00
@SerialName(isRevokedField)
override val isRevoked: Boolean = false,
@SerialName(expireDateField)
private val expireDate: TelegramDate? = null
) : SecondaryChatInviteLink {
override val expirationDateTime: DateTime?
get() = expireDate ?.asDate
}
2021-11-11 06:11:58 +00:00
/**
* Represent [SecondaryChatInviteLink] which will have limitation for the amount of chat members to join
*/
2021-11-08 11:27:15 +00:00
@Serializable
data class ChatInviteLinkWithLimitedMembers(
@SerialName(inviteLinkField)
override val inviteLink: String,
@SerialName(creatorField)
override val creator: User,
2021-11-08 12:00:43 +00:00
@SerialName(nameField)
override val name: String? = null,
@SerialName(memberLimitField)
2021-11-08 11:27:15 +00:00
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
}
2021-11-11 06:11:58 +00:00
/**
* Represent [SecondaryChatInviteLink] which have no any restrictions like [ChatInviteLinkWithJoinRequest] or
* [ChatInviteLinkWithLimitedMembers]
*/
@Serializable
2021-11-08 11:27:15 +00:00
data class ChatInviteLinkUnlimited(
@SerialName(inviteLinkField)
override val inviteLink: String,
@SerialName(creatorField)
override val creator: User,
2021-11-08 12:00:43 +00:00
@SerialName(nameField)
override val name: String? = null,
@SerialName(isRevokedField)
override val isRevoked: Boolean = false,
@SerialName(expireDateField)
private val expireDate: TelegramDate? = null,
2021-11-08 11:27:15 +00:00
) : SecondaryChatInviteLink {
override val expirationDateTime: DateTime?
get() = expireDate ?.asDate
}
2021-06-03 17:34:11 +00:00
@RiskFeature
object ChatInviteLinkSerializer : KSerializer<ChatInviteLink> {
override val descriptor: SerialDescriptor
get() = RawChatInviteLink.serializer().descriptor
override fun deserialize(decoder: Decoder): ChatInviteLink {
val deserializedRaw = RawChatInviteLink.serializer().deserialize(decoder)
return deserializedRaw.run {
when {
2021-11-08 11:27:15 +00:00
isPrimary -> PrimaryInviteLink(
inviteLink, creator, isRevoked, expirationDateTime
)
2021-11-08 11:27:15 +00:00
createsJoinRequest == true -> {
ChatInviteLinkWithJoinRequest(
2021-11-08 12:00:43 +00:00
inviteLink, creator, name, pendingJoinRequestCount ?: 0, isRevoked, expirationDateTime
2021-11-08 11:27:15 +00:00
)
}
membersLimit != null -> {
ChatInviteLinkWithLimitedMembers(
2021-11-08 12:00:43 +00:00
inviteLink, creator, name, membersLimit, isRevoked, expirationDateTime
2021-11-08 11:27:15 +00:00
)
}
else -> ChatInviteLinkUnlimited(
2021-11-08 12:00:43 +00:00
inviteLink, creator, name, isRevoked, expirationDateTime
)
}
}
}
override fun serialize(encoder: Encoder, value: ChatInviteLink) {
RawChatInviteLink.serializer().serialize(encoder, value.toRawChatInviteLink())
}
}